Filter系列-AbstractFilter

23

Filter系列-AbstractFilter

抽象filter

public abstract class AbstractFilter {}

抽取公共方法

// 定义默认初始化方法
public void init(FilterConfig config) throws ServletException {}

// 定义默认销毁方法
public void destroy() {}

// 核心方法
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {...
  doFilterInternal(request, response, filterChain);
}

// 抽象执行方法
protected abstract void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
			FilterChain filterChain) throws ServletException, IOException;


工具方法

/**
	 * 假设一个 Web 应用程序的上下文路径(Context Path)是 /myapp,并且用户发出了以下 HTTP 请求:
	 * plaintext
	 * GET /myapp/products/list?category=books&page=2
	 * 在这个例子中,getRequestURI() 返回的是 /myapp/products/list?category=books&page=2,而 getContextPath() 返回的是 /myapp。
	 * 最终方法将返回路径的子字符串 /products/list,这就是请求的 URL 路径部分,没有了上下文路径和任何查询参数。
	 * 因此,parseURL(request) 方法将在这种场景下返回 /products/list。这个字符串可以用于查找应用程序中对应的资源、服务或控制器逻辑。
	 */
protected String parseURL(HttpServletRequest request) {
	    String path = request.getRequestURI();
	    String contextPath = request.getContextPath();
	    int startIndex = path.indexOf(contextPath);
	    int endIndex = path.indexOf("?");
	  
	    if (endIndex == -1) {
	    		return path.substring(startIndex + contextPath.length() + 1);
	    }
	  
	    return path.substring(startIndex + contextPath.length() + 1, endIndex);
	}

代码

public abstract class AbstractFilter {

	public static final Collection<String> HTTP_METHODS = new HashSet<String>(
            Arrays.asList("OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE",
                    "TRACE", "CONNECT"));

	public void init(FilterConfig config) throws ServletException {

	}

	public void destroy() {

	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		if (!(servletRequest instanceof HttpServletRequest)
                || !(servletResponse instanceof HttpServletResponse)) {
            throw new ServletException("Do not support non Http request or response");
        }

		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;

		String method = request.getMethod();
		if (method == null || !HTTP_METHODS.contains(method)) {
			throw new ServletException("Do not support non Http request method");
		}

		doFilterInternal(request, response, filterChain);
	}

	protected abstract void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
			FilterChain filterChain) throws ServletException, IOException;


	/**
	 * 假设一个 Web 应用程序的上下文路径(Context Path)是 /myapp,并且用户发出了以下 HTTP 请求:
	 * plaintext
	 * GET /myapp/products/list?category=books&page=2
	 * 在这个例子中,getRequestURI() 返回的是 /myapp/products/list?category=books&page=2,而 getContextPath() 返回的是 /myapp。
	 * 最终方法将返回路径的子字符串 /products/list,这就是请求的 URL 路径部分,没有了上下文路径和任何查询参数。
	 * 因此,parseURL(request) 方法将在这种场景下返回 /products/list。这个字符串可以用于查找应用程序中对应的资源、服务或控制器逻辑。
	 */
	protected String parseURL(HttpServletRequest request) {
	    String path = request.getRequestURI();
	    String contextPath = request.getContextPath();
	    int startIndex = path.indexOf(contextPath);
	    int endIndex = path.indexOf("?");
	  
	    if (endIndex == -1) {
	    		return path.substring(startIndex + contextPath.length() + 1);
	    }
	  
	    return path.substring(startIndex + contextPath.length() + 1, endIndex);
	}

	protected String getParamValue(HttpServletRequest request, String paramName) {
		String paramValue = request.getParameter(paramName);
		return paramValue;
	}
}