Example usage for javax.servlet.http HttpServletRequest getScheme

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

Introduction

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

Prototype

public String getScheme();

Source Link

Document

Returns the name of the scheme used to make this request, for example, <code>http</code>, <code>https</code>, or <code>ftp</code>.

Usage

From source file:org.jahia.modules.newsletter.action.UnsubscribeAction.java

public static String generateUnsubscribeLink(JCRNodeWrapper newsletterNode, String confirmationKey,
        HttpServletRequest req) throws RepositoryException {
    return req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + Jahia.getContextPath()
            + Render.getRenderServletPath() + "/live/" + newsletterNode.getLanguage() + newsletterNode.getPath()
            + ".confirm.do?key=" + confirmationKey + "&exec=rem";
}

From source file:ch.unifr.pai.twice.widgets.mpproxy.server.SimpleHttpUrlConnectionServletFilter.java

/**
 * @param request// ww  w .  jav a  2s. c  o m
 * @return the path to the current servlet
 */
public static String getServletPath(HttpServletRequest request) {
    return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath();
}

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

/**
 * //w  w  w. j a v a2s  .  c  om
 * 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:com.vmware.demo.HomeController.java

public static String getURLWithContextPath(HttpServletRequest request) {
    return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/";
}

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 . ja  v a2  s. 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:de.qucosa.servlet.MetsDisseminatorServlet.java

private static String extractTargetUrlFromRequest(HttpServletRequest request) {
    return String.format("%s://%s:%s/fedora", request.getScheme(), request.getLocalName(),
            request.getLocalPort());/*  w  w w .j a  v  a  2s .  c o m*/
}

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   w w  w.  ja  v  a2s  .  co m
    return UriBuilder.fromUri(requestBaseUri).build();
}

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 w  w . j  a  v a2 s  .co 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.eclipse.orion.server.openid.core.OpenIdHelper.java

private static StringBuffer getRequestServer(HttpServletRequest req) {
    StringBuffer url = new StringBuffer();
    String scheme = req.getScheme();
    int port = req.getServerPort();

    url.append(scheme);/*from  w  ww  .j  ava 2  s  .co m*/
    url.append("://"); //$NON-NLS-1$
    url.append(req.getServerName());
    if ((scheme.equals("http") && port != 80) //$NON-NLS-1$
            || (scheme.equals("https") && port != 443)) { //$NON-NLS-1$
        url.append(':');
        url.append(req.getServerPort());
    }
    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 .j  ava  2  s . co m
        if (!path.startsWith(request.getContextPath())) {
            sb.append(request.getContextPath());
        }
        sb.append(path);
        path = sb.toString();
    }
    return path;
}