use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class MvcUriComponentsBuilderTests method fromMethodCallWithPathVariable.
@Test
public void fromMethodCallWithPathVariable() {
UriComponents uriComponents = fromMethodCall(on(ControllerWithMethods.class).methodWithPathVariable("1")).build();
assertThat(uriComponents.toUriString()).startsWith("http://localhost");
assertThat(uriComponents.toUriString()).endsWith("/something/1/foo");
}
use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class ServletUriComponentsBuilderTests method fromRequestWithForwardedPrefix.
// SPR-16650
@Test
public void fromRequestWithForwardedPrefix() throws Exception {
this.request.addHeader("X-Forwarded-Prefix", "/prefix");
this.request.setContextPath("/mvc-showcase");
this.request.setRequestURI("/mvc-showcase/bar");
HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request);
UriComponents result = ServletUriComponentsBuilder.fromRequest(requestToUse).build();
assertThat(result.toUriString()).isEqualTo("http://localhost/prefix/bar");
}
use of org.springframework.web.util.UriComponents in project ontrack by nemerosa.
the class DefaultURIBuilder method build.
@Override
public URI build(Object methodInvocation) {
// Default builder
UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(methodInvocation);
// Default URI
UriComponents uriComponents = builder.build();
// TODO #251 Workaround for SPR-12771
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
HttpRequest httpRequest = new ServletServerHttpRequest(request);
String portHeader = httpRequest.getHeaders().getFirst("X-Forwarded-Port");
if (StringUtils.hasText(portHeader)) {
int port = Integer.parseInt(portHeader);
String scheme = uriComponents.getScheme();
if (("https".equals(scheme) && port == 443) || ("http".equals(scheme) && port == 80)) {
port = -1;
}
builder.port(port);
}
// OK
return builder.build().toUri();
}
use of org.springframework.web.util.UriComponents in project lavagna by digitalfondue.
the class Redirector method sendRedirect.
public static void sendRedirect(HttpServletRequest req, HttpServletResponse resp, String page, Map<String, List<String>> params) throws IOException {
UriComponents urlToRedirect = UriComponentsBuilder.fromPath(page).queryParams(new LinkedMultiValueMap<>(params)).build();
resp.sendRedirect(urlToRedirect.toUriString());
}
use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class AbstractFlashMapManager method isFlashMapForRequest.
/**
* Whether the given FlashMap matches the current request.
* Uses the expected request path and query parameters saved in the FlashMap.
*/
protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest request) {
String expectedPath = flashMap.getTargetRequestPath();
if (expectedPath != null) {
String requestUri = getUrlPathHelper().getOriginatingRequestUri(request);
if (!requestUri.equals(expectedPath) && !requestUri.equals(expectedPath + "/")) {
return false;
}
}
UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(request).build();
MultiValueMap<String, String> actualParams = uriComponents.getQueryParams();
MultiValueMap<String, String> expectedParams = flashMap.getTargetRequestParams();
for (String expectedName : expectedParams.keySet()) {
List<String> actualValues = actualParams.get(expectedName);
if (actualValues == null) {
return false;
}
for (String expectedValue : expectedParams.get(expectedName)) {
if (!actualValues.contains(expectedValue)) {
return false;
}
}
}
return true;
}
Aggregations