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