View Javadoc
1   /*
2    * Copyright 2012-2021 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.mylasta.direction.sponsor;
17  
18  import java.util.stream.Collectors;
19  
20  import org.codelibs.fess.app.web.api.ApiResult;
21  import org.codelibs.fess.app.web.api.ApiResult.ApiErrorResponse;
22  import org.codelibs.fess.app.web.api.ApiResult.Status;
23  import org.dbflute.optional.OptionalThing;
24  import org.lastaflute.web.api.ApiFailureHook;
25  import org.lastaflute.web.api.ApiFailureResource;
26  import org.lastaflute.web.login.exception.LoginUnauthorizedException;
27  import org.lastaflute.web.response.ApiResponse;
28  import org.lastaflute.web.response.JsonResponse;
29  
30  /**
31   * @author jflute
32   */
33  public class FessApiFailureHook implements ApiFailureHook { // #change_it for handling API failure
34  
35      // ===================================================================================
36      //                                                                          Definition
37      //                                                                          ==========
38      protected static final int HTTP_BAD_REQUEST = 400;
39      protected static final int HTTP_UNAUTHORIZED = 401;
40  
41      // ===================================================================================
42      //                                                                    Business Failure
43      //                                                                    ================
44      @Override
45      public ApiResponse handleValidationError(final ApiFailureResource resource) {
46          return asJson(createFailureBean(Status.BAD_REQUEST, createMessage(resource, null))).httpStatus(HTTP_BAD_REQUEST);
47      }
48  
49      @Override
50      public ApiResponse handleApplicationException(final ApiFailureResource resource, final RuntimeException cause) {
51          if (cause instanceof LoginUnauthorizedException) {
52              return asJson(createFailureBean(Status.UNAUTHORIZED, "Unauthorized request.")).httpStatus(HTTP_UNAUTHORIZED);
53          }
54          return asJson(createFailureBean(Status.BAD_REQUEST, createMessage(resource, cause))).httpStatus(HTTP_BAD_REQUEST);
55      }
56  
57      // ===================================================================================
58      //                                                                      System Failure
59      //                                                                      ==============
60      @Override
61      public OptionalThing<ApiResponse> handleClientException(final ApiFailureResource resource, final RuntimeException cause) {
62          return OptionalThing.empty(); // means empty body (HTTP status will be automatically sent)
63      }
64  
65      @Override
66      public OptionalThing<ApiResponse> handleServerException(final ApiFailureResource resource, final Throwable cause) {
67          return OptionalThing.empty(); // means empty body (HTTP status will be automatically sent)
68      }
69  
70      // ===================================================================================
71      //                                                                        Assist Logic
72      //                                                                        ============
73      protected JsonResponse<ApiResult> asJson(final ApiResult bean) {
74          return new JsonResponse<>(bean);
75      }
76  
77      protected ApiResult createFailureBean(final Status status, final String message) {
78          return new ApiErrorResponse().message(message).status(status).result();
79      }
80  
81      protected String createMessage(final ApiFailureResource resource, final RuntimeException cause) {
82          if (!resource.getMessageList().isEmpty()) {
83              return resource.getMessageList().stream().collect(Collectors.joining(" "));
84          }
85          if (cause != null) {
86              return cause.getMessage();
87          }
88          return "Unknown error";
89      }
90  
91  }