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.Locale;
020: import javax.xml.namespace.QName;
021:
022: import org.apache.axiom.om.OMAttribute;
023: import org.apache.axiom.om.OMNamespace;
024: import org.apache.axiom.soap.SOAP11Constants;
025: import org.apache.axiom.soap.SOAPBody;
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.SOAPFaultReason;
030: import org.apache.axiom.soap.SOAPProcessingException;
031: import org.springframework.util.Assert;
032: import org.springframework.util.StringUtils;
033: import org.springframework.ws.soap.SoapFault;
034: import org.springframework.ws.soap.axiom.support.AxiomUtils;
035: import org.springframework.ws.soap.soap11.Soap11Body;
036: import org.springframework.ws.soap.soap11.Soap11Fault;
037: import org.springframework.xml.namespace.QNameUtils;
038:
039: /**
040: * Axiom-specific version of <code>org.springframework.ws.soap.Soap11Body</code>.
041: *
042: * @author Arjen Poutsma
043: * @since 1.0.0
044: */
045: class AxiomSoap11Body extends AxiomSoapBody implements Soap11Body {
046:
047: AxiomSoap11Body(SOAPBody axiomBody, SOAPFactory axiomFactory,
048: boolean payloadCaching) {
049: super (axiomBody, axiomFactory, payloadCaching);
050: }
051:
052: public SoapFault addMustUnderstandFault(String faultString,
053: Locale locale) {
054: SOAPFault fault = addStandardFault(
055: SOAP11Constants.FAULT_CODE_MUST_UNDERSTAND,
056: faultString, locale);
057: return new AxiomSoap11Fault(fault, getAxiomFactory());
058: }
059:
060: public SoapFault addClientOrSenderFault(String faultString,
061: Locale locale) {
062: SOAPFault fault = addStandardFault(
063: SOAP11Constants.FAULT_CODE_SENDER, faultString, locale);
064: return new AxiomSoap11Fault(fault, getAxiomFactory());
065: }
066:
067: public SoapFault addServerOrReceiverFault(String faultString,
068: Locale locale) {
069: SOAPFault fault = addStandardFault(
070: SOAP11Constants.FAULT_CODE_RECEIVER, faultString,
071: locale);
072: return new AxiomSoap11Fault(fault, getAxiomFactory());
073: }
074:
075: public SoapFault addVersionMismatchFault(String faultString,
076: Locale locale) {
077: SOAPFault fault = addStandardFault(
078: SOAP11Constants.FAULT_CODE_VERSION_MISMATCH,
079: faultString, locale);
080: return new AxiomSoap11Fault(fault, getAxiomFactory());
081: }
082:
083: public Soap11Fault addFault(QName code, String faultString,
084: Locale locale) {
085: Assert.notNull(code, "No faultCode given");
086: Assert.hasLength(faultString, "faultString cannot be empty");
087: if (!StringUtils.hasLength(code.getNamespaceURI())) {
088: throw new IllegalArgumentException(
089: "A fault code with namespace and local part must be specific for a custom fault code");
090: }
091: try {
092: detachAllBodyChildren();
093: SOAPFault fault = getAxiomFactory().createSOAPFault(
094: getAxiomBody());
095: SOAPFaultCode faultCode = getAxiomFactory()
096: .createSOAPFaultCode(fault);
097: setValueText(code, fault, faultCode);
098: SOAPFaultReason faultReason = getAxiomFactory()
099: .createSOAPFaultReason(fault);
100: if (locale != null) {
101: addLangAttribute(locale, faultReason);
102: }
103: faultReason.setText(faultString);
104: return new AxiomSoap11Fault(fault, getAxiomFactory());
105:
106: } catch (SOAPProcessingException ex) {
107: throw new AxiomSoapFaultException(ex);
108: }
109:
110: }
111:
112: private void setValueText(QName code, SOAPFault fault,
113: SOAPFaultCode faultCode) {
114: String prefix = QNameUtils.getPrefix(code);
115: if (StringUtils.hasLength(code.getNamespaceURI())
116: && StringUtils.hasLength(prefix)) {
117: OMNamespace namespace = fault.findNamespaceURI(prefix);
118: if (namespace == null) {
119: fault.declareNamespace(code.getNamespaceURI(), prefix);
120: }
121: } else if (StringUtils.hasLength(code.getNamespaceURI())) {
122: OMNamespace namespace = fault.findNamespace(code
123: .getNamespaceURI(), null);
124: if (namespace == null) {
125: throw new IllegalArgumentException(
126: "Could not resolve namespace of code [" + code
127: + "]");
128: }
129: code = QNameUtils.createQName(code.getNamespaceURI(), code
130: .getLocalPart(), namespace.getPrefix());
131: }
132: faultCode.setText(code);
133: }
134:
135: private SOAPFault addStandardFault(String localName,
136: String faultString, Locale locale) {
137: Assert.notNull(faultString, "No faultString given");
138: try {
139: detachAllBodyChildren();
140: SOAPFault fault = getAxiomFactory().createSOAPFault(
141: getAxiomBody());
142: SOAPFaultCode faultCode = getAxiomFactory()
143: .createSOAPFaultCode(fault);
144: faultCode.setText(new QName(fault.getNamespace()
145: .getNamespaceURI(), localName, fault.getNamespace()
146: .getPrefix()));
147: SOAPFaultReason faultReason = getAxiomFactory()
148: .createSOAPFaultReason(fault);
149: if (locale != null) {
150: addLangAttribute(locale, faultReason);
151: }
152: faultReason.setText(faultString);
153: return fault;
154: } catch (SOAPProcessingException ex) {
155: throw new AxiomSoapFaultException(ex);
156: }
157: }
158:
159: private void addLangAttribute(Locale locale,
160: SOAPFaultReason faultReason) {
161: OMNamespace xmlNamespace = getAxiomFactory().createOMNamespace(
162: "http://www.w3.org/XML/1998/namespace", "xml");
163: OMAttribute langAttribute = getAxiomFactory()
164: .createOMAttribute("lang", xmlNamespace,
165: AxiomUtils.toLanguage(locale));
166: faultReason.addAttribute(langAttribute);
167: }
168:
169: }
|