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 javax.annotation.Resource;
24
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.es.config.exentity.WebAuthentication;
37 import org.lastaflute.web.Execute;
38 import org.lastaflute.web.response.JsonResponse;
39
40
41
42
43 public class ApiAdminWebauthAction extends FessApiAdminAction {
44
45
46
47
48 @Resource
49 private WebAuthenticationService webAuthService;
50 @Resource
51 private WebConfigService webConfigService;
52
53
54
55
56
57
58
59 @Execute
60 public JsonResponse<ApiResult> settings(final SearchBody body) {
61 validateApi(body, messages -> {});
62 final WebAuthPager pager = copyBeanToNewBean(body, WebAuthPager.class);
63 final List<WebAuthentication> list = webAuthService.getWebAuthenticationList(pager);
64 return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
65 .settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList()))
66 .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
67 }
68
69
70 @Execute
71 public JsonResponse<ApiResult> get$setting(final String id) {
72 return asJson(new ApiConfigResponse()
73 .setting(webAuthService.getWebAuthentication(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
74 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
75 return null;
76 })).status(Status.OK).result());
77 }
78
79
80 @Execute
81 public JsonResponse<ApiResult> put$setting(final CreateBody body) {
82 validateApi(body, messages -> {});
83 if (!isValidWebConfigId(body.webConfigId)) {
84 return asJson(new ApiErrorResponse().message("invalid webConfigId").status(Status.BAD_REQUEST).result());
85 }
86
87 body.crudMode = CrudMode.CREATE;
88 final WebAuthentication webAuth = getWebAuthentication(body).map(entity -> {
89 try {
90 webAuthService.store(entity);
91 } catch (final Exception e) {
92 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
93 }
94 return entity;
95 }).orElseGet(() -> {
96 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
97 return null;
98 });
99
100 return asJson(new ApiUpdateResponse().id(webAuth.getId()).created(true).status(Status.OK).result());
101 }
102
103
104 @Execute
105 public JsonResponse<ApiResult> post$setting(final EditBody body) {
106 validateApi(body, messages -> {});
107 body.crudMode = CrudMode.EDIT;
108 final WebAuthentication webAuth = getWebAuthentication(body).map(entity -> {
109 try {
110 webAuthService.store(entity);
111 } catch (final Exception e) {
112 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
113 }
114 return entity;
115 }).orElseGet(() -> {
116 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
117 return null;
118 });
119 return asJson(new ApiUpdateResponse().id(webAuth.getId()).created(false).status(Status.OK).result());
120 }
121
122
123 @Execute
124 public JsonResponse<ApiResult> delete$setting(final String id) {
125 webAuthService.getWebAuthentication(id).ifPresent(entity -> {
126 try {
127 webAuthService.delete(entity);
128 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
129 } catch (final Exception e) {
130 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
131 }
132 }).orElse(() -> {
133 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
134 });
135 return asJson(new ApiResponse().status(Status.OK).result());
136 }
137
138 protected EditBody createEditBody(final WebAuthentication entity) {
139 final EditBody body = new EditBody();
140 copyBeanToBean(entity, body, copyOp -> {
141 copyOp.excludeNull();
142 });
143 return body;
144 }
145
146 protected Boolean isValidWebConfigId(final String webconfigId) {
147 return webConfigService.getWebConfig(webconfigId).isPresent();
148 }
149 }