All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.liuanxin.api.util.Requests Maven / Gradle / Ivy

The newest version!
package com.github.liuanxin.api.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public final class Requests {

    public static String getDomain() {
        HttpServletRequest request = getRequestAttributes().getRequest();
        String scheme = request.getScheme();
        int port = request.getServerPort();

        StringBuilder sbd = new StringBuilder();
        // sbd.append(scheme).append(":");
        sbd.append("//").append(request.getServerName());
        boolean notHttp = ("http".equalsIgnoreCase(scheme) && port != 80);
        boolean notHttps = ("https".equalsIgnoreCase(scheme) && port != 443 && port != 80);
        if (notHttp || notHttps) {
            sbd.append(':').append(port);
        }
        return sbd.toString();
    }

    private static ServletRequestAttributes getRequestAttributes() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy