1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.servlet;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  
24  import java.io.IOException;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServlet;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  /**
35   * <a href="PortalDelegatorServlet.java.html"><b><i>View Source</i></b></a>
36   *
37   * <p>
38   * See http://support.liferay.com/browse/LEP-2297.
39   * </p>
40   *
41   * @author Olaf Fricke
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class PortalDelegatorServlet extends HttpServlet {
46  
47      public static void addDelegate(String subContext, HttpServlet delegate) {
48          if (subContext == null) {
49              throw new IllegalArgumentException();
50          }
51  
52          if (delegate == null) {
53              throw new IllegalArgumentException();
54          }
55  
56          _delegates.put(subContext, delegate);
57      }
58  
59      public static void removeDelegate(String subContext) {
60          if (subContext == null) {
61              throw new IllegalArgumentException();
62          }
63  
64          _delegates.remove(subContext);
65      }
66  
67      public void service(
68              HttpServletRequest request, HttpServletResponse response)
69          throws IOException, ServletException {
70  
71          String uri = request.getPathInfo();
72  
73          if ((uri == null) || (uri.length() == 0)) {
74              throw new ServletException("Path information is not specified");
75          }
76  
77          String[] paths = uri.split(StringPool.SLASH);
78  
79          if (paths.length < 2) {
80              throw new ServletException("Path " + uri + " is invalid");
81          }
82  
83          HttpServlet delegate = _delegates.get(paths[1]);
84  
85          if (delegate == null) {
86              throw new ServletException(
87                  "No servlet registred for context " + paths[1]);
88          }
89  
90          Thread currentThread = Thread.currentThread();
91  
92          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
93  
94          try {
95              ClassLoader delegateClassLoader =
96                  delegate.getClass().getClassLoader();
97  
98              currentThread.setContextClassLoader(delegateClassLoader);
99  
100             delegate.service(request, response);
101         }
102         finally {
103             currentThread.setContextClassLoader(contextClassLoader);
104         }
105     }
106 
107     private static Map<String, HttpServlet> _delegates =
108         new HashMap<String, HttpServlet>();
109 
110 }