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