src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/FaultImpl.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial