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:info.magnolia.cms.util.ServletUtil.java

/**
 * Returns the original request url. If the request has been forwarded it reconstructs the url from  request
 * attributes. The returned url is not decoded.
 *//*from  w  w  w  .  ja v  a 2 s .c  o m*/
public static String getOriginalRequestURLIncludingQueryString(HttpServletRequest request) {
    if (request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE) != null) {
        StringBuilder builder = new StringBuilder();

        String scheme = request.getScheme();
        builder.append(scheme).append("://").append(request.getServerName());

        int port = request.getServerPort();
        if ((scheme.equalsIgnoreCase("http") && port == 80)
                || (scheme.equalsIgnoreCase("https") && port == 443)) {
            // adding port is not necessary
        } else {
            builder.append(":").append(port);
        }

        String requestUri = (String) request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE);
        builder.append(requestUri);

        String queryString = (String) request.getAttribute(FORWARD_QUERY_STRING_ATTRIBUTE);
        if (StringUtils.isNotEmpty(queryString)) {
            builder.append("?").append(queryString);
        }

        return builder.toString();
    }
    StringBuilder builder = new StringBuilder();
    builder.append(request.getRequestURL());
    String queryString = request.getQueryString();
    if (StringUtils.isNotEmpty(queryString)) {
        builder.append("?").append(queryString);
    }
    return builder.toString();
}

From source file:alpha.portal.webapp.util.RequestUtil.java

/**
 * Convenience method to get the application's URL based on request
 * variables./*  w w  w.j  a v  a 2  s  . c  om*/
 * 
 * @param request
 *            the current request
 * @return URL to application
 */
public static String getAppURL(final HttpServletRequest request) {
    if (request == null)
        return "";

    final StringBuffer url = new StringBuffer();
    int port = request.getServerPort();
    if (port < 0) {
        port = 80; // Work around java.net.URL bug
    }
    final 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:ch.entwine.weblounge.common.url.UrlUtils.java

/**
 * Returns the request url as a string.//from  w  ww.j  av  a  2s  .  c  om
 * 
 * @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:com.flowlogix.util.PathUtil.java

/**
 * @param request//from w w w . j  a  v  a  2s . co  m
 * @return complete server path i.e. https://www.glowlogix.com:8080
 */
public static String getServerPath(HttpServletRequest request) {
    String port = "";
    switch (request.getScheme()) {
    case "http":
        if (request.getServerPort() != 80) {
            port = ":" + Integer.toString(request.getServerPort());
        }
        break;
    case "https":
        if (request.getServerPort() != 443) {
            port = ":" + Integer.toString(request.getServerPort());
        }
        break;
    }

    return String.format("%s://%s%s", request.getScheme(), request.getServerName(), port);
}

From source file:com.rantop.web.util.web.ServletUtils.java

/**
 * Convenience method to get the application's URL based on request
 * variables.//from   ww w.  j  a  va  2 s . c o  m
 */
public static String getAppURL(HttpServletRequest request) {
    StringBuffer url = new StringBuffer();
    int port = request.getServerPort();
    if (port < 0) {
        port = 80; // Work around java.net.URL bug
    }
    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: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:it.geosolutions.geostore.services.rest.auditing.AuditInfoExtractorTest.java

private static HttpServletRequest getHttpServletRequest() {
    HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
    Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
    Mockito.when(httpServletRequest.getRemoteHost()).thenReturn("127.0.0.1");
    Mockito.when(httpServletRequest.getRemoteUser())
            .thenReturn("User[id=2, name=admin, group=[UserGroup[id=1, groupName=everyone]], role=ADMIN]");
    Mockito.when(httpServletRequest.getServerName()).thenReturn("localhost");
    UserGroup userGroup = Mockito.mock(UserGroup.class);
    Mockito.when(userGroup.getGroupName()).thenReturn("everyone");
    User user = Mockito.mock(User.class);
    Mockito.when(user.getName()).thenReturn("admin");
    Mockito.when(user.getRole()).thenReturn(Role.ADMIN);
    Mockito.when(user.getGroups()).thenReturn(Collections.singleton(userGroup));
    Authentication authentication = Mockito.mock(Authentication.class);
    Mockito.when(authentication.getPrincipal()).thenReturn(user);
    Mockito.when(httpServletRequest.getUserPrincipal()).thenReturn(authentication);
    return httpServletRequest;
}

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  w ww. j  a  v a2 s.  c  o m
    return scheme + "://" + httpRequest.getServerName() + ":" + port;
}

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:architecture.ee.web.util.CookieUtils.java

public static Cookie getCookie(HttpServletRequest request, String name) {
    if (null == request || null == name)
        return null;
    Cookie cookies[];//from  w  w w.  j  av  a  2s. c  o  m
    cookies = request.getCookies();
    if (cookies == null || name == null || name.length() == 0)
        return null;
    try {
        Cookie cookie = null;
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i] == null || cookies[i].getName() == null || !cookies[i].getName().equals(name))
                continue;
            cookie = cookies[i];
            if (request.getServerName() != null && request.getServerName().equals(cookie.getDomain()))
                break;
        }

        return cookie;
    } catch (NullPointerException e) {
        log.debug("NPE retrieving cookies from request, returning null", e);
    }
    return null;
}