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.soap.axiom;
18  
19  import javax.xml.stream.XMLStreamReader;
20  import javax.xml.transform.Result;
21  import javax.xml.transform.Source;
22  
23  import org.springframework.util.Assert;
24  import org.springframework.util.xml.StaxUtils;
25  import org.springframework.ws.soap.axiom.support.AxiomUtils;
26  
27  import org.apache.axiom.om.OMElement;
28  import org.apache.axiom.om.OMException;
29  import org.apache.axiom.soap.SOAPBody;
30  import org.apache.axiom.soap.SOAPFactory;
31  
32  /**
33   * Abstract base class for {@link Payload} implementations.
34   *
35   * @author Arjen Poutsma
36   * @since 2.0
37   */
38  abstract class AbstractPayload extends Payload {
39  
40      private final SOAPBody axiomBody;
41  
42      private final SOAPFactory axiomFactory;
43  
44      protected AbstractPayload(SOAPBody axiomBody, SOAPFactory axiomFactory) {
45          Assert.notNull(axiomBody, "'axiomBody' must not be null");
46          Assert.notNull(axiomFactory, "'axiomFactory' must not be null");
47          this.axiomBody = axiomBody;
48          this.axiomFactory = axiomFactory;
49      }
50  
51      @Override
52      public final Source getSource() {
53          try {
54              OMElement payloadElement = getPayloadElement();
55              if (payloadElement != null) {
56                  XMLStreamReader streamReader = getStreamReader(payloadElement);
57                  return StaxUtils.createCustomStaxSource(streamReader);
58              }
59              else {
60                  return null;
61              }
62          }
63          catch (OMException ex) {
64              throw new AxiomSoapBodyException(ex);
65          }
66      }
67  
68      protected abstract XMLStreamReader getStreamReader(OMElement payloadElement);
69  
70      @Override
71      public final Result getResult() {
72          AxiomUtils.removeContents(getAxiomBody());
73          return getResultInternal();
74      }
75  
76      protected abstract Result getResultInternal();
77  
78      public SOAPFactory getAxiomFactory() {
79          return axiomFactory;
80      }
81  
82      protected SOAPBody getAxiomBody() {
83          return axiomBody;
84      }
85  
86      protected OMElement getPayloadElement() throws OMException {
87          return getAxiomBody().getFirstElement();
88      }
89  
90  }