Search in sources :

Example 96 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-integration by spring-projects.

the class AbstractWebServiceOutboundGateway method prepareUri.

private URI prepareUri(Message<?> requestMessage) throws URISyntaxException {
    if (this.destinationProvider != null) {
        return this.destinationProvider.getDestination();
    }
    Map<String, Object> uriVariables = ExpressionEvalMap.from(this.uriVariableExpressions).usingEvaluationContext(this.evaluationContext).withRoot(requestMessage).build();
    UriComponents uriComponents = UriComponentsBuilder.fromUriString(this.uri).buildAndExpand(uriVariables);
    return this.encodeUri ? uriComponents.toUri() : new URI(uriComponents.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) URI(java.net.URI)

Example 97 with UriComponents

use of org.springframework.web.util.UriComponents in project easy-tests by malinink.

the class SwaggerRequestValidationService method buildRequest.

/**
 * @param servletRequest the {@link HttpServletRequest}
 * @return the build {@link Request} created out of given {@link HttpServletRequest}
 * @throws IOException if the request body can't be read
 */
Request buildRequest(final HttpServletRequest servletRequest) throws IOException {
    Objects.requireNonNull(servletRequest, "A request is required.");
    final Request.Method method = Request.Method.valueOf(servletRequest.getMethod());
    final String requestUri = getCompleteRequestUri(servletRequest);
    final UriComponents uriComponents = UriComponentsBuilder.fromUriString(requestUri).build();
    final String path = uriComponents.getPath();
    final SimpleRequest.Builder builder = new SimpleRequest.Builder(method, path);
    final String body = readReader(servletRequest.getReader());
    // really empty.
    if (servletRequest.getContentLength() >= 0 || (body != null && !body.isEmpty())) {
        builder.withBody(body);
    }
    for (final Map.Entry<String, List<String>> entry : uriComponents.getQueryParams().entrySet()) {
        builder.withQueryParam(entry.getKey(), entry.getValue());
    }
    for (final String headerName : Collections.list(servletRequest.getHeaderNames())) {
        builder.withHeader(headerName, Collections.list(servletRequest.getHeaders(headerName)));
    }
    return builder.build();
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Request(com.atlassian.oai.validator.model.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) SimpleRequest(com.atlassian.oai.validator.model.SimpleRequest) SimpleRequest(com.atlassian.oai.validator.model.SimpleRequest)

Example 98 with UriComponents

use of org.springframework.web.util.UriComponents in project tutorials by eugenp.

the class SpringUriBuilderIntegrationTest method constructUriFromTemplate.

@Test
public void constructUriFromTemplate() {
    UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/{article-name}").buildAndExpand("junit-5");
    assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Example 99 with UriComponents

use of org.springframework.web.util.UriComponents in project tutorials by eugenp.

the class SpringUriBuilderIntegrationTest method constructUriWithQueryParameter.

@Test
public void constructUriWithQueryParameter() {
    UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.google.com").path("/").query("q={keyword}").buildAndExpand("baeldung");
    assertEquals("http://www.google.com/?q=baeldung", uriComponents.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Example 100 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-cloud-netflix by spring-cloud.

the class LocationRewriteFilter method run.

@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 newPath = getRestoredPath(this.zuulProperties, route, redirectedUriComps);
            String modifiedLocation = redirectedUriBuilder.scheme(originalRequestUri.getScheme()).host(originalRequestUri.getHost()).port(originalRequestUri.getPort()).replacePath(newPath).build().toUriString();
            lh.setSecond(modifiedLocation);
        }
    }
    return null;
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RequestContext(com.netflix.zuul.context.RequestContext) URI(java.net.URI) Route(org.springframework.cloud.netflix.zuul.filters.Route)

Aggregations

UriComponents (org.springframework.web.util.UriComponents)133 Test (org.junit.jupiter.api.Test)43 Test (org.junit.Test)37 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)26 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)7 URI (java.net.URI)5 HashMap (java.util.HashMap)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)4 MethodParameter (org.springframework.core.MethodParameter)3 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Matchers.anyString (org.mockito.Matchers.anyString)2