View Javadoc
1   /*
2    * Copyright 2012-2017 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          } else {
54              return asJson(createFailureBean(Status.BAD_REQUEST, createMessage(resource, cause))).httpStatus(HTTP_BAD_REQUEST);
55          }
56      }
57  
58      // ===================================================================================
59      //                                                                      System Failure
60      //                                                                      ==============
61      @Override
62      public OptionalThing<ApiResponse> handleClientException(final ApiFailureResource resource, final RuntimeException cause) {
63          return OptionalThing.empty(); // means empty body (HTTP status will be automatically sent)
64      }
65  
66      @Override
67      public OptionalThing<ApiResponse> handleServerException(final ApiFailureResource resource, final Throwable cause) {
68          return OptionalThing.empty(); // means empty body (HTTP status will be automatically sent)
69      }
70  
71      // ===================================================================================
72      //                                                                        Assist Logic
73      //                                                                        ============
74      protected JsonResponse<ApiResult> asJson(final ApiResult bean) {
75          return new JsonResponse<>(bean);
76      }
77  
78      protected ApiResult createFailureBean(final Status status, final String message) {
79          return new ApiErrorResponse().message(message).status(status).result();
80      }
81  
82      protected String createMessage(final ApiFailureResource resource, final RuntimeException cause) {
83          if (!resource.getMessageList().isEmpty()) {
84              return resource.getMessageList().stream().collect(Collectors.joining(" "));
85          }
86          if (cause != null) {
87              return cause.getMessage();
88          }
89          return "Unknown error";
90      }
91  
92  }