1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.test.client;
18
19 import java.io.IOException;
20 import java.net.URI;
21 import java.util.Locale;
22 import javax.xml.transform.Source;
23
24 import org.springframework.core.io.Resource;
25 import org.springframework.util.Assert;
26 import org.springframework.ws.WebServiceMessage;
27 import org.springframework.ws.WebServiceMessageFactory;
28 import org.springframework.ws.soap.SoapBody;
29 import org.springframework.ws.test.support.creator.PayloadMessageCreator;
30 import org.springframework.ws.test.support.creator.SoapEnvelopeMessageCreator;
31 import org.springframework.ws.test.support.creator.WebServiceMessageCreator;
32 import org.springframework.xml.transform.ResourceSource;
33
34
35
36
37
38
39
40
41 public abstract class ResponseCreators {
42
43 private ResponseCreators() {
44 }
45
46
47
48
49
50
51
52
53
54 public static ResponseCreator withPayload(Source payload) {
55 Assert.notNull(payload, "'payload' must not be null");
56 return new WebServiceMessageCreatorAdapter(new PayloadMessageCreator(payload));
57 }
58
59
60
61
62
63
64
65 public static ResponseCreator withPayload(Resource payload) throws IOException {
66 Assert.notNull(payload, "'payload' must not be null");
67 return withPayload(new ResourceSource(payload));
68 }
69
70
71
72
73
74
75
76
77
78
79
80 public static ResponseCreator withError(String errorMessage) {
81 Assert.hasLength(errorMessage, "'errorMessage' must not be empty");
82 return new ErrorResponseCreator(errorMessage);
83 }
84
85
86
87
88
89
90
91 public static ResponseCreator withException(IOException ioException) {
92 Assert.notNull(ioException, "'ioException' must not be null");
93 return new ExceptionResponseCreator(ioException);
94 }
95
96
97
98
99
100
101
102 public static ResponseCreator withException(RuntimeException ex) {
103 Assert.notNull(ex, "'ex' must not be null");
104 return new ExceptionResponseCreator(ex);
105 }
106
107
108
109
110
111
112
113
114
115
116 public static ResponseCreator withSoapEnvelope(Source soapEnvelope) {
117 Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
118 return new WebServiceMessageCreatorAdapter(new SoapEnvelopeMessageCreator(soapEnvelope));
119 }
120
121
122
123
124
125
126
127
128 public static ResponseCreator withSoapEnvelope(Resource soapEnvelope) throws IOException {
129 Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
130 return withSoapEnvelope(new ResourceSource(soapEnvelope));
131 }
132
133
134
135
136
137
138
139
140
141 public static ResponseCreator withMustUnderstandFault(final String faultStringOrReason, final Locale locale) {
142 Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
143 return new SoapFaultResponseCreator() {
144 @Override
145 public void addSoapFault(SoapBody soapBody) {
146 soapBody.addMustUnderstandFault(faultStringOrReason, locale);
147 }
148 };
149 }
150
151
152
153
154
155
156
157
158 public static ResponseCreator withClientOrSenderFault(final String faultStringOrReason, final Locale locale) {
159 Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
160 return new SoapFaultResponseCreator() {
161 @Override
162 public void addSoapFault(SoapBody soapBody) {
163 soapBody.addClientOrSenderFault(faultStringOrReason, locale);
164 }
165 };
166 }
167
168
169
170
171
172
173
174
175 public static ResponseCreator withServerOrReceiverFault(final String faultStringOrReason, final Locale locale) {
176 Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
177 return new SoapFaultResponseCreator() {
178 @Override
179 public void addSoapFault(SoapBody soapBody) {
180 soapBody.addServerOrReceiverFault(faultStringOrReason, locale);
181 }
182 };
183 }
184
185
186
187
188
189
190
191
192 public static ResponseCreator withVersionMismatchFault(final String faultStringOrReason, final Locale locale) {
193 Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
194 return new SoapFaultResponseCreator() {
195 @Override
196 public void addSoapFault(SoapBody soapBody) {
197 soapBody.addVersionMismatchFault(faultStringOrReason, locale);
198 }
199 };
200 }
201
202
203
204
205 private static class WebServiceMessageCreatorAdapter implements ResponseCreator {
206
207 private final WebServiceMessageCreator adaptee;
208
209 private WebServiceMessageCreatorAdapter(WebServiceMessageCreator adaptee) {
210 this.adaptee = adaptee;
211 }
212
213 public WebServiceMessage createResponse(URI uri,
214 WebServiceMessage request,
215 WebServiceMessageFactory messageFactory) throws IOException {
216 return adaptee.createMessage(messageFactory);
217 }
218 }
219
220
221 }