1
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
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 }