com.blade.web.http.Path Maven / Gradle / Ivy
/**
* Copyright (c) 2015, biezhi 王爵 ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blade.web.http;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import blade.kit.CollectionKit;
import blade.kit.StringKit;
/**
*
*
* 路径、URL处理
*
*
* @author biezhi
* @since 1.0
*/
public final class Path {
public static final String VAR_REGEXP = ":(\\w+)";
public static final String VAR_REPLACE = "([^#/?]+)";
public static final String ALL_PATHS = "/*";
private static final String SLASH = "/";
public static String getRelativePath(String path, String contextPath) {
// String path = request.getRequestURI();
// String contextPath = request.getContextPath();
path = path.substring(contextPath.length());
if (path.length() > 0) {
path = path.substring(1);
}
if (!path.startsWith(SLASH)) {
path = SLASH + path;
}
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException ex) {
}
return path;
}
public static List convertRouteToList(String route) {
String[] pathArray = StringKit.split(route, "/");
if(null != pathArray && pathArray.length > 0){
List path = CollectionKit.newArrayList();
for (String p : pathArray) {
if (p.length() > 0) {
path.add(p);
}
}
return path;
}
return CollectionKit.newArrayList(0);
}
public static boolean isParam(String routePart) {
return routePart.startsWith(":");
}
public static boolean isSplat(String routePart) {
return routePart.equals("*");
}
public static String fixPath(String path) {
if (path == null) {
return "/";
}
if (!path.startsWith("/")) {
path = "/" + path;
}
if (path.length() > 1 && path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}
public static String cleanPath(String path){
if (path == null) {
return null;
}
return path.replaceAll("[/]+", "/");
}
}