Example usage for org.springframework.web.servlet.support ServletUriComponentsBuilder fromServletMapping

List of usage examples for org.springframework.web.servlet.support ServletUriComponentsBuilder fromServletMapping

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support ServletUriComponentsBuilder fromServletMapping.

Prototype

public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) 

Source Link

Document

Prepare a builder from the host, port, scheme, context path, and servlet mapping of the given HttpServletRequest.

Usage

From source file:springfox.documentation.swagger2.web.HostNameProvider.java

static UriComponents componentsFrom(HttpServletRequest request) {
    ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);

    ForwardedHeader forwarded = ForwardedHeader.of(request.getHeader(ForwardedHeader.NAME));
    String proto = hasText(forwarded.getProto()) ? forwarded.getProto()
            : request.getHeader("X-Forwarded-Proto");
    String forwardedSsl = request.getHeader("X-Forwarded-Ssl");

    if (hasText(proto)) {
        builder.scheme(proto);/*from   w w  w  .j a  v a 2s  .c  o m*/
    } else if (hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) {
        builder.scheme("https");
    }

    String host = forwarded.getHost();
    host = hasText(host) ? host : request.getHeader("X-Forwarded-Host");

    if (!hasText(host)) {
        return builder.build();
    }

    String[] hosts = commaDelimitedListToStringArray(host);
    String hostToUse = hosts[0];

    if (hostToUse.contains(":")) {

        String[] hostAndPort = split(hostToUse, ":");

        builder.host(hostAndPort[0]);
        builder.port(Integer.parseInt(hostAndPort[1]));

    } else {
        builder.host(hostToUse);
        builder.port(-1); // reset port if it was forwarded from default port
    }

    String port = request.getHeader("X-Forwarded-Port");

    if (hasText(port)) {
        builder.port(Integer.parseInt(port));
    }

    return builder.build();
}

From source file:tv.arte.resteventapi.core.presentation.decoration.RestEventApiControllerLinkBuilder.java

/**
 * Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with the host tweaked in case the
 * request contains an {@code X-Forwarded-Host} header and the scheme tweaked in case the request contains an
 * {@code X-Forwarded-Ssl} header/*from w  w w  . ja v a2  s  .  c o  m*/
 * 
 * @return
 */
static UriComponentsBuilder getBuilder() {

    HttpServletRequest request = getCurrentRequest();
    ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);

    String forwardedSsl = request.getHeader("X-Forwarded-Ssl");

    if (StringUtils.hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) {
        builder.scheme("https");
    }

    String host = request.getHeader("X-Forwarded-Host");

    if (!StringUtils.hasText(host)) {
        return builder;
    }

    String[] hosts = StringUtils.commaDelimitedListToStringArray(host);
    String hostToUse = hosts[0];

    if (hostToUse.contains(":")) {

        String[] hostAndPort = StringUtils.split(hostToUse, ":");

        builder.host(hostAndPort[0]);
        builder.port(Integer.parseInt(hostAndPort[1]));

    } else {
        builder.host(hostToUse);
        builder.port(-1); // reset port if it was forwarded from default port
    }

    String port = request.getHeader("X-Forwarded-Port");

    if (StringUtils.hasText(port)) {
        builder.port(Integer.parseInt(port));
    }

    return builder;
}

From source file:com.github.hateoas.forms.spring.AffordanceBuilder.java

/**
 * Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with the host tweaked in case the request contains
 * an {@code X-Forwarded-Host} header and the scheme tweaked in case the request contains an {@code X-Forwarded-Ssl} header
 *
 * @return builder//from  www . j  ava  2 s  .c o  m
 */
static UriComponentsBuilder getBuilder() {

    HttpServletRequest request = getCurrentRequest();
    ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);

    String forwardedSsl = request.getHeader("X-Forwarded-Ssl");

    if (StringUtils.hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) {
        builder.scheme("https");
    }

    String host = request.getHeader("X-Forwarded-Host");

    if (!StringUtils.hasText(host)) {
        return builder;
    }

    String[] hosts = StringUtils.commaDelimitedListToStringArray(host);
    String hostToUse = hosts[0];

    if (hostToUse.contains(":")) {

        String[] hostAndPort = StringUtils.split(hostToUse, ":");

        builder.host(hostAndPort[0]);
        builder.port(Integer.parseInt(hostAndPort[1]));
    } else {
        builder.host(hostToUse);
        builder.port(-1); // reset port if it was forwarded from default port
    }

    String port = request.getHeader("X-Forwarded-Port");

    if (StringUtils.hasText(port)) {
        builder.port(Integer.parseInt(port));
    }

    return builder;
}

From source file:org.activiti.rest.content.ContentRestUrlBuilder.java

/** Extracts the base URL from the request */
public static ContentRestUrlBuilder fromRequest(HttpServletRequest request) {
    return usingBaseUrl(ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString());
}

From source file:org.activiti.rest.dmn.service.api.DmnRestUrlBuilder.java

/** Extracts the base URL from the request */
public static DmnRestUrlBuilder fromRequest(HttpServletRequest request) {
    return usingBaseUrl(ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString());
}

From source file:org.activiti.rest.form.FormRestUrlBuilder.java

/** Extracts the base URL from the request */
public static FormRestUrlBuilder fromRequest(HttpServletRequest request) {
    return usingBaseUrl(ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString());
}

From source file:org.activiti.rest.service.api.RestUrlBuilder.java

/** Extracts the base URL from the request */
public static RestUrlBuilder fromRequest(HttpServletRequest request) {
    return usingBaseUrl(ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString());
}

From source file:org.flowable.app.rest.AppRestUrlBuilder.java

/** Extracts the base URL from the request */
public static AppRestUrlBuilder fromRequest(HttpServletRequest request) {
    return usingBaseUrl(ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString());
}