KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > filters > MgnlServletDispatcherFilter


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.filters;
14
15 import info.magnolia.cms.util.ClassUtil;
16
17 import java.io.IOException JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.Filter JavaDoc;
23 import javax.servlet.FilterChain JavaDoc;
24 import javax.servlet.FilterConfig JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38 /**
39  * This class initializes the current context.
40  * @author Philipp Bracher
41  * @version $Revision: 6341 $ ($Author: philipp $)
42  */

43 public class MgnlServletDispatcherFilter implements Filter JavaDoc {
44
45     /**
46      * filterConfig.
47      */

48     private FilterConfig JavaDoc filterConfig;
49
50     private Map JavaDoc servletMap = new Hashtable JavaDoc();
51
52     /**
53      * Logger.
54      */

55     private static Logger log = LoggerFactory.getLogger(MgnlServletDispatcherFilter.class);
56
57     /**
58      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
59      */

60     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
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     /**
78      * @see javax.servlet.Filter#destroy()
79      */

80     public void destroy() {
81         servletMap.clear();
82     }
83
84     /**
85      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
86      * javax.servlet.FilterChain)
87      */

88     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc resp, FilterChain JavaDoc chain) throws IOException JavaDoc,
89         ServletException JavaDoc {
90
91         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
92         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) resp;
93
94         String JavaDoc requestURI = getRequestUri(request);
95
96         for (Iterator JavaDoc iter = servletMap.keySet().iterator(); iter.hasNext();) {
97             String JavaDoc path = (String JavaDoc) iter.next();
98             if (requestURI.startsWith(path)) {
99
100                 HttpServletRequest JavaDoc alteredRequest = new AlteredHttpServletRequest(request, path);
101                 log.info("Dispatching to servlet {} - pathInfo={} requestURI={}", new Object JavaDoc[]{
102                     path,
103                     alteredRequest.getPathInfo(),
104                     requestURI});
105                 HttpServlet JavaDoc servlet = (HttpServlet JavaDoc) 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     /**
119      * @param request
120      * @return
121      */

122     private String JavaDoc getRequestUri(HttpServletRequest JavaDoc request) {
123         // handle includes
124
String JavaDoc requestURI = (String JavaDoc) request.getAttribute("javax.servlet.include.path_info");
125
126         // handle forwards
127
if (StringUtils.isEmpty(requestURI)) {
128             requestURI = (String JavaDoc) request.getAttribute("javax.servlet.forward.path_info");
129         }
130
131         // standard request
132
if (StringUtils.isEmpty(requestURI)) {
133             requestURI = request.getPathInfo();
134         }
135         return requestURI;
136     }
137
138     private void addServlet(String JavaDoc path, String JavaDoc className, Map JavaDoc params) {
139
140         try {
141             HttpServlet JavaDoc servlet = (HttpServlet JavaDoc) 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 JavaDoc 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 JavaDoc {
159
160         /**
161          * The path to which this servlet is mapped.
162          */

163         private String JavaDoc mapping;
164
165         /**
166          * @param request
167          */

168         public AlteredHttpServletRequest(HttpServletRequest JavaDoc request, String JavaDoc mapping) {
169             super(request);
170             this.mapping = mapping;
171         }
172
173         /**
174          * @see javax.servlet.http.HttpServletRequestWrapper#getPathInfo()
175          */

176         public String JavaDoc getPathInfo() {
177
178             String JavaDoc 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