Example usage for javax.servlet.http HttpServletRequest getServerName

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

Introduction

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

Prototype

public String getServerName();

Source Link

Document

Returns the host name of the server to which the request was sent.

Usage

From source file:org.ofbiz.passport.util.PassportUtil.java

public static String getEnvPrefixByHost(HttpServletRequest request) {
    String prefix = "test";
    try {/*ww w .  java 2s  .c  o m*/
        InetAddress[] addresses = InetAddress.getAllByName(request.getServerName());
        for (InetAddress address : addresses) {
            if (address.isAnyLocalAddress() || address.isLinkLocalAddress() || address.isLoopbackAddress()) {
                return prefix;
            }
        }
        prefix = "live";
    } catch (UnknownHostException e) {
        Debug.logError(e.getMessage(), module);
    }
    return prefix;
}

From source file:com.vmware.photon.controller.api.common.Responses.java

private static URI getBaseUri(HttpServletRequest request) throws IllegalArgumentException {
    String requestBaseUri = request.getScheme() + "://" + request.getServerName() + ":"
            + request.getServerPort();//from ww w.ja  v  a 2s. c  o m
    return UriBuilder.fromUri(requestBaseUri).build();
}

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

/**
 * //from   www  .j a  v a  2s  .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:be.fedict.eid.idp.sp.ConfigServlet.java

public static String getIdpBaseLocation(HttpServletRequest request) {

    String baseLocation = idpBaseLocation;
    if (null == baseLocation || baseLocation.trim().isEmpty()) {
        baseLocation = "https://" + request.getServerName() + ":" + request.getServerPort() + "/eid-idp/";
    }//from  w ww  .j a  va  2  s  . c om
    if (!baseLocation.endsWith("/")) {
        baseLocation += '/';
    }
    idpBaseLocation = baseLocation;
    return baseLocation;
}

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/*from w ww.  j  a  va 2s. c o 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:net.siegmar.japtproxy.JaptProxy.java

public static String getURL(HttpServletRequest req, boolean serverOnly, Configuration configuration) {

    String scheme = req.getScheme(); // http
    String serverName = req.getServerName(); // hostname.com

    // do we have a remap for this serverName??
    String remap = configuration.getRemap(serverName);
    if (remap != null) {
        serverName = remap;/*  w ww.j  a  v a2s .c o  m*/
    }

    int serverPort = req.getServerPort(); // 80
    String contextPath = req.getContextPath(); // /mywebapp
    String servletPath = req.getServletPath(); // /servlet/MyServlet
    String pathInfo = req.getPathInfo(); // /a/b;c=123
    String queryString = req.getQueryString(); // d=789

    // Reconstruct original requesting URL
    StringBuilder url = new StringBuilder();
    url.append(scheme).append("://").append(serverName);

    if (serverPort != 80 && serverPort != 443) {
        url.append(":").append(serverPort);
    }

    if (serverOnly)
        return url.toString();

    url.append(contextPath).append(servletPath);

    if (pathInfo != null) {
        url.append(pathInfo);
    }
    if (queryString != null) {
        url.append("?").append(queryString);
    }
    return url.toString();
}

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./*www .  j  a  va2s  .  com*/
 */
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.eryansky.common.web.utils.RequestUtil.java

public static String getAppURL(HttpServletRequest request) {
    StringBuffer url = new StringBuffer();
    int port = request.getServerPort();
    if (port < 0) {
        port = 80;/*from  www. ja  va  2  s .  c  om*/
    }
    String scheme = request.getScheme();
    url.append(scheme);
    url.append("://");
    url.append(request.getServerName());
    if (((scheme.equals("http")) && (port != 80)) || ((scheme.equals("https")) && (port != 443))) {
        url.append(':');
        url.append(port);
    }
    url.append(request.getContextPath());
    return url.toString();
}

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  a  v a2s .co  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:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * URL??URL?// ww  w  .  jav  a 2 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();
}