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

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

Introduction

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

Prototype

@Override
public UriComponentsBuilder replacePath(@Nullable String path) 

Source Link

Document

Set the path of this builder overriding all existing path and path segment values.

Usage

From source file:org.moserp.infrastructure.gateway.filter.ResponseLinksMapper.java

Object fixLink(Object value) {
    if (!(value instanceof String)) {
        return value;
    }//w ww . j  a va 2s . c  o  m
    String href = (String) value;
    Matcher urlWithServiceNameMatcher = urlWithServiceNamePattern.matcher(href);
    Matcher standardUrlMatcher = standardUrlPattern.matcher(href);
    if (!standardUrlMatcher.matches() && urlWithServiceNameMatcher.matches()) {
        String possibleServiceName = urlWithServiceNameMatcher.group(1);
        log.debug("Possible service name: " + possibleServiceName);
        if (services.contains(possibleServiceName)) {
            log.debug("Service found");
            String gatewayPath = serviceRouteMapper.apply(possibleServiceName);
            String originalRestPath = urlWithServiceNameMatcher.group(2);
            ServletUriComponentsBuilder servletUriComponentsBuilder = ServletUriComponentsBuilder
                    .fromCurrentRequest();
            UriComponents uriComponents = servletUriComponentsBuilder.replacePath(gatewayPath)
                    .path(originalRestPath).build();
            log.debug("Mapping " + value + " to " + uriComponents);
            return uriComponents.toString();
        }
    }
    return href;
}