View Javadoc

1   /*
2    * Copyright 2005-2010 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  
23  import org.springframework.util.Assert;
24  import org.springframework.ws.WebServiceMessage;
25  import org.springframework.xml.transform.TransformerHelper;
26  
27  import static org.springframework.ws.test.support.AssertionErrors.fail;
28  
29  /**
30   * Implementation of {@link WebServiceMessageCreator} that creates a request based on a {@link Source}.
31   *
32   * @author Arjen Poutsma
33   * @since 2.0
34   */
35  public class PayloadMessageCreator extends AbstractMessageCreator {
36  
37      private final Source payload;
38  
39      private TransformerHelper transformerHelper = new TransformerHelper();
40  
41      /**
42       * Creates a new instance of the {@code PayloadMessageCreator} with the given payload source.
43       *
44       * @param payload the payload source
45       */
46      public PayloadMessageCreator(Source payload) {
47          Assert.notNull(payload, "'payload' must not be null");
48          this.payload = payload;
49      }
50  
51      @Override
52      protected void doWithMessage(WebServiceMessage message) throws IOException {
53          try {
54              transformerHelper.transform(payload, message.getPayloadResult());
55          }
56          catch (TransformerException ex) {
57              fail("Could not transform request payload to message: " + ex.getMessage());
58          }
59      }
60  }