|
25 | 25 | import org.springframework.web.context.request.ServletRequestAttributes;
|
26 | 26 | import org.springframework.web.util.UriComponentsBuilder;
|
27 | 27 | import org.springframework.web.util.UrlPathHelper;
|
| 28 | +import org.springframework.web.util.WebUtils; |
28 | 29 |
|
29 | 30 | /**
|
30 | 31 | * A UriComponentsBuilder that extracts information from an HttpServletRequest.
|
|
34 | 35 | */
|
35 | 36 | public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
36 | 37 |
|
| 38 | + private String servletRequestURI; |
| 39 | + |
| 40 | + |
37 | 41 | /**
|
38 | 42 | * Default constructor. Protected to prevent direct instantiation.
|
39 | 43 | *
|
@@ -82,7 +86,7 @@ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest
|
82 | 86 | */
|
83 | 87 | public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request) {
|
84 | 88 | ServletUriComponentsBuilder builder = fromRequest(request);
|
85 |
| - builder.replacePath(request.getRequestURI()); |
| 89 | + builder.pathFromRequest(request); |
86 | 90 | builder.replaceQuery(null);
|
87 | 91 | return builder;
|
88 | 92 | }
|
@@ -115,7 +119,7 @@ public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request
|
115 | 119 | if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
|
116 | 120 | builder.port(port);
|
117 | 121 | }
|
118 |
| - builder.path(request.getRequestURI()); |
| 122 | + builder.pathFromRequest(request); |
119 | 123 | builder.query(request.getQueryString());
|
120 | 124 | return builder;
|
121 | 125 | }
|
@@ -164,4 +168,39 @@ protected static HttpServletRequest getCurrentRequest() {
|
164 | 168 | return servletRequest;
|
165 | 169 | }
|
166 | 170 |
|
| 171 | + private void pathFromRequest(HttpServletRequest request) { |
| 172 | + this.servletRequestURI = request.getRequestURI(); |
| 173 | + replacePath(request.getRequestURI()); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Removes any path extension from the {@link HttpServletRequest#getRequestURI() |
| 178 | + * requestURI}. This method must be invoked before any calls to {@link #path(String)} |
| 179 | + * or {@link #pathSegment(String...)}. |
| 180 | + * <pre> |
| 181 | + * // GET http://foo.com/rest/books/6.json |
| 182 | + * |
| 183 | + * ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request); |
| 184 | + * String ext = builder.removePathExtension(); |
| 185 | + * String uri = builder.path("/pages/1.{ext}").buildAndExpand(ext).toUriString(); |
| 186 | + * |
| 187 | + * assertEquals("http://foo.com/rest/books/6/pages/1.json", result); |
| 188 | + * </pre> |
| 189 | + * @return the removed path extension for possible re-use, or {@code null} |
| 190 | + * @since 4.0 |
| 191 | + */ |
| 192 | + public String removePathExtension() { |
| 193 | + String extension = null; |
| 194 | + if (this.servletRequestURI != null) { |
| 195 | + String filename = WebUtils.extractFullFilenameFromUrlPath(this.servletRequestURI); |
| 196 | + extension = StringUtils.getFilenameExtension(filename); |
| 197 | + if (!StringUtils.isEmpty(extension)) { |
| 198 | + int end = this.servletRequestURI.length() - (extension.length() + 1); |
| 199 | + replacePath(this.servletRequestURI.substring(0, end)); |
| 200 | + } |
| 201 | + this.servletRequestURI = null; |
| 202 | + } |
| 203 | + return extension; |
| 204 | + } |
| 205 | + |
167 | 206 | }
|
0 commit comments