1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .io .ByteArrayOutputStream ;
20
20
import java .util .Map ;
21
-
22
- import javax .servlet .ServletException ;
23
21
import javax .servlet .http .HttpServletRequest ;
24
22
import javax .servlet .http .HttpServletResponse ;
25
23
import javax .xml .transform .stream .StreamResult ;
26
24
27
- import org .springframework .beans .BeansException ;
28
25
import org .springframework .oxm .Marshaller ;
29
26
import org .springframework .util .Assert ;
30
- import org .springframework .util .FileCopyUtils ;
27
+ import org .springframework .util .StreamUtils ;
31
28
import org .springframework .web .servlet .View ;
32
29
import org .springframework .web .servlet .view .AbstractView ;
33
30
34
31
/**
35
- * Spring-MVC {@link View} that allows for response context to be rendered as the result of marshalling by a {@link
36
- * Marshaller}.
32
+ * Spring-MVC {@link View} that allows for response context to be rendered as the result
33
+ * of marshalling by a {@link Marshaller}.
37
34
*
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.
35
+ * <p>The Object to be marshalled is supplied as a parameter in the model and then
36
+ * {@linkplain #locateToBeMarshalled(Map) detected} during response rendering. Users can
37
+ * either specify a specific entry in the model via the {@link #setModelKey(String) sourceKey}
38
+ * property or have Spring locate the Source object.
41
39
*
42
40
* @author Arjen Poutsma
43
41
* @since 3.0
@@ -49,13 +47,15 @@ public class MarshallingView extends AbstractView {
49
47
*/
50
48
public static final String DEFAULT_CONTENT_TYPE = "application/xml" ;
51
49
50
+
52
51
private Marshaller marshaller ;
53
52
54
53
private String modelKey ;
55
54
55
+
56
56
/**
57
- * Constructs a new {@code MarshallingView} with no {@link Marshaller} set. The marshaller must be set after
58
- * construction by invoking {@link #setMarshaller(Marshaller) }.
57
+ * Constructs a new {@code MarshallingView} with no {@link Marshaller} set.
58
+ * The marshaller must be set after construction by invoking {@link #setMarshaller}.
59
59
*/
60
60
public MarshallingView () {
61
61
setContentType (DEFAULT_CONTENT_TYPE );
@@ -66,80 +66,79 @@ public MarshallingView() {
66
66
* Constructs a new {@code MarshallingView} with the given {@link Marshaller} set.
67
67
*/
68
68
public MarshallingView (Marshaller marshaller ) {
69
- Assert .notNull (marshaller , "'marshaller' must not be null" );
70
- setContentType (DEFAULT_CONTENT_TYPE );
71
- this .marshaller = marshaller ;
72
- setExposePathVariables (false );
69
+ this ();
70
+ setMarshaller (marshaller );
73
71
}
74
72
73
+
75
74
/**
76
75
* Sets the {@link Marshaller} to be used by this view.
77
76
*/
78
77
public void setMarshaller (Marshaller marshaller ) {
79
- Assert .notNull (marshaller , "'marshaller' must not be null" );
78
+ Assert .notNull (marshaller , "Marshaller must not be null" );
80
79
this .marshaller = marshaller ;
81
80
}
82
81
83
82
/**
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
- *
83
+ * Set the name of the model key that represents the object to be marshalled.
84
+ * If not specified, the model map will be searched for a supported value type.
87
85
* @see Marshaller#supports(Class)
88
86
*/
89
87
public void setModelKey (String modelKey ) {
90
88
this .modelKey = modelKey ;
91
89
}
92
90
93
91
@ Override
94
- protected void initApplicationContext () throws BeansException {
95
- Assert .notNull (marshaller , "Property 'marshaller' is required" );
92
+ protected void initApplicationContext () {
93
+ Assert .notNull (this . marshaller , "Property 'marshaller' is required" );
96
94
}
97
95
96
+
98
97
@ Override
99
- protected void renderMergedOutputModel (Map <String , Object > model ,
100
- HttpServletRequest request ,
101
- HttpServletResponse response ) throws Exception {
98
+ protected void renderMergedOutputModel (Map <String , Object > model , HttpServletRequest request ,
99
+ HttpServletResponse response ) throws Exception {
100
+
102
101
Object toBeMarshalled = locateToBeMarshalled (model );
103
102
if (toBeMarshalled == null ) {
104
- throw new ServletException ("Unable to locate object to be marshalled in model: " + model );
103
+ throw new IllegalStateException ("Unable to locate object to be marshalled in model: " + model );
105
104
}
106
105
ByteArrayOutputStream bos = new ByteArrayOutputStream (2048 );
107
- marshaller .marshal (toBeMarshalled , new StreamResult (bos ));
106
+ this . marshaller .marshal (toBeMarshalled , new StreamResult (bos ));
108
107
109
108
setResponseContentType (request , response );
110
109
response .setContentLength (bos .size ());
111
110
112
- FileCopyUtils .copy (bos .toByteArray (), response .getOutputStream ());
111
+ StreamUtils .copy (bos .toByteArray (), response .getOutputStream ());
113
112
}
114
113
115
114
/**
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
- *
115
+ * Locates the object to be marshalled. The default implementation first attempts to look
116
+ * under the configured {@linkplain #setModelKey(String) model key}, if any, before attempting
117
+ * to locate an object of {@linkplain Marshaller#supports(Class) supported type}.
120
118
* @param model the model Map
121
119
* @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
120
+ * @throws IllegalStateException if the model object specified by the
121
+ * {@linkplain #setModelKey(String) model key} is not supported by the marshaller
124
122
* @see #setModelKey(String)
125
123
*/
126
- protected Object locateToBeMarshalled (Map <String , Object > model ) throws ServletException {
124
+ protected Object locateToBeMarshalled (Map <String , Object > model ) throws IllegalStateException {
127
125
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 + "]" );
126
+ Object obj = model .get (this .modelKey );
127
+ if (obj == null ) {
128
+ throw new IllegalStateException ("Model contains no object with key [" + this . modelKey + "]" );
131
129
}
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" );
130
+ if (!this .marshaller .supports (obj .getClass ())) {
131
+ throw new IllegalStateException ("Model object [" + obj + "] retrieved via key [" +
132
+ this . modelKey + "] is not supported by the Marshaller" );
135
133
}
136
- return o ;
134
+ return obj ;
137
135
}
138
- for (Object o : model .values ()) {
139
- if (o != null && this .marshaller .supports (o .getClass ())) {
140
- return o ;
136
+ for (Object obj : model .values ()) {
137
+ if (obj != null && this .marshaller .supports (obj .getClass ())) {
138
+ return obj ;
141
139
}
142
140
}
143
141
return null ;
144
142
}
143
+
145
144
}
0 commit comments