Skip to content

Commit 5017855

Browse files
committed
Add option to remove path ext in ServletUriCompBuilder
Issue: SPR-10272
1 parent caa1218 commit 5017855

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.web.context.request.ServletRequestAttributes;
2626
import org.springframework.web.util.UriComponentsBuilder;
2727
import org.springframework.web.util.UrlPathHelper;
28+
import org.springframework.web.util.WebUtils;
2829

2930
/**
3031
* A UriComponentsBuilder that extracts information from an HttpServletRequest.
@@ -34,6 +35,9 @@
3435
*/
3536
public class ServletUriComponentsBuilder extends UriComponentsBuilder {
3637

38+
private String servletRequestURI;
39+
40+
3741
/**
3842
* Default constructor. Protected to prevent direct instantiation.
3943
*
@@ -82,7 +86,7 @@ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest
8286
*/
8387
public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request) {
8488
ServletUriComponentsBuilder builder = fromRequest(request);
85-
builder.replacePath(request.getRequestURI());
89+
builder.pathFromRequest(request);
8690
builder.replaceQuery(null);
8791
return builder;
8892
}
@@ -115,7 +119,7 @@ public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request
115119
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
116120
builder.port(port);
117121
}
118-
builder.path(request.getRequestURI());
122+
builder.pathFromRequest(request);
119123
builder.query(request.getQueryString());
120124
return builder;
121125
}
@@ -164,4 +168,39 @@ protected static HttpServletRequest getCurrentRequest() {
164168
return servletRequest;
165169
}
166170

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+
167206
}

spring-webmvc/src/test/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,22 @@ public void fromCurrentRequest() {
141141
}
142142
}
143143

144+
// SPR-10272
145+
146+
@Test
147+
public void pathExtension() {
148+
this.request.setRequestURI("/rest/books/6.json");
149+
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request);
150+
String extension = builder.removePathExtension();
151+
String result = builder.path("/pages/1.{ext}").buildAndExpand(extension).toUriString();
152+
153+
assertEquals("http://localhost/rest/books/6/pages/1.json", result);
154+
}
155+
156+
@Test
157+
public void pathExtensionNone() {
158+
this.request.setRequestURI("/rest/books/6");
159+
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request);
160+
assertNull(builder.removePathExtension());
161+
}
144162
}

0 commit comments

Comments
 (0)