java 过滤器Filter

一、首先在web.xml里进行拦截过滤

  <filter>
        <filter-name>platformServiceAgreementFilter</filter-name>
        <filter-class>com.yingle.web.lawyer.filter.PlatformServiceAgreementFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>platformServiceAgreementFilter</filter-name>
        <url-pattern>/faces/pc/secure/v/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>platformServiceAgreementFilter</filter-name>
        <url-pattern>/faces/mobile/secure/v/*</url-pattern>
    </filter-mapping>

 

二、在com.yingle.web.lawyer.filter.PlatformServiceAgreementFilter包里处理响应

PlatformServiceAgreementFilter extends HttpFilter


    /**
     *
     */
    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session,
        FilterChain chain) throws ServletException, IOException {
        Integer userProfileId = LoggedUserUtil.getLoggedUserProfileId();
        try {
            LawyerProfile lawyer = null;
            if (null != userProfileId) {
                lawyer = lawyerProfileService.getById(userProfileId);
            }
            Page page = pageConfig.getLatest(PageConfig.PLATFORM_SERVICE_AGREEMENT);
            if (null != lawyer && null != page) {
                Date publishTime = page.getPublishTime();
                if (null == lawyer.getAcceptServiceAgreementTime()
                    || lawyer.getAcceptServiceAgreementTime().compareTo(publishTime) <= 0) {
                    String userAccessEnvironment = FacesEnvironmentContextListener.getUserAccessEnvironment(request);
                    String originRequesetURL = request.getRequestURL().toString();
                    String queryString = request.getQueryString();

                    if (StringUtil.isNotEmpty(queryString)) {
                        if (queryString.indexOf("faces-redirect") >= 0) {
                            originRequesetURL = originRequesetURL + "?" + queryString;
                        } else {
                            originRequesetURL = originRequesetURL + "?faces-redirect=true&" + queryString;
                        }
                    } else {
                        originRequesetURL = originRequesetURL + "?faces-redirect=true";
                    }

                    session.setAttribute(SessionKey.REQUEST_URL_BEFORE_AGREEMENT_KEY, originRequesetURL);

                    String redirectPagePath =
                        request.getContextPath()
                            + String.format(PLATFORM_SERVICE_AGREEMENT_URL, userAccessEnvironment);
                    ServletUtil.redirect(response, redirectPagePath);
                }
            }
        } catch (ApplicationServiceException e) {
            throw new ServletException("encounter error while showing platform service agreement", e);
        }
        chain.doFilter(request, response);
    }

 

posted @ 2016-12-19 14:33  潇潇留客  阅读(145)  评论(0编辑  收藏  举报