/** * Copyright (C) 2005-2009 Alfresco Software Limited. * * This file is part of the Spring Surf Extension project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.alfresco.repo.web.scripts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.extensions.config.ServerProperties; import org.springframework.extensions.surf.util.URLDecoder; import org.springframework.extensions.webscripts.Match; import org.springframework.extensions.webscripts.RuntimeContainer; import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.servlet.ServletAuthenticatorFactory; import org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime; /** * HTTP Servlet Web Script Runtime which can handle a tenant id in a web script path * * @author davidc */ public class TenantWebScriptServletRuntime extends WebScriptServletRuntime { public TenantWebScriptServletRuntime(RuntimeContainer container, ServletAuthenticatorFactory authFactory, HttpServletRequest req, HttpServletResponse res, ServerProperties serverProperties) { super(container, authFactory, req, res, serverProperties); } /* (non-Javadoc) * @see org.alfresco.web.scripts.WebScriptRuntime#getScriptUrl() */ @Override protected String getScriptUrl() { // NOTE: Don't use req.getPathInfo() - it truncates the path at first semi-colon in Tomcat final String requestURI = req.getRequestURI(); final String serviceContextPath = req.getContextPath() + req.getServletPath(); String pathInfo; if (serviceContextPath.length() > requestURI.length()) { // NOTE: assume a redirect has taken place e.g. tomcat welcome-page // NOTE: this is unlikely, and we'll take the hit if the path contains a semi-colon pathInfo = req.getPathInfo(); } else { pathInfo = URLDecoder.decode(requestURI.substring(serviceContextPath.length())); } // ensure tenant is specified at beginning of path // NOTE: must contain at least root / and single character for tenant name if (pathInfo.length() < 2) { throw new WebScriptException("Missing tenant name in path: " + pathInfo); } // remove tenant int idx = pathInfo.indexOf('/', 1); pathInfo = pathInfo.substring(idx == -1 ? pathInfo.length() : idx); return pathInfo; } /* (non-Javadoc) * @see org.alfresco.web.scripts.WebScriptRuntime#createRequest(org.alfresco.web.scripts.WebScriptMatch) */ @Override protected WebScriptRequest createRequest(Match match) { // TODO: construct org.springframework.extensions.webscripts.servlet.WebScriptServletResponse when // org.alfresco.web.scripts.WebScriptServletResponse (deprecated) is removed servletReq = new TenantWebScriptServletRequest(this, req, match, serverProperties); return servletReq; } /* (non-Javadoc) * @see org.alfresco.web.scripts.WebScriptContainer#getName() */ public String getName() { return "TenantServletRuntime"; } }