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 ; 14 import java.io.UnsupportedEncodingException ; 15 import java.util.Date ; 16 import java.util.Locale ; 17 18 import javax.servlet.Filter ; 19 import javax.servlet.FilterChain ; 20 import javax.servlet.FilterConfig ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.ServletRequest ; 23 import javax.servlet.ServletResponse ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 import javax.servlet.jsp.jstl.core.Config; 28 29 30 38 public class RequestFilter implements Filter 39 { 40 private FilterConfig mFilterConfig = null; 41 private static Log mLogger = 42 LogFactory.getFactory().getInstance(RequestFilter.class); 43 44 47 public void destroy() 48 { 49 } 50 51 55 public void doFilter( 56 ServletRequest req, ServletResponse res, FilterChain chain) 57 throws IOException , ServletException 58 { 59 try 60 { 61 req.setCharacterEncoding("UTF-8"); 63 } 64 catch (UnsupportedEncodingException e) 65 { 66 throw new ServletException ("Can't set incoming encoding to UTF-8"); 67 } 68 69 HttpSession session = ((HttpServletRequest )req).getSession(); 71 if (null != session) 72 { 73 Locale locale = (Locale )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 (req.getParameter("locale")); 81 } 82 session.setAttribute(Globals.LOCALE_KEY, locale); 83 Config.set(session, Config.FMT_LOCALE, locale); 84 } 85 86 HttpServletRequest request = (HttpServletRequest )req; 87 HttpServletResponse response = (HttpServletResponse )res; 88 Roller roller = RollerContext.getRoller( request ); 89 RollerRequest rreq = null; 90 try 91 { 92 rreq = RollerRequest.getRollerRequest( 93 request, mFilterConfig.getServletContext()); 94 95 String 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 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 response.sendError(HttpServletResponse.SC_NOT_FOUND); 120 return; 121 } 122 123 if (session != null) 124 { 125 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 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 162 public void init(FilterConfig filterConfig) throws ServletException 163 { 164 mFilterConfig = filterConfig; 165 } 166 } 167 168 | Popular Tags |