001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: Fault1_1Impl.java,v 1.5 2007/07/16 16:41:24 ofung Exp $
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: /**
061: *
062: * @author SAAJ RI Development Team
063: */package com.sun.xml.messaging.saaj.soap.ver1_1;
064:
065: import java.util.Iterator;
066: import java.util.Locale;
067: import java.util.logging.Logger;
068: import java.util.logging.Level;
069:
070: import javax.xml.namespace.QName;
071: import javax.xml.soap.SOAPElement;
072: import javax.xml.soap.SOAPFaultElement;
073: import javax.xml.soap.SOAPException;
074: import javax.xml.soap.SOAPConstants;
075: import javax.xml.soap.Name;
076: import javax.xml.soap.Name;
077:
078: import com.sun.xml.messaging.saaj.soap.SOAPDocument;
079: import com.sun.xml.messaging.saaj.soap.SOAPDocumentImpl;
080: import com.sun.xml.messaging.saaj.soap.impl.*;
081: import com.sun.xml.messaging.saaj.soap.name.NameImpl;
082: import com.sun.xml.messaging.saaj.util.LogDomainConstants;
083: import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
084:
085: public class Fault1_1Impl extends FaultImpl {
086:
087: protected static Logger log = Logger.getLogger(
088: LogDomainConstants.SOAP_VER1_1_DOMAIN,
089: "com.sun.xml.messaging.saaj.soap.ver1_1.LocalStrings");
090:
091: public Fault1_1Impl(SOAPDocumentImpl ownerDocument, String prefix) {
092: super (ownerDocument, NameImpl.createFault1_1Name(prefix));
093: }
094:
095: protected NameImpl getDetailName() {
096: return NameImpl.createDetail1_1Name();
097: }
098:
099: protected NameImpl getFaultCodeName() {
100: return NameImpl.createFromUnqualifiedName("faultcode");
101: }
102:
103: protected NameImpl getFaultStringName() {
104: return NameImpl.createFromUnqualifiedName("faultstring");
105: }
106:
107: protected NameImpl getFaultActorName() {
108: return NameImpl.createFromUnqualifiedName("faultactor");
109: }
110:
111: protected DetailImpl createDetail() {
112: return new Detail1_1Impl(((SOAPDocument) getOwnerDocument())
113: .getDocument());
114: }
115:
116: protected FaultElementImpl createSOAPFaultElement(String localName) {
117: return new FaultElement1_1Impl(
118: ((SOAPDocument) getOwnerDocument()).getDocument(),
119: localName);
120: }
121:
122: protected void checkIfStandardFaultCode(String faultCode, String uri)
123: throws SOAPException {
124: // SOAP 1.1 doesn't seem to mandate using faultcode from a particular
125: // set of values.
126: // Also need to be backward compatible.
127: }
128:
129: protected void finallySetFaultCode(String faultcode)
130: throws SOAPException {
131: this .faultCodeElement.addTextNode(faultcode);
132: }
133:
134: public String getFaultCode() {
135: if (this .faultCodeElement == null)
136: findFaultCodeElement();
137: return this .faultCodeElement.getValue();
138: }
139:
140: public Name getFaultCodeAsName() {
141:
142: String faultcodeString = getFaultCode();
143: if (faultcodeString == null) {
144: return null;
145: }
146: int prefixIndex = faultcodeString.indexOf(':');
147: if (prefixIndex == -1) {
148: // Not a valid SOAP message, but we return the unqualified name
149: // anyway since some apps do not strictly conform to SOAP
150: // specs. A message that does not contain a <faultcode>
151: // element itself is also not valid in which case we return
152: // null instead of throwing an exception so this is consistent.
153: return NameImpl.createFromUnqualifiedName(faultcodeString);
154: }
155:
156: // Get the prefix and map it to a namespace name (AKA namespace URI)
157: String prefix = faultcodeString.substring(0, prefixIndex);
158: if (this .faultCodeElement == null)
159: findFaultCodeElement();
160: String nsName = this .faultCodeElement.getNamespaceURI(prefix);
161: return NameImpl
162: .createFromQualifiedName(faultcodeString, nsName);
163: }
164:
165: public QName getFaultCodeAsQName() {
166: String faultcodeString = getFaultCode();
167: if (faultcodeString == null) {
168: return null;
169: }
170: if (this .faultCodeElement == null)
171: findFaultCodeElement();
172: return convertCodeToQName(faultcodeString,
173: this .faultCodeElement);
174: }
175:
176: public void setFaultString(String faultString) throws SOAPException {
177:
178: if (this .faultStringElement == null)
179: findFaultStringElement();
180:
181: if (this .faultStringElement == null)
182: this .faultStringElement = addSOAPFaultElement("faultstring");
183: else {
184: this .faultStringElement.removeContents();
185: //this.faultStringElement.removeAttributeNS("http://www.w3.org/XML/1998/namespace", "lang");
186: this .faultStringElement.removeAttribute("xml:lang");
187: }
188:
189: this .faultStringElement.addTextNode(faultString);
190: }
191:
192: public String getFaultString() {
193: if (this .faultStringElement == null)
194: findFaultStringElement();
195: return this .faultStringElement.getValue();
196:
197: }
198:
199: public Locale getFaultStringLocale() {
200: if (this .faultStringElement == null)
201: findFaultStringElement();
202: if (this .faultStringElement != null) {
203: String xmlLangAttr = this .faultStringElement
204: .getAttributeValue(NameImpl
205: .createFromUnqualifiedName("xml:lang"));
206: if (xmlLangAttr != null)
207: return xmlLangToLocale(xmlLangAttr);
208: }
209: return null;
210: }
211:
212: public void setFaultString(String faultString, Locale locale)
213: throws SOAPException {
214: setFaultString(faultString);
215: this .faultStringElement
216: .addAttribute(NameImpl.createFromTagName("xml:lang"),
217: localeToXmlLang(locale));
218: }
219:
220: protected boolean isStandardFaultElement(String localName) {
221: if (localName.equalsIgnoreCase("detail")
222: || localName.equalsIgnoreCase("faultcode")
223: || localName.equalsIgnoreCase("faultstring")
224: || localName.equalsIgnoreCase("faultactor")) {
225: return true;
226: }
227: return false;
228: }
229:
230: public void appendFaultSubcode(QName subcode) {
231: log.log(Level.SEVERE,
232: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
233: "appendFaultSubcode");
234: throw new UnsupportedOperationException(
235: "Not supported in SOAP 1.1");
236: }
237:
238: public void removeAllFaultSubcodes() {
239: log.log(Level.SEVERE,
240: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
241: "removeAllFaultSubcodes");
242: throw new UnsupportedOperationException(
243: "Not supported in SOAP 1.1");
244: }
245:
246: public Iterator getFaultSubcodes() {
247: log.log(Level.SEVERE,
248: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
249: "getFaultSubcodes");
250: throw new UnsupportedOperationException(
251: "Not supported in SOAP 1.1");
252: }
253:
254: public String getFaultReasonText(Locale locale) {
255: log.log(Level.SEVERE,
256: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
257: "getFaultReasonText");
258: throw new UnsupportedOperationException(
259: "Not supported in SOAP 1.1");
260: }
261:
262: public Iterator getFaultReasonTexts() {
263: log.log(Level.SEVERE,
264: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
265: "getFaultReasonTexts");
266: throw new UnsupportedOperationException(
267: "Not supported in SOAP 1.1");
268: }
269:
270: public Iterator getFaultReasonLocales() {
271: log.log(Level.SEVERE,
272: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
273: "getFaultReasonLocales");
274: throw new UnsupportedOperationException(
275: "Not supported in SOAP 1.1");
276: }
277:
278: public void addFaultReasonText(String text, java.util.Locale locale)
279: throws SOAPException {
280: log.log(Level.SEVERE,
281: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
282: "addFaultReasonText");
283: throw new UnsupportedOperationException(
284: "Not supported in SOAP 1.1");
285: }
286:
287: public String getFaultRole() {
288: log.log(Level.SEVERE,
289: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
290: "getFaultRole");
291: throw new UnsupportedOperationException(
292: "Not supported in SOAP 1.1");
293: }
294:
295: public void setFaultRole(String uri) {
296: log.log(Level.SEVERE,
297: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
298: "setFaultRole");
299: throw new UnsupportedOperationException(
300: "Not supported in SOAP 1.1");
301: }
302:
303: public String getFaultNode() {
304: log.log(Level.SEVERE,
305: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
306: "getFaultNode");
307: throw new UnsupportedOperationException(
308: "Not supported in SOAP 1.1");
309: }
310:
311: public void setFaultNode(String uri) {
312: log.log(Level.SEVERE,
313: "SAAJ0303.ver1_1.msg.op.unsupported.in.SOAP1.1",
314: "setFaultNode");
315: throw new UnsupportedOperationException(
316: "Not supported in SOAP 1.1");
317: }
318:
319: protected QName getDefaultFaultCode() {
320: return new QName(SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE,
321: "Server");
322: }
323:
324: public SOAPElement addChildElement(SOAPElement element)
325: throws SOAPException {
326: String localName = element.getLocalName();
327: if ("Detail".equalsIgnoreCase(localName)) {
328: if (hasDetail()) {
329: log.severe("SAAJ0305.ver1_2.detail.exists.error");
330: throw new SOAPExceptionImpl(
331: "Cannot add Detail, Detail already exists");
332: }
333: }
334: return super .addChildElement(element);
335: }
336:
337: protected FaultElementImpl createSOAPFaultElement(QName qname) {
338: return new FaultElement1_1Impl(
339: ((SOAPDocument) getOwnerDocument()).getDocument(),
340: qname);
341: }
342:
343: protected FaultElementImpl createSOAPFaultElement(Name qname) {
344: return new FaultElement1_1Impl(
345: ((SOAPDocument) getOwnerDocument()).getDocument(),
346: (NameImpl) qname);
347: }
348:
349: public void setFaultCode(String faultCode, String prefix, String uri)
350: throws SOAPException {
351: if (prefix == null || prefix.equals("")) {
352: if (uri != null && !"".equals(uri)) {
353: prefix = getNamespacePrefix(uri);
354: if (prefix == null || prefix.equals("")) {
355: prefix = "ns0";
356: }
357: }
358: }
359:
360: if (this .faultCodeElement == null)
361: findFaultCodeElement();
362:
363: if (this .faultCodeElement == null)
364: this .faultCodeElement = addFaultCodeElement();
365: else
366: this .faultCodeElement.removeContents();
367:
368: if (uri == null || uri.equals("")) {
369: if (prefix != null && !"".equals("prefix")) {
370: uri = this .faultCodeElement.getNamespaceURI(prefix);
371: }
372: }
373:
374: if (standardFaultCode(faultCode)
375: && ((uri == null) || uri.equals(""))) {
376: log.log(Level.SEVERE,
377: "SAAJ0306.ver1_1.faultcode.incorrect.namespace",
378: new Object[] { faultCode });
379: throw new SOAPExceptionImpl(
380: "Namespace Error, Standard Faultcode: " + faultCode
381: + ", should be in SOAP 1.1 Namespace");
382: }
383:
384: if (uri == null || uri.equals("")) {
385: //SOAP 1.1 Allows this
386: if (prefix != null && !"".equals(prefix)) {
387: log.severe("SAAJ0140.impl.no.ns.URI");
388: throw new SOAPExceptionImpl(
389: "No NamespaceURI, SOAP requires faultcode content to be a QName");
390: }
391: } else {
392: checkIfStandardFaultCode(faultCode, uri);
393: ((FaultElementImpl) this .faultCodeElement)
394: .ensureNamespaceIsDeclared(prefix, uri);
395: }
396:
397: if (prefix == null || "".equals(prefix)) {
398: finallySetFaultCode(faultCode);
399: } else {
400: finallySetFaultCode(prefix + ":" + faultCode);
401: }
402: }
403:
404: private boolean standardFaultCode(String faultCode) {
405: if (faultCode.equals("VersionMismatch")
406: || faultCode.equals("MustUnderstand")
407: || faultCode.equals("Client")
408: || faultCode.equals("Server")) {
409: return true;
410: }
411: if (faultCode.startsWith("VersionMismatch.")
412: || faultCode.startsWith("MustUnderstand.")
413: || faultCode.startsWith("Client.")
414: || faultCode.startsWith("Server.")) {
415: return true;
416: }
417: return false;
418: }
419: }
|