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.xdi.oxauth.service.net.HttpService.java

public final String constructServerUrl(final HttpServletRequest request) {
    int serverPort = request.getServerPort();

    String redirectUrl;/*from w ww. ja v  a 2 s. c  o m*/
    if ((serverPort == 80) || (serverPort == 443)) {
        redirectUrl = String.format("%s://%s%s", request.getScheme(), request.getServerName(),
                request.getContextPath());
    } else {
        redirectUrl = String.format("%s://%s:%s%s", request.getScheme(), request.getServerName(),
                request.getServerPort(), request.getContextPath());
    }

    return redirectUrl.toLowerCase();
}

From source file:com.springsource.hq.plugin.tcserver.serverconfig.web.support.HqAuthenticationEntryPoint.java

@Override
protected String buildRedirectUrlToLoginPage(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) {
    String loginForm = determineUrlToUseForThisRequest(request, response, authException);
    int serverPort = getPortResolver().getServerPort(request);
    String scheme = request.getScheme();

    RedirectUrlBuilder urlBuilder = new RedirectUrlBuilder();

    urlBuilder.setScheme(scheme);/*from  w  ww. j  av  a  2 s.  c om*/
    urlBuilder.setServerName(request.getServerName());
    urlBuilder.setPort(serverPort);

    logger.debug("contextPath = " + request.getContextPath());

    urlBuilder.setPathInfo(loginForm);

    if (isForceHttps() && "http".equals(scheme)) {
        Integer httpsPort = getPortMapper().lookupHttpsPort(Integer.valueOf(serverPort));

        if (httpsPort != null) {
            // Overwrite scheme and port in the redirect URL
            urlBuilder.setScheme("https");
            urlBuilder.setPort(httpsPort.intValue());
        } else {
            logger.warn("Unable to redirect to HTTPS as no port mapping found for HTTP port " + serverPort);
        }
    }

    logger.debug("Redirecting to " + urlBuilder.getUrl());

    return urlBuilder.getUrl();
}

From source file:org.openmrs.module.webservices.rest.web.controller.HelpController.java

@RequestMapping("/module/webservices/rest/help")
public void showPage(ModelMap map, HttpServletRequest request)
        throws IllegalAccessException, InstantiationException, IOException, ConversionException {

    // TODO put content into map about controller annotations and resource
    // views//from   ww  w  .j  av  a 2s. co m

    StringBuilder baseUrl = new StringBuilder();
    String scheme = request.getScheme();
    int port = request.getServerPort();

    baseUrl.append(scheme); // http, https
    baseUrl.append("://");
    baseUrl.append(request.getServerName());
    if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
        baseUrl.append(':');
        baseUrl.append(request.getServerPort());
    }

    baseUrl.append(request.getContextPath());

    String url = Context.getAdministrationService()
            .getGlobalProperty(RestConstants.URI_PREFIX_GLOBAL_PROPERTY_NAME, baseUrl.toString());

    url += "/ws";

    map.put("data", ResourceDocCreator.create(url));
}

From source file:de.fau.amos4.web.LoginFormController.java

@RequestMapping("/client/submit")
public String ClientSubmit(HttpServletRequest request, @ModelAttribute(value = "client") Client client)
        throws MessagingException {
    // Generate new confirmation string for the client
    client.generateConfirmationString();
    // Set client to inactive
    client.setActivated(false);/* ww w. j a  v  a 2 s .c o m*/
    // Save new, in-activate client
    clientRepository.save(client);

    // Prepare and send email
    String contextPath = "http://" + request.getServerName() + ":" + request.getServerPort()
            + request.getServletPath().replace("/client/submit", "/client/confirm");
    String ConfirmationCode = client.getConfirmationString();
    // TODO: Replace this with Thymeleaf based tample generated content
    String Content = "<a href='" + contextPath + "?id=" + client.getId() + "&confirmation=" + ConfirmationCode
            + "'>Confirm my email address.</a>";
    EmailSender sender = new EmailSender();
    sender.SendEmail(client.getEmail(), "Personalragebogen 2.0 - Confirmation", Content, null, null);

    // Display login screen after
    return "redirect:/?m=registered";
}

From source file:com.sun.identity.provider.springsecurity.OpenSSOProcessingFilterEntryPoint.java

private String buildGotoUrl(HttpServletRequest request) {
    StringBuffer result = new StringBuffer();
    if (getGotoUrl() != null && getGotoUrl().length() > 0) {
        result.append(getGotoUrl());/* ww  w  .  ja va2 s .  com*/
    } else {
        result.append(getScheme() == null ? request.getScheme() : getScheme());
        result.append("://");
        result.append(getServerName() == null ? request.getServerName() : getServerName());
        if (getServerPort() != null) {
            result.append(":").append(getServerPort());
        } else if (request.getServerPort() != 80 && request.getServerPort() != 443) {
            result.append(":").append(request.getServerPort());
        }
        result.append(request.getContextPath());
        if (getFilterProcessesUrl() != null) {
            result.append(getFilterProcessesUrl());
        }
    }
    return result.toString();
}

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

private StringBuilder getBaseUrl(final HttpServletRequest req) {
    final StringBuilder builder = new StringBuilder(50);
    builder.append(req.getScheme());//from   w w  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:org.cloudfoundry.identity.uaa.varz.VarzEndpoint.java

/**
 * Compute a base url for links.//from w  w w  .  j  av a  2s  .c o m
 * 
 * @param request the current request
 * @return a computed base url for this application, or the hard-coded {@link #setBaseUrl(String) value} if set
 */
@ModelAttribute("baseUrl")
public String getBaseUrl(HttpServletRequest request) {
    if (this.baseUrl != null) {
        return this.baseUrl;
    }
    String scheme = request.getScheme();
    StringBuffer url = new StringBuffer(scheme + "://");
    url.append(request.getServerName());
    int port = request.getServerPort();
    if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
        url.append(":" + port);
    }
    url.append(request.getContextPath());
    return url.toString();
}

From source file:eionet.cr.web.action.SpatialSearchActionBean.java

/**
 * @return the contextUrl//w w w  .  j  a  v  a  2  s.  c om
 */
public String getContextUrl() {

    if (contextUrl == null) {
        HttpServletRequest request = getContext().getRequest();

        StringBuffer buf = new StringBuffer(request.getScheme()).append("://").append(request.getServerName());
        int port = request.getServerPort();
        if (port > 0 && !(port == 80 && request.getScheme().equalsIgnoreCase("http"))) {
            buf.append(":").append(port);
        }
        String contextPath = request.getContextPath();
        if (contextPath != null) {
            buf.append(contextPath.trim());
        }

        contextUrl = buf.toString();
    }

    return contextUrl;
}

From source file:cn.vlabs.duckling.vwb.VWBDriverServlet.java

private String buildURL(URLParser info, HttpServletRequest request) {
    boolean isHttp;
    StringBuffer buff = new StringBuffer();
    buff.append(request.getScheme() + "://");
    isHttp = "http".equalsIgnoreCase(request.getScheme());

    buff.append(request.getServerName());
    if (isHttp) {
        if (request.getServerPort() != 80)
            buff.append(":" + request.getServerPort());
    } else if (request.getServerPort() != 443) {
        buff.append(":" + request.getServerPort());
    }//www . j av a  2s.  c  om

    buff.append(info.getURI());
    if (request.getQueryString() != null) {
        buff.append("?" + request.getQueryString());
    }
    return buff.toString();
}

From source file:edu.usc.lunchnlearn.elasticsearch.interceptor.BreadCrumbInterceptor.java

private Crumb createIndexCrumb(Crumb firstCrumb, HttpServletRequest request) {
    Crumb indexCrumb = new Crumb();
    StringBuffer sb = new StringBuffer();

    if (request.getScheme().equals("http") && request.getHeader("x-forwarded-proto") != null) {
        sb.append("https://");
    } else {/*from  ww  w. j a  v a2s  .c  o m*/
        sb.append(request.getScheme()).append("://");
    }

    sb.append(request.getServerName());

    if (request.getServerPort() != 80 && request.getServerPort() != 443) {
        sb.append(":").append(request.getServerPort());
    }

    sb.append(request.getContextPath());
    sb.append(request.getServletPath());
    sb.append("/index.html");

    indexCrumb.setUrl(sb.toString());
    indexCrumb.setDisplayText("Index");
    indexCrumb.setX(firstCrumb.getX() - 100L);

    return indexCrumb;
}