View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.api.admin.failureurl;
17  
18  import java.util.List;
19  import java.util.stream.Collectors;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.codelibs.fess.app.pager.FailureUrlPager;
24  import org.codelibs.fess.app.service.FailureUrlService;
25  import org.codelibs.fess.app.web.api.ApiResult;
26  import org.codelibs.fess.app.web.api.ApiResult.ApiLogResponse;
27  import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
28  import org.codelibs.fess.app.web.api.ApiResult.Status;
29  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
30  import org.codelibs.fess.helper.ProcessHelper;
31  import org.codelibs.fess.opensearch.config.exentity.FailureUrl;
32  import org.lastaflute.web.Execute;
33  import org.lastaflute.web.response.JsonResponse;
34  
35  import jakarta.annotation.Resource;
36  
37  /**
38   * API action for admin failure URL.
39   *
40   */
41  public class ApiAdminFailureurlAction extends FessApiAdminAction {
42  
43      private static final Logger logger = LogManager.getLogger(ApiAdminFailureurlAction.class);
44  
45      // ===================================================================================
46      //                                                                         Constructor
47      //                                                                         ===========
48      /**
49       * Default constructor.
50       */
51      public ApiAdminFailureurlAction() {
52          super();
53      }
54  
55      // ===================================================================================
56      //                                                                           Attribute
57      //                                                                           =========
58      @Resource
59      private FailureUrlService failureUrlService;
60      @Resource
61      private FailureUrlPager failureUrlPager;
62      /** Helper for managing crawler processes */
63      @Resource
64      protected ProcessHelper processHelper;
65  
66      // ===================================================================================
67      //                                                                      Search Execute
68      //                                                                      ==============
69  
70      // GET /api/admin/failureurl/logs
71      // PUT /api/admin/failureurl/logs
72      /**
73       * Retrieves failure URL logs with pagination.
74       *
75       * @param body the search criteria
76       * @return JSON response containing the failure URL logs
77       */
78      @Execute
79      public JsonResponse<ApiResult> logs(final SearchBody body) {
80          validateApi(body, messages -> {});
81          final FailureUrlPager pager = copyBeanToNewBean(body, FailureUrlPager.class);
82          final List<FailureUrl> list = failureUrlService.getFailureUrlList(pager);
83          return asJson(new ApiResult.ApiLogsResponse<EditBody>().logs(list.stream().map(this::createEditBody).collect(Collectors.toList()))
84                  .total(pager.getAllRecordCount())
85                  .status(ApiResult.Status.OK)
86                  .result());
87      }
88  
89      // GET /api/admin/failureurl/log/{id}
90      /**
91       * Retrieves a specific failure URL log by ID.
92       *
93       * @param id the failure URL log ID
94       * @return JSON response containing the failure URL log
95       */
96      @Execute
97      public JsonResponse<ApiResult> get$log(final String id) {
98          return asJson(new ApiLogResponse().log(failureUrlService.getFailureUrl(id).map(this::createEditBody).orElseGet(() -> {
99              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
100             return null;
101         })).status(Status.OK).result());
102     }
103 
104     // DELETE /api/admin/failureurl/log/{id}
105     /**
106      * Deletes a failure URL log by ID.
107      *
108      * @param id the failure URL log ID to delete
109      * @return JSON response with result status
110      */
111     @Execute
112     public JsonResponse<ApiResult> delete$log(final String id) {
113         failureUrlService.getFailureUrl(id).ifPresent(entity -> {
114             try {
115                 failureUrlService.delete(entity);
116                 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
117             } catch (final Exception e) {
118                 logger.warn("Failed to process a request.", e);
119                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
120             }
121         }).orElse(() -> {
122             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
123         });
124         return asJson(new ApiResponse().status(Status.OK).result());
125     }
126 
127     // DELETE /api/admin/failureurl/all
128     /**
129      * Deletes all failure URL logs.
130      *
131      * @return JSON response with result status
132      */
133     @Execute
134     public JsonResponse<ApiResult> delete$all() {
135         try {
136             failureUrlService.deleteAll(failureUrlPager);
137             failureUrlPager.clear();
138             saveInfo(messages -> messages.addSuccessFailureUrlDeleteAll(GLOBAL));
139         } catch (final Exception e) {
140             logger.warn("Failed to process a request.", e);
141             throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
142         }
143         return asJson(new ApiResponse().status(Status.OK).result());
144     }
145 
146     /**
147      * Creates an EditBody from a FailureUrl entity.
148      *
149      * @param entity the FailureUrl entity
150      * @return the EditBody representation
151      */
152     protected EditBody createEditBody(final FailureUrl entity) {
153         final EditBody body = new EditBody();
154         copyBeanToBean(entity, body, copyOp -> {
155             copyOp.excludeNull();
156         });
157         return body;
158     }
159 }