001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.axiom;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Locale;
023: import javax.xml.namespace.QName;
024:
025: import org.apache.axiom.om.OMNamespace;
026: import org.apache.axiom.soap.SOAPFactory;
027: import org.apache.axiom.soap.SOAPFault;
028: import org.apache.axiom.soap.SOAPFaultCode;
029: import org.apache.axiom.soap.SOAPFaultNode;
030: import org.apache.axiom.soap.SOAPFaultReason;
031: import org.apache.axiom.soap.SOAPFaultSubCode;
032: import org.apache.axiom.soap.SOAPFaultText;
033: import org.apache.axiom.soap.SOAPFaultValue;
034: import org.apache.axiom.soap.SOAPProcessingException;
035: import org.springframework.util.StringUtils;
036: import org.springframework.ws.soap.axiom.support.AxiomUtils;
037: import org.springframework.ws.soap.soap12.Soap12Fault;
038: import org.springframework.xml.namespace.QNameUtils;
039:
040: /** Axiom-specific version of <code>org.springframework.ws.soap.Soap12Fault</code>. */
041: class AxiomSoap12Fault extends AxiomSoapFault implements Soap12Fault {
042:
043: AxiomSoap12Fault(SOAPFault axiomFault, SOAPFactory axiomFactory) {
044: super (axiomFault, axiomFactory);
045: }
046:
047: public QName getFaultCode() {
048: return getAxiomFault().getCode().getValue().getTextAsQName();
049: }
050:
051: public Iterator getFaultSubcodes() {
052: List subcodes = new ArrayList();
053: SOAPFaultSubCode subcode = getAxiomFault().getCode()
054: .getSubCode();
055: while (subcode != null) {
056: subcodes.add(subcode.getValue().getTextAsQName());
057: subcode = subcode.getSubCode();
058: }
059: return subcodes.iterator();
060: }
061:
062: public void addFaultSubcode(QName subcode) {
063: SOAPFaultCode faultCode = getAxiomFault().getCode();
064: SOAPFaultSubCode faultSubCode = null;
065: if (faultCode.getSubCode() == null) {
066: faultSubCode = getAxiomFactory().createSOAPFaultSubCode(
067: faultCode);
068: } else {
069: faultSubCode = faultCode.getSubCode();
070: while (true) {
071: if (faultSubCode.getSubCode() != null) {
072: faultSubCode = faultSubCode.getSubCode();
073: } else {
074: faultSubCode = getAxiomFactory()
075: .createSOAPFaultSubCode(faultSubCode);
076: break;
077: }
078: }
079: }
080: SOAPFaultValue faultValue = getAxiomFactory()
081: .createSOAPFaultValue(faultSubCode);
082: setValueText(subcode, faultValue);
083: }
084:
085: private void setValueText(QName code, SOAPFaultValue faultValue) {
086: String prefix = QNameUtils.getPrefix(code);
087: if (StringUtils.hasLength(code.getNamespaceURI())
088: && StringUtils.hasLength(prefix)) {
089: OMNamespace namespace = getAxiomFault().findNamespaceURI(
090: prefix);
091: if (namespace == null) {
092: getAxiomFault().declareNamespace(
093: code.getNamespaceURI(), prefix);
094: }
095: } else if (StringUtils.hasLength(code.getNamespaceURI())) {
096: OMNamespace namespace = getAxiomFault().findNamespace(
097: code.getNamespaceURI(), null);
098: if (namespace == null) {
099: throw new IllegalArgumentException(
100: "Could not resolve namespace of code [" + code
101: + "]");
102: }
103: code = QNameUtils.createQName(code.getNamespaceURI(), code
104: .getLocalPart(), namespace.getPrefix());
105: }
106: faultValue.setText(prefix + ":" + code.getLocalPart());
107: }
108:
109: public String getFaultNode() {
110: SOAPFaultNode faultNode = getAxiomFault().getNode();
111: if (faultNode == null) {
112: return null;
113: } else {
114: return faultNode.getNodeValue();
115: }
116: }
117:
118: public void setFaultNode(String uri) {
119: try {
120: SOAPFaultNode faultNode = getAxiomFactory()
121: .createSOAPFaultNode(getAxiomFault());
122: faultNode.setNodeValue(uri);
123: getAxiomFault().setNode(faultNode);
124: } catch (SOAPProcessingException ex) {
125: throw new AxiomSoapFaultException(ex);
126: }
127: }
128:
129: public String getFaultStringOrReason() {
130: return getFaultReasonText(Locale.getDefault());
131: }
132:
133: public String getFaultReasonText(Locale locale) {
134: SOAPFaultReason faultReason = getAxiomFault().getReason();
135: String language = AxiomUtils.toLanguage(locale);
136: SOAPFaultText faultText = faultReason
137: .getSOAPFaultText(language);
138: return faultText != null ? faultText.getText() : null;
139: }
140:
141: public void setFaultReasonText(Locale locale, String text) {
142: SOAPFaultReason faultReason = getAxiomFault().getReason();
143: String language = AxiomUtils.toLanguage(locale);
144: try {
145: SOAPFaultText faultText = getAxiomFactory()
146: .createSOAPFaultText(faultReason);
147: faultText.setLang(language);
148: faultText.setText(text);
149: } catch (SOAPProcessingException ex) {
150: throw new AxiomSoapFaultException(ex);
151: }
152: }
153:
154: }
|