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