|
@@ -8,6 +8,8 @@ import java.io.OutputStream;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLDecoder;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
@@ -34,6 +36,11 @@ public final class HttpUtils {
|
|
|
*/
|
|
|
public static final String UNIFIED_RESPONSE_HEADER = "unified-response-body";
|
|
|
|
|
|
+ /**
|
|
|
+ * 资源分隔符匹配模式
|
|
|
+ */
|
|
|
+ private static final Pattern URI_SEPARATOR_PATTERN = Pattern.compile("/+");
|
|
|
+
|
|
|
/**
|
|
|
* 客户端主机地址请求头数组
|
|
|
*/
|
|
@@ -70,9 +77,9 @@ public final class HttpUtils {
|
|
|
public static String getRequestURI(@NonNull HttpServletRequest request) {
|
|
|
String uri = request.getRequestURI();
|
|
|
if (StringUtils.isEmpty(uri)) {
|
|
|
- return StringUtils.URI_ROOT;
|
|
|
- } else if (uri.charAt(0) != StringUtils.URI_ROOT.charAt(0)) {
|
|
|
- return StringUtils.URI_ROOT.concat(uri);
|
|
|
+ return StringUtils.URI_SEPARATOR;
|
|
|
+ } else if (uri.charAt(0) != StringUtils.URI_SEPARATOR.charAt(0)) {
|
|
|
+ return StringUtils.URI_SEPARATOR.concat(uri);
|
|
|
}
|
|
|
return uri;
|
|
|
}
|
|
@@ -217,6 +224,30 @@ public final class HttpUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * URL地址拼接
|
|
|
+ *
|
|
|
+ * @param domain 域名
|
|
|
+ * @param uris 资源地址数组
|
|
|
+ * @return URL地址
|
|
|
+ */
|
|
|
+ public static String url(@NonNull String domain, @NonNull String... uris) {
|
|
|
+ if (uris == null || uris.length == 0) {
|
|
|
+ return domain;
|
|
|
+ }
|
|
|
+ Matcher matcher = URI_SEPARATOR_PATTERN.matcher(StringUtils.join(uris, StringUtils.URI_SEPARATOR));
|
|
|
+ String uri = matcher.replaceAll(StringUtils.URI_SEPARATOR);
|
|
|
+ if (domain.charAt(domain.length() - 1) == StringUtils.URI_SEPARATOR.charAt(0)) {
|
|
|
+ if (uri.charAt(0) == StringUtils.URI_SEPARATOR.charAt(0)) {
|
|
|
+ return domain.concat(uri.substring(1));
|
|
|
+ }
|
|
|
+ return domain.concat(uri);
|
|
|
+ } else if (uri.charAt(0) == StringUtils.URI_SEPARATOR.charAt(0)) {
|
|
|
+ return domain.concat(uri);
|
|
|
+ }
|
|
|
+ return domain.concat(StringUtils.URI_SEPARATOR).concat(uri);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 构建Http请求消息
|
|
|
*
|