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: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;//from  w w w.ja va 2s . com
    }

    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.ambraproject.wombat.controller.FeedbackController.java

private static Map<String, String> getUserSessionAttributes(HttpServletRequest request) {
    Map<String, String> headers = new LinkedHashMap<>();

    for (String headerName : Collections.list(request.getHeaderNames())) {
        List<String> headerValues = Collections.list(request.getHeaders(headerName));
        headers.put(headerName, headerValues.stream().collect(joinWithComma()));
    }/*w ww.  j a  v  a 2s  .com*/

    headers.put("server-name", request.getServerName() + ":" + request.getServerPort());
    headers.put("remote-addr", request.getRemoteAddr());
    headers.put("local-addr", request.getLocalAddr() + ":" + request.getLocalPort());

    /*
     * Keeping this in case more values get passed from the client other than just the visible form
     * fields
     */
    for (String paramName : Collections.list(request.getParameterNames())) {
        String[] paramValues = request.getParameterValues(paramName);
        headers.put(paramName, Stream.of(paramValues).collect(joinWithComma()));
    }

    return headers;
}

From source file:org.craftercms.commons.http.HttpUtils.java

/**
 * Returns the portion from the URL that includes the scheme, server name and port number, without the server
 * path./*  www. j a v  a 2  s.c  om*/
 *
 * @param request       the request object used to build the base URL
 * @param forceHttps    if HTTPS should be enforced
 *
 * @return the base request URL
 */
public static StringBuilder getBaseRequestUrl(HttpServletRequest request, boolean forceHttps) {
    String scheme = forceHttps ? HTTPS_SCHEME : request.getScheme();
    String serverName = request.getServerName();
    int serverPort = request.getServerPort();
    StringBuilder url = new StringBuilder();

    url.append(scheme).append("://").append(serverName);

    if (!(scheme.equals(HTTP_SCHEME) && serverPort == DEFAULT_HTTP_PORT)
            && !(scheme.equals(HTTPS_SCHEME) && serverPort == DEFAULT_HTTPS_PORT)) {
        url.append(":").append(serverPort);
    }

    return url;
}

From source file:ch.entwine.weblounge.common.url.UrlUtils.java

/**
 * Returns the request url as a string./*from  w ww . ja  v a  2s .  co  m*/
 * 
 * @param request
 *          the request
 * @param includePath
 *          <code>true</code> to also include the request uri
 * @param includeQuery
 *          <code>true</code> to include the query string
 * @return the url as a string
 */
public static URL toURL(HttpServletRequest request, boolean includePath, boolean includeQuery) {
    try {
        StringBuffer buf = new StringBuffer(request.getScheme());
        buf.append("://");
        buf.append(request.getServerName());
        if (request.getServerPort() != 80)
            buf.append(":").append(request.getServerPort());
        if (includePath && request.getRequestURI() != null)
            buf.append(request.getRequestURI());
        if (includeQuery && StringUtils.isNotBlank(request.getQueryString()))
            buf.append(request.getQueryString());
        return new URL(buf.toString());
    } catch (MalformedURLException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.geoserver.ows.util.ResponseUtils.java

/**
 * Pulls out the base url ( from the client point of view ), from the given request object.
 * // w  w  w .  j  ava2 s  . c o m
 * @return A String of the form "&lt;scheme&gt;://&lt;server&gt;:&lt;port&gt;/&lt;context&gt;/"
 */
public static String baseURL(HttpServletRequest req) {
    StringBuffer sb = new StringBuffer(req.getScheme());
    sb.append("://").append(req.getServerName()).append(":").append(req.getServerPort())
            .append(req.getContextPath()).append("/");
    return sb.toString();
}

From source file:de.betterform.agent.web.WebUtil.java

public static String getRequestURI(HttpServletRequest request) {
    StringBuffer buffer = new StringBuffer(request.getScheme());
    buffer.append("://");
    buffer.append(request.getServerName());
    buffer.append(":");
    buffer.append(request.getServerPort());
    buffer.append(request.getContextPath());
    return buffer.toString();
}

From source file:com.twinsoft.convertigo.engine.servlets.GenericServlet.java

protected static String getServletBaseUrl(HttpServletRequest request) {
    String base = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    String requestURI = request.getRequestURI();
    int i = requestURI.lastIndexOf('/');
    return base + requestURI.substring(0, i);
}

From source file:net.ymate.framework.core.util.WebUtils.java

/**
 * @param request HttpServletRequest//  w w w.  j  a  va 2 s  . com
 * @return ??URL
 */
public static String baseURL(HttpServletRequest request) {
    StringBuilder basePath = new StringBuilder(request.getScheme()).append("://")
            .append(__doGetSafeServerName(request));
    if (request.getServerPort() != 80 && request.getServerPort() != 443) {
        basePath.append(":").append(request.getServerPort());
    }
    if (StringUtils.isNotBlank(request.getContextPath())) {
        basePath.append(request.getContextPath());
    }
    if (!basePath.toString().endsWith("/")) {
        basePath.append("/");
    }
    return basePath.toString();
}

From source file:org.hippoecm.frontend.util.RequestUtils.java

/**
 * Returns the original host information requested by the client
 * @param request//from www  .  j a v a2s . com
 * @return the farthest request host
 */
public static String getFarthestRequestHost(HttpServletRequest request) {
    String host = request.getHeader("X-Forwarded-Host");

    if (host != null) {
        String[] hosts = host.split(",");
        return hosts[0].trim();
    }

    host = request.getHeader("Host");

    if (host != null && !"".equals(host)) {
        return host;
    }

    // fallback to request server name for HTTP/1.0 clients.
    // e.g., HTTP/1.0 based browser clients or load balancer not providing 'Host' header.

    int serverPort = request.getServerPort();

    if (serverPort == 80 || serverPort == 443 || serverPort <= 0) {
        host = request.getServerName();
    } else {
        host = request.getServerName() + ":" + serverPort;
    }

    return host;
}

From source file:net.cloudfree.apps.shop.internal.app.JsonListingServlet.java

private static StringBuilder getBaseUrl(final HttpServletRequest req) {
    final StringBuilder builder = new StringBuilder(50);
    builder.append(req.getScheme());/*from  w  w  w.ja v a2s .c  om*/
    builder.append("://");
    builder.append(req.getServerName());
    if ((req.getScheme().equals("http") && (req.getServerPort() != 80))
            || (req.getScheme().equals("https") && (req.getServerPort() != 443))) {
        builder.append(":");
        builder.append(req.getServerPort());
    }
    builder.append(req.getContextPath());
    builder.append(req.getServletPath());
    builder.append("/");
    return builder;
}