Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.messaging.saaj.soap.impl; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.Locale; |
aoqi@0 | 29 | import java.util.logging.Level; |
aoqi@0 | 30 | |
aoqi@0 | 31 | import javax.xml.namespace.QName; |
aoqi@0 | 32 | import javax.xml.soap.*; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import org.w3c.dom.Element; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
aoqi@0 | 37 | import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl; |
aoqi@0 | 38 | import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl; |
aoqi@0 | 39 | |
aoqi@0 | 40 | public abstract class FaultImpl extends ElementImpl implements SOAPFault { |
aoqi@0 | 41 | |
aoqi@0 | 42 | /* This can also represent a fault reason element */ |
aoqi@0 | 43 | protected SOAPFaultElement faultStringElement; |
aoqi@0 | 44 | |
aoqi@0 | 45 | /* This can also represent a fault role element */ |
aoqi@0 | 46 | protected SOAPFaultElement faultActorElement; |
aoqi@0 | 47 | |
aoqi@0 | 48 | protected SOAPFaultElement faultCodeElement; |
aoqi@0 | 49 | |
aoqi@0 | 50 | protected Detail detail; |
aoqi@0 | 51 | |
aoqi@0 | 52 | protected FaultImpl(SOAPDocumentImpl ownerDoc, NameImpl name) { |
aoqi@0 | 53 | super(ownerDoc, name); |
aoqi@0 | 54 | } |
aoqi@0 | 55 | |
aoqi@0 | 56 | |
aoqi@0 | 57 | protected abstract NameImpl getDetailName(); |
aoqi@0 | 58 | protected abstract NameImpl getFaultCodeName(); |
aoqi@0 | 59 | protected abstract NameImpl getFaultStringName(); |
aoqi@0 | 60 | protected abstract NameImpl getFaultActorName(); |
aoqi@0 | 61 | protected abstract DetailImpl createDetail(); |
aoqi@0 | 62 | protected abstract FaultElementImpl createSOAPFaultElement(String localName); |
aoqi@0 | 63 | protected abstract FaultElementImpl createSOAPFaultElement(QName qname); |
aoqi@0 | 64 | protected abstract FaultElementImpl createSOAPFaultElement(Name qname); |
aoqi@0 | 65 | protected abstract void checkIfStandardFaultCode(String faultCode, String uri) throws SOAPException; |
aoqi@0 | 66 | protected abstract void finallySetFaultCode(String faultcode) throws SOAPException; |
aoqi@0 | 67 | protected abstract boolean isStandardFaultElement(String localName); |
aoqi@0 | 68 | protected abstract QName getDefaultFaultCode(); |
aoqi@0 | 69 | |
aoqi@0 | 70 | |
aoqi@0 | 71 | protected void findFaultCodeElement() { |
aoqi@0 | 72 | this.faultCodeElement = |
aoqi@0 | 73 | (SOAPFaultElement) findChild(getFaultCodeName()); |
aoqi@0 | 74 | } |
aoqi@0 | 75 | |
aoqi@0 | 76 | protected void findFaultActorElement() { |
aoqi@0 | 77 | this.faultActorElement = |
aoqi@0 | 78 | (SOAPFaultElement) findChild(getFaultActorName()); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | protected void findFaultStringElement() { |
aoqi@0 | 82 | this.faultStringElement = |
aoqi@0 | 83 | (SOAPFaultElement) findChild(getFaultStringName()); |
aoqi@0 | 84 | } |
aoqi@0 | 85 | |
aoqi@0 | 86 | public void setFaultCode(String faultCode) throws SOAPException { |
aoqi@0 | 87 | setFaultCode( |
aoqi@0 | 88 | NameImpl.getLocalNameFromTagName(faultCode), |
aoqi@0 | 89 | NameImpl.getPrefixFromTagName(faultCode), |
aoqi@0 | 90 | null); |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | public void setFaultCode(String faultCode, String prefix, String uri) |
aoqi@0 | 94 | throws SOAPException { |
aoqi@0 | 95 | |
aoqi@0 | 96 | if (prefix == null || "".equals(prefix)) { |
aoqi@0 | 97 | if (uri != null && !"".equals(uri)) { |
aoqi@0 | 98 | prefix = getNamespacePrefix(uri); |
aoqi@0 | 99 | if (prefix == null || "".equals(prefix)) { |
aoqi@0 | 100 | prefix = "ns0"; |
aoqi@0 | 101 | } |
aoqi@0 | 102 | } |
aoqi@0 | 103 | } |
aoqi@0 | 104 | if (this.faultCodeElement == null) |
aoqi@0 | 105 | findFaultCodeElement(); |
aoqi@0 | 106 | |
aoqi@0 | 107 | if (this.faultCodeElement == null) |
aoqi@0 | 108 | this.faultCodeElement = addFaultCodeElement(); |
aoqi@0 | 109 | else |
aoqi@0 | 110 | this.faultCodeElement.removeContents(); |
aoqi@0 | 111 | |
aoqi@0 | 112 | if (uri == null || "".equals(uri)) { |
aoqi@0 | 113 | uri = this.faultCodeElement.getNamespaceURI(prefix); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | if (uri == null || "".equals(uri)) { |
aoqi@0 | 116 | if (prefix != null && !"".equals(prefix)) { |
aoqi@0 | 117 | //cannot allow an empty URI for a non-Empty prefix |
aoqi@0 | 118 | log.log(Level.SEVERE, "SAAJ0140.impl.no.ns.URI", new Object[]{prefix + ":" + faultCode}); |
aoqi@0 | 119 | throw new SOAPExceptionImpl("Empty/Null NamespaceURI specified for faultCode \"" + prefix + ":" + faultCode + "\""); |
aoqi@0 | 120 | } else { |
aoqi@0 | 121 | uri = ""; |
aoqi@0 | 122 | } |
aoqi@0 | 123 | } |
aoqi@0 | 124 | checkIfStandardFaultCode(faultCode, uri); |
aoqi@0 | 125 | ((FaultElementImpl) this.faultCodeElement).ensureNamespaceIsDeclared(prefix, uri); |
aoqi@0 | 126 | |
aoqi@0 | 127 | if (prefix == null || "".equals(prefix)) { |
aoqi@0 | 128 | finallySetFaultCode(faultCode); |
aoqi@0 | 129 | } else { |
aoqi@0 | 130 | finallySetFaultCode(prefix + ":" + faultCode); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | public void setFaultCode(Name faultCodeQName) throws SOAPException { |
aoqi@0 | 135 | setFaultCode( |
aoqi@0 | 136 | faultCodeQName.getLocalName(), |
aoqi@0 | 137 | faultCodeQName.getPrefix(), |
aoqi@0 | 138 | faultCodeQName.getURI()); |
aoqi@0 | 139 | } |
aoqi@0 | 140 | |
aoqi@0 | 141 | public void setFaultCode(QName faultCodeQName) throws SOAPException { |
aoqi@0 | 142 | setFaultCode( |
aoqi@0 | 143 | faultCodeQName.getLocalPart(), |
aoqi@0 | 144 | faultCodeQName.getPrefix(), |
aoqi@0 | 145 | faultCodeQName.getNamespaceURI()); |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | protected static QName convertCodeToQName( |
aoqi@0 | 149 | String code, |
aoqi@0 | 150 | SOAPElement codeContainingElement) { |
aoqi@0 | 151 | |
aoqi@0 | 152 | int prefixIndex = code.indexOf(':'); |
aoqi@0 | 153 | if (prefixIndex == -1) { |
aoqi@0 | 154 | return new QName(code); |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | String prefix = code.substring(0, prefixIndex); |
aoqi@0 | 158 | String nsName =((ElementImpl) codeContainingElement).lookupNamespaceURI(prefix); |
aoqi@0 | 159 | //((ElementImpl) codeContainingElement).getNamespaceURI(prefix); |
aoqi@0 | 160 | return new QName(nsName, getLocalPart(code), prefix); |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | protected void initializeDetail() { |
aoqi@0 | 164 | NameImpl detailName = getDetailName(); |
aoqi@0 | 165 | detail = (Detail) findChild(detailName); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | public Detail getDetail() { |
aoqi@0 | 169 | if (detail == null) |
aoqi@0 | 170 | initializeDetail(); |
aoqi@0 | 171 | if ((detail != null) && (detail.getParentNode() == null)) { |
aoqi@0 | 172 | // a detach node was called on it |
aoqi@0 | 173 | detail = null; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | return detail; |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | public Detail addDetail() throws SOAPException { |
aoqi@0 | 179 | if (detail == null) |
aoqi@0 | 180 | initializeDetail(); |
aoqi@0 | 181 | if (detail == null) { |
aoqi@0 | 182 | detail = createDetail(); |
aoqi@0 | 183 | addNode(detail); |
aoqi@0 | 184 | return detail; |
aoqi@0 | 185 | } else { |
aoqi@0 | 186 | // Log |
aoqi@0 | 187 | throw new SOAPExceptionImpl("Error: Detail already exists"); |
aoqi@0 | 188 | } |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | public boolean hasDetail() { |
aoqi@0 | 192 | return (getDetail() != null); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | public abstract void setFaultActor(String faultActor) throws SOAPException; |
aoqi@0 | 196 | |
aoqi@0 | 197 | public String getFaultActor() { |
aoqi@0 | 198 | if (this.faultActorElement == null) |
aoqi@0 | 199 | findFaultActorElement(); |
aoqi@0 | 200 | if (this.faultActorElement != null) { |
aoqi@0 | 201 | return this.faultActorElement.getValue(); |
aoqi@0 | 202 | } |
aoqi@0 | 203 | return null; |
aoqi@0 | 204 | } |
aoqi@0 | 205 | |
aoqi@0 | 206 | public SOAPElement setElementQName(QName newName) throws SOAPException { |
aoqi@0 | 207 | |
aoqi@0 | 208 | log.log( |
aoqi@0 | 209 | Level.SEVERE, |
aoqi@0 | 210 | "SAAJ0146.impl.invalid.name.change.requested", |
aoqi@0 | 211 | new Object[] {elementQName.getLocalPart(), newName.getLocalPart()}); |
aoqi@0 | 212 | throw new SOAPException( |
aoqi@0 | 213 | "Cannot change name for " + elementQName.getLocalPart() + " to " + newName.getLocalPart()); |
aoqi@0 | 214 | } |
aoqi@0 | 215 | |
aoqi@0 | 216 | protected SOAPElement convertToSoapElement(Element element) { |
aoqi@0 | 217 | if (element instanceof SOAPFaultElement) { |
aoqi@0 | 218 | return (SOAPElement) element; |
aoqi@0 | 219 | } else if (element instanceof SOAPElement) { |
aoqi@0 | 220 | SOAPElement soapElement = (SOAPElement) element; |
aoqi@0 | 221 | if (getDetailName().equals(soapElement.getElementName())) { |
aoqi@0 | 222 | return replaceElementWithSOAPElement(element, createDetail()); |
aoqi@0 | 223 | } else { |
aoqi@0 | 224 | String localName = |
aoqi@0 | 225 | soapElement.getElementName().getLocalName(); |
aoqi@0 | 226 | if (isStandardFaultElement(localName)) |
aoqi@0 | 227 | return replaceElementWithSOAPElement( |
aoqi@0 | 228 | element, |
aoqi@0 | 229 | createSOAPFaultElement(soapElement.getElementQName())); |
aoqi@0 | 230 | return soapElement; |
aoqi@0 | 231 | } |
aoqi@0 | 232 | } else { |
aoqi@0 | 233 | Name elementName = NameImpl.copyElementName(element); |
aoqi@0 | 234 | ElementImpl newElement; |
aoqi@0 | 235 | if (getDetailName().equals(elementName)) { |
aoqi@0 | 236 | newElement = (ElementImpl) createDetail(); |
aoqi@0 | 237 | } else { |
aoqi@0 | 238 | String localName = elementName.getLocalName(); |
aoqi@0 | 239 | if (isStandardFaultElement(localName)) |
aoqi@0 | 240 | newElement = |
aoqi@0 | 241 | (ElementImpl) createSOAPFaultElement(elementName); |
aoqi@0 | 242 | else |
aoqi@0 | 243 | newElement = (ElementImpl) createElement(elementName); |
aoqi@0 | 244 | } |
aoqi@0 | 245 | return replaceElementWithSOAPElement(element, newElement); |
aoqi@0 | 246 | } |
aoqi@0 | 247 | } |
aoqi@0 | 248 | |
aoqi@0 | 249 | protected SOAPFaultElement addFaultCodeElement() throws SOAPException { |
aoqi@0 | 250 | if (this.faultCodeElement == null) |
aoqi@0 | 251 | findFaultCodeElement(); |
aoqi@0 | 252 | if (this.faultCodeElement == null) { |
aoqi@0 | 253 | this.faultCodeElement = |
aoqi@0 | 254 | addSOAPFaultElement(getFaultCodeName().getLocalName()); |
aoqi@0 | 255 | return this.faultCodeElement; |
aoqi@0 | 256 | } else { |
aoqi@0 | 257 | throw new SOAPExceptionImpl("Error: Faultcode already exists"); |
aoqi@0 | 258 | } |
aoqi@0 | 259 | } |
aoqi@0 | 260 | |
aoqi@0 | 261 | private SOAPFaultElement addFaultStringElement() throws SOAPException { |
aoqi@0 | 262 | if (this.faultStringElement == null) |
aoqi@0 | 263 | findFaultStringElement(); |
aoqi@0 | 264 | if (this.faultStringElement == null) { |
aoqi@0 | 265 | this.faultStringElement = |
aoqi@0 | 266 | addSOAPFaultElement(getFaultStringName().getLocalName()); |
aoqi@0 | 267 | return this.faultStringElement; |
aoqi@0 | 268 | } else { |
aoqi@0 | 269 | // Log |
aoqi@0 | 270 | throw new SOAPExceptionImpl("Error: Faultstring already exists"); |
aoqi@0 | 271 | } |
aoqi@0 | 272 | } |
aoqi@0 | 273 | |
aoqi@0 | 274 | private SOAPFaultElement addFaultActorElement() throws SOAPException { |
aoqi@0 | 275 | if (this.faultActorElement == null) |
aoqi@0 | 276 | findFaultActorElement(); |
aoqi@0 | 277 | if (this.faultActorElement == null) { |
aoqi@0 | 278 | this.faultActorElement = |
aoqi@0 | 279 | addSOAPFaultElement(getFaultActorName().getLocalName()); |
aoqi@0 | 280 | return this.faultActorElement; |
aoqi@0 | 281 | } else { |
aoqi@0 | 282 | // Log |
aoqi@0 | 283 | throw new SOAPExceptionImpl("Error: Faultactor already exists"); |
aoqi@0 | 284 | } |
aoqi@0 | 285 | } |
aoqi@0 | 286 | |
aoqi@0 | 287 | protected SOAPElement addElement(Name name) throws SOAPException { |
aoqi@0 | 288 | if (getDetailName().equals(name)) { |
aoqi@0 | 289 | return addDetail(); |
aoqi@0 | 290 | } else if(getFaultCodeName().equals(name)) { |
aoqi@0 | 291 | return addFaultCodeElement(); |
aoqi@0 | 292 | } else if(getFaultStringName().equals(name)) { |
aoqi@0 | 293 | return addFaultStringElement(); |
aoqi@0 | 294 | } else if(getFaultActorName().equals(name)) { |
aoqi@0 | 295 | return addFaultActorElement(); |
aoqi@0 | 296 | } |
aoqi@0 | 297 | return super.addElement(name); |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | protected SOAPElement addElement(QName name) throws SOAPException { |
aoqi@0 | 301 | return addElement(NameImpl.convertToName(name)); |
aoqi@0 | 302 | } |
aoqi@0 | 303 | |
aoqi@0 | 304 | protected FaultElementImpl addSOAPFaultElement(String localName) |
aoqi@0 | 305 | throws SOAPException { |
aoqi@0 | 306 | |
aoqi@0 | 307 | FaultElementImpl faultElem = createSOAPFaultElement(localName); |
aoqi@0 | 308 | addNode(faultElem); |
aoqi@0 | 309 | return faultElem; |
aoqi@0 | 310 | } |
aoqi@0 | 311 | |
aoqi@0 | 312 | /** |
aoqi@0 | 313 | * Convert an xml:lang attribute value into a Locale object |
aoqi@0 | 314 | */ |
aoqi@0 | 315 | protected static Locale xmlLangToLocale(String xmlLang) { |
aoqi@0 | 316 | if (xmlLang == null) { |
aoqi@0 | 317 | return null; |
aoqi@0 | 318 | } |
aoqi@0 | 319 | |
aoqi@0 | 320 | // Spec uses hyphen as separator |
aoqi@0 | 321 | int index = xmlLang.indexOf("-"); |
aoqi@0 | 322 | |
aoqi@0 | 323 | // Accept underscore as separator as well |
aoqi@0 | 324 | if (index == -1) { |
aoqi@0 | 325 | index = xmlLang.indexOf("_"); |
aoqi@0 | 326 | } |
aoqi@0 | 327 | |
aoqi@0 | 328 | if (index == -1) { |
aoqi@0 | 329 | // No separator so assume only a language component |
aoqi@0 | 330 | return new Locale(xmlLang, ""); |
aoqi@0 | 331 | } |
aoqi@0 | 332 | |
aoqi@0 | 333 | String language = xmlLang.substring(0, index); |
aoqi@0 | 334 | String country = xmlLang.substring(index + 1); |
aoqi@0 | 335 | return new Locale(language, country); |
aoqi@0 | 336 | } |
aoqi@0 | 337 | |
aoqi@0 | 338 | protected static String localeToXmlLang(Locale locale) { |
aoqi@0 | 339 | String xmlLang = locale.getLanguage(); |
aoqi@0 | 340 | String country = locale.getCountry(); |
aoqi@0 | 341 | if (!"".equals(country)) { |
aoqi@0 | 342 | xmlLang += "-" + country; |
aoqi@0 | 343 | } |
aoqi@0 | 344 | return xmlLang; |
aoqi@0 | 345 | } |
aoqi@0 | 346 | } |