001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import java.io.IOException;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import javax.servlet.ServletException;
025    import javax.servlet.http.HttpServlet;
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * <p>
031     * See http://issues.liferay.com/browse/LEP-2297.
032     * </p>
033     *
034     * @author Olaf Fricke
035     * @author Brian Wing Shun Chan
036     */
037    public class PortalDelegatorServlet extends HttpServlet {
038    
039            public static void addDelegate(String subContext, HttpServlet delegate) {
040                    if (subContext == null) {
041                            throw new IllegalArgumentException();
042                    }
043    
044                    if (delegate == null) {
045                            throw new IllegalArgumentException();
046                    }
047    
048                    _delegates.put(subContext, delegate);
049            }
050    
051            public static void removeDelegate(String subContext) {
052                    if (subContext == null) {
053                            throw new IllegalArgumentException();
054                    }
055    
056                    _delegates.remove(subContext);
057            }
058    
059            public void service(
060                            HttpServletRequest request, HttpServletResponse response)
061                    throws IOException, ServletException {
062    
063                    String uri = request.getPathInfo();
064    
065                    if ((uri == null) || (uri.length() == 0)) {
066                            throw new ServletException("Path information is not specified");
067                    }
068    
069                    String[] paths = uri.split(StringPool.SLASH);
070    
071                    if (paths.length < 2) {
072                            throw new ServletException("Path " + uri + " is invalid");
073                    }
074    
075                    HttpServlet delegate = _delegates.get(paths[1]);
076    
077                    if (delegate == null) {
078                            throw new ServletException(
079                                    "No servlet registred for context " + paths[1]);
080                    }
081    
082                    Thread currentThread = Thread.currentThread();
083    
084                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
085    
086                    try {
087                            ClassLoader delegateClassLoader =
088                                    delegate.getClass().getClassLoader();
089    
090                            currentThread.setContextClassLoader(delegateClassLoader);
091    
092                            delegate.service(request, response);
093                    }
094                    finally {
095                            currentThread.setContextClassLoader(contextClassLoader);
096                    }
097            }
098    
099            private static Map<String, HttpServlet> _delegates =
100                    new HashMap<String, HttpServlet>();
101    
102    }