1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.servlet;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  
19  import java.io.IOException;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  /**
30   * <a href="PortalDelegatorServlet.java.html"><b><i>View Source</i></b></a>
31   *
32   * <p>
33   * See http://issues.liferay.com/browse/LEP-2297.
34   * </p>
35   *
36   * @author Olaf Fricke
37   * @author Brian Wing Shun Chan
38   */
39  public class PortalDelegatorServlet extends HttpServlet {
40  
41      public static void addDelegate(String subContext, HttpServlet delegate) {
42          if (subContext == null) {
43              throw new IllegalArgumentException();
44          }
45  
46          if (delegate == null) {
47              throw new IllegalArgumentException();
48          }
49  
50          _delegates.put(subContext, delegate);
51      }
52  
53      public static void removeDelegate(String subContext) {
54          if (subContext == null) {
55              throw new IllegalArgumentException();
56          }
57  
58          _delegates.remove(subContext);
59      }
60  
61      public void service(
62              HttpServletRequest request, HttpServletResponse response)
63          throws IOException, ServletException {
64  
65          String uri = request.getPathInfo();
66  
67          if ((uri == null) || (uri.length() == 0)) {
68              throw new ServletException("Path information is not specified");
69          }
70  
71          String[] paths = uri.split(StringPool.SLASH);
72  
73          if (paths.length < 2) {
74              throw new ServletException("Path " + uri + " is invalid");
75          }
76  
77          HttpServlet delegate = _delegates.get(paths[1]);
78  
79          if (delegate == null) {
80              throw new ServletException(
81                  "No servlet registred for context " + paths[1]);
82          }
83  
84          Thread currentThread = Thread.currentThread();
85  
86          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
87  
88          try {
89              ClassLoader delegateClassLoader =
90                  delegate.getClass().getClassLoader();
91  
92              currentThread.setContextClassLoader(delegateClassLoader);
93  
94              delegate.service(request, response);
95          }
96          finally {
97              currentThread.setContextClassLoader(contextClassLoader);
98          }
99      }
100 
101     private static Map<String, HttpServlet> _delegates =
102         new HashMap<String, HttpServlet>();
103 
104 }