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

The following examples show how to use org.springframework.web.util.UriComponentsBuilder#fromUriString() . 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: RemoteJobServerService.java    From spring-batch-lightmin with Apache License 2.0 6 votes vote down vote up
@Override
public JobExecutionPage getJobExecutionPage(final Long jobInstanceId,
                                            final Integer startIndex,
                                            final Integer pageSize,
                                            final LightminClientApplication lightminClientApplication) {
    final String uri = this.getClientUri(lightminClientApplication) + "/jobexecutionpages";
    final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
    final Integer startIndexParam = startIndex != null ? startIndex : 0;
    final Integer pageSizeParam = pageSize != null ? pageSize : 10;
    uriComponentsBuilder.queryParam("jobinstanceid", jobInstanceId);
    uriComponentsBuilder.queryParam("startindex", startIndexParam);
    uriComponentsBuilder.queryParam("pagesize", pageSizeParam);
    final ResponseEntity<JobExecutionPage> response =
            this.restTemplate.getForEntity(uriComponentsBuilder.toUriString(), JobExecutionPage.class, jobInstanceId);
    this.checkHttpOk(response);
    return response.getBody();
}
 
Example 2
Source File: HLSController.java    From airsonic with GNU General Public License v3.0 6 votes vote down vote up
private String createStreamUrl(HttpServletRequest request, Player player, int id, int offset, int duration, Pair<Integer, Dimension> bitRate) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getContextPath(request) + "ext/stream/stream.ts");
    builder.queryParam("id", id);
    builder.queryParam("hls", "true");
    builder.queryParam("timeOffset", offset);
    builder.queryParam("player", player.getId());
    builder.queryParam("duration", duration);
    if (bitRate != null) {
        builder.queryParam("maxBitRate", bitRate.getLeft());
        Dimension dimension = bitRate.getRight();
        if (dimension != null) {
            builder.queryParam("size", dimension.width);
            builder.queryParam("x", dimension.height);
        }
    }
    jwtSecurityService.addJWTToken(builder);
    return builder.toUriString();
}
 
Example 3
Source File: MvcUriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromMappingNameWithCustomBaseUrl() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(WebConfig.class);
	context.refresh();

	this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);

	UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("http://example.org:9999/base");
	MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(baseUrl);
	String url = mvcBuilder.withMappingName("PAC#getAddressesForCountry").arg(0, "DE").buildAndExpand(123);
	assertEquals("http://example.org:9999/base/people/123/addresses/DE", url);
}
 
Example 4
Source File: MvcUriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fromMethodNameWithCustomBaseUrlViaInstance() {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
	MvcUriComponentsBuilder mvcBuilder = relativeTo(builder);
	UriComponents uriComponents = mvcBuilder.withMethodName(ControllerWithMethods.class,
			"methodWithPathVariable", "1").build();

	assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString());
	assertEquals("http://example.org:9090/base", builder.toUriString());
}
 
Example 5
Source File: MvcUriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fromMappingNameWithCustomBaseUrl() {

	initWebApplicationContext(WebConfig.class);

	UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("http://example.org:9999/base");
	MvcUriComponentsBuilder mvcBuilder = relativeTo(baseUrl);
	String url = mvcBuilder.withMappingName("PAC#getAddressesForCountry").arg(0, "DE").buildAndExpand(123);
	assertEquals("http://example.org:9999/base/people/123/addresses/DE", url);
}
 
Example 6
Source File: URLBuilder.java    From zstack with Apache License 2.0 5 votes vote down vote up
public static String hideUrlPassword(String url){
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
    UriComponents u = builder.build();
    if (u.getUserInfo() == null) {
        return url;
    } else {
        return builder.userInfo(u.getUserInfo().split(":")[0]).build().toUriString();
    }
}
 
Example 7
Source File: DataModelServiceStub.java    From wecube-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Issue a request from request url with place holders and param map
 *
 * @param requestUrl request url with place holders
 * @param paramMap   generated param map
 * @return common response dto
 */
public UrlToResponseDto initiateGetRequest(String requestUrl, Map<String, Object> paramMap) {
    UrlToResponseDto responseDto;

    // combine url with param map
    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(requestUrl);
    UriComponents uriComponents = uriComponentsBuilder.buildAndExpand(paramMap);
    String uriStr = uriComponents.toString();
    responseDto = sendGetRequest(uriStr);

    return responseDto;
}
 
Example 8
Source File: LocationHeaderRewritingFilter.java    From pulsar-manager with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();

    Route route = routeLocator.getMatchingRoute(
            urlPathHelper.getPathWithinApplication(ctx.getRequest()));
    if (route != null) {
        Pair<String, String> lh = locationHeader(ctx);
        if (lh != null) {
            String location = lh.second();
            URI originalRequestUri = UriComponentsBuilder
                    .fromHttpRequest(new ServletServerHttpRequest(ctx.getRequest()))
                    .build().toUri();
            UriComponentsBuilder redirectedUriBuilder = UriComponentsBuilder
                    .fromUriString(location);

            UriComponents redirectedUriComps = redirectedUriBuilder.build();

            String modifiedLocation = redirectedUriBuilder
                    .scheme(scheme)
                    .host(host)
                    .port(port).replacePath(redirectedUriComps.getPath())
                    .queryParam("redirect", true)
                    .queryParam("redirect.scheme", redirectedUriComps.getScheme())
                    .queryParam("redirect.host", redirectedUriComps.getHost())
                    .queryParam("redirect.port", redirectedUriComps.getPort())
                    .toUriString();
            lh.setSecond(modifiedLocation);
        }
    }
    return null;
}
 
Example 9
Source File: RedirectAuthenticationFailureHandler.java    From pizzeria with MIT License 5 votes vote down vote up
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(request.getServletPath());
    uriComponentsBuilder.queryParam(queryParam);
    uriComponentsBuilder.queryParam(StringUtils.defaultIfEmpty(request.getQueryString(), StringUtils.EMPTY));
    UriComponents uriComponents = uriComponentsBuilder.build();
    String returnUrlWithQueryParameter = uriComponents.toUriString();
    LOGGER.debug(returnUrlWithQueryParameter);
    redirectStrategy.sendRedirect(request, response, returnUrlWithQueryParameter);
}
 
Example 10
Source File: Util.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return an URL for the given HTTP request, with anonymized sensitive parameters.
 *
 * @param request An HTTP request instance
 * @return The associated anonymized URL
 */
public static String getAnonymizedURLForRequest(HttpServletRequest request) {

    String url = getURLForRequest(request);
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
    MultiValueMap<String, String> components = builder.build().getQueryParams();

    // Subsonic REST API authentication (see RESTRequestParameterProcessingFilter)
    if (components.containsKey("p")) builder.replaceQueryParam("p", URL_SENSITIVE_REPLACEMENT_STRING);  // Cleartext password
    if (components.containsKey("t")) builder.replaceQueryParam("t", URL_SENSITIVE_REPLACEMENT_STRING);  // Token
    if (components.containsKey("s")) builder.replaceQueryParam("s", URL_SENSITIVE_REPLACEMENT_STRING);  // Salt
    if (components.containsKey("u")) builder.replaceQueryParam("u", URL_SENSITIVE_REPLACEMENT_STRING);  // Username

    return builder.build().toUriString();
}
 
Example 11
Source File: MvcUriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fromMethodNameWithCustomBaseUrlViaInstance() {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
	MvcUriComponentsBuilder mvcBuilder = relativeTo(builder);
	UriComponents uriComponents = mvcBuilder.withMethodName(ControllerWithMethods.class,
			"methodWithPathVariable", "1").build();

	assertEquals("https://example.org:9090/base/something/1/foo", uriComponents.toString());
	assertEquals("https://example.org:9090/base", builder.toUriString());
}
 
Example 12
Source File: VaultClients.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Override
public UriBuilder uriString(String uriTemplate) {

	if (uriTemplate.startsWith("http:") || uriTemplate.startsWith("https:")) {
		return UriComponentsBuilder.fromUriString(uriTemplate);
	}

	VaultEndpoint endpoint = this.endpointProvider.getVaultEndpoint();

	String baseUri = toBaseUri(endpoint);
	UriComponents uriComponents = UriComponentsBuilder.fromUriString(prepareUriTemplate(baseUri, uriTemplate))
			.build();

	return UriComponentsBuilder.fromUriString(baseUri).uriComponents(uriComponents);
}
 
Example 13
Source File: InfluxDBService.java    From influx-proxy with Apache License 2.0 5 votes vote down vote up
private static URI getUri(String updateRetentionUrl, String updateRetentionData) {
    Map<String, String> params = Collections.singletonMap("q", updateRetentionData);
    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(updateRetentionUrl);
    for (Map.Entry<String, String> entry : params.entrySet()) {
        uriComponentsBuilder.queryParam(entry.getKey(), entry.getValue());
    }
    return uriComponentsBuilder.build().encode().toUri();
}
 
Example 14
Source File: HtmlUnitRequestBuilder.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private UriComponents uriComponents() {
	URL url = this.webRequest.getUrl();
	UriComponentsBuilder uriBldr = UriComponentsBuilder.fromUriString(url.toExternalForm());
	return uriBldr.build();
}
 
Example 15
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 16
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 17
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 18
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 19
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 ttl
 *            the time-to-live
 * @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, int ttl, boolean prevExist)
		throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("ttl", ttl == -1 ? "" : ttl);
	builder.queryParam("prevExist", prevExist);

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

	return execute(builder, HttpMethod.PUT, payload, EtcdResponse.class);
}
 
Example 20
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 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 compareAndDelete(final String key, String prevValue) throws EtcdException {
	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(KEYSPACE);
	builder.pathSegment(key);
	builder.queryParam("prevValue", prevValue);

	return execute(builder, HttpMethod.DELETE, null, EtcdResponse.class);
}