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.util;
17  
18  import org.codelibs.fess.exception.WebApiException;
19  import org.lastaflute.web.util.LaRequestUtil;
20  
21  /**
22   * Utility class for web API operations.
23   * Provides functionality for setting and retrieving objects from request attributes,
24   * error handling, and validation in web API context.
25   */
26  public final class WebApiUtil {
27  
28      /**
29       * Request attribute key for storing web API exceptions.
30       */
31      private static final String WEB_API_EXCEPTION = "webApiException";
32  
33      /**
34       * Private constructor to prevent instantiation.
35       */
36      private WebApiUtil() {
37      }
38  
39      /**
40       * Sets an object in the current request attributes.
41       *
42       * @param name The attribute name
43       * @param value The attribute value
44       */
45      public static void setObject(final String name, final Object value) {
46          LaRequestUtil.getOptionalRequest().ifPresent(req -> req.setAttribute(name, value));
47      }
48  
49      /**
50       * Gets an object from the current request attributes.
51       *
52       * @param <T> The type of the object
53       * @param name The attribute name
54       * @return The attribute value, or null if not found
55       */
56      @SuppressWarnings("unchecked")
57      public static <T> T getObject(final String name) {
58          return LaRequestUtil.getOptionalRequest().map(req -> (T) req.getAttribute(name)).orElse(null);
59      }
60  
61      /**
62       * Sets an error in the current request with the specified status code and message.
63       *
64       * @param statusCode The HTTP status code
65       * @param message The error message
66       */
67      public static void setError(final int statusCode, final String message) {
68          LaRequestUtil.getOptionalRequest().ifPresent(req -> req.setAttribute(WEB_API_EXCEPTION, new WebApiException(statusCode, message)));
69      }
70  
71      /**
72       * Sets an error in the current request with the specified status code and exception.
73       *
74       * @param statusCode The HTTP status code
75       * @param e The exception that caused the error
76       */
77      public static void setError(final int statusCode, final Exception e) {
78          LaRequestUtil.getOptionalRequest().ifPresent(req -> req.setAttribute(WEB_API_EXCEPTION, new WebApiException(statusCode, e)));
79      }
80  
81      /**
82       * Validates the current request by checking for stored web API exceptions.
83       * Throws any stored WebApiException if found.
84       *
85       * @throws WebApiException If a web API exception was previously stored
86       */
87      public static void validate() {
88          LaRequestUtil.getOptionalRequest().map(req -> (WebApiException) req.getAttribute(WEB_API_EXCEPTION)).ifPresent(e -> {
89              throw e;
90          });
91      }
92  
93  }