SPR-9075 Add fromRequestUri() methods to ServletUriComponentsBuilder

This commit is contained in:
Rossen Stoyanchev 2012-02-01 18:54:08 -05:00
parent f61410705c
commit 95683f5137
2 changed files with 54 additions and 33 deletions

View File

@ -23,13 +23,11 @@ import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.UrlPathHelper;
/** /**
* A builder for {@link UriComponents} that offers static factory methods to * A UriComponentsBuilder that extracts information from an HttpServletRequest.
* extract information from an {@code HttpServletRequest}.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.1 * @since 3.1
@ -50,24 +48,25 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
} }
/** /**
* Return a builder initialized with the host, port, scheme, and the * Prepare a builder from the host, port, scheme, and context path of
* context path of the given request. * an HttpServletRequest.
*/ */
public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request) { public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request) {
ServletUriComponentsBuilder builder = fromRequest(request); ServletUriComponentsBuilder builder = fromRequest(request);
builder.replacePath(new UrlPathHelper().getContextPath(request)); builder.replacePath(request.getContextPath());
builder.replaceQuery(null); builder.replaceQuery(null);
return builder; return builder;
} }
/** /**
* Return a builder initialized with the host, port, scheme, context path, * Prepare a builder from the host, port, scheme, context path, and
* and the servlet mapping of the given request. * servlet mapping of an HttpServletRequest. The results may vary depending
* on the type of servlet mapping used.
* *
* <p>For example if the servlet is mapped by name, i.e. {@code "/main/*"}, * <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path
* then the resulting path will be {@code /appContext/main}. If the servlet * will end with "/main". If the servlet is mapped otherwise, e.g.
* path is not mapped by name, i.e. {@code "/"} or {@code "*.html"}, then * {@code "/"} or {@code "*.do"}, the result will be the same as
* the resulting path will contain the context path only. * if calling {@link #fromContextPath(HttpServletRequest)}.
*/ */
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) { public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) {
ServletUriComponentsBuilder builder = fromContextPath(request); ServletUriComponentsBuilder builder = fromContextPath(request);
@ -78,8 +77,19 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
} }
/** /**
* Return a builder initialized with all available information in the given * Prepare a builder from the host, port, scheme, and path of
* request including scheme, host, port, path, and query string. * an HttpSevletRequest.
*/
public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request) {
ServletUriComponentsBuilder builder = fromRequest(request);
builder.replacePath(request.getRequestURI());
builder.replaceQuery(null);
return builder;
}
/**
* Prepare a builder by copying the scheme, host, port, path, and
* query string of an HttpServletRequest.
*/ */
public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request) { public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request) {
String scheme = request.getScheme(); String scheme = request.getScheme();
@ -91,30 +101,38 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) { if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
builder.port(port); builder.port(port);
} }
builder.path(new UrlPathHelper().getRequestUri(request)); builder.path(request.getRequestURI());
builder.query(request.getQueryString()); builder.query(request.getQueryString());
return builder; return builder;
} }
/** /**
* Equivalent to {@link #fromContextPath(HttpServletRequest)} except the * Same as {@link #fromContextPath(HttpServletRequest)} except the
* request is obtained via {@link RequestContextHolder}. * request is obtained through {@link RequestContextHolder}.
*/ */
public static ServletUriComponentsBuilder fromCurrentContextPath() { public static ServletUriComponentsBuilder fromCurrentContextPath() {
return fromContextPath(getCurrentRequest()); return fromContextPath(getCurrentRequest());
} }
/** /**
* Equivalent to {@link #fromServletMapping(HttpServletRequest)} except the * Same as {@link #fromServletMapping(HttpServletRequest)} except the
* request is obtained via {@link RequestContextHolder}. * request is obtained through {@link RequestContextHolder}.
*/ */
public static ServletUriComponentsBuilder fromCurrentServletMapping() { public static ServletUriComponentsBuilder fromCurrentServletMapping() {
return fromServletMapping(getCurrentRequest()); return fromServletMapping(getCurrentRequest());
} }
/** /**
* Equivalent to {@link #fromRequest(HttpServletRequest)} except the * Same as {@link #fromRequestUri(HttpServletRequest)} except the
* request is obtained via {@link RequestContextHolder}. * request is obtained through {@link RequestContextHolder}.
*/
public static ServletUriComponentsBuilder fromCurrentRequestUri() {
return fromRequestUri(getCurrentRequest());
}
/**
* Same as {@link #fromRequest(HttpServletRequest)} except the
* request is obtained through {@link RequestContextHolder}.
*/ */
public static ServletUriComponentsBuilder fromCurrentRequest() { public static ServletUriComponentsBuilder fromCurrentRequest() {
return fromRequest(getCurrentRequest()); return fromRequest(getCurrentRequest());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -44,7 +44,6 @@ public class ServletUriComponentsBuilderTests {
public void fromRequest() { public void fromRequest() {
request.setRequestURI("/mvc-showcase/data/param"); request.setRequestURI("/mvc-showcase/data/param");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result); assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result);
@ -52,11 +51,10 @@ public class ServletUriComponentsBuilderTests {
@Test @Test
public void fromRequestEncodedPath() { public void fromRequestEncodedPath() {
request.setRequestURI("/mvc-showcase/data/foo%20bar;jsessionid=123"); request.setRequestURI("/mvc-showcase/data/foo%20bar");
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase/data/foo bar", result); assertEquals("http://localhost/mvc-showcase/data/foo%20bar", result);
} }
@Test @Test
@ -71,17 +69,24 @@ public class ServletUriComponentsBuilderTests {
public void fromRequestAtypicalHttpsPort() { public void fromRequestAtypicalHttpsPort() {
request.setScheme("https"); request.setScheme("https");
request.setServerPort(9043); request.setServerPort(9043);
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
assertEquals("https://localhost:9043", result); assertEquals("https://localhost:9043", result);
} }
@Test
public void fromRequestUri() {
request.setRequestURI("/mvc-showcase/data/param");
request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromRequestUri(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase/data/param", result);
}
@Test @Test
public void fromContextPath() { public void fromContextPath() {
request.setRequestURI("/mvc-showcase/data/param"); request.setRequestURI("/mvc-showcase/data/param");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromContextPath(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromContextPath(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase", result); assertEquals("http://localhost/mvc-showcase", result);
@ -92,7 +97,6 @@ public class ServletUriComponentsBuilderTests {
request.setRequestURI("/mvc-showcase/app/simple"); request.setRequestURI("/mvc-showcase/app/simple");
request.setServletPath("/app"); request.setServletPath("/app");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase/app", result); assertEquals("http://localhost/mvc-showcase/app", result);
@ -103,7 +107,6 @@ public class ServletUriComponentsBuilderTests {
request.setRequestURI("/mvc-showcase/data/param"); request.setRequestURI("/mvc-showcase/data/param");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request));
try { try {
String result = ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString(); String result = ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString();