Class WebApiUtil

java.lang.Object
org.codelibs.fess.util.WebApiUtil

public final class WebApiUtil extends Object
Utility class for web API operations. Provides functionality for setting and retrieving objects from request attributes, error handling, and validation in web API context.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    Gets an object from the current request attributes.
    static boolean
    isApiRequestUri(String requestUri, String contextPath)
    Determines whether a request URI addresses one of the web API endpoints.
    static void
    setError(int statusCode, Exception e)
    Sets an error in the current request with the specified status code and exception.
    static void
    setError(int statusCode, String message)
    Sets an error in the current request with the specified status code and message.
    static void
    setObject(String name, Object value)
    Sets an object in the current request attributes.
    static void
    Validates the current request by checking for stored web API exceptions.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isApiRequestUri

      public static boolean isApiRequestUri(String requestUri, String contextPath)
      Determines whether a request URI addresses one of the web API endpoints.

      This approximates the WebApiManager#matches(HttpServletRequest) implementations, which are the established way Fess tells an API request from a browser request: each manager claims a path prefix and matches on the servlet path. This method exists for callers that cannot consult the managers directly because the servlet path no longer describes the original request -- notably the container error page, where the path elements describe the error page itself and only the jakarta.servlet.error.request_uri attribute still carries the original URI.

      An approximation, not a faithful copy, in two ways. It knows only the prefixes claimed by the managers bundled here; plugins register further ones (/api/v1, /json, /suggest, /mcp) that it cannot see. And it matches the raw, undecoded URI, whereas the managers compare the decoded servlet path, so a percent-encoded prefix reads as a browser request. Both are tolerable because the only caller decides whether to replace an error status with an HTML page: every manager that can reach a container error page issues sendError under /admin/server_, and a miss merely takes the browser arm, as this branch did unconditionally before.

      Deliberately dependency-free: it performs no component lookup and reads no configuration, so it stays safe to call while rendering an error response.

      Parameters:
      requestUri - The original request URI, including the context path (may be null)
      contextPath - The context path to strip, as returned by getContextPath() (may be null)
      Returns:
      true if the URI addresses a web API endpoint
    • setObject

      public static void setObject(String name, Object value)
      Sets an object in the current request attributes.
      Parameters:
      name - The attribute name
      value - The attribute value
    • getObject

      public static <T> T getObject(String name)
      Gets an object from the current request attributes.
      Type Parameters:
      T - The type of the object
      Parameters:
      name - The attribute name
      Returns:
      The attribute value, or null if not found
    • setError

      public static void setError(int statusCode, String message)
      Sets an error in the current request with the specified status code and message.
      Parameters:
      statusCode - The HTTP status code
      message - The error message
    • setError

      public static void setError(int statusCode, Exception e)
      Sets an error in the current request with the specified status code and exception.
      Parameters:
      statusCode - The HTTP status code
      e - The exception that caused the error
    • validate

      public static void validate()
      Validates the current request by checking for stored web API exceptions. Throws any stored WebApiException if found.
      Throws:
      WebApiException - If a web API exception was previously stored