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.api;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.logging.log4j.LogManager;
23  import org.apache.logging.log4j.Logger;
24  
25  import jakarta.servlet.http.HttpServletRequest;
26  
27  /**
28   * Factory class for managing and retrieving web API managers.
29   * This factory maintains a collection of web API managers and provides
30   * functionality to find the appropriate manager for incoming requests.
31   */
32  public class WebApiManagerFactory {
33  
34      private static final Logger logger = LogManager.getLogger(WebApiManagerFactory.class);
35  
36      /**
37       * Default constructor.
38       */
39      public WebApiManagerFactory() {
40          // Default constructor
41      }
42  
43      /**
44       * Array of registered web API managers.
45       */
46      protected WebApiManager[] webApiManagers = {};
47  
48      /**
49       * Adds a web API manager to the factory.
50       *
51       * @param webApiManager The web API manager to add
52       */
53      public void add(final WebApiManager webApiManager) {
54          if (logger.isDebugEnabled()) {
55              logger.debug("Adding WebApiManager. class={}", webApiManager.getClass().getSimpleName());
56          }
57          final List<WebApiManager> list = new ArrayList<>();
58          Collections.addAll(list, webApiManagers);
59          list.add(webApiManager);
60          webApiManagers = list.toArray(new WebApiManager[list.size()]);
61          if (logger.isDebugEnabled()) {
62              logger.debug("WebApiManager added. totalManagers={}", webApiManagers.length);
63          }
64      }
65  
66      /**
67       * Gets the appropriate web API manager for the given request.
68       *
69       * @param request The HTTP servlet request
70       * @return The matching web API manager, or null if no match found
71       */
72      public WebApiManager get(final HttpServletRequest request) {
73          final String servletPath = request.getServletPath();
74          if (logger.isDebugEnabled()) {
75              logger.debug("Looking for WebApiManager. servletPath={}, registeredManagers={}", servletPath, webApiManagers.length);
76          }
77          for (final WebApiManager webApiManager : webApiManagers) {
78              if (webApiManager.matches(request)) {
79                  if (logger.isDebugEnabled()) {
80                      logger.debug("WebApiManager matched. servletPath={}, manager={}", servletPath,
81                              webApiManager.getClass().getSimpleName());
82                  }
83                  return webApiManager;
84              }
85          }
86          if (logger.isDebugEnabled()) {
87              logger.debug("No WebApiManager matched. servletPath={}", servletPath);
88          }
89          return null;
90      }
91  
92  }