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