1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
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.helper.PermissionHelper;
38 import org.codelibs.fess.opensearch.config.exentity.WebConfig;
39 import org.codelibs.fess.util.ComponentUtil;
40 import org.lastaflute.web.Execute;
41 import org.lastaflute.web.response.JsonResponse;
42
43 import jakarta.annotation.Resource;
44
45
46
47
48
49 public class ApiAdminWebconfigAction extends FessApiAdminAction {
50
51
52 private static final Logger logger = LogManager.getLogger(ApiAdminWebconfigAction.class);
53
54
55
56
57
58
59
60 public ApiAdminWebconfigAction() {
61 super();
62 }
63
64
65
66
67
68 @Resource
69 private WebConfigService webConfigService;
70
71
72
73
74
75
76
77
78
79
80
81
82
83 @Execute
84 public JsonResponse<ApiResult> settings(final SearchBody body) {
85 validateApi(body, messages -> {});
86 final WebConfigPager pager = copyBeanToNewBean(body, WebConfigPager.class);
87 final List<WebConfig> list = webConfigService.getWebConfigList(pager);
88 return asJson(
89 new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
90 .total(pager.getAllRecordCount())
91 .status(ApiResult.Status.OK)
92 .result());
93 }
94
95
96
97
98
99
100
101
102 @Execute
103 public JsonResponse<ApiResult> get$setting(final String id) {
104 return asJson(new ApiConfigResponse().setting(webConfigService.getWebConfig(id).map(this::createEditBody).orElseGet(() -> {
105 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
106 return null;
107 })).status(Status.OK).result());
108 }
109
110
111
112
113
114
115
116
117 @Execute
118 public JsonResponse<ApiResult> post$setting(final CreateBody body) {
119 validateApi(body, messages -> {});
120 body.crudMode = CrudMode.CREATE;
121 final WebConfig webConfig = getWebConfig(body).map(entity -> {
122 try {
123 webConfigService.store(entity);
124 } catch (final Exception e) {
125 logger.warn("Failed to process a request.", e);
126 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
127 }
128 return entity;
129 }).orElseGet(() -> {
130 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
131 return null;
132 });
133
134 return asJson(new ApiUpdateResponse().id(webConfig.getId()).created(true).status(Status.OK).result());
135 }
136
137
138
139
140
141
142
143
144 @Execute
145 public JsonResponse<ApiResult> put$setting(final EditBody body) {
146 validateApi(body, messages -> {});
147 body.crudMode = CrudMode.EDIT;
148 final WebConfig webConfig = getWebConfig(body).map(entity -> {
149 try {
150 webConfigService.store(entity);
151 } catch (final Exception e) {
152 logger.warn("Failed to process a request.", e);
153 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
154 }
155 return entity;
156 }).orElseGet(() -> {
157 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
158 return null;
159 });
160 return asJson(new ApiUpdateResponse().id(webConfig.getId()).created(false).status(Status.OK).result());
161 }
162
163
164
165
166
167
168
169
170 @Execute
171 public JsonResponse<ApiResult> delete$setting(final String id) {
172 webConfigService.getWebConfig(id).ifPresent(entity -> {
173 try {
174 webConfigService.delete(entity);
175 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
176 } catch (final Exception e) {
177 logger.warn("Failed to process a request.", e);
178 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
179 }
180 }).orElse(() -> {
181 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
182 });
183 return asJson(new ApiResponse().status(Status.OK).result());
184 }
185
186
187
188
189
190
191
192 protected EditBody createEditBody(final WebConfig entity) {
193 final EditBody body = new EditBody();
194 copyBeanToBean(entity, body, copyOp -> {
195 copyOp.excludeNull();
196 copyOp.exclude(Constants.PERMISSIONS, Constants.VIRTUAL_HOSTS);
197 });
198 final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
199 body.permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s))
200 .filter(StringUtil::isNotBlank)
201 .distinct()
202 .collect(Collectors.joining("\n")));
203 body.virtualHosts = stream(entity.getVirtualHosts())
204 .get(stream -> stream.filter(StringUtil::isNotBlank).distinct().map(String::trim).collect(Collectors.joining("\n")));
205 return body;
206 }
207 }