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