KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.config.VirtualURIManager;
16 import info.magnolia.cms.core.Path;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.Filter JavaDoc;
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.FilterConfig JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.commons.lang.ClassUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 /**
36  * Handle redirects configured using VirtualURIMappings.
37  * @author Fabrizio Giustina
38  * @version $Id: MgnlVirtualUriFilter.java 6357 2006-09-13 12:18:04Z scharles $
39  */

40 public class MgnlVirtualUriFilter implements Filter JavaDoc {
41
42     /**
43      * Logger.
44      */

45     private static Logger log = LoggerFactory.getLogger(MgnlVirtualUriFilter.class);
46
47     private static final int REDIRECT = 1;
48
49     private static final int INCLUDE = 2;
50
51     private static final int NO_ACTION = 3;
52
53     /**
54      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
55      */

56     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
57         // unused
58
}
59
60     /**
61      * @see javax.servlet.Filter#destroy()
62      */

63     public void destroy() {
64         // unused
65
}
66
67     /**
68      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
69      * javax.servlet.FilterChain)
70      */

71     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc resp, FilterChain JavaDoc filterChain) throws IOException JavaDoc,
72         ServletException JavaDoc {
73
74         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
75         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) resp;
76
77         int result = redirect(request, response);
78         if (result == REDIRECT) {
79             return;
80         } else if (result == INCLUDE) {
81             ((MagnoliaManagedFilter.CustomFilterChain)filterChain).reset();
82         }
83         filterChain.doFilter(req, resp);
84     }
85
86     /**
87      * Redirect based on the mapping in config/server/.node.xml
88      * @param request HttpServletRequest
89      * @param response HttpServletResponse
90      * @return <code>true</code> if request has been redirected, <code>false</code> otherwise
91      */

92     private int redirect(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
93         String JavaDoc uri = this.getURIMap(request);
94         if (StringUtils.isNotEmpty(uri)) {
95             if (!response.isCommitted()) {
96
97                 if (uri.startsWith("redirect:")) {
98                     try {
99                         response.sendRedirect(request.getContextPath() + StringUtils.substringAfter(uri, "redirect:"));
100                         return REDIRECT;
101                     }
102                     catch (IOException JavaDoc e) {
103                         log.error("Failed to redirect to {}:{}", //$NON-NLS-1$
104
new Object JavaDoc[]{uri, e.getMessage()});
105                     }
106                 } else if (uri.startsWith("forward:")) {
107                     uri = StringUtils.substringAfter(uri, "forward:");
108                     try {
109                         request.getRequestDispatcher(uri).forward(request, response);
110                         return REDIRECT;
111                     } catch (Exception JavaDoc e) {
112                         log.error("Failed to forward to {} - {}:{}", //$NON-NLS-1$
113
new Object JavaDoc[]{uri, ClassUtils.getShortClassName(e.getClass()), e.getMessage()});
114                     }
115                 } else {
116                     Path.setURI(uri, request);
117                     return INCLUDE;
118                 }
119             }
120             else {
121                 log.warn("Response is already committed, cannot forward to {} (original URI was {})",//$NON-NLS-1$
122
uri,
123                     request.getRequestURI());
124             }
125
126             //return true;
127
}
128         return NO_ACTION;
129     }
130
131     /**
132      * @return URI mapping as in ServerInfo
133      * @param request HttpServletRequest
134      */

135     private String JavaDoc getURIMap(HttpServletRequest JavaDoc request) {
136         return VirtualURIManager.getInstance().getURIMapping(Path.getURI(request));
137     }
138
139 }
Popular Tags