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.cors;
17  
18  import jakarta.servlet.ServletRequest;
19  import jakarta.servlet.ServletResponse;
20  
21  /**
22   * Abstract base class for handling CORS (Cross-Origin Resource Sharing) requests.
23   * Provides common CORS header constants and defines the processing interface.
24   */
25  public abstract class CorsHandler {
26  
27      /**
28       * Creates a new instance of CorsHandler.
29       */
30      public CorsHandler() {
31          // Default constructor
32      }
33  
34      /**
35       * CORS header for specifying allowed origin.
36       */
37      protected static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
38  
39      /**
40       * CORS header for specifying allowed headers.
41       */
42      protected static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers";
43  
44      /**
45       * CORS header for specifying allowed HTTP methods.
46       */
47      protected static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
48  
49      /**
50       * CORS header for allowing private network access.
51       */
52      protected static final String ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK = "Access-Control-Allow-Private-Network";
53  
54      /**
55       * CORS header for allowing credentials.
56       */
57      protected static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
58  
59      /**
60       * CORS header for specifying cache duration for preflight requests.
61       */
62      protected static final String ACCESS_CONTROL_MAX_AGE = "Access-Control-Max-Age";
63  
64      /**
65       * Processes the CORS request by setting appropriate headers.
66       *
67       * @param origin the origin of the request
68       * @param request the servlet request
69       * @param response the servlet response
70       */
71      public abstract void process(String origin, ServletRequest request, ServletResponse response);
72  
73  }