ResourceController support for server defined default label.

Fixes #824
This commit is contained in:
Daniel Lavoie
2017-10-19 12:29:02 -04:00
parent 46253b746b
commit 01981aad53
2 changed files with 38 additions and 3 deletions

View File

@@ -49,6 +49,7 @@ import org.springframework.web.util.UrlPathHelper;
* to replace placeholders in the resource text.
*
* @author Dave Syer
* @author Daniel Lavoie
*
*/
@RestController
@@ -77,9 +78,23 @@ public class ResourceController {
return retrieve(name, profile, label, path, resolvePlaceholders);
}
@RequestMapping(value = "/{name}/{profile}/**", params = "useDefaultLabel")
public String retrieve(@PathVariable String name, @PathVariable String profile,
HttpServletRequest request,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws IOException {
String path = getFilePath(request, name, profile, null);
return retrieve(name, profile, null, path, resolvePlaceholders);
}
private String getFilePath(HttpServletRequest request, String name, String profile,
String label) {
String stem = String.format("/%s/%s/%s/", name, profile, label);
String stem;
if(label != null ) {
stem = String.format("/%s/%s/%s/", name, profile, label);
}else {
stem = String.format("/%s/%s/", name, profile);
}
String path = this.helper.getPathWithinApplication(request);
path = path.substring(path.indexOf(stem) + stem.length());
return path;

View File

@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.environment.EnvironmentController;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.resource.ResourceControllerIntegrationTests.ControllerConfiguration;
import org.springframework.context.annotation.Bean;
@@ -41,10 +42,11 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* @author Dave Syer
* @author Daniel Lavoie
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ControllerConfiguration.class)
@SpringBootTest(classes = ControllerConfiguration.class, properties = "trace")
@DirtiesContext
public class ResourceControllerIntegrationTests {
@@ -74,6 +76,19 @@ public class ResourceControllerIntegrationTests {
Mockito.verify(this.resources).findOne("foo", "default", "master", "foo.txt");
}
@Test
public void resourceNoLabel() throws Exception {
Mockito.when(this.repository.findOne("foo", "default", null))
.thenReturn(new Environment("foo", "default", "master"));
Mockito.when(this.resources.findOne("foo", "default", null, "foo.txt"))
.thenReturn(new ByteArrayResource("hello".getBytes()));
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/foo.txt")
.param("useDefaultLabel", ""))
.andExpect(MockMvcResultMatchers.status().isOk());
Mockito.verify(this.repository).findOne("foo", "default", null);
Mockito.verify(this.resources).findOne("foo", "default", null, "foo.txt");
}
@Configuration
@EnableWebMvc
@Import(PropertyPlaceholderAutoConfiguration.class)
@@ -92,7 +107,12 @@ public class ResourceControllerIntegrationTests {
}
@Bean
public ResourceController controller() {
public EnvironmentController environmentController() {
return new EnvironmentController(environmentRepository());
}
@Bean
public ResourceController resourceController() {
return new ResourceController(resourceRepository(), environmentRepository());
}