1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.admin.fileauth;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.Map;
23
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.codelibs.fess.Constants;
27 import org.codelibs.fess.annotation.Secured;
28 import org.codelibs.fess.app.pager.FileAuthPager;
29 import org.codelibs.fess.app.service.FileAuthenticationService;
30 import org.codelibs.fess.app.service.FileConfigService;
31 import org.codelibs.fess.app.web.CrudMode;
32 import org.codelibs.fess.app.web.base.FessAdminAction;
33 import org.codelibs.fess.helper.SystemHelper;
34 import org.codelibs.fess.opensearch.config.exentity.FileAuthentication;
35 import org.codelibs.fess.opensearch.config.exentity.FileConfig;
36 import org.codelibs.fess.util.ComponentUtil;
37 import org.codelibs.fess.util.RenderDataUtil;
38 import org.dbflute.optional.OptionalEntity;
39 import org.dbflute.optional.OptionalThing;
40 import org.lastaflute.web.Execute;
41 import org.lastaflute.web.response.HtmlResponse;
42 import org.lastaflute.web.response.render.RenderData;
43 import org.lastaflute.web.ruts.process.ActionRuntime;
44
45 import jakarta.annotation.Resource;
46
47
48
49
50
51 public class AdminFileauthAction extends FessAdminAction {
52
53
54
55
56 public AdminFileauthAction() {
57 super();
58 }
59
60
61 public static final String ROLE = "admin-fileauth";
62
63
64 private static final Logger logger = LogManager.getLogger(AdminFileauthAction.class);
65
66
67
68
69
70 @Resource
71 private FileAuthenticationService fileAuthenticationService;
72
73
74 @Resource
75 private FileAuthPager fileAuthenticationPager;
76
77
78 @Resource
79 protected FileConfigService fileConfigService;
80
81
82
83
84
85
86
87
88
89 @Override
90 protected void setupHtmlData(final ActionRuntime runtime) {
91 super.setupHtmlData(runtime);
92 runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameFileauth()));
93 }
94
95
96
97
98
99
100 @Override
101 protected String getActionRole() {
102 return ROLE;
103 }
104
105
106
107
108
109
110
111
112
113 @Execute
114 @Secured({ ROLE, ROLE + VIEW })
115 public HtmlResponse index() {
116 return asListHtml();
117 }
118
119
120
121
122
123
124
125
126 @Execute
127 @Secured({ ROLE, ROLE + VIEW })
128 public HtmlResponse list(final OptionalThing<Integer> pageNumber, final SearchForm form) {
129 pageNumber.ifPresent(num -> {
130 fileAuthenticationPager.setCurrentPageNumber(pageNumber.get());
131 }).orElse(() -> {
132 fileAuthenticationPager.setCurrentPageNumber(0);
133 });
134 return asHtml(path_AdminFileauth_AdminFileauthJsp).renderWith(data -> {
135 searchPaging(data, form);
136 });
137 }
138
139
140
141
142
143
144
145 @Execute
146 @Secured({ ROLE, ROLE + VIEW })
147 public HtmlResponse search(final SearchForm form) {
148 copyBeanToBean(form, fileAuthenticationPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
149 return asHtml(path_AdminFileauth_AdminFileauthJsp).renderWith(data -> {
150 searchPaging(data, form);
151 });
152 }
153
154
155
156
157
158
159
160 @Execute
161 @Secured({ ROLE, ROLE + VIEW })
162 public HtmlResponse reset(final SearchForm form) {
163 fileAuthenticationPager.clear();
164 return asHtml(path_AdminFileauth_AdminFileauthJsp).renderWith(data -> {
165 searchPaging(data, form);
166 });
167 }
168
169
170
171
172
173
174
175 protected void searchPaging(final RenderData data, final SearchForm form) {
176 RenderDataUtil.register(data, "fileAuthenticationItems",
177 fileAuthenticationService.getFileAuthenticationList(fileAuthenticationPager));
178 RenderDataUtil.register(data, "displayCreateLink", !crawlingConfigHelper.getAllFileConfigList(false, false, false, null).isEmpty());
179
180 copyBeanToBean(fileAuthenticationPager, form, op -> op.include("id"));
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194 @Execute
195 @Secured({ ROLE })
196 public HtmlResponse createnew() {
197 saveToken();
198 return asHtml(path_AdminFileauth_AdminFileauthEditJsp).useForm(CreateForm.class, op -> {
199 op.setup(form -> {
200 form.initialize();
201 form.crudMode = CrudMode.CREATE;
202 });
203 }).renderWith(data -> {
204 registerProtocolSchemeItems(data);
205 registerFileConfigItems(data);
206 });
207 }
208
209
210
211
212
213
214
215 @Execute
216 @Secured({ ROLE })
217 public HtmlResponse edit(final EditForm form) {
218 validate(form, messages -> {}, this::asListHtml);
219 final String id = form.id;
220 fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
221 copyBeanToBean(entity, form, op -> {});
222 }).orElse(() -> {
223 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
224 });
225 saveToken();
226 if (form.crudMode.intValue() == CrudMode.EDIT) {
227
228 form.crudMode = CrudMode.DETAILS;
229 return asDetailsHtml();
230 }
231 form.crudMode = CrudMode.EDIT;
232 return asEditHtml();
233 }
234
235
236
237
238
239
240
241
242
243
244
245 @Execute
246 @Secured({ ROLE, ROLE + VIEW })
247 public HtmlResponse details(final int crudMode, final String id) {
248 verifyCrudMode(crudMode, CrudMode.DETAILS, this::asListHtml);
249 saveToken();
250 return asDetailsHtml().useForm(EditForm.class, op -> {
251 op.setup(form -> {
252 fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
253 copyBeanToBean(entity, form, copyOp -> {
254 copyOp.excludeNull();
255 });
256 form.crudMode = crudMode;
257 }).orElse(() -> {
258 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
259 });
260 });
261 });
262 }
263
264
265
266
267
268
269
270
271
272
273 @Execute
274 @Secured({ ROLE })
275 public HtmlResponse create(final CreateForm form) {
276 verifyCrudMode(form.crudMode, CrudMode.CREATE, this::asListHtml);
277 validate(form, messages -> {}, this::asEditHtml);
278 verifyToken(this::asEditHtml);
279 getFileAuthentication(form).ifPresent(entity -> {
280 try {
281 fileAuthenticationService.store(entity);
282 saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
283 } catch (final Exception e) {
284 logger.warn("Failed to process a request.", e);
285 throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)),
286 this::asEditHtml);
287 }
288 }).orElse(() -> {
289 throwValidationError(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL), this::asEditHtml);
290 });
291 return redirect(getClass());
292 }
293
294
295
296
297
298
299
300 @Execute
301 @Secured({ ROLE })
302 public HtmlResponse update(final EditForm form) {
303 verifyCrudMode(form.crudMode, CrudMode.EDIT, this::asListHtml);
304 validate(form, messages -> {}, this::asEditHtml);
305 verifyToken(this::asEditHtml);
306 getFileAuthentication(form).ifPresent(entity -> {
307 try {
308 fileAuthenticationService.store(entity);
309 saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
310 } catch (final Exception e) {
311 logger.warn("Failed to process a request.", e);
312 throwValidationError(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)),
313 this::asEditHtml);
314 }
315 }).orElse(() -> {
316 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), this::asEditHtml);
317 });
318 return redirect(getClass());
319 }
320
321
322
323
324
325
326
327 @Execute
328 @Secured({ ROLE })
329 public HtmlResponse delete(final EditForm form) {
330 verifyCrudMode(form.crudMode, CrudMode.DETAILS, this::asListHtml);
331 validate(form, messages -> {}, this::asDetailsHtml);
332 verifyToken(this::asDetailsHtml);
333 final String id = form.id;
334 fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
335 try {
336 fileAuthenticationService.delete(entity);
337 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
338 } catch (final Exception e) {
339 logger.warn("Failed to process a request.", e);
340 throwValidationError(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)),
341 this::asEditHtml);
342 }
343 }).orElse(() -> {
344 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asDetailsHtml);
345 });
346 return redirect(getClass());
347 }
348
349
350
351
352
353
354
355
356
357
358
359
360 public static OptionalEntity<FileAuthentication> getEntity(final CreateForm form, final String username, final long currentTime) {
361 switch (form.crudMode) {
362 case CrudMode.CREATE:
363 return OptionalEntity.of(new FileAuthentication()).map(entity -> {
364 entity.setCreatedBy(username);
365 entity.setCreatedTime(currentTime);
366 return entity;
367 });
368 case CrudMode.EDIT:
369 if (form instanceof EditForm) {
370 return ComponentUtil.getComponent(FileAuthenticationService.class).getFileAuthentication(((EditForm) form).id);
371 }
372 break;
373 default:
374 break;
375 }
376 return OptionalEntity.empty();
377 }
378
379
380
381
382
383
384
385 public static OptionalEntity<FileAuthentication> getFileAuthentication(final CreateForm form) {
386 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
387 final String username = systemHelper.getUsername();
388 final long currentTime = systemHelper.getCurrentTimeAsLong();
389 return getEntity(form, username, currentTime).map(entity -> {
390 entity.setUpdatedBy(username);
391 entity.setUpdatedTime(currentTime);
392 copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
393 return entity;
394 });
395 }
396
397
398
399
400
401
402 protected void registerProtocolSchemeItems(final RenderData data) {
403 final List<Map<String, String>> itemList = new ArrayList<>();
404 final Locale locale = ComponentUtil.getRequestManager().getUserLocale();
405 itemList.add(createItem(ComponentUtil.getMessageManager().getMessage(locale, "labels.file_auth_scheme_samba"), Constants.SAMBA));
406 itemList.add(createItem(ComponentUtil.getMessageManager().getMessage(locale, "labels.file_auth_scheme_ftp"), Constants.FTP));
407 RenderDataUtil.register(data, "protocolSchemeItems", itemList);
408 }
409
410
411
412
413
414
415 protected void registerFileConfigItems(final RenderData data) {
416 final List<Map<String, String>> itemList = new ArrayList<>();
417 final List<FileConfig> fileConfigList = crawlingConfigHelper.getAllFileConfigList(false, false, false, null);
418 for (final FileConfig fileConfig : fileConfigList) {
419 itemList.add(createItem(fileConfig.getName(), fileConfig.getId().toString()));
420 }
421 RenderDataUtil.register(data, "fileConfigItems", itemList);
422 }
423
424
425
426
427
428
429
430
431 protected Map<String, String> createItem(final String label, final String value) {
432 final Map<String, String> map = new HashMap<>(2);
433 map.put(Constants.ITEM_LABEL, label);
434 map.put(Constants.ITEM_VALUE, value);
435 return map;
436 }
437
438
439
440
441
442
443
444
445
446
447
448
449 private HtmlResponse asListHtml() {
450 return asHtml(path_AdminFileauth_AdminFileauthJsp).renderWith(data -> {
451 RenderDataUtil.register(data, "fileAuthenticationItems",
452 fileAuthenticationService.getFileAuthenticationList(fileAuthenticationPager));
453 RenderDataUtil.register(data, "displayCreateLink",
454 !crawlingConfigHelper.getAllFileConfigList(false, false, false, null).isEmpty());
455 }).useForm(SearchForm.class, setup -> {
456 setup.setup(form -> {
457 copyBeanToBean(fileAuthenticationPager, form, op -> op.include("id"));
458 });
459 });
460 }
461
462
463
464
465
466
467 private HtmlResponse asEditHtml() {
468 return asHtml(path_AdminFileauth_AdminFileauthEditJsp).renderWith(data -> {
469 registerProtocolSchemeItems(data);
470 registerFileConfigItems(data);
471 });
472 }
473
474
475
476
477
478
479 private HtmlResponse asDetailsHtml() {
480 return asHtml(path_AdminFileauth_AdminFileauthDetailsJsp).renderWith(data -> {
481 registerProtocolSchemeItems(data);
482 registerFileConfigItems(data);
483 });
484 }
485 }