1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.api;
17
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Locale;
21 import java.util.Map;
22 import java.util.stream.Collectors;
23
24 import org.codelibs.fess.Constants;
25 import org.codelibs.fess.entity.SearchRenderData;
26 import org.codelibs.fess.mylasta.action.FessMessages;
27 import org.codelibs.fess.util.ComponentUtil;
28 import org.codelibs.fess.util.FacetResponse;
29 import org.lastaflute.web.util.LaRequestUtil;
30 import org.lastaflute.web.validation.VaMessenger;
31
32 public class ApiResult {
33
34 protected ApiResponse response = null;
35
36 public ApiResult(final ApiResponse response) {
37 this.response = response;
38 }
39
40 public enum Status {
41 OK(0), BAD_REQUEST(1), SYSTEM_ERROR(2), UNAUTHORIZED(3);
42 private final int id;
43
44 private Status(final int id) {
45 this.id = id;
46 }
47
48 public int getId() {
49 return id;
50 }
51 }
52
53 public static class ApiResponse {
54 protected String version = Constants.WEB_API_VERSION;
55 protected int status;
56
57 public ApiResponse status(final Status status) {
58 this.status = status.getId();
59 return this;
60 }
61
62 public ApiResult result() {
63 return new ApiResult(this);
64 }
65 }
66
67 public static class ApiUpdateResponse extends ApiResponse {
68 protected String id;
69 protected boolean created;
70
71 public ApiUpdateResponse id(final String id) {
72 this.id = id;
73 return this;
74 }
75
76 public ApiUpdateResponse created(final boolean created) {
77 this.created = created;
78 return this;
79 }
80
81 @Override
82 public ApiResult result() {
83 return new ApiResult(this);
84 }
85 }
86
87 public static class ApiDeleteResponse extends ApiResponse {
88 protected long count = 1;
89
90 public ApiDeleteResponse count(final long count) {
91 this.count = count;
92 return this;
93 }
94
95 @Override
96 public ApiResult result() {
97 return new ApiResult(this);
98 }
99 }
100
101 public static class ApiConfigResponse extends ApiResponse {
102 protected Object setting;
103
104 public ApiConfigResponse setting(final Object setting) {
105 this.setting = setting;
106 return this;
107 }
108
109 @Override
110 public ApiResult result() {
111 return new ApiResult(this);
112 }
113 }
114
115 public static class ApiConfigsResponse<T> extends ApiResponse {
116 protected List<T> settings;
117 protected long total = 0;
118
119 public ApiConfigsResponse<T> settings(final List<T> settings) {
120 this.settings = settings;
121 this.total = settings.size();
122 return this;
123 }
124
125 public ApiConfigsResponse<T> total(final long total) {
126 this.total = total;
127 return this;
128 }
129
130 @Override
131 public ApiResult result() {
132 return new ApiResult(this);
133 }
134 }
135
136 public static class ApiDocResponse extends ApiResponse {
137 protected Object doc;
138
139 public ApiDocResponse doc(final Object doc) {
140 this.doc = doc;
141 return this;
142 }
143
144 @Override
145 public ApiResult result() {
146 return new ApiResult(this);
147 }
148 }
149
150 public static class ApiDocsResponse extends ApiResponse {
151 protected String queryId;
152 protected List<Map<String, Object>> docs;
153 protected String highlightParams;
154 protected String execTime;
155 protected int pageSize;
156 protected int pageNumber;
157 protected long recordCount;
158 protected int pageCount;
159 protected boolean nextPage;
160 protected boolean prevPage;
161 protected long startRecordNumber;
162 protected long endRecordNumber;
163 protected List<String> pageNumbers;
164 protected boolean partial;
165 protected long queryTime;
166 protected String searchQuery;
167 protected long requestedTime;
168 protected List<Map<String, Object>> facetField;
169 protected List<Map<String, Object>> facetQuery;
170
171 public ApiDocsResponse renderData(final SearchRenderData data) {
172 queryId = data.getQueryId();
173 docs = data.getDocumentItems();
174 highlightParams = data.getAppendHighlightParams();
175 execTime = data.getExecTime();
176 pageSize = data.getPageSize();
177 pageNumber = data.getCurrentPageNumber();
178 recordCount = data.getAllRecordCount();
179 pageCount = data.getAllPageCount();
180 nextPage = data.isExistNextPage();
181 prevPage = data.isExistPrevPage();
182 startRecordNumber = data.getCurrentStartRecordNumber();
183 endRecordNumber = data.getCurrentEndRecordNumber();
184 pageNumbers = data.getPageNumberList();
185 partial = data.isPartialResults();
186 queryTime = data.getQueryTime();
187 searchQuery = data.getSearchQuery();
188 requestedTime = data.getRequestedTime();
189 final FacetResponse facetResponse = data.getFacetResponse();
190 if (facetResponse != null && facetResponse.hasFacetResponse()) {
191
192 if (facetResponse.getFieldList() != null) {
193 facetField = facetResponse.getFieldList().stream().map(field -> {
194 final Map<String, Object> fieldMap = new HashMap<>(2, 1f);
195 fieldMap.put("name", field.getName());
196 fieldMap.put("result", field.getValueCountMap().entrySet().stream().map(e -> {
197 final Map<String, Object> valueCount = new HashMap<>(2, 1f);
198 valueCount.put("value", e.getKey());
199 valueCount.put("count", e.getValue());
200 return valueCount;
201 }).collect(Collectors.toList()));
202 return fieldMap;
203 }).collect(Collectors.toList());
204 }
205
206 if (facetResponse.getQueryCountMap() != null) {
207 facetQuery = facetResponse.getQueryCountMap().entrySet().stream().map(e -> {
208 final Map<String, Object> valueCount = new HashMap<>(2, 1f);
209 valueCount.put("value", e.getKey());
210 valueCount.put("count", e.getValue());
211 return valueCount;
212 }).collect(Collectors.toList());
213
214 }
215 }
216 return this;
217 }
218
219 @Override
220 public ApiResult result() {
221 return new ApiResult(this);
222 }
223 }
224
225 public static class ApiLogResponse extends ApiResponse {
226 protected Object log;
227
228 public ApiLogResponse log(final Object log) {
229 this.log = log;
230 return this;
231 }
232
233 @Override
234 public ApiResult result() {
235 return new ApiResult(this);
236 }
237 }
238
239 public static class ApiLogsResponse<T> extends ApiResponse {
240 protected List<T> logs;
241 protected long total = 0;
242
243 public ApiLogsResponse<T> logs(final List<T> logs) {
244 this.logs = logs;
245 return this;
246 }
247
248 public ApiLogsResponse<T> total(final long total) {
249 this.total = total;
250 return this;
251 }
252
253 @Override
254 public ApiResult result() {
255 return new ApiResult(this);
256 }
257 }
258
259 public static class ApiLogFilesResponse extends ApiResponse {
260 protected List<Map<String, Object>> files;
261 protected long total = 0;
262
263 public ApiLogFilesResponse files(final List<Map<String, Object>> files) {
264 this.files = files;
265 return this;
266 }
267
268 public ApiLogFilesResponse total(final long total) {
269 this.total = total;
270 return this;
271 }
272
273 @Override
274 public ApiResult result() {
275 return new ApiResult(this);
276 }
277 }
278
279 public static class ApiBackupFilesResponse extends ApiResponse {
280 protected List<Map<String, String>> files;
281 protected long total = 0;
282
283 public ApiBackupFilesResponse files(final List<Map<String, String>> files) {
284 this.files = files;
285 return this;
286 }
287
288 public ApiBackupFilesResponse total(final long total) {
289 this.total = total;
290 return this;
291 }
292
293 @Override
294 public ApiResult result() {
295 return new ApiResult(this);
296 }
297 }
298
299 public static class ApiSystemInfoResponse extends ApiResponse {
300 protected List<Map<String, String>> envProps;
301 protected List<Map<String, String>> systemProps;
302 protected List<Map<String, String>> fessProps;
303 protected List<Map<String, String>> bugReportProps;
304
305 public ApiSystemInfoResponse envProps(final List<Map<String, String>> envProps) {
306 this.envProps = envProps;
307 return this;
308 }
309
310 public ApiSystemInfoResponse systemProps(final List<Map<String, String>> systemProps) {
311 this.systemProps = systemProps;
312 return this;
313 }
314
315 public ApiSystemInfoResponse fessProps(final List<Map<String, String>> fessProps) {
316 this.fessProps = fessProps;
317 return this;
318 }
319
320 public ApiSystemInfoResponse bugReportProps(final List<Map<String, String>> bugReportProps) {
321 this.bugReportProps = bugReportProps;
322 return this;
323 }
324
325 @Override
326 public ApiResult result() {
327 return new ApiResult(this);
328 }
329 }
330
331 public static class ApiErrorResponse extends ApiResponse {
332 protected String message;
333
334 public ApiErrorResponse message(final String message) {
335 this.message = message;
336 return this;
337 }
338
339 public ApiErrorResponse message(final VaMessenger<FessMessages> validationMessagesLambda) {
340 final FessMessages messages = new FessMessages();
341 validationMessagesLambda.message(messages);
342 message =
343 ComponentUtil.getMessageManager()
344 .toMessageList(LaRequestUtil.getOptionalRequest().map(r -> r.getLocale()).orElse(Locale.ENGLISH), messages)
345 .stream().collect(Collectors.joining(" "));
346 return this;
347 }
348 }
349 }