Example usage for javax.servlet.http HttpServletRequest getServerPort

List of usage examples for javax.servlet.http HttpServletRequest getServerPort

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getServerPort.

Prototype

public int getServerPort();

Source Link

Document

Returns the port number to which the request was sent.

Usage

From source file:your.microservice.core.util.YourServletUriComponentsBuilder.java

/**
 * Prepare a builder by copying the scheme, host, port, path, and
 * query string of an HttpServletRequest.
 * @param request to build path from/*ww  w  . j  ava2s.  co  m*/
 * @return a URI component builder
 */
public static YourServletUriComponentsBuilder fromRequest(HttpServletRequest request) {
    String scheme = request.getScheme();
    String host = request.getServerName();
    int port = request.getServerPort();

    String hostHeader = request.getHeader("X-Forwarded-Host");
    if (StringUtils.hasText(hostHeader)) {
        String[] hosts = StringUtils.commaDelimitedListToStringArray(hostHeader);
        String hostToUse = hosts[0];
        if (hostToUse.contains(":")) {
            String[] hostAndPort = StringUtils.split(hostToUse, ":");
            host = hostAndPort[0];
            port = Integer.parseInt(hostAndPort[1]);
        } else {
            host = hostToUse;
            port = -1;
        }
    }

    String portHeader = request.getHeader("X-Forwarded-Port");
    if (StringUtils.hasText(portHeader)) {
        port = Integer.parseInt(portHeader);
    }

    String protocolHeader = request.getHeader("X-Forwarded-Proto");
    if (StringUtils.hasText(protocolHeader)) {
        scheme = protocolHeader;
    }

    YourServletUriComponentsBuilder builder = new YourServletUriComponentsBuilder();
    builder.scheme(scheme);
    builder.host(host);
    if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
        builder.port(port);
    }
    builder.pathFromRequest(request);
    builder.query(request.getQueryString());
    return builder;
}

From source file:de.kurashigegollub.dev.gcatest.Utils.java

/**
 * /*from  w  ww.ja va2s.  co m*/
 * Will return a String with the request URL. 
 * @param req The current HttpServletRequest.
 * @param includeServlet Will include the servlet name in the return value.
 * @param includePathInfo Will include the path and query parts in the return value (only added, if includeServlet is true as well).
 * @return 
 */
// http://hostname.com:80/appname/servlet/MyServlet/a/b;c=123?d=789
public static String reconstructURL(HttpServletRequest req, boolean includeServlet, boolean includePathInfo) {
    String scheme = req.getScheme(); // http
    String serverName = req.getServerName(); // hostname.com
    int serverPort = req.getServerPort(); // 80
    String contextPath = req.getContextPath(); // /appname
    String servletPath = req.getServletPath(); // /servlet/MyServlet
    String pathInfo = req.getPathInfo(); // /a/b;c=123
    String queryString = req.getQueryString(); // d=789

    // Reconstruct original requesting URL
    String url = scheme + "://" + serverName + ":" + serverPort + contextPath;

    if (includeServlet) {
        url += servletPath;
        if (includePathInfo) {
            if (pathInfo != null) {
                url += pathInfo;
            }
            if (queryString != null) {
                url += "?" + queryString;
            }
        }
    }
    return url;
}

From source file:org.beangle.web.url.UrlBuilder.java

public static String getFullPath(String path) {
    HttpServletRequest request = ServletActionContext.getRequest();
    if (path.indexOf("http") != 0) {
        StringBuilder sb = new StringBuilder(request.getScheme());
        sb.append("://").append(request.getServerName());
        if (request.getServerPort() != 80) {
            sb.append(":").append(request.getServerPort());
        }/*from w w w  .ja  v a2  s .c o  m*/
        if (!path.startsWith(request.getContextPath())) {
            sb.append(request.getContextPath());
        }
        sb.append(path);
        path = sb.toString();
    }
    return path;
}

From source file:com.netsteadfast.greenstep.util.ApplicationSiteUtils.java

@SuppressWarnings("unchecked")
public static String getBasePath(String sysId, HttpServletRequest request) {
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/";
    if (StringUtils.isBlank(sysId)) {
        return basePath;
    }/*  w  w w  . j ava 2  s .  c  o  m*/
    ISysService<SysVO, TbSys, String> sysService = (ISysService<SysVO, TbSys, String>) AppContext
            .getBean("core.service.SysService");
    SysVO sys = new SysVO();
    sys.setSysId(sysId);
    try {
        DefaultResult<SysVO> result = sysService.findByUK(sys);
        if (result.getValue() == null) {
            return basePath;
        }
        sys = result.getValue();
        basePath = request.getScheme() + "://" + sys.getHost() + "/" + sys.getContextPath() + "/";
    } catch (ServiceException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return basePath;
}

From source file:org.mytms.common.web.util.RequestUtil.java

/**
 * Convenience method to obtain the server prefix of the current request.
 * Useful for many modules that configure Relative URL's and need to send absolute URL's
 * to Third Party Gateways.//w w  w  . j  ava  2s .c om
 */
public static String getRequestedServerPrefix() {
    HttpServletRequest request = RequestContext.getBroadleafRequestContext().getRequest();
    String scheme = request.getScheme();
    StringBuilder serverPrefix = new StringBuilder(scheme);
    serverPrefix.append("://");
    serverPrefix.append(request.getServerName());
    if ((scheme.equalsIgnoreCase("http") && request.getServerPort() != 80)
            || (scheme.equalsIgnoreCase("https") && request.getServerPort() != 443)) {
        serverPrefix.append(":");
        serverPrefix.append(request.getServerPort());
    }
    return serverPrefix.toString();
}

From source file:com.ibm.sbt.sample.web.util.SnippetFactory.java

private static String computeSbtxSampleWebUrl(HttpServletRequest request) {
    return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + "/sbtx.sample.web";
}

From source file:com.ibm.sbt.sample.web.util.SnippetFactory.java

private static String computeSbtApiWebUrl(HttpServletRequest request) {
    return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + "/sbt.api.web";
}

From source file:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * URL??URL?/*from  w  w  w  .j  ava2 s  . co  m*/
 * http://wap.mo168.com:8081/index.jsp -> http://wap.mo168.com:8081
 * @param req
 * @return
 */
public static String getUrlPrefix(HttpServletRequest req) {
    StringBuffer url = new StringBuffer(req.getScheme());
    url.append("://");
    url.append(req.getServerName());
    int port = req.getServerPort();
    if (port != 80) {
        url.append(":");
        url.append(port);
    }
    return url.toString();
}

From source file:com.netsteadfast.greenstep.util.ApplicationSiteUtils.java

private static boolean checkCrossSite(String host, HttpServletRequest request) {
    boolean corssSite = false;
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    String basePath80 = request.getScheme() + "://" + request.getServerName();
    basePath = basePath.toLowerCase();/*from   w  ww  .  j  a  v  a  2  s .c o m*/
    basePath80 = basePath80.toLowerCase();
    if (request.getServerPort() == 80 || request.getServerPort() == 443) {
        if (basePath.indexOf(host) == -1 && basePath80.indexOf(host) == -1) {
            corssSite = true;
        }
    } else {
        if (basePath.indexOf(host) == -1) {
            corssSite = true;
        }
    }
    return corssSite;
}

From source file:org.artifactory.util.HttpUtils.java

public static String getServerUrl(HttpServletRequest httpRequest) {
    int port = httpRequest.getServerPort();
    String scheme = httpRequest.getScheme();
    if (isDefaultPort(scheme, port)) {
        return scheme + "://" + httpRequest.getServerName();
    }//from www .  ja v a2  s .  c  om
    return scheme + "://" + httpRequest.getServerName() + ":" + port;
}