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.filter;
17
18 import java.io.IOException;
19
20 import org.codelibs.fess.api.WebApiManager;
21 import org.codelibs.fess.api.WebApiManagerFactory;
22 import org.codelibs.fess.util.ComponentUtil;
23
24 import jakarta.servlet.Filter;
25 import jakarta.servlet.FilterChain;
26 import jakarta.servlet.FilterConfig;
27 import jakarta.servlet.ServletException;
28 import jakarta.servlet.ServletRequest;
29 import jakarta.servlet.ServletResponse;
30 import jakarta.servlet.http.HttpServletRequest;
31 import jakarta.servlet.http.HttpServletResponse;
32
33 /**
34 * Servlet filter for processing web API requests.
35 * This filter intercepts HTTP requests and delegates processing to appropriate web API managers.
36 */
37 public class WebApiFilter implements Filter {
38
39 /**
40 * Default constructor.
41 */
42 public WebApiFilter() {
43 // Default constructor
44 }
45
46 /**
47 * Initializes the web API filter.
48 *
49 * @param filterConfig The filter configuration
50 * @throws ServletException If initialization fails
51 */
52 @Override
53 public void init(final FilterConfig filterConfig) throws ServletException {
54 // nothing
55 }
56
57 /**
58 * Destroys the web API filter and cleans up resources.
59 */
60 @Override
61 public void destroy() {
62 // nothing
63 }
64
65 /**
66 * Filters HTTP requests and processes them through appropriate web API managers.
67 *
68 * @param request The servlet request
69 * @param response The servlet response
70 * @param chain The filter chain
71 * @throws IOException If an I/O error occurs
72 * @throws ServletException If a servlet error occurs
73 */
74 @Override
75 public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
76 throws IOException, ServletException {
77 final WebApiManagerFactory webApiManagerFactory = ComponentUtil.getWebApiManagerFactory();
78 final WebApiManager webApiManager = webApiManagerFactory.get((HttpServletRequest) request);
79 if (webApiManager == null) {
80 chain.doFilter(request, response);
81 } else {
82 webApiManager.process((HttpServletRequest) request, (HttpServletResponse) response, chain);
83 }
84 }
85
86 }