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                            response.sendError(
068                                    HttpServletResponse.SC_NOT_FOUND,
069                                    "Path information is not specified");
070    
071                            return;
072                    }
073    
074                    String[] paths = uri.split(StringPool.SLASH);
075    
076                    if (paths.length < 2) {
077                            response.sendError(
078                                    HttpServletResponse.SC_NOT_FOUND,
079                                    "Path " + uri + " is invalid");
080    
081                            return;
082                    }
083    
084                    HttpServlet delegate = _delegates.get(paths[1]);
085    
086                    if (delegate == null) {
087                            response.sendError(
088                                    HttpServletResponse.SC_NOT_FOUND,
089                                    "No servlet registred for context " + paths[1]);
090    
091                            return;
092                    }
093    
094                    Thread currentThread = Thread.currentThread();
095    
096                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
097    
098                    try {
099                            ClassLoader delegateClassLoader =
100                                    delegate.getClass().getClassLoader();
101    
102                            currentThread.setContextClassLoader(delegateClassLoader);
103    
104                            delegate.service(request, response);
105                    }
106                    finally {
107                            currentThread.setContextClassLoader(contextClassLoader);
108                    }
109            }
110    
111            private static Map<String, HttpServlet> _delegates =
112                    new HashMap<String, HttpServlet>();
113    
114    }