cn.guoyukun.spring.web.utils.ServletUtils.java Source code

Java tutorial

Introduction

Here is the source code for cn.guoyukun.spring.web.utils.ServletUtils.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.guoyukun.spring.web.utils;

import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>User: 
 * <p>Date: 13-2-7 ?8:46
 * <p>Version: 1.0
 */
public class ServletUtils {

    /**
     * url?method firstPrefix+lastPrefixes
     * ?url/sample/create ?firstPrefix:/sample lastPrefixed /create
     *
     * @param request
     * @param method       
     * @param firstPrefix
     * @param lastPrefixes
     * @return
     */
    public static boolean startWith(HttpServletRequest request, RequestMethod method, String firstPrefix,
            String... lastPrefixes) {
        String requestMethod = request.getMethod();
        if (!requestMethod.equalsIgnoreCase(method.name())) {
            return false;
        }
        String url = request.getServletPath();
        if (!url.startsWith(firstPrefix)) {
            return false;
        }

        if (lastPrefixes.length == 0) {
            return true;
        }

        url = url.substring(firstPrefix.length());
        for (String lastPrefix : lastPrefixes) {
            if (url.startsWith(lastPrefix)) {
                return true;
            }
        }

        return false;
    }
}