Browse Source

MarshallingView unwraps JAXBElement value for Marshaller.supports(Class) check

Issue: SPR-11827
pull/551/head
Juergen Hoeller 11 years ago
parent
commit
e4aabd5288
  1. 42
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java
  2. 22
      spring-webmvc/src/test/java/org/springframework/web/servlet/view/xml/MarshallingViewTests.java

42
spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java

@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBElement;
import javax.xml.transform.stream.StreamResult;
import org.springframework.oxm.Marshaller;
@ -55,7 +56,7 @@ public class MarshallingView extends AbstractView { @@ -55,7 +56,7 @@ public class MarshallingView extends AbstractView {
/**
* Constructs a new {@code MarshallingView} with no {@link Marshaller} set.
* Construct a new {@code MarshallingView} with no {@link Marshaller} set.
* The marshaller must be set after construction by invoking {@link #setMarshaller}.
*/
public MarshallingView() {
@ -74,7 +75,7 @@ public class MarshallingView extends AbstractView { @@ -74,7 +75,7 @@ public class MarshallingView extends AbstractView {
/**
* Sets the {@link Marshaller} to be used by this view.
* Set the {@link Marshaller} to be used by this view.
*/
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
@ -124,23 +125,42 @@ public class MarshallingView extends AbstractView { @@ -124,23 +125,42 @@ public class MarshallingView extends AbstractView {
*/
protected Object locateToBeMarshalled(Map<String, Object> model) throws IllegalStateException {
if (this.modelKey != null) {
Object obj = model.get(this.modelKey);
if (obj == null) {
Object value = model.get(this.modelKey);
if (value == null) {
throw new IllegalStateException("Model contains no object with key [" + this.modelKey + "]");
}
if (!this.marshaller.supports(obj.getClass())) {
throw new IllegalStateException("Model object [" + obj + "] retrieved via key [" +
if (!isEligibleForMarshalling(this.modelKey, value)) {
throw new IllegalStateException("Model object [" + value + "] retrieved via key [" +
this.modelKey + "] is not supported by the Marshaller");
}
return obj;
return value;
}
for (Object obj : model.values()) {
if (obj != null && (model.size() == 1 || !(obj instanceof BindingResult)) &&
this.marshaller.supports(obj.getClass())) {
return obj;
for (Map.Entry<String, Object> entry : model.entrySet()) {
Object value = entry.getValue();
if (value != null && (model.size() == 1 || !(value instanceof BindingResult)) &&
isEligibleForMarshalling(entry.getKey(), value)) {
return value;
}
}
return null;
}
/**
* Check whether the given value from the current view's model is eligible
* for marshalling through the configured {@link Marshaller}.
* <p>The default implementation calls {@link Marshaller#supports(Class)},
* unwrapping a given {@link JAXBElement} first if applicable.
* @param modelKey the value's key in the model (never {@code null})
* @param value the value to check (never {@code null})
* @return whether the given value is to be considered as eligible
* @see Marshaller#supports(Class)
*/
protected boolean isEligibleForMarshalling(String modelKey, Object value) {
Class<?> classToCheck = value.getClass();
if (value instanceof JAXBElement) {
classToCheck = ((JAXBElement) value).getDeclaredType();
}
return this.marshaller.supports(classToCheck);
}
}

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

@ -19,6 +19,8 @@ package org.springframework.web.servlet.view.xml; @@ -19,6 +19,8 @@ package org.springframework.web.servlet.view.xml;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamResult;
import org.junit.Before;
@ -35,6 +37,7 @@ import static org.mockito.BDDMockito.*; @@ -35,6 +37,7 @@ import static org.mockito.BDDMockito.*;
/**
* @author Arjen Poutsma
* @author Juergen Hoeller
*/
public class MarshallingViewTests {
@ -84,6 +87,25 @@ public class MarshallingViewTests { @@ -84,6 +87,25 @@ public class MarshallingViewTests {
assertEquals("Invalid content length", 0, response.getContentLength());
}
@Test
public void renderModelKeyWithJaxbElement() throws Exception {
String toBeMarshalled = "value";
String modelKey = "key";
view.setModelKey(modelKey);
Map<String, Object> model = new HashMap<String, Object>();
model.put(modelKey, new JAXBElement<String>(new QName("model"), String.class, toBeMarshalled));
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
given(marshallerMock.supports(String.class)).willReturn(true);
marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));
view.render(model, request, response);
assertEquals("Invalid content type", "application/xml", response.getContentType());
assertEquals("Invalid content length", 0, response.getContentLength());
}
@Test
public void renderInvalidModelKey() throws Exception {
Object toBeMarshalled = new Object();

Loading…
Cancel
Save