Java Code Examples for cn.hutool.core.util.URLUtil#normalize()

The following examples show how to use cn.hutool.core.util.URLUtil#normalize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UrlRedirectUtil.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 获取 protocol 协议完全跳转
 *
 * @param request 请求
 * @param url     跳转url
 * @see javax.servlet.http.HttpUtils#getRequestURL
 */
public static String getRedirect(HttpServletRequest request, String url, int port) {
    String proto = ServletUtil.getHeaderIgnoreCase(request, "X-Forwarded-Proto");
    if (proto == null) {
        return url;
    } else {
        String host = request.getHeader(HttpHeaders.HOST);
        if (StrUtil.isEmpty(host)) {
            throw new RuntimeException("请配置host header");
        }
        if ("http".equals(proto) && port == 0) {
            port = 80;
        } else if ("https".equals(proto) && port == 0) {
            port = 443;
        }
        String format = StrUtil.format("{}://{}:{}{}", proto, host, port, url);
        return URLUtil.normalize(format);
    }
}
 
Example 2
Source File: HaloUtils.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Normalize url
 *
 * @param originalUrl original url
 * @return normalized url.
 */
@NonNull
public static String normalizeUrl(@NonNull String originalUrl) {
    Assert.hasText(originalUrl, "Original Url must not be blank");

    if (StringUtils.startsWithAny(originalUrl, URL_SEPARATOR, HaloConst.PROTOCOL_HTTPS, HaloConst.PROTOCOL_HTTP)
        && !StringUtils.startsWith(originalUrl, "//")) {
        return originalUrl;
    }

    return URLUtil.normalize(originalUrl);
}