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.codehaus.groovy.grails.plugins.springsecurity.RedirectUtils.java

/**
 * Build a redirect url.//from  w  ww.  j  a  v a2s .c o  m
 * @param request  the request
 * @param response  the response
 * @param url the target url to redirect to
 * @return  the url
 */
public static String buildRedirectUrl(final HttpServletRequest request, final HttpServletResponse response,
        final String url) {

    if (!url.startsWith("http://") && !url.startsWith("https://")) {
        String scheme = request.getScheme();
        int serverPort = RESOLVER.getServerPort(request);
        boolean inHttp = "http".equalsIgnoreCase(scheme);
        boolean inHttps = "https".equalsIgnoreCase(scheme);
        boolean includePort = !((inHttp && serverPort == 80) || (inHttps && serverPort == 443));
        String port = includePort ? ":" + serverPort : "";
        return scheme + "://" + request.getServerName() + port + request.getContextPath() + url;
    }

    return url;
}

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());/*  ww w . j  a v a 2 s . c  o m*/
    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;
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditInfoExtractor.java

private static void handleInMessage(Map<String, String> auditInfo, Message message) {
    if (message == null) {
        LogUtils.info(LOGGER, "Input message is NULL.");
        return;//from ww w. j av  a 2s  .  c o  m
    }
    try {
        auditInfo.put(AuditInfo.HTTP_METHOD.getKey(), safeToString(message.get(Message.HTTP_REQUEST_METHOD)));
        auditInfo.put(AuditInfo.PATH.getKey(), removeGeoStore((String) message.get(Message.PATH_INFO)));
        auditInfo.put(AuditInfo.BASE_PATH.getKey(), removeGeoStore((String) message.get(Message.BASE_PATH)));
        auditInfo.put(AuditInfo.QUERY_STRING.getKey(), safeToString(message.get(Message.QUERY_STRING)));
        HttpServletRequest httpServletRequest = (HttpServletRequest) message
                .get(AbstractHTTPDestination.HTTP_REQUEST);
        auditInfo.put(AuditInfo.REMOTE_ADDR.getKey(), safeToString(httpServletRequest.getRemoteAddr()));
        auditInfo.put(AuditInfo.REMOTE_HOST.getKey(), safeToString(httpServletRequest.getRemoteHost()));
        auditInfo.put(AuditInfo.REMOTE_USER.getKey(), safeToString(httpServletRequest.getRemoteUser()));
        auditInfo.put(AuditInfo.HOST.getKey(), safeToString(httpServletRequest.getServerName()));
        fillAuthInfo(auditInfo, httpServletRequest);
        auditInfo.put(AuditInfo.BODY_AS_STRING.getKey(), getPaylod(message));
    } catch (Exception exception) {
        LogUtils.error(LOGGER, exception, "Error obtaining auditing information for input message.");
    }
}

From source file:com.tc.utils.XSPUtils.java

public static String hostName() {
    HttpServletRequest req = (HttpServletRequest) XSPUtils.context().getExternalContext().getRequest();
    return req.getServerName().toLowerCase();
}

From source file:com.tc.utils.XSPUtils.java

public static String getURL() {

    HttpServletRequest req = XSPUtils.getRequest();
    String scheme = req.getScheme(); // http
    String serverName = req.getServerName(); // hostname.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
    StringBuffer url = new StringBuffer();
    url.append(scheme).append("://").append(serverName);

    if ((serverPort != 80) && (serverPort != 443)) {
        url.append(":").append(serverPort);
    }/*from   ww w  .  ja v a  2s .com*/

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

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

From source file:net.naijatek.myalumni.util.utilities.ParamUtil.java

public static String getServer(final HttpServletRequest request) {

    StringBuffer server = new StringBuffer(128);
    String scheme = request.getScheme();
    int port = request.getServerPort();
    if (port < 0) {
        port = 80; // Work around java.net.URL bug
    }/*from www . j  a va 2s. c  om*/
    server.append(scheme);
    server.append("://");
    server.append(request.getServerName());
    if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
        server.append(':');
        server.append(port);
    }
    return server.toString();
}

From source file:com.zimbra.common.util.HttpUtil.java

/**
 * bug 32207/* ww w .  jav  a 2s.c  o m*/
 *
 * The apache reverse proxy is re-writing the Host header to be the MBS IP.  It sets
 * the original request hostname in the X-Forwarded-Host header.  To work around it,
 * we first check for X-Forwarded-Host and then fallback to Host.
 *
 * @param req
 * @return the original request hostname
 */
public static String getVirtualHost(HttpServletRequest req) {
    String virtualHost = req.getHeader("X-Forwarded-Host");
    if (virtualHost != null)
        return virtualHost;
    else
        return req.getServerName();
}

From source file:com.zimbra.cs.servlet.util.AuthUtil.java

public static String getRedirectURL(HttpServletRequest req, Server server, boolean isAdminRequest,
        boolean relative) throws ServiceException, MalformedURLException {
    String redirectUrl;/*from  w ww. j a v  a2  s  .co  m*/
    if (isAdminRequest) {
        redirectUrl = getAdminURL(server, relative);
    } else {
        redirectUrl = getMailURL(server, relative);
    }
    if (!relative) {
        URL url = new URL(redirectUrl);

        // replace host of the URL to the host the request was sent to
        String reqHost = req.getServerName();
        String host = url.getHost();

        if (!reqHost.equalsIgnoreCase(host)) {
            URL destUrl = new URL(url.getProtocol(), reqHost, url.getPort(), url.getFile());
            redirectUrl = destUrl.toString();
        }
    }
    return redirectUrl;
}

From source file:com.googlecode.fascinator.portal.services.impl.CachingDynamicPageServiceImpl.java

public static String getURL(HttpServletRequest req, JsonSimpleConfig config) {

    // Sometimes when proxying https behind apache or another web server, if
    // this is managed by the web server the scheme may be reported
    // incorrectly to Fascinator
    String scheme = config.getString(req.getScheme(), "urlSchemeName");
    String serverName = req.getServerName(); // hostname.com
    int serverPort = req.getServerPort(); // 80
    String contextPath = req.getContextPath(); // /redbox

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

    if ((serverPort != 80) && (serverPort != 443)) {
        url.append(":").append(serverPort);
    }//from www. ja v a2s .c o  m

    url.append(contextPath).append("/");

    return url.toString();
}

From source file:net.siegmar.japtproxy.JaptProxy.java

/**
 * Analyzes (validates) request url and extract required information.
 *
 * @param request the object to populate with extracted information.
 * @throws net.siegmar.japtproxy.exception.InvalidRequestException is thrown if requested url is invalid.
 *///from ww  w  . j a va 2s  .com
private static RequestedData buildRequestedData(final HttpServletRequest request,
        final Configuration configuration) throws InvalidRequestException {
    final String resource = request.getPathInfo();
    final RequestedData requestedData = new RequestedData();
    requestedData.setRequestedResource(resource);
    requestedData.setRequestModifiedSince(request.getDateHeader(HttpHeaderConstants.IF_MODIFIED_SINCE));

    requestedData.setUserAgent(request.getHeader(HttpHeaderConstants.USER_AGENT));
    requestedData.setUrl(getURL(request, configuration));
    requestedData.setHostUrl(getURL(request, true, configuration));
    requestedData.setScheme(request.getScheme());
    requestedData.setServerName(request.getServerName());
    requestedData.setServerPort(request.getServerPort());

    final String requestedResource = requestedData.getRequestedResource();

    // Reject if no requested resource is specified
    if (requestedResource == null) {
        throw new InvalidRequestException("Rejected request because it doesn't contain a resource request");
    }

    // Reject requests that contain /../ for security reason
    if (requestedResource.contains("/../")) {
        throw new InvalidRequestException(
                "Rejected request '" + requestedResource + "' because it contains /../");
    }

    // Reject requests that doesn't contain a backend
    final int endIdx = requestedResource.indexOf('/', 1);
    if (endIdx <= 1) {
        throw new InvalidRequestException(
                "Rejected request '" + requestedResource + "' because it doesn't specify a backend");
    }

    // Reject requests that doesn't contain a target resource
    if (requestedResource.length() == endIdx + 1) {
        throw new InvalidRequestException("Rejected request '" + requestedResource
                + "' because it doesn't specify a target " + "resource");
    }

    // Extract the backend and target resource parts of the request
    final String requestedBackend = requestedResource.substring(1, endIdx);
    final String requestedTarget = requestedResource.substring(endIdx);

    // Set requestedData
    requestedData.setRequestedTarget(requestedTarget);
    requestedData.setRequestedBackend(requestedBackend);

    return requestedData;
}