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.support.creator;
18  
19  import java.io.IOException;
20  import javax.xml.transform.Source;
21  import javax.xml.transform.TransformerException;
22  import javax.xml.transform.dom.DOMResult;
23  
24  import org.springframework.util.Assert;
25  import org.springframework.ws.WebServiceMessage;
26  import org.springframework.ws.soap.SoapMessage;
27  import org.springframework.xml.transform.TransformerHelper;
28  
29  import org.w3c.dom.Document;
30  
31  import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
32  import static org.springframework.ws.test.support.AssertionErrors.fail;
33  
34  /**
35   * Implementation of {@link WebServiceMessageCreator} that creates a request based on a SOAP envelope {@link Source}.
36   *
37   * @author Alexander Shutyaev
38   * @since 2.1.1
39   */
40  public class SoapEnvelopeMessageCreator extends AbstractMessageCreator {
41  	
42  	private final Source soapEnvelope;
43  
44      private final TransformerHelper transformerHelper = new TransformerHelper();
45  	
46  	/**
47       * Creates a new instance of the {@code SoapEnvelopeMessageCreator} with the given SOAP envelope source.
48       *
49       * @param soapEnvelope the SOAP envelope source
50       */
51      public SoapEnvelopeMessageCreator(Source soapEnvelope) {
52          Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
53          this.soapEnvelope = soapEnvelope;
54      }
55  
56  	@Override
57  	protected void doWithMessage(WebServiceMessage message) throws IOException {
58  		assertTrue("Message created with factory is not a SOAP message", message instanceof SoapMessage);
59          SoapMessage soapMessage = (SoapMessage) message;
60  		try {
61  			DOMResult result = new DOMResult();
62              transformerHelper.transform(soapEnvelope, result);
63              soapMessage.setDocument((Document) result.getNode());
64          }
65          catch (TransformerException ex) {
66              fail("Could not transform request SOAP envelope to message: " + ex.getMessage());
67          }		
68  	}
69  
70  }