1 13 package info.magnolia.cms.filters; 14 15 import info.magnolia.cms.util.ClassUtil; 16 17 import java.io.IOException ; 18 import java.util.Hashtable ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import javax.servlet.Filter ; 23 import javax.servlet.FilterChain ; 24 import javax.servlet.FilterConfig ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletRequestWrapper ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.apache.commons.lang.StringUtils; 34 import org.slf4j.Logger; 35 import org.slf4j.LoggerFactory; 36 37 38 43 public class MgnlServletDispatcherFilter implements Filter { 44 45 48 private FilterConfig filterConfig; 49 50 private Map servletMap = new Hashtable (); 51 52 55 private static Logger log = LoggerFactory.getLogger(MgnlServletDispatcherFilter.class); 56 57 60 public void init(FilterConfig filterConfig) throws ServletException { 61 this.filterConfig = filterConfig; 62 addServlet("/ActivationHandler", "info.magnolia.cms.exchange.simple.SimpleExchangeServlet", null); 63 addServlet("/.resources", "info.magnolia.cms.servlets.ClasspathSpool", null); 64 addServlet("/.magnolia/trees/", "info.magnolia.module.admininterface.AdminTreeMVCServlet", null); 65 addServlet("/.magnolia/dialogs/", "info.magnolia.module.admininterface.DialogMVCServlet", null); 66 addServlet("/.magnolia/pages/", "info.magnolia.module.admininterface.PageMVCServlet", null); 67 addServlet("/.magnolia/log4j/", "it.openutils.log4j.Log4jConfigurationServlet", null); 68 addServlet("/uuid/", "info.magnolia.cms.servlets.UUIDRequestDispatcher", null); 69 addServlet("/.magnolia/cache-all/", "info.magnolia.cms.cache.CacheGeneratorServlet", null); 70 addServlet("/dms/", "info.magnolia.module.dms.DMSDownloadServlet", null); 71 addServlet("/dms-static/", "info.magnolia.module.dms.DMSDownloadServlet", null); 72 addServlet("/.magnolia/flows", "info.magnolia.module.workflow.servlets.FlowDefServlet", null); 73 addServlet("/.magnolia/mail/", "info.magnolia.cms.mail.servlets.MgnlMailServlet", null); 74 75 } 76 77 80 public void destroy() { 81 servletMap.clear(); 82 } 83 84 88 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException , 89 ServletException { 90 91 HttpServletRequest request = (HttpServletRequest ) req; 92 HttpServletResponse response = (HttpServletResponse ) resp; 93 94 String requestURI = getRequestUri(request); 95 96 for (Iterator iter = servletMap.keySet().iterator(); iter.hasNext();) { 97 String path = (String ) iter.next(); 98 if (requestURI.startsWith(path)) { 99 100 HttpServletRequest alteredRequest = new AlteredHttpServletRequest(request, path); 101 log.info("Dispatching to servlet {} - pathInfo={} requestURI={}", new Object []{ 102 path, 103 alteredRequest.getPathInfo(), 104 requestURI}); 105 HttpServlet servlet = (HttpServlet ) servletMap.get(path); 106 107 servlet.service(new AlteredHttpServletRequest(request, path), response); 108 109 return; 110 } 111 112 } 113 114 chain.doFilter(request, response); 115 116 } 117 118 122 private String getRequestUri(HttpServletRequest request) { 123 String requestURI = (String ) request.getAttribute("javax.servlet.include.path_info"); 125 126 if (StringUtils.isEmpty(requestURI)) { 128 requestURI = (String ) request.getAttribute("javax.servlet.forward.path_info"); 129 } 130 131 if (StringUtils.isEmpty(requestURI)) { 133 requestURI = request.getPathInfo(); 134 } 135 return requestURI; 136 } 137 138 private void addServlet(String path, String className, Map params) { 139 140 try { 141 HttpServlet servlet = (HttpServlet ) ClassUtil.newInstance(className); 142 servlet.init(); 143 servletMap.put(path, servlet); 144 145 log.info("Registered servlet mapping from {} to {}", path, className); 146 } 147 catch (Throwable e) { 148 log.error("Unable to load servlet " 149 + className 150 + " mapped to path " 151 + path 152 + " due to a " 153 + e.getClass().getName() 154 + " exception", e); 155 } 156 } 157 158 private class AlteredHttpServletRequest extends HttpServletRequestWrapper { 159 160 163 private String mapping; 164 165 168 public AlteredHttpServletRequest(HttpServletRequest request, String mapping) { 169 super(request); 170 this.mapping = mapping; 171 } 172 173 176 public String getPathInfo() { 177 178 String pathInfo = StringUtils.substring(getRequestURI(), mapping.length()); 179 if (!pathInfo.startsWith("/")) { 180 pathInfo = "/" + pathInfo; 181 } 182 return pathInfo; 183 } 184 185 } 186 187 } 188 | Popular Tags |