View Javadoc

1   /*
2    * Copyright 2005-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Factory methods for [email protected] ResponseCreator} classes. Typically used to provide input for [email protected]
36   * ResponseActions#andRespond(ResponseCreator)}.
37   *
38   * @author Arjen Poutsma
39   * @since 2.0
40   */
41  public abstract class ResponseCreators {
42  
43      private ResponseCreators() {
44      }
45  
46      // Payload
47  
48      /**
49       * Respond with the given [email protected] javax.xml.transform.Source} XML as payload response.
50       *
51       * @param payload the response payload
52       * @return the response callback
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       * Respond with the given [email protected] org.springframework.core.io.Resource} XML as payload response.
61       *
62       * @param payload the response payload
63       * @return the response callback
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      // Error/Exception
71      
72      /**
73       * Respond with an error.
74       *
75       * @param errorMessage the error message
76       * @return the response callback
77       * @see org.springframework.ws.transport.WebServiceConnection#hasError()
78       * @see org.springframework.ws.transport.WebServiceConnection#getErrorMessage()
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       * Respond with an [email protected] java.io.IOException}.
87       *
88       * @param ioException the exception to be thrown
89       * @return the response callback
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       * Respond with an [email protected] RuntimeException}.
98       *
99       * @param ex the runtime exception to be thrown
100      * @return the response callback
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     // SOAP
108 
109     /**
110      * Respond with the given [email protected] javax.xml.transform.Source} XML as SOAP envelope response.
111      *
112      * @param soapEnvelope the response SOAP envelope
113      * @return the response callback
114      * @since 2.1.1
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      * Respond with the given [email protected] org.springframework.core.io.Resource} XML as SOAP envelope response.
123      *
124      * @param soapEnvelope the response SOAP envelope
125      * @return the response callback
126      * @since 2.1.1
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      * Respond with a [email protected] MustUnderstand} fault.
136      *
137      * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text
138      * @param locale              the language of faultStringOrReason. Optional for SOAP 1.1
139      * @see SoapBody#addMustUnderstandFault(String, java.util.Locale)
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      * Respond with a [email protected] Client} (SOAP 1.1) or [email protected] Sender} (SOAP 1.2) fault.
153      *
154      * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text
155      * @param locale              the language of faultStringOrReason. Optional for SOAP 1.1
156      * @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale)
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      * Respond with a [email protected] Server} (SOAP 1.1) or [email protected] Receiver} (SOAP 1.2) fault.
170      *
171      * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text
172      * @param locale              the language of faultStringOrReason. Optional for SOAP 1.1
173      * @see org.springframework.ws.soap.SoapBody#addServerOrReceiverFault(String, Locale)
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      * Respond with a [email protected] VersionMismatch} fault.
187      *
188      * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text
189      * @param locale              the language of faultStringOrReason. Optional for SOAP 1.1
190      * @see org.springframework.ws.soap.SoapBody#addVersionMismatchFault(String, Locale)
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      * Adapts a [email protected] WebServiceMessageCreator} to the [email protected] ResponseCreator} contract.
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 }