1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.axiom;
18
19 import java.util.Locale;
20 import javax.xml.namespace.QName;
21
22 import org.apache.axiom.om.OMAttribute;
23 import org.apache.axiom.om.OMNamespace;
24 import org.apache.axiom.soap.SOAP11Constants;
25 import org.apache.axiom.soap.SOAPBody;
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.SOAPFaultReason;
30 import org.apache.axiom.soap.SOAPProcessingException;
31
32 import org.springframework.util.Assert;
33 import org.springframework.util.StringUtils;
34 import org.springframework.ws.soap.SoapFault;
35 import org.springframework.ws.soap.axiom.support.AxiomUtils;
36 import org.springframework.ws.soap.soap11.Soap11Body;
37 import org.springframework.ws.soap.soap11.Soap11Fault;
38 import org.springframework.xml.namespace.QNameUtils;
39
40
41
42
43
44
45
46 class AxiomSoap11Body extends AxiomSoapBody implements Soap11Body {
47
48 private final boolean langAttributeOnSoap11FaultString;
49
50 AxiomSoap11Body(SOAPBody axiomBody,
51 SOAPFactory axiomFactory,
52 boolean payloadCaching,
53 boolean langAttributeOnSoap11FaultString) {
54 super(axiomBody, axiomFactory, payloadCaching);
55 this.langAttributeOnSoap11FaultString = langAttributeOnSoap11FaultString;
56 }
57
58 public SoapFault addMustUnderstandFault(String faultString, Locale locale) {
59 SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_MUST_UNDERSTAND, faultString, locale);
60 return new AxiomSoap11Fault(fault, getAxiomFactory());
61 }
62
63 public SoapFault addClientOrSenderFault(String faultString, Locale locale) {
64 SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_SENDER, faultString, locale);
65 return new AxiomSoap11Fault(fault, getAxiomFactory());
66 }
67
68 public SoapFault addServerOrReceiverFault(String faultString, Locale locale) {
69 SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_RECEIVER, faultString, locale);
70 return new AxiomSoap11Fault(fault, getAxiomFactory());
71 }
72
73 public SoapFault addVersionMismatchFault(String faultString, Locale locale) {
74 SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_VERSION_MISMATCH, faultString, locale);
75 return new AxiomSoap11Fault(fault, getAxiomFactory());
76 }
77
78 public Soap11Fault addFault(QName code, String faultString, Locale faultStringLocale) {
79 Assert.notNull(code, "No faultCode given");
80 Assert.hasLength(faultString, "faultString cannot be empty");
81 if (!StringUtils.hasLength(code.getNamespaceURI())) {
82 throw new IllegalArgumentException(
83 "A fault code with namespace and local part must be specific for a custom fault code");
84 }
85 if (!langAttributeOnSoap11FaultString) {
86 faultStringLocale = null;
87 }
88 try {
89 AxiomUtils.removeContents(getAxiomBody());
90 SOAPFault fault = getAxiomFactory().createSOAPFault(getAxiomBody());
91 SOAPFaultCode faultCode = getAxiomFactory().createSOAPFaultCode(fault);
92 setValueText(code, fault, faultCode);
93 SOAPFaultReason faultReason = getAxiomFactory().createSOAPFaultReason(fault);
94 if (faultStringLocale != null) {
95 addLangAttribute(faultStringLocale, faultReason);
96 }
97 faultReason.setText(faultString);
98 return new AxiomSoap11Fault(fault, getAxiomFactory());
99
100 }
101 catch (SOAPProcessingException ex) {
102 throw new AxiomSoapFaultException(ex);
103 }
104 }
105
106 private void setValueText(QName code, SOAPFault fault, SOAPFaultCode faultCode) {
107 String prefix = QNameUtils.getPrefix(code);
108 if (StringUtils.hasLength(code.getNamespaceURI()) && StringUtils.hasLength(prefix)) {
109 OMNamespace namespace = fault.findNamespaceURI(prefix);
110 if (namespace == null) {
111 fault.declareNamespace(code.getNamespaceURI(), prefix);
112 }
113 }
114 else if (StringUtils.hasLength(code.getNamespaceURI())) {
115 OMNamespace namespace = fault.findNamespace(code.getNamespaceURI(), null);
116 if (namespace == null) {
117 namespace = fault.declareNamespace(code.getNamespaceURI(), "");
118 }
119 code = QNameUtils.createQName(code.getNamespaceURI(), code.getLocalPart(), namespace.getPrefix());
120 }
121 faultCode.setText(code);
122 }
123
124 private SOAPFault addStandardFault(String localName, String faultString, Locale locale) {
125 Assert.notNull(faultString, "No faultString given");
126 try {
127 AxiomUtils.removeContents(getAxiomBody());
128 SOAPFault fault = getAxiomFactory().createSOAPFault(getAxiomBody());
129 SOAPFaultCode faultCode = getAxiomFactory().createSOAPFaultCode(fault);
130 faultCode.setText(QNameUtils.createQName(fault.getNamespace().getNamespaceURI(), localName,
131 fault.getNamespace().getPrefix()));
132 SOAPFaultReason faultReason = getAxiomFactory().createSOAPFaultReason(fault);
133 if (locale != null) {
134 addLangAttribute(locale, faultReason);
135 }
136 faultReason.setText(faultString);
137 return fault;
138 }
139 catch (SOAPProcessingException ex) {
140 throw new AxiomSoapFaultException(ex);
141 }
142 }
143
144 private void addLangAttribute(Locale locale, SOAPFaultReason faultReason) {
145 OMNamespace xmlNamespace = getAxiomFactory().createOMNamespace("http://www.w3.org/XML/1998/namespace", "xml");
146 OMAttribute langAttribute =
147 getAxiomFactory().createOMAttribute("lang", xmlNamespace, AxiomUtils.toLanguage(locale));
148 faultReason.addAttribute(langAttribute);
149 }
150
151 public SoapFault getFault() {
152 SOAPFault axiomFault = getAxiomBody().getFault();
153 return axiomFault != null ? new AxiomSoap11Fault(axiomFault, getAxiomFactory()) : null;
154 }
155
156 }