View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions Copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.http.servlet;
18  
19  import jakarta.servlet.http.HttpServletRequest;
20  
21  import org.forgerock.http.HttpApplication;
22  
23  /**
24   * Determines whether the context path and/or the servlet path will get
25   * consumed when routing to the {@link HttpApplication}.
26   *
27   * <p>Configured by Servlet init-param of
28   * {@link HttpFrameworkServlet#ROUTING_BASE_INIT_PARAM_NAME}.</p>
29   *
30   * <p>By default, if no servlet init-param is set, {@link #SERVLET_PATH} will
31   * be used, meaning that the {@code HttpApplication} will be given requests
32   * which DO NOT contain any information about the web application
33   * configuration.
34   * When {@link #CONTEXT_PATH} is selected the {@code HttpApplication} will be
35   * given requests which DO contain the relative Servlet context path and
36   * therefore will be dependant on the web application configuration.</p>
37   */
38  enum ServletRoutingBase {
39  
40      /**
41       * Only the context path will be consumed when routing requests to the
42       * {@link HttpApplication}.
43       */
44      CONTEXT_PATH {
45          @Override
46          String extractMatchedUri(HttpServletRequest request) {
47              String contextPath = forceEmptyIfNull(request.getContextPath());
48              return contextPath.startsWith("/") ? contextPath.substring(1) : contextPath;
49          }
50      },
51  
52      /**
53       * Both the context path and the servlet path will be consumed when routing
54       * requests to the {@link HttpApplication}.
55       */
56      SERVLET_PATH {
57          @Override
58          String extractMatchedUri(HttpServletRequest request) {
59              String contextPath = CONTEXT_PATH.extractMatchedUri(request);
60              return contextPath + forceEmptyIfNull(request.getServletPath());
61          }
62      };
63  
64      /**
65       * Determines the portion of the request URI that will be matched based on
66       * the selected {@code ServletRoutingBase}.
67       *
68       * @param request The request.
69       * @return The matched portion of the request URI.
70       */
71      abstract String extractMatchedUri(HttpServletRequest request);
72  
73      private static String forceEmptyIfNull(final String s) {
74          return s != null ? s : "";
75      }
76  }