Java Code Examples for org.springframework.web.util.UriComponentsBuilder#pathSegment()

The following examples show how to use org.springframework.web.util.UriComponentsBuilder#pathSegment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultSkipperClient.java    From spring-cloud-skipper with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<Release> history(String releaseName) {
	ParameterizedTypeReference<HateoasResponseWrapper<ReleasesResponseWrapper>> typeReference =
		new ParameterizedTypeReference<HateoasResponseWrapper<ReleasesResponseWrapper>>() { };
	UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUri);
	builder.pathSegment("releases", "search", "findByNameIgnoreCaseContainingOrderByNameAscVersionDesc");
	builder.queryParam("name", releaseName);

	ResponseEntity<HateoasResponseWrapper<ReleasesResponseWrapper>> resourceResponseEntity =
			restTemplate.exchange(builder.toUriString(),
					HttpMethod.GET,
					null,
					typeReference);
	ReleasesResponseWrapper embedded = resourceResponseEntity.getBody().getEmbedded();
	if (embedded != null) {
		return embedded.getReleases();
	}
	else {
		return Collections.emptyList();
	}
}
 
Example 2
Source File: LoginController.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
private String buildRedirectUri(HttpServletRequest request, String identity) {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance();

    String scheme = request.getHeader(HttpHeaders.X_FORWARDED_PROTO);
    if (scheme != null && !scheme.isEmpty()) {
        builder.scheme(scheme);
    } else {
        builder.scheme(request.getScheme());
    }

    String host = request.getHeader(HttpHeaders.X_FORWARDED_HOST);
    if (host != null && !host.isEmpty()) {
        if (host.contains(":")) {
            // Forwarded host contains both host and port
            String[] parts = host.split(":");
            builder.host(parts[0]);
            builder.port(parts[1]);
        } else {
            builder.host(host);
        }
    } else {
        builder.host(request.getServerName());
        if (request.getServerPort() != 80 && request.getServerPort() != 443) {
            builder.port(request.getServerPort());
        }
    }
    // append context path
    builder.path(request.getContextPath());
    builder.pathSegment("auth/login/callback");

    // append identity provider id
    builder.queryParam("provider", identity);

    return builder.build().toUriString();
}
 
Example 3
Source File: DefaultSkipperClient.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<PackageMetadata> search(String name, boolean details) {
	ParameterizedTypeReference<HateoasResponseWrapper<PackageMetadatasResponseWrapper>> typeReference =
			new ParameterizedTypeReference<HateoasResponseWrapper<PackageMetadatasResponseWrapper>>() { };
	Map<String, String> uriVariables = new HashMap<String, String>();
	uriVariables.put("size", "2000");
	String url = baseUri + "/packageMetadata";
	UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
	builder.queryParam("size", "2000");
	if (StringUtils.hasText(name)) {
		uriVariables.put("name", name);
		builder.pathSegment("search", "findByNameContainingIgnoreCase");
		builder.queryParam("name", name);
	}
	if (!details) {
		builder.queryParam("projection", "summary");
		builder.queryParam("sort", "name,asc");
	}

	ResponseEntity<HateoasResponseWrapper<PackageMetadatasResponseWrapper>> resourceResponseEntity =
			restTemplate.exchange(builder.toUriString(),
					HttpMethod.GET,
					null,
					typeReference,
					uriVariables);
	PackageMetadatasResponseWrapper embedded = resourceResponseEntity.getBody().getEmbedded();
	if (embedded != null) {
		return embedded.getPackageMetadata();
	}
	else {
		return Collections.emptyList();
	}
}
 
Example 4
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 5 votes vote down vote up
/**
 * Creates a directory node in etcd.
 * 
 * @param key
 *            the key
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse putDir(final String key) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("dir", "true");

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 5
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 5 votes vote down vote up
public EtcdResponse deleteDir(String key, boolean recursive) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("recursive", recursive);

	return execute(builder, HttpMethod.DELETE, null, EtcdResponse.class);
}
 
Example 6
Source File: EntityMapperImpl.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private URI createEntityResponseUri(Entity entity, @Nullable @CheckForNull String attributeName) {
  UriComponentsBuilder uriComponentsBuilder =
      MolgenisServletUriComponentsBuilder.fromCurrentRequestUri()
          .replacePath(null)
          .path(EntityController.API_ENTITY_PATH)
          .pathSegment(entity.getEntityType().getId(), entity.getIdValue().toString());
  if (attributeName != null) {
    uriComponentsBuilder.pathSegment(attributeName);
  }
  return uriComponentsBuilder.build().toUri();
}
 
Example 7
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 5 votes vote down vote up
public EtcdResponse deleteDir(String key) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("dir", "true");

	return execute(builder, HttpMethod.DELETE, null, EtcdResponse.class);
}
 
Example 8
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Creates a directory node in etcd.
 * 
 * @param key
 *            the key
 * @param ttl
 *            the time-to-live
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse putDir(String key, int ttl) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("dir", "true");
	payload.set("ttl", ttl == -1 ? "" : String.valueOf(ttl));

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 9
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Atomically deletes a key-value pair in etcd.
 * 
 * @param key
 *            the key
 * @param prevIndex
 *            the modified index of the key
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse compareAndDelete(final String key, int prevIndex) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("prevIndex", prevIndex);

	return execute(builder, HttpMethod.DELETE, null, EtcdResponse.class);
}
 
Example 10
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Atomically updates a key-value pair in etcd.
 * 
 * @param key
 *            the key
 * @param value
 *            the value
 * @param ttl
 *            the time-to-live
 * @param prevValue
 *            the previous value of the key
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse compareAndSwap(String key, String value, int ttl, String prevValue) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("ttl", ttl == -1 ? "" : ttl);
	builder.queryParam("prevValue", prevValue);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("value", value);

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 11
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Atomically updates a key-value pair in etcd.
 * 
 * @param key
 *            the key
 * @param value
 *            the value
 * @param prevValue
 *            the previous value of the key
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse compareAndSwap(String key, String value, String prevValue) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("prevValue", prevValue);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("value", value);

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 12
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Atomically updates a key-value pair in etcd.
 * 
 * @param key
 *            the key
 * @param value
 *            the value
 * @param ttl
 *            the time-to-live
 * @param prevIndex
 *            the modified index of the key
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse compareAndSwap(String key, String value, int ttl, int prevIndex) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("ttl", ttl == -1 ? "" : ttl);
	builder.queryParam("prevIndex", prevIndex);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("value", value);

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 13
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Atomically updates a key-value pair in etcd.
 * 
 * @param key
 *            the key
 * @param value
 *            the value
 * @param prevIndex
 *            the modified index of the key
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse compareAndSwap(String key, String value, int prevIndex) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("prevIndex", prevIndex);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("value", value);

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 14
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Atomically creates or updates a key-value pair in etcd.
 * 
 * @param key
 *            the key
 * @param value
 *            the value
 * @param prevExist
 *            <code>true</code> if the existing node should be updated,
 *            <code>false</code> of the node should be created
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse compareAndSwap(final String key, final String value, boolean prevExist) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("prevExist", prevExist);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("value", value);

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 15
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Creates a new node with the given key-value pair under the node with the
 * given key.
 * 
 * @param key
 *            the directory node's key
 * @param value
 *            the value of the created node
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse create(final String key, final String value) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("value", value);

	return execute(builder, HttpMethod.POST, payload, EtcdResponse.class);
}
 
Example 16
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Deletes the node with the given key from etcd.
 * 
 * @param key
 *            the node's key
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse delete(final String key) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);

	return execute(builder, HttpMethod.DELETE, null, EtcdResponse.class);
}
 
Example 17
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Sets the value of the node with the given key in etcd.
 * 
 * @param key
 *            the node's key
 * @param value
 *            the node's value
 * @param ttl
 *            the node's time-to-live or <code>-1</code> to unset existing
 *            ttl
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse put(String key, String value, int ttl) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("ttl", ttl == -1 ? "" : ttl);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("value", value);

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 18
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Sets the value of the node with the given key in etcd. Any previously
 * existing key-value pair is returned as prevNode in the etcd response.
 * 
 * @param key
 *            the node's key
 * @param value
 *            the node's value
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse put(final String key, final String value) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);

	MultiValueMap<String, String> payload = new LinkedMultiValueMap<>(1);
	payload.set("value", value);

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 19
Source File: EtcdClient.java    From spring-boot-etcd with MIT License 3 votes vote down vote up
/**
 * Returns the node with the given key from etcd.
 * 
 * @param key
 *            the node's key
 * @return the response from etcd with the node
 * @throws EtcdException
 *             in case etcd returned an error
 */
public EtcdResponse get(String key) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);

	return execute(builder, HttpMethod.GET, null, EtcdResponse.class);
}
 
Example 20
Source File: BaseClient.java    From mojito with Apache License 2.0 3 votes vote down vote up
/**
 * Construct a base path for a resource and subresources matching the {@param resourceId}
 * <p/>
 * Example 1:
 * <pre class="code">
 * getBasePathForResource(repoId, "repositoryLocales");
 * </pre>
 * will print: <blockquote>{@code /api/entityName/repoId/repositoryLocales/}</blockquote>
 * Example 2:
 * <pre class="code">
 * getBasePathForResource(repoId, "repositoryLocales", 1);
 * </pre>
 * will print: <blockquote>{@code /api/entityName/repoId/repositoryLocales/1}</blockquote>
 *
 * @param resourceId The resourceId of the resource
 * @param pathSegments An undefined number of path segments to concatenate to the URI
 * @return
 */
protected String getBasePathForResource(Long resourceId, Object... pathSegments) {
    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromPath(getBasePathForResource(resourceId));

    if (!ObjectUtils.isEmpty(pathSegments)) {
        for (Object pathSegment : pathSegments) {
            uriBuilder.pathSegment(pathSegment.toString());
        }
    }

    return uriBuilder.toUriString();
}