⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 requestfilter.java

📁 这个weblogging 设计得比较精巧
💻 JAVA
字号:
package org.roller.presentation.filters;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts.Globals;import org.roller.RollerException;import org.roller.model.Roller;import org.roller.model.UserManager;import org.roller.presentation.util.RequestUtil;import org.roller.presentation.RollerContext;import org.roller.presentation.RollerRequest;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Date;import java.util.Locale;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.servlet.jsp.jstl.core.Config;/** * Entry point filter for Weblog page and Editor UI, this filter  * creates a RollerRequest object to parse pathinfo and request parameters. *  * @web.filter name="RequestFilter" *  * @author David M. Johnson, Matt Raible */public class RequestFilter implements Filter{    private FilterConfig mFilterConfig = null;    private static Log mLogger =        LogFactory.getFactory().getInstance(RequestFilter.class);    /**     * destroy     */    public void destroy()    {    }    /**     * As the first and last filter in the chain, it is necessary that     * RequestFilter releases its Roller resources before it returns.     */    public void doFilter(        ServletRequest req, ServletResponse res, FilterChain chain)        throws IOException, ServletException    {        try        {            // insure that incoming data is parsed as UTF-8            req.setCharacterEncoding("UTF-8");        }        catch (UnsupportedEncodingException e)        {            throw new ServletException("Can't set incoming encoding to UTF-8");        }        // keep JSTL and Struts Locale's in sync        HttpSession session = ((HttpServletRequest)req).getSession();        if (null != session)        {            Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);            if (locale == null)            {                locale = req.getLocale();            }            if (req.getParameter("locale") != null)            {                locale = new Locale(req.getParameter("locale"));            }            session.setAttribute(Globals.LOCALE_KEY, locale);            Config.set(session, Config.FMT_LOCALE, locale);        }        HttpServletRequest request = (HttpServletRequest)req;        HttpServletResponse response = (HttpServletResponse)res;        Roller roller = RollerContext.getRoller( request );        RollerRequest rreq = null;        try         {            rreq = RollerRequest.getRollerRequest(                request, mFilterConfig.getServletContext());            // if user wants to be remembered, create a remember me cookie            // TODO: Figure out a better place to put this - so it will            // only be called when the user initially logs in            String username = request.getRemoteUser();            if (username != null)            {                if (session.getAttribute(RollerRequest.LOGIN_COOKIE) != null)                {                    session.removeAttribute(RollerRequest.LOGIN_COOKIE);                    UserManager mgr = rreq.getRoller().getUserManager();                    String loginCookie = mgr.createLoginCookie(username);                    rreq.getRoller().commit();                    RequestUtil.setCookie(response, RollerRequest.LOGIN_COOKIE,                                          loginCookie, request.getContextPath());                }            }        }        catch (RollerException e)        {            // An error initializing the request is considered to be a 404            response.sendError(HttpServletResponse.SC_NOT_FOUND);            return;        }        if (session != null)        {            // look for messages and errors in the request, and if they            // exist, stuff them in the request - in Struts 1.2, you don't            // need to do this            if (session.getAttribute(Globals.MESSAGE_KEY) != null)            {                request.setAttribute(Globals.MESSAGE_KEY,                        session.getAttribute(Globals.MESSAGE_KEY));                session.removeAttribute(Globals.MESSAGE_KEY);            }            if (session.getAttribute(Globals.ERROR_KEY) != null)            {                request.setAttribute(Globals.ERROR_KEY,                        session.getAttribute(Globals.ERROR_KEY));                session.removeAttribute(Globals.ERROR_KEY);            }        }        Date updateTime = null;        try        {            updateTime = IfModifiedFilter.getLastPublishedDate(request);        }        catch (RollerException e1)        {            mLogger.debug("Getting lastUpdateTime", e1);        }        if (updateTime != null)        {            request.setAttribute("updateTime", updateTime);        }        chain.doFilter(req, res);    }    /**     * init     */    public void init(FilterConfig filterConfig) throws ServletException    {        mFilterConfig = filterConfig;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -