/
ApiAdminFileauthAction.java
155 lines (138 loc) · 6.82 KB
/
ApiAdminFileauthAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2012-2024 CodeLibs Project and the Others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.codelibs.fess.app.web.api.admin.fileauth;
import static org.codelibs.fess.app.web.admin.fileauth.AdminFileauthAction.getFileAuthentication;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codelibs.fess.app.pager.FileAuthPager;
import org.codelibs.fess.app.service.FileAuthenticationService;
import org.codelibs.fess.app.service.FileConfigService;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.api.ApiResult;
import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
import org.codelibs.fess.app.web.api.ApiResult.ApiErrorResponse;
import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
import org.codelibs.fess.app.web.api.ApiResult.Status;
import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
import org.codelibs.fess.es.config.exentity.FileAuthentication;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.JsonResponse;
/**
* @author Keiichi Watanabe
*/
public class ApiAdminFileauthAction extends FessApiAdminAction {
private static final Logger logger = LogManager.getLogger(ApiAdminFileauthAction.class);
// ===================================================================================
// Attribute
// =========
@Resource
private FileAuthenticationService fileAuthService;
@Resource
private FileConfigService fileConfigService;
// ===================================================================================
// Search Execute
// ==============
// GET /api/admin/fileauth/settings
// POST /api/admin/fileauth/settings
@Execute
public JsonResponse<ApiResult> settings(final SearchBody body) {
validateApi(body, messages -> {});
final FileAuthPager pager = copyBeanToNewBean(body, FileAuthPager.class);
final List<FileAuthentication> list = fileAuthService.getFileAuthenticationList(pager);
return asJson(
new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
.total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
}
// GET /api/admin/fileauth/setting/{id}
@Execute
public JsonResponse<ApiResult> get$setting(final String id) {
return asJson(new ApiConfigResponse().setting(fileAuthService.getFileAuthentication(id).map(this::createEditBody).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
return null;
})).status(Status.OK).result());
}
// PUT /api/admin/fileauth/setting
@Execute
public JsonResponse<ApiResult> put$setting(final CreateBody body) {
validateApi(body, messages -> {});
if (!isValidFileConfigId(body.fileConfigId)) {
return asJson(new ApiErrorResponse().message("invalid fileConfigId").status(Status.BAD_REQUEST).result());
}
body.crudMode = CrudMode.CREATE;
final FileAuthentication fileAuth = getFileAuthentication(body).map(entity -> {
try {
fileAuthService.store(entity);
} catch (final Exception e) {
logger.warn("Failed to process a request.", e);
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return entity;
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
return null;
});
return asJson(new ApiUpdateResponse().id(fileAuth.getId()).created(true).status(Status.OK).result());
}
// POST /api/admin/fileauth/setting
@Execute
public JsonResponse<ApiResult> post$setting(final EditBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.EDIT;
final FileAuthentication fileAuth = getFileAuthentication(body).map(entity -> {
try {
fileAuthService.store(entity);
} catch (final Exception e) {
logger.warn("Failed to process a request.", e);
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return entity;
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
return null;
});
return asJson(new ApiUpdateResponse().id(fileAuth.getId()).created(false).status(Status.OK).result());
}
// DELETE /api/admin/fileauth/setting/{id}
@Execute
public JsonResponse<ApiResult> delete$setting(final String id) {
fileAuthService.getFileAuthentication(id).ifPresent(entity -> {
try {
fileAuthService.delete(entity);
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
} catch (final Exception e) {
logger.warn("Failed to process a request.", e);
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
}
}).orElse(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
});
return asJson(new ApiResponse().status(Status.OK).result());
}
protected EditBody createEditBody(final FileAuthentication entity) {
final EditBody body = new EditBody();
copyBeanToBean(entity, body, copyOp -> {
copyOp.excludeNull();
});
return body;
}
protected Boolean isValidFileConfigId(final String fileconfigId) {
return fileConfigService.getFileConfig(fileconfigId).isPresent();
}
}