View Javadoc

1   /*
2    * Copyright 2008 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.apache.axiom.om.OMElement;
24  import org.apache.axiom.om.OMException;
25  import org.apache.axiom.soap.SOAPBody;
26  import org.apache.axiom.soap.SOAPFactory;
27  
28  import org.springframework.util.Assert;
29  import org.springframework.ws.soap.axiom.support.AxiomUtils;
30  import org.springframework.xml.transform.StaxSource;
31  
32  /**
33   * Abstract base class for payloads in Axiom. Comes in two flavors: {@link CachingPayload} and {@link
34   * NonCachingPayload}.
35   *
36   * @author Arjen Poutsma
37   * @since 1.5.2
38   */
39  abstract class Payload {
40  
41      private final SOAPBody axiomBody;
42  
43      private final SOAPFactory axiomFactory;
44  
45      protected Payload(SOAPBody axiomBody, SOAPFactory axiomFactory) {
46          Assert.notNull(axiomBody, "'axiomBody' must not be null");
47          Assert.notNull(axiomFactory, "'axiomFactory' must not be null");
48          this.axiomBody = axiomBody;
49          this.axiomFactory = axiomFactory;
50      }
51  
52      public final Source getSource() {
53          try {
54              OMElement payloadElement = getPayloadElement();
55              if (payloadElement != null) {
56                  XMLStreamReader streamReader = getStreamReader(payloadElement);
57                  return new StaxSource(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      public final Result getResult() {
71          AxiomUtils.removeContents(getAxiomBody());
72          return getResultInternal();
73      }
74  
75      protected abstract Result getResultInternal();
76  
77      public SOAPFactory getAxiomFactory() {
78          return axiomFactory;
79      }
80  
81      protected SOAPBody getAxiomBody() {
82          return axiomBody;
83      }
84  
85      protected OMElement getPayloadElement() throws OMException {
86          return getAxiomBody().getFirstElement();
87      }
88  
89  }