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