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

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

Introduction

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

Prototype

public UriComponents buildAndExpand(Object... uriVariableValues) 

Source Link

Document

Build a UriComponents instance and replaces URI template variables with the values from an array.

Usage

From source file:com.orange.ngsi2.client.Ngsi2Client.java

/**
 * Update existing or append some attributes to an entity
 * @param entityId the entity ID//w w w  . jav  a 2  s  .  c  o  m
 * @param type optional entity type to avoid ambiguity when multiple entities have the same ID, null or zero-length for empty
 * @param attributes the attributes to update or to append
 * @param append if true, will only allow to append new attributes
 * @return the listener to notify of completion
 */
public ListenableFuture<Void> updateEntity(String entityId, String type, Map<String, Attribute> attributes,
        boolean append) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseURL);
    builder.path("v2/entities/{entityId}");
    addParam(builder, "type", type);
    if (append) {
        addParam(builder, "options", "append");
    }
    return adapt(
            request(HttpMethod.POST, builder.buildAndExpand(entityId).toUriString(), attributes, Void.class));
}

From source file:org.zalando.boot.etcd.EtcdClient.java

/**
 * Executes the given method on the given location using the given request
 * data./*w  w  w  .  jav  a 2  s  .co  m*/
 * 
 * @param uri
 *            the location
 * @param method
 *            the HTTP method
 * @param requestData
 *            the request data
 * @return the etcd response
 * @throws EtcdException
 *             in case etcd returned an error
 */
private <T> T execute(UriComponentsBuilder uriTemplate, HttpMethod method,
        MultiValueMap<String, String> requestData, Class<T> responseType) throws EtcdException {
    long startTimeMillis = System.currentTimeMillis();
    int retry = -1;

    ResourceAccessException lastException = null;
    do {
        lastException = null;

        URI uri = uriTemplate.buildAndExpand(locations[locationIndex]).toUri();

        RequestEntity<MultiValueMap<String, String>> requestEntity = new RequestEntity<>(requestData, null,
                method, uri);

        try {
            ResponseEntity<T> responseEntity = template.exchange(requestEntity, responseType);
            return responseEntity.getBody();
        } catch (HttpStatusCodeException e) {
            EtcdError error = null;
            try {
                error = responseConverter.getObjectMapper().readValue(e.getResponseBodyAsByteArray(),
                        EtcdError.class);
            } catch (IOException ex) {
                error = null;
            }
            throw new EtcdException(error, "Failed to execute " + requestEntity + ".", e);
        } catch (ResourceAccessException e) {
            log.debug("Failed to execute " + requestEntity + ", retrying if possible.", e);

            if (locationIndex == locations.length - 1) {
                locationIndex = 0;
            } else {
                locationIndex++;
            }
            lastException = e;
        }
    } while (retry <= retryCount && System.currentTimeMillis() - startTimeMillis < retryDuration);

    if (lastException != null) {
        throw lastException;
    } else {
        return null;
    }
}