KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > filters > RequestFilter


1 package org.roller.presentation.filters;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.struts.Globals;
6 import org.roller.RollerException;
7 import org.roller.model.Roller;
8 import org.roller.model.UserManager;
9 import org.roller.presentation.util.RequestUtil;
10 import org.roller.presentation.RollerContext;
11 import org.roller.presentation.RollerRequest;
12
13 import java.io.IOException JavaDoc;
14 import java.io.UnsupportedEncodingException JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.Locale JavaDoc;
17
18 import javax.servlet.Filter JavaDoc;
19 import javax.servlet.FilterChain JavaDoc;
20 import javax.servlet.FilterConfig JavaDoc;
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.ServletRequest JavaDoc;
23 import javax.servlet.ServletResponse JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import javax.servlet.jsp.jstl.core.Config;
28
29
30 /**
31  * Entry point filter for Weblog page and Editor UI, this filter
32  * creates a RollerRequest object to parse pathinfo and request parameters.
33  *
34  * @web.filter name="RequestFilter"
35  *
36  * @author David M. Johnson, Matt Raible
37  */

38 public class RequestFilter implements Filter JavaDoc
39 {
40     private FilterConfig JavaDoc mFilterConfig = null;
41     private static Log mLogger =
42         LogFactory.getFactory().getInstance(RequestFilter.class);
43
44     /**
45      * destroy
46      */

47     public void destroy()
48     {
49     }
50
51     /**
52      * As the first and last filter in the chain, it is necessary that
53      * RequestFilter releases its Roller resources before it returns.
54      */

55     public void doFilter(
56         ServletRequest JavaDoc req, ServletResponse JavaDoc res, FilterChain JavaDoc chain)
57         throws IOException JavaDoc, ServletException JavaDoc
58     {
59         try
60         {
61             // insure that incoming data is parsed as UTF-8
62
req.setCharacterEncoding("UTF-8");
63         }
64         catch (UnsupportedEncodingException JavaDoc e)
65         {
66             throw new ServletException JavaDoc("Can't set incoming encoding to UTF-8");
67         }
68
69         // keep JSTL and Struts Locale's in sync
70
HttpSession JavaDoc session = ((HttpServletRequest JavaDoc)req).getSession();
71         if (null != session)
72         {
73             Locale JavaDoc locale = (Locale JavaDoc)session.getAttribute(Globals.LOCALE_KEY);
74             if (locale == null)
75             {
76                 locale = req.getLocale();
77             }
78             if (req.getParameter("locale") != null)
79             {
80                 locale = new Locale JavaDoc(req.getParameter("locale"));
81             }
82             session.setAttribute(Globals.LOCALE_KEY, locale);
83             Config.set(session, Config.FMT_LOCALE, locale);
84         }
85
86         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)req;
87         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)res;
88         Roller roller = RollerContext.getRoller( request );
89         RollerRequest rreq = null;
90         try
91         {
92             rreq = RollerRequest.getRollerRequest(
93                 request, mFilterConfig.getServletContext());
94
95             // if user wants to be remembered, create a remember me cookie
96
// TODO: Figure out a better place to put this - so it will
97
// only be called when the user initially logs in
98
String JavaDoc username = request.getRemoteUser();
99
100             if (username != null)
101             {
102                 if (session.getAttribute(RollerRequest.LOGIN_COOKIE) != null)
103                 {
104                     session.removeAttribute(RollerRequest.LOGIN_COOKIE);
105
106                     UserManager mgr = rreq.getRoller().getUserManager();
107
108                     String JavaDoc loginCookie = mgr.createLoginCookie(username);
109                     rreq.getRoller().commit();
110                     RequestUtil.setCookie(response, RollerRequest.LOGIN_COOKIE,
111                                           loginCookie, request.getContextPath());
112                 }
113             }
114
115         }
116         catch (RollerException e)
117         {
118             // An error initializing the request is considered to be a 404
119
response.sendError(HttpServletResponse.SC_NOT_FOUND);
120             return;
121         }
122
123         if (session != null)
124         {
125             // look for messages and errors in the request, and if they
126
// exist, stuff them in the request - in Struts 1.2, you don't
127
// need to do this
128
if (session.getAttribute(Globals.MESSAGE_KEY) != null)
129             {
130                 request.setAttribute(Globals.MESSAGE_KEY,
131                         session.getAttribute(Globals.MESSAGE_KEY));
132                 session.removeAttribute(Globals.MESSAGE_KEY);
133             }
134             if (session.getAttribute(Globals.ERROR_KEY) != null)
135             {
136                 request.setAttribute(Globals.ERROR_KEY,
137                         session.getAttribute(Globals.ERROR_KEY));
138                 session.removeAttribute(Globals.ERROR_KEY);
139             }
140         }
141
142         Date JavaDoc updateTime = null;
143         try
144         {
145             updateTime = IfModifiedFilter.getLastPublishedDate(request);
146         }
147         catch (RollerException e1)
148         {
149             mLogger.debug("Getting lastUpdateTime", e1);
150         }
151         if (updateTime != null)
152         {
153             request.setAttribute("updateTime", updateTime);
154         }
155
156         chain.doFilter(req, res);
157     }
158
159     /**
160      * init
161      */

162     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
163     {
164         mFilterConfig = filterConfig;
165     }
166 }
167
168
Popular Tags