1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.api.admin.webauth;
17
18 import static org.codelibs.fess.app.web.admin.webauth.AdminWebauthAction.getWebAuthentication;
19
20 import java.util.List;
21 import java.util.stream.Collectors;
22
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.codelibs.fess.app.pager.WebAuthPager;
26 import org.codelibs.fess.app.service.WebAuthenticationService;
27 import org.codelibs.fess.app.service.WebConfigService;
28 import org.codelibs.fess.app.web.CrudMode;
29 import org.codelibs.fess.app.web.api.ApiResult;
30 import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
31 import org.codelibs.fess.app.web.api.ApiResult.ApiErrorResponse;
32 import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
33 import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
34 import org.codelibs.fess.app.web.api.ApiResult.Status;
35 import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
36 import org.codelibs.fess.opensearch.config.exentity.WebAuthentication;
37 import org.lastaflute.web.Execute;
38 import org.lastaflute.web.response.JsonResponse;
39
40 import jakarta.annotation.Resource;
41
42
43
44
45
46 public class ApiAdminWebauthAction extends FessApiAdminAction {
47
48
49 private static final Logger logger = LogManager.getLogger(ApiAdminWebauthAction.class);
50
51
52
53
54
55
56
57 public ApiAdminWebauthAction() {
58 super();
59 }
60
61
62
63
64
65
66
67
68
69 @Resource
70 private WebAuthenticationService webAuthService;
71
72 @Resource
73 private WebConfigService webConfigService;
74
75
76
77
78
79
80
81
82
83
84
85
86
87 @Execute
88 public JsonResponse<ApiResult> settings(final SearchBody body) {
89 validateApi(body, messages -> {});
90 final WebAuthPager pager = copyBeanToNewBean(body, WebAuthPager.class);
91 final List<WebAuthentication> list = webAuthService.getWebAuthenticationList(pager);
92 return asJson(
93 new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
94 .total(pager.getAllRecordCount())
95 .status(ApiResult.Status.OK)
96 .result());
97 }
98
99
100
101
102
103
104
105
106 @Execute
107 public JsonResponse<ApiResult> get$setting(final String id) {
108 return asJson(new ApiConfigResponse().setting(webAuthService.getWebAuthentication(id).map(this::createEditBody).orElseGet(() -> {
109 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
110 return null;
111 })).status(Status.OK).result());
112 }
113
114
115
116
117
118
119
120
121 @Execute
122 public JsonResponse<ApiResult> post$setting(final CreateBody body) {
123 validateApi(body, messages -> {});
124 if (!isValidWebConfigId(body.webConfigId)) {
125 return asJson(new ApiErrorResponse().message("invalid webConfigId").status(Status.BAD_REQUEST).result());
126 }
127
128 body.crudMode = CrudMode.CREATE;
129 final WebAuthentication webAuth = getWebAuthentication(body).map(entity -> {
130 try {
131 webAuthService.store(entity);
132 } catch (final Exception e) {
133 logger.warn("Failed to process a request.", e);
134 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
135 }
136 return entity;
137 }).orElseGet(() -> {
138 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
139 return null;
140 });
141
142 return asJson(new ApiUpdateResponse().id(webAuth.getId()).created(true).status(Status.OK).result());
143 }
144
145
146
147
148
149
150
151
152 @Execute
153 public JsonResponse<ApiResult> put$setting(final EditBody body) {
154 validateApi(body, messages -> {});
155 body.crudMode = CrudMode.EDIT;
156 final WebAuthentication webAuth = getWebAuthentication(body).map(entity -> {
157 try {
158 webAuthService.store(entity);
159 } catch (final Exception e) {
160 logger.warn("Failed to process a request.", e);
161 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
162 }
163 return entity;
164 }).orElseGet(() -> {
165 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
166 return null;
167 });
168 return asJson(new ApiUpdateResponse().id(webAuth.getId()).created(false).status(Status.OK).result());
169 }
170
171
172
173
174
175
176
177
178 @Execute
179 public JsonResponse<ApiResult> delete$setting(final String id) {
180 webAuthService.getWebAuthentication(id).ifPresent(entity -> {
181 try {
182 webAuthService.delete(entity);
183 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
184 } catch (final Exception e) {
185 logger.warn("Failed to process a request.", e);
186 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
187 }
188 }).orElse(() -> {
189 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
190 });
191 return asJson(new ApiResponse().status(Status.OK).result());
192 }
193
194
195
196
197
198
199
200 protected EditBody createEditBody(final WebAuthentication entity) {
201 final EditBody body = new EditBody();
202 copyBeanToBean(entity, body, copyOp -> {
203 copyOp.excludeNull();
204 });
205 return body;
206 }
207
208
209
210
211
212
213
214 protected Boolean isValidWebConfigId(final String webconfigId) {
215 return webConfigService.getWebConfig(webconfigId).isPresent();
216 }
217 }