View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.api.admin.webconfig;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  import static org.codelibs.fess.app.web.admin.webconfig.AdminWebconfigAction.getWebConfig;
20  
21  import java.util.List;
22  import java.util.stream.Collectors;
23  
24  import javax.annotation.Resource;
25  
26  import org.codelibs.core.lang.StringUtil;
27  import org.codelibs.fess.Constants;
28  import org.codelibs.fess.app.pager.WebConfigPager;
29  import org.codelibs.fess.app.service.WebConfigService;
30  import org.codelibs.fess.app.web.CrudMode;
31  import org.codelibs.fess.app.web.api.ApiResult;
32  import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
33  import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
34  import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
35  import org.codelibs.fess.app.web.api.ApiResult.Status;
36  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
37  import org.codelibs.fess.es.config.exentity.WebConfig;
38  import org.codelibs.fess.helper.PermissionHelper;
39  import org.codelibs.fess.util.ComponentUtil;
40  import org.lastaflute.web.Execute;
41  import org.lastaflute.web.response.JsonResponse;
42  
43  /**
44   * @author Keiichi Watanabe
45   */
46  public class ApiAdminWebconfigAction extends FessApiAdminAction {
47  
48      // ===================================================================================
49      //                                                                           Attribute
50      //                                                                           =========
51      @Resource
52      private WebConfigService webConfigService;
53  
54      // ===================================================================================
55      //                                                                      Search Execute
56      //                                                                      ==============
57  
58      // GET /api/admin/webconfig/settings
59      // POST /api/admin/webconfig/settings
60      @Execute
61      public JsonResponse<ApiResult> settings(final SearchBody body) {
62          validateApi(body, messages -> {});
63          final WebConfigPager pager = copyBeanToNewBean(body, WebConfigPager.class);
64          final List<WebConfig> list = webConfigService.getWebConfigList(pager);
65          return asJson(
66                  new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
67                          .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
68      }
69  
70      // GET /api/admin/webconfig/setting/{id}
71      @Execute
72      public JsonResponse<ApiResult> get$setting(final String id) {
73          return asJson(new ApiConfigResponse().setting(webConfigService.getWebConfig(id).map(this::createEditBody).orElseGet(() -> {
74              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
75              return null;
76          })).status(Status.OK).result());
77      }
78  
79      // PUT /api/admin/webconfig/setting
80      @Execute
81      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
82          validateApi(body, messages -> {});
83          body.crudMode = CrudMode.CREATE;
84          final WebConfig webConfig = getWebConfig(body).map(entity -> {
85              try {
86                  webConfigService.store(entity);
87              } catch (final Exception e) {
88                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
89              }
90              return entity;
91          }).orElseGet(() -> {
92              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
93              return null;
94          });
95  
96          return asJson(new ApiUpdateResponse().id(webConfig.getId()).created(true).status(Status.OK).result());
97      }
98  
99      // POST /api/admin/webconfig/setting
100     @Execute
101     public JsonResponse<ApiResult> post$setting(final EditBody body) {
102         validateApi(body, messages -> {});
103         body.crudMode = CrudMode.EDIT;
104         final WebConfig webConfig = getWebConfig(body).map(entity -> {
105             try {
106                 webConfigService.store(entity);
107             } catch (final Exception e) {
108                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
109             }
110             return entity;
111         }).orElseGet(() -> {
112             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
113             return null;
114         });
115         return asJson(new ApiUpdateResponse().id(webConfig.getId()).created(false).status(Status.OK).result());
116     }
117 
118     // DELETE /api/admin/webconfig/setting/{id}
119     @Execute
120     public JsonResponse<ApiResult> delete$setting(final String id) {
121         webConfigService.getWebConfig(id).ifPresent(entity -> {
122             try {
123                 webConfigService.delete(entity);
124                 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
125             } catch (final Exception e) {
126                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
127             }
128         }).orElse(() -> {
129             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
130         });
131         return asJson(new ApiResponse().status(Status.OK).result());
132     }
133 
134     protected EditBody createEditBody(final WebConfig entity) {
135         final EditBody body = new EditBody();
136         copyBeanToBean(entity, body, copyOp -> {
137             copyOp.excludeNull();
138             copyOp.exclude(Constants.PERMISSIONS, Constants.VIRTUAL_HOSTS);
139         });
140         final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
141         body.permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s))
142                 .filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining("\n")));
143         body.virtualHosts = stream(entity.getVirtualHosts())
144                 .get(stream -> stream.filter(StringUtil::isNotBlank).distinct().map(String::trim).collect(Collectors.joining("\n")));
145         return body;
146     }
147 }