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.filter;
17  
18  import java.io.IOException;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.cors.CorsHandler;
24  import org.codelibs.fess.cors.CorsHandlerFactory;
25  import org.codelibs.fess.util.ComponentUtil;
26  
27  import jakarta.servlet.Filter;
28  import jakarta.servlet.FilterChain;
29  import jakarta.servlet.ServletException;
30  import jakarta.servlet.ServletRequest;
31  import jakarta.servlet.ServletResponse;
32  import jakarta.servlet.http.HttpServletRequest;
33  import jakarta.servlet.http.HttpServletResponse;
34  
35  /**
36   * Filter for handling Cross-Origin Resource Sharing (CORS) requests.
37   * Processes CORS headers and handles preflight OPTIONS requests.
38   */
39  public class CorsFilter implements Filter {
40  
41      /**
42       * Creates a new instance of CorsFilter.
43       */
44      public CorsFilter() {
45          // Default constructor
46      }
47  
48      private static final Logger logger = LogManager.getLogger(CorsFilter.class);
49  
50      /**
51       * HTTP OPTIONS method constant used for CORS preflight requests.
52       */
53      protected static final String OPTIONS = "OPTIONS";
54  
55      @Override
56      public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
57              throws IOException, ServletException {
58          final HttpServletRequest httpRequest = (HttpServletRequest) request;
59          final String origin = httpRequest.getHeader("Origin");
60          if (StringUtil.isNotBlank(origin)) {
61              if (logger.isDebugEnabled()) {
62                  logger.debug("HTTP Request: method={}", httpRequest.getMethod());
63              }
64              final CorsHandlerFactory factory = ComponentUtil.getCorsHandlerFactory();
65              final CorsHandler handler = factory.get(origin);
66              if (handler != null) {
67                  handler.process(origin, request, response);
68  
69                  if (OPTIONS.equals(httpRequest.getMethod())) {
70                      final HttpServletResponse httpResponse = (HttpServletResponse) response;
71                      httpResponse.setStatus(HttpServletResponse.SC_ACCEPTED);
72                      return;
73                  }
74              } else if (logger.isDebugEnabled()) {
75                  logger.debug("No CorsHandler: origin={}", origin);
76              }
77          }
78  
79          chain.doFilter(request, response);
80      }
81  
82  }