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.server;
18  
19  import java.io.IOException;
20  import javax.xml.transform.Source;
21  
22  import org.springframework.core.io.Resource;
23  import org.springframework.util.Assert;
24  import org.springframework.ws.WebServiceMessage;
25  import org.springframework.ws.WebServiceMessageFactory;
26  import org.springframework.ws.test.support.creator.PayloadMessageCreator;
27  import org.springframework.ws.test.support.creator.SoapEnvelopeMessageCreator;
28  import org.springframework.ws.test.support.creator.WebServiceMessageCreator;
29  import org.springframework.xml.transform.ResourceSource;
30  
31  /**
32   * Factory methods for {@link RequestCreator} classes. Typically used to provide input for {@link
33   * MockWebServiceClient#sendRequest(RequestCreator)}.
34   *
35   * @author Arjen Poutsma
36   * @since 2.0
37   */
38  public abstract class RequestCreators {
39  
40      private RequestCreators() {
41      }
42  
43      // Payload
44  
45      /**
46       * Create a request with the given {@link Source} XML as payload.
47       *
48       * @param payload the request payload
49       * @return the request creator
50       */
51      public static RequestCreator withPayload(Source payload) {
52          Assert.notNull(payload, "'payload' must not be null");
53          return new WebServiceMessageCreatorAdapter(new PayloadMessageCreator(payload));
54      }
55  
56      /**
57       * Create a request with the given {@link Resource} XML as payload.
58       *
59       * @param payload the request payload
60       * @return the request creator
61       */
62      public static RequestCreator withPayload(Resource payload) throws IOException {
63          Assert.notNull(payload, "'payload' must not be null");
64          return withPayload(new ResourceSource(payload));
65      }
66  
67      // SOAP
68      
69      /**
70       * Create a request with the given {@link Source} XML as SOAP envelope.
71       *
72       * @param soapEnvelope the request SOAP envelope
73       * @return the request creator
74       * @since 2.1.1
75       */
76      public static RequestCreator withSoapEnvelope(Source soapEnvelope) {
77          Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
78          return new WebServiceMessageCreatorAdapter(new SoapEnvelopeMessageCreator(soapEnvelope));
79      }
80  
81      /**
82       * Create a request with the given {@link Resource} XML as SOAP envelope.
83       *
84       * @param soapEnvelope the request SOAP envelope
85       * @return the request creator
86       * @since 2.1.1
87       */
88      public static RequestCreator withSoapEnvelope(Resource soapEnvelope) throws IOException {
89          Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
90          return withSoapEnvelope(new ResourceSource(soapEnvelope));
91      }
92  
93      /**
94       * Adapts a {@link WebServiceMessageCreator} to the {@link RequestCreator} contract.
95       */
96      private static class WebServiceMessageCreatorAdapter implements RequestCreator {
97  
98          private final WebServiceMessageCreator adaptee;
99  
100         private WebServiceMessageCreatorAdapter(WebServiceMessageCreator adaptee) {
101             this.adaptee = adaptee;
102         }
103 
104         public WebServiceMessage createRequest(WebServiceMessageFactory messageFactory) throws IOException {
105             return adaptee.createMessage(messageFactory);
106         }
107     }
108 
109 
110 }