Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package com.iterzp.momo.filter; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.util.AntPathMatcher; import org.springframework.web.filter.OncePerRequestFilter; import com.iterzp.momo.entity.Setting; import com.iterzp.momo.utils.SettingUtils; /** * Filter - ? * * @author yd14 Team * @version 3.0 */ @Component("siteStatusFilter") public class SiteStatusFilter extends OncePerRequestFilter { /** URL */ private static final String[] DEFAULT_IGNORE_URL_PATTERNS = new String[] { "/admin/**" }; /** ??URL */ private static final String DEFAULT_REDIRECT_URL = "/common/site_close.jhtml"; /** antPathMatcher */ private static AntPathMatcher antPathMatcher = new AntPathMatcher(); /** URL */ private String[] ignoreUrlPatterns = DEFAULT_IGNORE_URL_PATTERNS; /** ??URL */ private String redirectUrl = DEFAULT_REDIRECT_URL; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { Setting setting = SettingUtils.get(); if (setting.getIsSiteEnabled()) { filterChain.doFilter(request, response); } else { String path = request.getServletPath(); if (path.equals(redirectUrl)) { filterChain.doFilter(request, response); return; } if (ignoreUrlPatterns != null) { for (String ignoreUrlPattern : ignoreUrlPatterns) { if (antPathMatcher.match(ignoreUrlPattern, path)) { filterChain.doFilter(request, response); return; } } } response.sendRedirect(request.getContextPath() + redirectUrl); } } /** * ?URL * * @return URL */ public String[] getIgnoreUrlPatterns() { return ignoreUrlPatterns; } /** * URL * * @param ignoreUrlPatterns * URL */ public void setIgnoreUrlPatterns(String[] ignoreUrlPatterns) { this.ignoreUrlPatterns = ignoreUrlPatterns; } /** * ???URL * * @return ??URL */ public String getRedirectUrl() { return redirectUrl; } /** * ??URL * * @param redirectUrl * ??URL */ public void setRedirectUrl(String redirectUrl) { this.redirectUrl = redirectUrl; } }