• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2012 Google Inc.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.inject.servlet;
18 
19 import javax.servlet.http.HttpServletRequest;
20 
21 /**
22  * Some servlet utility methods.
23  *
24  * @author ntang@google.com (Michael Tang)
25  */
26 final class ServletUtils {
ServletUtils()27   private ServletUtils() {
28     // private to prevent instantiation.
29   }
30 
31   /**
32    * Gets the context path relative path of the URI. Returns the path of the
33    * resource relative to the context path for a request's URI, or null if no
34    * path can be extracted.
35    */
36   // @Nullable
getContextRelativePath( final HttpServletRequest request)37   public static String getContextRelativePath(
38       // @Nullable
39       final HttpServletRequest request) {
40     if (request != null) {
41       String contextPath = request.getContextPath();
42       String requestURI = request.getRequestURI();
43       if (contextPath.length() < requestURI.length()) {
44         return requestURI.substring(contextPath.length());
45       } else if (requestURI != null && requestURI.trim().length() > 0 &&
46           contextPath.length() == requestURI.length()) {
47         return "/";
48       }
49     }
50     return null;
51   }
52 }
53