use of org.springframework.web.util.UriComponents in project dhis2-core by dhis2.
the class MobileController method populateContextPath.
private void populateContextPath(Model model, HttpServletRequest request) {
UriComponents contextPath = ServletUriComponentsBuilder.fromContextPath(request).build();
String contextPathString = contextPath.toString();
String xForwardedProto = request.getHeader("X-Forwarded-Proto");
if (xForwardedProto != null) {
if (contextPathString.contains("http://") && xForwardedProto.equalsIgnoreCase("https")) {
contextPathString = contextPathString.replace("http://", "https://");
} else if (contextPathString.contains("https://") && xForwardedProto.equalsIgnoreCase("http")) {
contextPathString = contextPathString.replace("https://", "http://");
}
}
model.addAttribute("contextPath", contextPathString);
}
use of org.springframework.web.util.UriComponents in project java-crud-api by kolchagov.
the class TestApi method getMockHttpServletRequest.
private MockHttpServletRequest getMockHttpServletRequest() throws UnsupportedEncodingException {
final MockHttpServletRequest req = new MockHttpServletRequest();
req.setServerName("localhost");
req.setMethod(method);
final UriComponents build = UriComponentsBuilder.fromUriString(this.baseUrl).build();
req.setPathInfo(build.getPath());
req.setQueryString(build.getQuery());
setParamsFromBuild(req, build);
if (data != null) {
try {
if (data.endsWith("__is_null"))
throw new JsonParseException("");
// invalid json test expects json content
if (!"{\"}".equals(data)) {
parser.parse(data);
}
req.setContentType("application/json");
} catch (JsonParseException ignored) {
req.setContentType("application/x-www-form-urlencoded");
final String url = "/?" + URLDecoder.decode(data, "utf8");
setParamsFromBuild(req, UriComponentsBuilder.fromUriString(url).build());
}
req.setContent(data.getBytes("utf8"));
}
return req;
}
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;
}
use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class ServletUriComponentsBuilderTests method fromRequestWithForwardedPrefixTrailingSlash.
@Test
public void fromRequestWithForwardedPrefixTrailingSlash() {
this.request.setRequestURI("/bar");
this.request.addHeader("X-Forwarded-Prefix", "/foo/");
UriComponents result = ServletUriComponentsBuilder.fromRequest(this.request).build();
assertEquals("http://localhost/foo/bar", result.toUriString());
}
use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class ServletUriComponentsBuilderTests method fromRequestWithForwardedHostAndPort.
// Most X-Forwarded-* tests in UriComponentsBuilderTests
@Test
public void fromRequestWithForwardedHostAndPort() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(80);
request.setRequestURI("/mvc-showcase");
request.addHeader("X-Forwarded-Proto", "https");
request.addHeader("X-Forwarded-Host", "84.198.58.199");
request.addHeader("X-Forwarded-Port", "443");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
Aggregations