Skip to content

Commit b223e6e

Browse files
committed
MarshallingView should not close response OutputStream after copying to it
MarshallingView also explicitly skips BindingResult when searching for a model object now, implementing common custom subclass behavior out-of-the-box. Issue: SPR-11411 Issue: SPR-11417
1 parent d55a173 commit b223e6e

File tree

2 files changed

+73
-44
lines changed

2 files changed

+73
-44
lines changed
Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,28 +18,29 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.util.Map;
21-
2221
import javax.servlet.ServletException;
2322
import javax.servlet.http.HttpServletRequest;
2423
import javax.servlet.http.HttpServletResponse;
2524
import javax.xml.transform.stream.StreamResult;
2625

27-
import org.springframework.beans.BeansException;
2826
import org.springframework.oxm.Marshaller;
2927
import org.springframework.util.Assert;
30-
import org.springframework.util.FileCopyUtils;
28+
import org.springframework.util.StreamUtils;
29+
import org.springframework.validation.BindingResult;
3130
import org.springframework.web.servlet.View;
3231
import org.springframework.web.servlet.view.AbstractView;
3332

3433
/**
35-
* Spring-MVC {@link View} that allows for response context to be rendered as the result of marshalling by a {@link
36-
* Marshaller}.
34+
* Spring-MVC {@link View} that allows for response context to be rendered as the result
35+
* of marshalling by a {@link Marshaller}.
3736
*
38-
* <p>The Object to be marshalled is supplied as a parameter in the model and then {@linkplain
39-
* #locateToBeMarshalled(Map) detected} during response rendering. Users can either specify a specific entry in the
40-
* model via the {@link #setModelKey(String) sourceKey} property or have Spring locate the Source object.
37+
* <p>The Object to be marshalled is supplied as a parameter in the model and then
38+
* {@linkplain #locateToBeMarshalled(Map) detected} during response rendering. Users can
39+
* either specify a specific entry in the model via the {@link #setModelKey(String) sourceKey}
40+
* property or have Spring locate the Source object.
4141
*
4242
* @author Arjen Poutsma
43+
* @author Juergen Hoeller
4344
* @since 3.0
4445
*/
4546
public class MarshallingView extends AbstractView {
@@ -49,13 +50,15 @@ public class MarshallingView extends AbstractView {
4950
*/
5051
public static final String DEFAULT_CONTENT_TYPE = "application/xml";
5152

53+
5254
private Marshaller marshaller;
5355

5456
private String modelKey;
5557

58+
5659
/**
57-
* Constructs a new {@code MarshallingView} with no {@link Marshaller} set. The marshaller must be set after
58-
* construction by invoking {@link #setMarshaller(Marshaller)}.
60+
* Constructs a new {@code MarshallingView} with no {@link Marshaller} set.
61+
* The marshaller must be set after construction by invoking {@link #setMarshaller}.
5962
*/
6063
public MarshallingView() {
6164
setContentType(DEFAULT_CONTENT_TYPE);
@@ -66,80 +69,81 @@ public MarshallingView() {
6669
* Constructs a new {@code MarshallingView} with the given {@link Marshaller} set.
6770
*/
6871
public MarshallingView(Marshaller marshaller) {
69-
Assert.notNull(marshaller, "'marshaller' must not be null");
70-
setContentType(DEFAULT_CONTENT_TYPE);
72+
this();
73+
Assert.notNull(marshaller, "Marshaller must not be null");
7174
this.marshaller = marshaller;
72-
setExposePathVariables(false);
7375
}
7476

77+
7578
/**
7679
* Sets the {@link Marshaller} to be used by this view.
7780
*/
7881
public void setMarshaller(Marshaller marshaller) {
79-
Assert.notNull(marshaller, "'marshaller' must not be null");
8082
this.marshaller = marshaller;
8183
}
8284

8385
/**
84-
* Set the name of the model key that represents the object to be marshalled. If not specified, the model map will be
85-
* searched for a supported value type.
86-
*
86+
* Set the name of the model key that represents the object to be marshalled.
87+
* If not specified, the model map will be searched for a supported value type.
8788
* @see Marshaller#supports(Class)
8889
*/
8990
public void setModelKey(String modelKey) {
9091
this.modelKey = modelKey;
9192
}
9293

9394
@Override
94-
protected void initApplicationContext() throws BeansException {
95-
Assert.notNull(marshaller, "Property 'marshaller' is required");
95+
protected void initApplicationContext() {
96+
Assert.notNull(this.marshaller, "Property 'marshaller' is required");
9697
}
9798

99+
98100
@Override
99-
protected void renderMergedOutputModel(Map<String, Object> model,
100-
HttpServletRequest request,
101-
HttpServletResponse response) throws Exception {
101+
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
102+
HttpServletResponse response) throws Exception {
103+
102104
Object toBeMarshalled = locateToBeMarshalled(model);
103105
if (toBeMarshalled == null) {
104106
throw new ServletException("Unable to locate object to be marshalled in model: " + model);
105107
}
106108
ByteArrayOutputStream bos = new ByteArrayOutputStream(2048);
107-
marshaller.marshal(toBeMarshalled, new StreamResult(bos));
109+
this.marshaller.marshal(toBeMarshalled, new StreamResult(bos));
108110

109111
setResponseContentType(request, response);
110112
response.setContentLength(bos.size());
111113

112-
FileCopyUtils.copy(bos.toByteArray(), response.getOutputStream());
114+
StreamUtils.copy(bos.toByteArray(), response.getOutputStream());
113115
}
114116

115117
/**
116-
* Locates the object to be marshalled. The default implementation first attempts to look under the configured
117-
* {@linkplain #setModelKey(String) model key}, if any, before attempting to locate an object of {@linkplain
118-
* Marshaller#supports(Class) supported type}.
119-
*
118+
* Locate the object to be marshalled.
119+
* <p>The default implementation first attempts to look under the configured
120+
* {@linkplain #setModelKey(String) model key}, if any, before attempting to
121+
* locate an object of {@linkplain Marshaller#supports(Class) supported type}.
120122
* @param model the model Map
121123
* @return the Object to be marshalled (or {@code null} if none found)
122-
* @throws ServletException if the model object specified by the {@linkplain #setModelKey(String) model key} is not
123-
* supported by the marshaller
124+
* @throws ServletException if the model object specified by the
125+
* {@linkplain #setModelKey(String) model key} is not supported by the marshaller
124126
* @see #setModelKey(String)
125127
*/
126128
protected Object locateToBeMarshalled(Map<String, Object> model) throws ServletException {
127129
if (this.modelKey != null) {
128-
Object o = model.get(this.modelKey);
129-
if (o == null) {
130-
throw new ServletException("Model contains no object with key [" + modelKey + "]");
130+
Object obj = model.get(this.modelKey);
131+
if (obj == null) {
132+
throw new ServletException("Model contains no object with key [" + this.modelKey + "]");
131133
}
132-
if (!this.marshaller.supports(o.getClass())) {
133-
throw new ServletException("Model object [" + o + "] retrieved via key [" + modelKey +
134-
"] is not supported by the Marshaller");
134+
if (!this.marshaller.supports(obj.getClass())) {
135+
throw new ServletException("Model object [" + obj + "] retrieved via key [" +
136+
this.modelKey + "] is not supported by the Marshaller");
135137
}
136-
return o;
138+
return obj;
137139
}
138-
for (Object o : model.values()) {
139-
if (o != null && this.marshaller.supports(o.getClass())) {
140-
return o;
140+
for (Object obj : model.values()) {
141+
if (obj != null && (model.size() == 1 || !(obj instanceof BindingResult)) &&
142+
this.marshaller.supports(obj.getClass())) {
143+
return obj;
141144
}
142145
}
143146
return null;
144147
}
148+
145149
}

spring-webmvc/src/test/java/org/springframework/web/servlet/view/xml/MarshallingViewTests.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,16 +17,19 @@
1717
package org.springframework.web.servlet.view.xml;
1818

1919
import java.util.HashMap;
20+
import java.util.LinkedHashMap;
2021
import java.util.Map;
21-
2222
import javax.servlet.ServletException;
2323
import javax.xml.transform.stream.StreamResult;
2424

2525
import org.junit.Before;
2626
import org.junit.Test;
27+
2728
import org.springframework.mock.web.test.MockHttpServletRequest;
2829
import org.springframework.mock.web.test.MockHttpServletResponse;
2930
import org.springframework.oxm.Marshaller;
31+
import org.springframework.validation.BeanPropertyBindingResult;
32+
import org.springframework.validation.BindingResult;
3033

3134
import static org.junit.Assert.*;
3235
import static org.mockito.BDDMockito.*;
@@ -36,16 +39,18 @@
3639
*/
3740
public class MarshallingViewTests {
3841

42+
private Marshaller marshallerMock;
43+
3944
private MarshallingView view;
4045

41-
private Marshaller marshallerMock;
4246

4347
@Before
4448
public void createView() throws Exception {
4549
marshallerMock = mock(Marshaller.class);
4650
view = new MarshallingView(marshallerMock);
4751
}
4852

53+
4954
@Test
5055
public void getContentType() {
5156
assertEquals("Invalid content type", "application/xml", view.getContentType());
@@ -160,6 +165,26 @@ public void renderNoModelKey() throws Exception {
160165
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
161166
}
162167

168+
@Test
169+
public void renderNoModelKeyAndBindingResultFirst() throws Exception {
170+
Object toBeMarshalled = new Object();
171+
String modelKey = "key";
172+
Map<String, Object> model = new LinkedHashMap<String, Object>();
173+
model.put(BindingResult.MODEL_KEY_PREFIX + modelKey, new BeanPropertyBindingResult(toBeMarshalled, modelKey));
174+
model.put(modelKey, toBeMarshalled);
175+
176+
MockHttpServletRequest request = new MockHttpServletRequest();
177+
MockHttpServletResponse response = new MockHttpServletResponse();
178+
179+
given(marshallerMock.supports(BeanPropertyBindingResult.class)).willReturn(true);
180+
given(marshallerMock.supports(Object.class)).willReturn(true);
181+
182+
view.render(model, request, response);
183+
assertEquals("Invalid content type", "application/xml", response.getContentType());
184+
assertEquals("Invalid content length", 0, response.getContentLength());
185+
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
186+
}
187+
163188
@Test
164189
public void testRenderUnsupportedModel() throws Exception {
165190
Object toBeMarshalled = new Object();

0 commit comments

Comments
 (0)