Fix bug in request handling

For some reason path info is always null(?), so the way to get the
resource path is to use request.getRequestURI().
This commit is contained in:
Dave Syer
2016-05-03 09:11:28 +01:00
parent 7dc7de8357
commit 984cc5b885
2 changed files with 26 additions and 6 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UrlPathHelper;
/**
* An HTTP endpoint for serving up templated plain text resources from an underlying
@@ -57,20 +58,30 @@ public class ResourceController {
private EnvironmentRepository environmentRepository;
private UrlPathHelper helper = new UrlPathHelper();
public ResourceController(ResourceRepository resourceRepository,
EnvironmentRepository environmentRepository) {
this.resourceRepository = resourceRepository;
this.environmentRepository = environmentRepository;
this.helper.setAlwaysUseFullPath(true);
}
@RequestMapping("/{name}/{profile}/{label}/**")
public String resolve(@PathVariable String name, @PathVariable String profile,
@PathVariable String label, HttpServletRequest request) throws IOException {
String path = request.getPathInfo()
.substring(String.format("/%s/%s/%s/", name, profile, label).length());
String path = getFilePath(request, name, profile, label);
return resolve(name, profile, label, path);
}
private String getFilePath(HttpServletRequest request, String name, String profile,
String label) {
String stem = String.format("/%s/%s/%s/", name, profile, label);
String path = this.helper.getPathWithinApplication(request);
path = path.substring(path.indexOf(stem) + stem.length());
return path;
}
synchronized String resolve(String name, String profile, String label, String path)
throws IOException {
if (label != null && label.contains("(_)")) {
@@ -93,8 +104,7 @@ public class ResourceController {
public synchronized byte[] binary(@PathVariable String name,
@PathVariable String profile, @PathVariable String label,
HttpServletRequest request) throws IOException {
String path = request.getPathInfo()
.substring(String.format("/%s/%s/%s/", name, profile, label).length());
String path = getFilePath(request, name, profile, label);
return binary(name, profile, label, path);
}

View File

@@ -98,7 +98,17 @@ public class ResourceControllerTests {
public void resourceWithSlashRequest() throws Exception {
this.environmentRepository.setSearchLocations("classpath:/test");
MockHttpServletRequest request = new MockHttpServletRequest();
request.setPathInfo("/foo/bar/dev/" + "spam/foo.txt");
request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt");
String resource = this.controller.resolve("foo", "bar", "dev", request);
assertEquals("foo: dev_bar/spam", resource);
}
@Test
public void resourceWithSlashRequestAndServletPath() throws Exception {
this.environmentRepository.setSearchLocations("classpath:/test");
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServletPath("/spring");
request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt");
String resource = this.controller.resolve("foo", "bar", "dev", request);
assertEquals("foo: dev_bar/spam", resource);
}
@@ -121,7 +131,7 @@ public class ResourceControllerTests {
public void resourceWithSlashForBinaryRequest() throws Exception {
this.environmentRepository.setSearchLocations("classpath:/test");
MockHttpServletRequest request = new MockHttpServletRequest();
request.setPathInfo("/foo/bar/dev/" + "spam/foo.txt");
request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt");
byte[] resource = this.controller.binary("foo", "bar", "dev", request );
assertEquals("foo: dev_bar/spam", new String(resource));
}