Example usage for org.springframework.web.util UriComponentsBuilder fromUri

List of usage examples for org.springframework.web.util UriComponentsBuilder fromUri

Introduction

In this page you can find the example usage for org.springframework.web.util UriComponentsBuilder fromUri.

Prototype

public static UriComponentsBuilder fromUri(URI uri) 

Source Link

Document

Create a builder that is initialized with the given URI .

Usage

From source file:com.expedia.seiso.web.hateoas.link.RepoSearchLinks.java

public PaginationLinkBuilder toPaginationLinkBuilder(@NonNull Page page, @NonNull Class itemClass,
        @NonNull String path, @NonNull MultiValueMap<String, String> params) {

    // @formatter:off
    val uriComponents = UriComponentsBuilder.fromUri(versionUri)
            .pathSegment(repoPath(itemClass), BASE_SEARCH_PATH, path).build();
    // @formatter:on

    return new PaginationLinkBuilder(page, uriComponents, params);
}

From source file:com.github.jonpeterson.spring.mvc.versioning.VersionedModelResponseBodyAdvice.java

@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
        Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    VersionedResponseBody versionedResponseBody = returnType.getMethodAnnotation(VersionedResponseBody.class);
    String targetVersion = null;/*  w  w w .jav a 2  s . c  om*/

    String queryParamName = versionedResponseBody.queryParamName();
    if (!queryParamName.isEmpty()) {
        List<String> queryParamValues = UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams()
                .get(queryParamName);
        if (queryParamValues != null && !queryParamValues.isEmpty())
            targetVersion = queryParamValues.get(0);
    }

    if (targetVersion == null) {
        String headerName = versionedResponseBody.headerName();
        if (!headerName.isEmpty()) {
            List<String> headerValues = request.getHeaders().get(headerName);
            if (headerValues != null && !headerValues.isEmpty())
                targetVersion = headerValues.get(0);
        }
    }

    if (targetVersion == null)
        targetVersion = versionedResponseBody.defaultVersion();

    try {
        boolean serializeToSet = false;

        for (BeanPropertyDefinition beanPropertyDefinition : mapper.getSerializationConfig()
                .introspect(mapper.getTypeFactory().constructType(body.getClass())).findProperties()) {
            AnnotatedMember field = beanPropertyDefinition.getField();
            AnnotatedMember setter = beanPropertyDefinition.getSetter();
            if ((field != null && field.hasAnnotation(JsonSerializeToVersion.class))
                    || (setter != null && setter.hasAnnotation(JsonSerializeToVersion.class))) {
                if (setter != null)
                    setter.setValue(body, targetVersion);
                else
                    field.setValue(body, targetVersion);
                serializeToSet = true;
            }
        }

        if (!serializeToSet)
            throw new RuntimeException("no @" + JsonSerializeToVersion.class.getSimpleName()
                    + " annotation found on String field or setter method in " + body.getClass()
                    + "; this is a requirement when using @" + VersionedResponseBody.class.getSimpleName());

    } catch (Exception e) {
        throw new RuntimeException("unable to set the version of the response body model", e);
    }

    return body;
}

From source file:io.pivotal.strepsirrhini.chaoslemur.infrastructure.StandardDirectorUtils.java

@SuppressWarnings("unchecked")
@Override/*from w  ww . j  a  v  a  2 s . c  o m*/
public Set<String> getDeployments() {
    URI deploymentsUri = UriComponentsBuilder.fromUri(this.root).path("deployments").build().toUri();

    List<Map<String, String>> deployments = this.restTemplate.getForObject(deploymentsUri, List.class);

    return deployments.stream().map(deployment -> deployment.get("name")).collect(Collectors.toSet());
}

From source file:io.bosh.client.internal.AbstractSpringOperations.java

protected final <T> Observable<T> post(Class<T> responseType, Object request,
        Consumer<UriComponentsBuilder> builderCallback) {
    return createObservable(() -> {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUri(this.root);
        builderCallback.accept(builder);
        URI uri = builder.build().toUri();

        this.logger.debug("GET {}", uri);
        return this.restOperations.postForObject(uri, request, responseType);
    });/*from   w  w w .j a  v  a2  s .  c om*/
}

From source file:io.bosh.client.jobs.SpringJobs.java

private final Observable<File> getGzip(Consumer<UriComponentsBuilder> builderCallback) {
    // For responses that have a Content-Type of application/x-gzip, we need to
    // decompress them. The RestTemplate and HttpClient don't handle this for
    // us//from  w w  w  .jav  a 2s. c o m
    return createObservable(() -> {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUri(this.root);
        builderCallback.accept(builder);
        URI uri = builder.build().toUri();
        return this.restOperations.execute(uri, HttpMethod.GET, null, new ResponseExtractor<File>() {
            @Override
            public File extractData(ClientHttpResponse response) throws IOException {
                return decompress(response.getBody());
            }
        });
    });
}

From source file:de.codecentric.boot.admin.discovery.DefaultServiceInstanceConverter.java

protected URI getManagementUrl(ServiceInstance instance) {
    String managamentPath = defaultIfEmpty(instance.getMetadata().get(KEY_MANAGEMENT_PATH),
            managementContextPath);/*from w  ww.ja  v a 2 s . c o m*/
    managamentPath = stripStart(managamentPath, "/");

    URI serviceUrl = getServiceUrl(instance);
    String managamentPort = defaultIfEmpty(instance.getMetadata().get(KEY_MANAGEMENT_PORT),
            String.valueOf(serviceUrl.getPort()));

    return UriComponentsBuilder.fromUri(serviceUrl).port(managamentPort).pathSegment(managamentPath).build()
            .toUri();
}

From source file:io.pivotal.strepsirrhini.chaoslemur.infrastructure.StandardDirectorUtils.java

@SuppressWarnings("unchecked")
@Override//ww  w.ja  v  a  2 s.c  om
public Set<Map<String, String>> getVirtualMachines(String deployment) {
    URI vmsUri = UriComponentsBuilder.fromUri(this.root).pathSegment("deployments", deployment, "vms").build()
            .toUri();

    return this.restTemplate.getForObject(vmsUri, Set.class);
}

From source file:io.bosh.client.internal.AbstractSpringOperations.java

protected final <T> Observable<ResponseEntity<T>> postForEntity(Class<T> responseType, Object request,
        Consumer<UriComponentsBuilder> builderCallback) {
    return createObservable(() -> {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUri(this.root);
        builderCallback.accept(builder);
        URI uri = builder.build().toUri();

        this.logger.debug("POST {}", uri);
        return this.restOperations.postForEntity(uri, request, responseType);
    });/*  w w  w . j  a v  a  2  s  .  c  om*/
}

From source file:info.rmapproject.api.service.ResourceApiServiceTestIT.java

/**
 * Test the RMap Resource RDF stmts API call
 *//*  ww  w  .  j a v  a 2 s .c  o m*/
@Test
public void getRMapResourceRdfStmts() throws Exception {
    Response response = null;
    //create 1 disco
    RMapDiSCO rmapDisco = TestUtils.getRMapDiSCO(TestFile.DISCOA_XML);
    String discoURI = rmapDisco.getId().toString();
    assertNotNull(discoURI);
    rmapService.createDiSCO(rmapDisco, requestEventDetails);

    HttpHeaders httpheaders = mock(HttpHeaders.class);
    UriInfo uriInfo = mock(UriInfo.class);

    MultivaluedMap<String, String> params = new MultivaluedHashMap<String, String>();
    params.add(Constants.LIMIT_PARAM, "2");

    List<MediaType> mediatypes = new ArrayList<MediaType>();
    mediatypes.add(MediaType.APPLICATION_XML_TYPE);

    when(uriInfo.getQueryParameters()).thenReturn(params);
    when(httpheaders.getAcceptableMediaTypes()).thenReturn(mediatypes);

    response = resourceApiService.apiGetRMapResourceTriples(httpheaders, discoURI, uriInfo);

    assertNotNull(response);
    String body = response.getEntity().toString();

    assertEquals(303, response.getStatus());
    assertTrue(body.contains("page number"));

    URI location = response.getLocation();
    MultiValueMap<String, String> parameters = UriComponentsBuilder.fromUri(location).build().getQueryParams();
    String untildate = parameters.getFirst(Constants.UNTIL_PARAM);

    //check page 2 just has one statement
    params.add(Constants.PAGE_PARAM, "1");
    params.add(Constants.UNTIL_PARAM, untildate);

    response = resourceApiService.apiGetRMapResourceTriples(httpheaders, discoURI, uriInfo);

    assertEquals(200, response.getStatus());
    body = response.getEntity().toString();
    int numMatches = StringUtils.countMatches(body, "xmlns=");
    assertEquals(2, numMatches);
}