Skip to content

Commit 79ad03b

Browse files
committed
Merge branch '1.5.x'
2 parents 620208a + 23892e3 commit 79ad03b

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.stereotype.Controller;
3434
import org.springframework.util.Assert;
3535
import org.springframework.web.bind.annotation.RequestMapping;
36-
import org.springframework.web.bind.annotation.ResponseBody;
3736
import org.springframework.web.servlet.ModelAndView;
3837

3938
/**
@@ -83,6 +82,16 @@ public String getErrorPath() {
8382
return this.errorProperties.getPath();
8483
}
8584

85+
@RequestMapping(produces = { "application/xml", "text/xml", "application/json",
86+
"application/*+xml", "application/*+json" })
87+
public ResponseEntity<Map<String, Object>> errorStructured(
88+
HttpServletRequest request) {
89+
Map<String, Object> body = getErrorAttributes(request,
90+
isIncludeStackTrace(request, MediaType.ALL));
91+
HttpStatus status = getStatus(request);
92+
return new ResponseEntity<Map<String, Object>>(body, status);
93+
}
94+
8695
@RequestMapping(produces = "text/html")
8796
public ModelAndView errorHtml(HttpServletRequest request,
8897
HttpServletResponse response) {
@@ -95,12 +104,20 @@ public ModelAndView errorHtml(HttpServletRequest request,
95104
}
96105

97106
@RequestMapping
98-
@ResponseBody
99-
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
100-
Map<String, Object> body = getErrorAttributes(request,
101-
isIncludeStackTrace(request, MediaType.ALL));
107+
public ResponseEntity<String> errorText(HttpServletRequest request) {
108+
Map<String, Object> attributes = getErrorAttributes(request,
109+
isIncludeStackTrace(request, MediaType.TEXT_PLAIN));
110+
int padding = 0;
111+
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
112+
padding = Math.max(padding, entry.getKey().length());
113+
}
114+
StringBuffer body = new StringBuffer();
115+
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
116+
body.append(String.format("%-" + padding + "s : %s%n", entry.getKey(),
117+
entry.getValue()));
118+
}
102119
HttpStatus status = getStatus(request);
103-
return new ResponseEntity<>(body, status);
120+
return new ResponseEntity<>(body.toString(), status);
104121
}
105122

106123
/**

Diff for: spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public void testRequestBodyValidationForMachineClient() {
169169
load("--server.error.include-exception=true");
170170
RequestEntity request = RequestEntity
171171
.post(URI.create(createUrl("/bodyValidation")))
172+
.accept(MediaType.APPLICATION_JSON)
172173
.contentType(MediaType.APPLICATION_JSON).body("{}");
173174
ResponseEntity<Map> entity = new TestRestTemplate().exchange(request, Map.class);
174175
String resp = entity.getBody().toString();
@@ -189,6 +190,18 @@ public void testNoExceptionByDefaultForMachineClient() {
189190
assertThat(resp).doesNotContain("org.springframework.validation.BindException");
190191
}
191192

193+
@Test
194+
public void testRequestBodyValidationForText() {
195+
load();
196+
RequestEntity<Void> request = RequestEntity.post(URI.create(createUrl("/")))
197+
.accept(MediaType.TEXT_PLAIN).build();
198+
ResponseEntity<String> entity = new TestRestTemplate().exchange(request,
199+
String.class);
200+
String resp = entity.getBody().toString();
201+
assertThat(resp).contains("status");
202+
assertThat(resp).contains("error");
203+
}
204+
192205
@Test
193206
public void testConventionTemplateMapping() {
194207
load();

Diff for: spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public Handler(JarFile jarFile) {
9292

9393
@Override
9494
protected URLConnection openConnection(URL url) throws IOException {
95-
if (this.jarFile != null) {
95+
if (this.jarFile != null
96+
&& url.toString().startsWith(this.jarFile.getUrl().toString())) {
9697
return JarURLConnection.get(url, this.jarFile);
9798
}
9899
try {

Diff for: spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,24 @@ public void jarFileCanBeDeletedOnceItHasBeenClosed() throws Exception {
481481
assertThat(temp.delete()).isTrue();
482482
}
483483

484+
@Test
485+
public void createUrlFromStringWithContextWhenNotFound() throws Exception {
486+
// gh-12483
487+
JarURLConnection.setUseFastExceptions(true);
488+
try {
489+
JarFile.registerUrlProtocolHandler();
490+
JarFile nested = this.jarFile
491+
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
492+
URL context = nested.getUrl();
493+
new URL(context, "jar:" + this.rootJarFile.toURI() + "!/nested.jar!/3.dat")
494+
.openConnection().getInputStream().close();
495+
this.thrown.expect(FileNotFoundException.class);
496+
new URL(context, "jar:" + this.rootJarFile.toURI() + "!/no.dat")
497+
.openConnection().getInputStream().close();
498+
}
499+
finally {
500+
JarURLConnection.setUseFastExceptions(false);
501+
}
502+
}
503+
484504
}

0 commit comments

Comments
 (0)