Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License.package com.google.gerrit.httpd.plugins; 14 : 15 : package com.google.gerrit.httpd.plugins; 16 : 17 : import com.google.common.base.Strings; 18 : import javax.servlet.http.HttpServletRequest; 19 : import javax.servlet.http.HttpServletRequestWrapper; 20 : 21 : class ContextMapper { 22 : private static final String PLUGINS_PREFIX = "/plugins/"; 23 : private static final String AUTHORIZED_PREFIX = "/a" + PLUGINS_PREFIX; 24 : private final String base; 25 : private final String authorizedBase; 26 : 27 100 : ContextMapper(String contextPath) { 28 100 : base = Strings.nullToEmpty(contextPath) + PLUGINS_PREFIX; 29 100 : authorizedBase = Strings.nullToEmpty(contextPath) + AUTHORIZED_PREFIX; 30 100 : } 31 : 32 : private static boolean isAuthorizedCall(HttpServletRequest req) { 33 2 : return !Strings.isNullOrEmpty(req.getServletPath()) 34 2 : && req.getServletPath().startsWith(AUTHORIZED_PREFIX); 35 : } 36 : 37 : HttpServletRequest create(HttpServletRequest req, String name) { 38 2 : String contextPath = (isAuthorizedCall(req) ? authorizedBase : base) + name; 39 : 40 2 : return new WrappedRequest(req, contextPath); 41 : } 42 : 43 : public String getFullPath(String name) { 44 1 : return base + name; 45 : } 46 : 47 : private static class WrappedRequest extends HttpServletRequestWrapper { 48 : private final String contextPath; 49 : private final String pathInfo; 50 : 51 : private WrappedRequest(HttpServletRequest req, String contextPath) { 52 2 : super(req); 53 2 : this.contextPath = contextPath; 54 2 : this.pathInfo = getRequestURI().substring(contextPath.length()); 55 2 : } 56 : 57 : @Override 58 : public String getServletPath() { 59 2 : return ""; 60 : } 61 : 62 : @Override 63 : public String getContextPath() { 64 2 : return contextPath; 65 : } 66 : 67 : @Override 68 : public String getPathInfo() { 69 1 : return pathInfo; 70 : } 71 : } 72 : }