001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
060            public void service(
061                            HttpServletRequest request, HttpServletResponse response)
062                    throws IOException, ServletException {
063    
064                    String uri = request.getPathInfo();
065    
066                    if ((uri == null) || (uri.length() == 0)) {
067                            throw new ServletException("Path information is not specified");
068                    }
069    
070                    String[] paths = uri.split(StringPool.SLASH);
071    
072                    if (paths.length < 2) {
073                            throw new ServletException("Path " + uri + " is invalid");
074                    }
075    
076                    HttpServlet delegate = _delegates.get(paths[1]);
077    
078                    if (delegate == null) {
079                            throw new ServletException(
080                                    "No servlet registred for context " + paths[1]);
081                    }
082    
083                    Thread currentThread = Thread.currentThread();
084    
085                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
086    
087                    try {
088                            ClassLoader delegateClassLoader =
089                                    delegate.getClass().getClassLoader();
090    
091                            currentThread.setContextClassLoader(delegateClassLoader);
092    
093                            delegate.service(request, response);
094                    }
095                    finally {
096                            currentThread.setContextClassLoader(contextClassLoader);
097                    }
098            }
099    
100            private static Map<String, HttpServlet> _delegates =
101                    new HashMap<String, HttpServlet>();
102    
103    }