View Javadoc

1   /*
2    * Copyright 2006 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 java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Locale;
23  import javax.xml.namespace.QName;
24  
25  import org.apache.axiom.om.OMNamespace;
26  import org.apache.axiom.soap.SOAPFactory;
27  import org.apache.axiom.soap.SOAPFault;
28  import org.apache.axiom.soap.SOAPFaultCode;
29  import org.apache.axiom.soap.SOAPFaultNode;
30  import org.apache.axiom.soap.SOAPFaultReason;
31  import org.apache.axiom.soap.SOAPFaultSubCode;
32  import org.apache.axiom.soap.SOAPFaultText;
33  import org.apache.axiom.soap.SOAPFaultValue;
34  import org.apache.axiom.soap.SOAPProcessingException;
35  import org.springframework.util.StringUtils;
36  import org.springframework.ws.soap.axiom.support.AxiomUtils;
37  import org.springframework.ws.soap.soap12.Soap12Fault;
38  import org.springframework.xml.namespace.QNameUtils;
39  
40  /** Axiom-specific version of <code>org.springframework.ws.soap.Soap12Fault</code>. */
41  class AxiomSoap12Fault extends AxiomSoapFault implements Soap12Fault {
42  
43      AxiomSoap12Fault(SOAPFault axiomFault, SOAPFactory axiomFactory) {
44          super(axiomFault, axiomFactory);
45      }
46  
47      public QName getFaultCode() {
48          return getAxiomFault().getCode().getValue().getTextAsQName();
49      }
50  
51      public Iterator getFaultSubcodes() {
52          List subcodes = new ArrayList();
53          SOAPFaultSubCode subcode = getAxiomFault().getCode().getSubCode();
54          while (subcode != null) {
55              subcodes.add(subcode.getValue().getTextAsQName());
56              subcode = subcode.getSubCode();
57          }
58          return subcodes.iterator();
59      }
60  
61      public void addFaultSubcode(QName subcode) {
62          SOAPFaultCode faultCode = getAxiomFault().getCode();
63          SOAPFaultSubCode faultSubCode = null;
64          if (faultCode.getSubCode() == null) {
65              faultSubCode = getAxiomFactory().createSOAPFaultSubCode(faultCode);
66          }
67          else {
68              faultSubCode = faultCode.getSubCode();
69              while (true) {
70                  if (faultSubCode.getSubCode() != null) {
71                      faultSubCode = faultSubCode.getSubCode();
72                  }
73                  else {
74                      faultSubCode = getAxiomFactory().createSOAPFaultSubCode(faultSubCode);
75                      break;
76                  }
77              }
78          }
79          SOAPFaultValue faultValue = getAxiomFactory().createSOAPFaultValue(faultSubCode);
80          setValueText(subcode, faultValue);
81      }
82  
83      private void setValueText(QName code, SOAPFaultValue faultValue) {
84          String prefix = QNameUtils.getPrefix(code);
85          if (StringUtils.hasLength(code.getNamespaceURI()) && StringUtils.hasLength(prefix)) {
86              OMNamespace namespace = getAxiomFault().findNamespaceURI(prefix);
87              if (namespace == null) {
88                  getAxiomFault().declareNamespace(code.getNamespaceURI(), prefix);
89              }
90          }
91          else if (StringUtils.hasLength(code.getNamespaceURI())) {
92              OMNamespace namespace = getAxiomFault().findNamespace(code.getNamespaceURI(), null);
93              if (namespace == null) {
94                  throw new IllegalArgumentException("Could not resolve namespace of code [" + code + "]");
95              }
96              code = QNameUtils.createQName(code.getNamespaceURI(), code.getLocalPart(), namespace.getPrefix());
97          }
98          faultValue.setText(prefix + ":" + code.getLocalPart());
99      }
100 
101     public String getFaultNode() {
102         SOAPFaultNode faultNode = getAxiomFault().getNode();
103         if (faultNode == null) {
104             return null;
105         }
106         else {
107             return faultNode.getNodeValue();
108         }
109     }
110 
111     public void setFaultNode(String uri) {
112         try {
113             SOAPFaultNode faultNode = getAxiomFactory().createSOAPFaultNode(getAxiomFault());
114             faultNode.setNodeValue(uri);
115             getAxiomFault().setNode(faultNode);
116         }
117         catch (SOAPProcessingException ex) {
118             throw new AxiomSoapFaultException(ex);
119         }
120     }
121 
122     public String getFaultStringOrReason() {
123         return getFaultReasonText(Locale.getDefault());
124     }
125 
126     public String getFaultReasonText(Locale locale) {
127         SOAPFaultReason faultReason = getAxiomFault().getReason();
128         String language = AxiomUtils.toLanguage(locale);
129         SOAPFaultText faultText = faultReason.getSOAPFaultText(language);
130         return faultText != null ? faultText.getText() : null;
131     }
132 
133     public void setFaultReasonText(Locale locale, String text) {
134         SOAPFaultReason faultReason = getAxiomFault().getReason();
135         String language = AxiomUtils.toLanguage(locale);
136         try {
137             SOAPFaultText faultText = getAxiomFactory().createSOAPFaultText(faultReason);
138             faultText.setLang(language);
139             faultText.setText(text);
140         }
141         catch (SOAPProcessingException ex) {
142             throw new AxiomSoapFaultException(ex);
143         }
144     }
145 
146 }