001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.saaj;
021:
022: import org.apache.axiom.om.OMNamespace;
023: import org.apache.axiom.om.impl.OMNamespaceImpl;
024: import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
025: import org.apache.axiom.om.impl.dom.ElementImpl;
026: import org.apache.axiom.om.impl.dom.NodeImpl;
027: import org.apache.axiom.soap.SOAP11Constants;
028: import org.apache.axiom.soap.SOAP12Constants;
029: import org.apache.axiom.soap.SOAPFactory;
030: import org.apache.axiom.soap.SOAPFaultCode;
031: import org.apache.axiom.soap.SOAPFaultDetail;
032: import org.apache.axiom.soap.SOAPFaultNode;
033: import org.apache.axiom.soap.SOAPFaultReason;
034: import org.apache.axiom.soap.SOAPFaultRole;
035: import org.apache.axiom.soap.SOAPFaultSubCode;
036: import org.apache.axiom.soap.SOAPFaultText;
037: import org.apache.axiom.soap.SOAPFaultValue;
038: import org.apache.axiom.soap.impl.dom.SOAPFaultValueImpl;
039: import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
040: import org.apache.axiom.soap.impl.dom.soap11.SOAP11FaultDetailImpl;
041: import org.apache.axiom.soap.impl.dom.soap11.SOAP11FaultReasonImpl;
042: import org.apache.axiom.soap.impl.dom.soap11.SOAP11FaultRoleImpl;
043: import org.apache.axiom.soap.impl.dom.soap12.SOAP12Factory;
044: import org.apache.axiom.soap.impl.dom.soap12.SOAP12FaultDetailImpl;
045: import org.apache.axiom.soap.impl.dom.soap12.SOAP12FaultRoleImpl;
046: import org.apache.axiom.soap.impl.dom.soap12.SOAP12FaultTextImpl;
047: import org.apache.axiom.soap.impl.dom.soap12.SOAP12FaultValueImpl;
048:
049: import javax.xml.namespace.QName;
050: import javax.xml.soap.Detail;
051: import javax.xml.soap.Name;
052: import javax.xml.soap.Node;
053: import javax.xml.soap.SOAPConstants;
054: import javax.xml.soap.SOAPException;
055: import javax.xml.soap.SOAPFault;
056: import javax.xml.soap.SOAPFaultElement;
057: import java.util.ArrayList;
058: import java.util.Collection;
059: import java.util.Iterator;
060: import java.util.List;
061: import java.util.Locale;
062:
063: public class SOAPFaultImpl extends SOAPBodyElementImpl implements
064: SOAPFault {
065:
066: protected org.apache.axiom.soap.SOAPFault fault;
067: private boolean isDetailAdded;
068: private Locale faultReasonLocale;
069: private boolean defaultsSet;
070:
071: /** @param fault */
072: public SOAPFaultImpl(org.apache.axiom.soap.SOAPFault fault) {
073: super ((ElementImpl) fault);
074: this .fault = fault;
075: }
076:
077: void setDefaults() throws SOAPException {
078: if (this .element.getOMFactory() instanceof SOAP11Factory) {
079: setFaultCode(SOAP11Constants.QNAME_SENDER_FAULTCODE);
080: } else {
081: setFaultCode(SOAP12Constants.QNAME_SENDER_FAULTCODE);
082: }
083: setFaultString("Fault string, and possibly fault code, not set");
084: defaultsSet = true;
085: }
086:
087: void removeDefaults() {
088: if (defaultsSet) {
089: SOAPFaultReason reason = this .fault.getReason();
090: if (reason != null) {
091: reason.detach();
092: }
093: defaultsSet = false;
094: }
095: }
096:
097: /**
098: * Sets this <CODE>SOAPFault</CODE> object with the given fault code.
099: * <p/>
100: * Fault codes, which given information about the fault, are defined in the SOAP 1.1
101: * specification. This element is mandatory in SOAP 1.1. Because the fault code is required to
102: * be a QName it is preferable to use the setFaultCode(Name)form of this method.
103: *
104: * @param faultCode - a String giving the fault code to be set. It must be of the form
105: * "prefix:localName" where the prefix has been defined in a namespace
106: * declaration.
107: * @throws SOAPException - if there was an error in adding the faultCode to the underlying XML
108: * tree.
109: * @see setFaultCode(Name), getFaultCode(),SOAPElement.addNamespaceDeclaration(String, String)
110: */
111: public void setFaultCode(String faultCode) throws SOAPException {
112: org.apache.axiom.soap.SOAPFactory soapFactory = null;
113: SOAPFaultCode soapFaultCode = null;
114:
115: //It must be of the form "prefix:localName" where the prefix has been defined in a
116: //namespace declaration.
117: if (faultCode.indexOf(":") == -1) {
118: throw new SOAPException(
119: "faultCode must be of the form prefix:localName");
120: }
121: // else{
122: // String prefix,localName ="";
123: // prefix = faultCode.substring(0, faultCode.indexOf(":"));
124: // localName = faultCode.substring(faultCode.indexOf(":")+1);
125: // }
126:
127: if (this .element.getOMFactory() instanceof SOAP11Factory) {
128: soapFactory = (SOAP11Factory) this .element.getOMFactory();
129: soapFaultCode = soapFactory.createSOAPFaultCode(fault);
130: soapFaultCode.setText(faultCode);
131: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
132: soapFactory = (SOAP12Factory) this .element.getOMFactory();
133: soapFaultCode = soapFactory.createSOAPFaultCode(fault);
134: SOAPFaultValue soapFaultValue = soapFactory
135: .createSOAPFaultValue(soapFaultCode);
136: soapFaultCode.setValue(soapFaultValue);
137: soapFaultValue.setText(faultCode);
138: }
139:
140: this .fault.setCode(soapFaultCode);
141: }
142:
143: /**
144: * Gets the fault code for this <CODE>SOAPFault</CODE> object.
145: *
146: * @return a <CODE>String</CODE> with the fault code
147: * @see #setFaultCode(String) setFaultCode(java.lang.String)
148: */
149: public String getFaultCode() {
150: if (fault != null && fault.getCode() != null) {
151: if (this .element.getOMFactory() instanceof SOAP11Factory) {
152: return fault.getCode().getText();
153: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
154: return fault.getCode().getValue().getText();
155: } else {
156: return null;
157: }
158: } else {
159: return null;
160: }
161: }
162:
163: /**
164: * Sets this SOAPFault object with the given fault actor.The fault actor is the recipient in the
165: * message path who caused the fault to happen. If this SOAPFault supports SOAP 1.2 then this
166: * call is equivalent to setFaultRole(String)
167: *
168: * @param faultActor - a String identifying the actor that caused this SOAPFault object
169: * @throws SOAPException - if there was an error in adding the faultActor to the underlying XML
170: * tree.
171: */
172: public void setFaultActor(String faultActor) throws SOAPException {
173:
174: if (this .element.getOMFactory() instanceof SOAP11Factory) {
175: if (this .fault.getRole() == null) {
176: SOAP11FaultRoleImpl faultRoleImpl = new SOAP11FaultRoleImpl(
177: this .fault, (SOAPFactory) this .element
178: .getOMFactory());
179:
180: faultRoleImpl.setRoleValue(faultActor);
181: this .fault.setRole(faultRoleImpl);
182: } else {
183: SOAPFaultRole role = this .fault.getRole();
184: role.setRoleValue(faultActor);
185: }
186: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
187: if (this .fault.getRole() == null) {
188: SOAP12FaultRoleImpl faultRoleImpl = new SOAP12FaultRoleImpl(
189: this .fault, (SOAPFactory) this .element
190: .getOMFactory());
191:
192: faultRoleImpl.setRoleValue(faultActor);
193: this .fault.setRole(faultRoleImpl);
194: } else {
195: SOAPFaultRole role = this .fault.getRole();
196: role.setRoleValue(faultActor);
197: }
198: }
199: }
200:
201: /* (non-Javadoc)
202: * @see javax.xml.soap.SOAPFault#getFaultActor()
203: */
204: public String getFaultActor() {
205: if (this .fault.getRole() != null) {
206: return this .fault.getRole().getRoleValue();
207: }
208: return null;
209: }
210:
211: /**
212: * Sets the fault string for this <CODE>SOAPFault</CODE> object to the given string.
213: *
214: * @param faultString a <CODE>String</CODE> giving an explanation of the fault
215: * @throws SOAPException if there was an error in adding the <CODE>faultString</CODE> to the
216: * underlying XML tree.
217: * @see #getFaultString() getFaultString()
218: */
219: public void setFaultString(String faultString) throws SOAPException {
220: if (this .element.getOMFactory() instanceof SOAP11Factory) {
221: setFaultString(faultString, null);
222: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
223: setFaultString(faultString, Locale.getDefault());
224: }
225: }
226:
227: /* (non-Javadoc)
228: * @see javax.xml.soap.SOAPFault#getFaultString()
229: */
230: public String getFaultString() {
231:
232: if (this .fault.getNamespace().getNamespaceURI().equals(
233: SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
234: return this .fault.getReason().getText();
235: } else {
236: if (this .fault.getReason() != null
237: && this .fault.getReason().getFirstSOAPText() != null) {
238: return this .fault.getReason().getFirstSOAPText()
239: .getText();
240: }
241: }
242: return null;
243: }
244:
245: /* (non-Javadoc)
246: * @see javax.xml.soap.SOAPFault#getDetail()
247: */
248: public Detail getDetail() {
249: return (Detail) toSAAJNode((org.w3c.dom.Node) fault.getDetail());
250: }
251:
252: /**
253: * Sets this SOAPFault object with the given fault code.Fault codes, which give information
254: * about the fault, are defined in the SOAP 1.1 specification. A fault code is mandatory and
255: * must be of type QName. This method provides a convenient way to set a fault code. For
256: * example,
257: * <p/>
258: * SOAPEnvelope se = ...; // Create a qualified name in the SOAP namespace with a localName //
259: * of Client. Note that prefix parameter is optional and is null // here which causes the
260: * implementation to use an appropriate prefix. Name qname = se.createName(Client,
261: * null,SOAPConstants.URI_NS_SOAP_ENVELOPE); SOAPFault fault = ...; fault.setFaultCode(qname);
262: * <p/>
263: * It is preferable to use this method over setFaultCode(String).
264: *
265: * @param faultCodeQName - a Name object giving the fault code to be set. It must be namespace
266: * qualified.
267: * @throws SOAPException - if there was an error in adding the faultcode element to the
268: * underlying XML tree.
269: */
270: public void setFaultCode(Name faultCodeName) throws SOAPException {
271: if (faultCodeName.getURI() == null
272: || faultCodeName.getURI().trim().length() == 0) {
273: throw new SOAPException(
274: "faultCodeQName must be namespace qualified.");
275: }
276: QName faultCodeQName = new QName(faultCodeName.getURI(),
277: faultCodeName.getLocalName(), faultCodeName.getPrefix());
278: setFaultCode(faultCodeQName);
279: }
280:
281: /* (non-Javadoc)
282: * @see javax.xml.soap.SOAPFault#addDetail()
283: */
284: public Detail addDetail() throws SOAPException {
285: if (isDetailAdded) {
286: throw new SOAPException(
287: "This SOAPFault already contains a Detail element. "
288: + "Please remove the existing Detail element before "
289: + "calling addDetail()");
290: }
291:
292: SOAPFaultDetail omDetail;
293: SOAPFactory factory = (SOAPFactory) this .element.getOMFactory();
294: if (factory instanceof SOAP11Factory) {
295: omDetail = new SOAP11FaultDetailImpl(this .fault, factory);
296: } else {
297: omDetail = new SOAP12FaultDetailImpl(this .fault, factory);
298: }
299: Detail saajDetail = new DetailImpl(omDetail);
300: ((NodeImpl) fault.getDetail()).setUserData(SAAJ_NODE,
301: saajDetail, null);
302: isDetailAdded = true;
303: return saajDetail;
304: }
305:
306: /* (non-Javadoc)
307: * @see javax.xml.soap.SOAPFault#getFaultCodeAsName()
308: */
309: public Name getFaultCodeAsName() {
310: return new PrefixedQName(getFaultCodeAsQName());
311: }
312:
313: /**
314: * Sets the fault string for this SOAPFault object to the given string. If this SOAPFault is
315: * part of a message that supports SOAP 1.2 then this call is equivalent to:
316: * addFaultReasonText(faultString, Locale.getDefault());
317: *
318: * @param faultString - a String giving an explanation of the fault
319: * @throws SOAPException - if there was an error in adding the faultString to the underlying XML
320: * tree.
321: * @see getFaultString()
322: */
323:
324: public void setFaultString(String faultString, Locale locale)
325: throws SOAPException {
326: if (this .fault.getReason() != null) {
327: SOAPFaultReason reason = this .fault.getReason();
328: if (this .element.getOMFactory() instanceof SOAP11Factory) {
329: reason.setText(faultString);
330: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
331: addFaultReasonText(faultString, locale);
332: }
333: } else {
334: if (this .element.getOMFactory() instanceof SOAP11Factory) {
335: SOAPFaultReason reason = new SOAP11FaultReasonImpl(
336: this .fault, (SOAPFactory) this .element
337: .getOMFactory());
338: reason.setText(faultString);
339: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
340: addFaultReasonText(faultString, locale);
341: }
342: }
343: this .faultReasonLocale = locale;
344: }
345:
346: /**
347: * Gets the locale of the fault string for this SOAPFault object. If this SOAPFault is part of a
348: * message that supports SOAP 1.2 then this call is equivalent to:
349: * <p/>
350: * Locale locale = null; try { locale = (Locale) getFaultReasonLocales().next(); } catch
351: * (SOAPException e) {} return locale;
352: *
353: * @return a Locale object indicating the native language of the fault string or null if no
354: * locale was specified
355: * @see setFaultString(String, Locale)
356: * @since SAAJ 1.2
357: */
358: public Locale getFaultStringLocale() {
359: if (this .element.getOMFactory() instanceof SOAP11Factory) {
360: return this .faultReasonLocale;
361: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
362: Locale locale = null;
363: try {
364: if (getFaultReasonLocales().hasNext()) {
365: locale = (Locale) getFaultReasonLocales().next();
366: }
367: } catch (SOAPException e) {
368: e.printStackTrace();
369: }
370: return locale;
371: } else {
372: return null;
373: }
374:
375: }
376:
377: /**
378: * Appends or replaces a Reason Text item containing the specified text message and an xml:lang
379: * derived from locale. If a Reason Text item with this xml:lang already exists its text value
380: * will be replaced with text. The locale parameter should not be null Code sample: SOAPFault
381: * fault = ...; fault.addFaultReasonText(Version Mismatch, Locale.ENGLISH);
382: *
383: * @param text - reason message string locale - Locale object representing the locale of the
384: * message
385: * @throws SOAPException - if there was an error in adding the Reason text or the locale passed
386: * was null. java.lang.UnsupportedOperationException - if this message
387: * does not support the SOAP 1.2 concept of Fault Reason.
388: */
389: public void addFaultReasonText(String text, Locale locale)
390: throws SOAPException {
391: if (locale == null) {
392: throw new SOAPException("Received null for locale");
393: }
394: if (this .element.getOMFactory() instanceof SOAP11Factory) {
395: throw new UnsupportedOperationException(
396: "Not supported in SOAP 1.1");
397: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
398: removeDefaults();
399:
400: String existingReasonText = getFaultReasonText(locale);
401: if (existingReasonText == null) {
402: org.apache.axiom.soap.SOAPFactory soapFactory = null;
403: soapFactory = (SOAP12Factory) this .element
404: .getOMFactory();
405: if (this .fault.getReason() == null) {
406: SOAPFaultReason soapFaultReason = soapFactory
407: .createSOAPFaultReason(this .fault);
408: this .fault.setReason(soapFaultReason);
409: }
410: SOAPFaultText soapFaultText = soapFactory
411: .createSOAPFaultText(this .fault.getReason());
412: soapFaultText.setText(text);
413: soapFaultText.setLang(locale.toString());
414: } else {
415: //update the text
416: Iterator soapTextsItr = this .fault.getReason()
417: .getAllSoapTexts().iterator();
418: while (soapTextsItr.hasNext()) {
419: SOAPFaultText soapFaultText = (SOAPFaultText) soapTextsItr
420: .next();
421: if (soapFaultText.getLang().equals(
422: locale.toString())) {
423: soapFaultText.setText(text);
424: }
425: }
426:
427: }
428:
429: }
430:
431: }
432:
433: /**
434: * Adds a Subcode to the end of the sequence of Subcodes contained by this SOAPFault. Subcodes,
435: * which were introduced in SOAP 1.2, are represented by a recursive sequence of subelements
436: * rooted in the mandatory Code subelement of a SOAP Fault.
437: *
438: * @param subcode - a QName containing the Value of the Subcode.
439: * @throws SOAPException - if there was an error in setting the Subcode java.lang.UnsupportedOperationException
440: * - if this message does not support the SOAP 1.2 concept of Subcode.
441: */
442:
443: public void appendFaultSubcode(QName subcode) throws SOAPException {
444: org.apache.axiom.soap.SOAPFactory soapFactory = null;
445: SOAPFaultSubCode soapFaultSubCode = null;
446:
447: if (subcode.getNamespaceURI() == null
448: || subcode.getNamespaceURI().trim().length() == 0) {
449: throw new SOAPException("Unqualified QName object : "
450: + subcode);
451: }
452: if (this .element.getOMFactory() instanceof SOAP11Factory) {
453: throw new UnsupportedOperationException();
454: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
455: soapFactory = DOOMAbstractFactory.getSOAP12Factory();
456: }
457:
458: if (this .fault.getCode() == null) {
459: soapFactory.createSOAPFaultCode(this .fault);
460: //if SOAPFault is null, there cannot be a subcode.
461: //Hence should create one
462: soapFaultSubCode = soapFactory
463: .createSOAPFaultSubCode(this .fault.getCode());
464: } else if (this .fault.getCode().getSubCode() != null) {
465: //find the last subcode.parent of the new subcode should be the this last subcode
466: soapFaultSubCode = soapFactory
467: .createSOAPFaultSubCode(getLastSubCode(this .fault
468: .getCode().getSubCode()));
469: } else {
470: //FaultCode is there, but no FaultSubCode
471: soapFaultSubCode = soapFactory
472: .createSOAPFaultSubCode(this .fault.getCode());
473: }
474:
475: if (soapFaultSubCode != null) {
476: SOAPFaultValueImpl soapFaultValueimpl = new SOAP12FaultValueImpl(
477: soapFaultSubCode, soapFactory);
478: soapFaultValueimpl.setText(subcode.getPrefix() + ":"
479: + subcode.getLocalPart());
480: soapFaultValueimpl.declareNamespace(subcode
481: .getNamespaceURI(), subcode.getPrefix());
482: }
483: }
484:
485: private SOAPFaultSubCode getLastSubCode(
486: SOAPFaultSubCode firstSubCodeElement) {
487: SOAPFaultSubCode soapFaultSubCode = firstSubCodeElement
488: .getSubCode();
489: if (soapFaultSubCode != null) {
490: return getLastSubCode(soapFaultSubCode);
491: }
492: return firstSubCodeElement;
493: }
494:
495: /**
496: * Gets the fault code for this SOAPFault object as a <CODE>QName</CODE> object.
497: * <p/>
498: */
499: public QName getFaultCodeAsQName() {
500: SOAPFaultCode soapFaultCode = this .fault.getCode();
501: if (soapFaultCode != null) {
502: if (this .element.getOMFactory() instanceof SOAP11Factory) {
503: return soapFaultCode.getTextAsQName();
504: } else {
505: return soapFaultCode.getValue().getTextAsQName();
506: }
507: }
508: return null;
509: }
510:
511: /**
512: * Returns the optional Node element value for this SOAPFault object. The Node element is
513: * optional in SOAP 1.2.
514: *
515: * @return Content of the env:Fault/env:Node element as a String or null if none
516: * @throws UnsupportedOperationException
517: * - if this message does not support the SOAP 1.2 concept of Fault Node.
518: */
519: public String getFaultNode() {
520: if (this .element.getOMFactory() instanceof SOAP11Factory) {
521: throw new UnsupportedOperationException(
522: "Message does not support the "
523: + "SOAP 1.2 concept of Fault Node");
524: } else {
525: if (fault != null && fault.getNode() != null
526: && fault.getNode().getText() != null) {
527: return fault.getNode().getText();
528: }
529: }
530: return null;
531:
532: }
533:
534: /**
535: * Returns an Iterator over a distinct sequence of Locales for which there are associated Reason
536: * Text items. Any of these Locales can be used in a call to getFaultReasonText in order to
537: * obtain a localized version of the Reason Text string.
538: *
539: * @return an Iterator over a sequence of Locale objects for which there are associated Reason
540: * Text items.
541: * @throws SOAPException - if there was an error in retrieving the fault Reason locales.
542: * java.lang.UnsupportedOperationException - if this message does not
543: * support the SOAP 1.2 concept of Fault Reason.
544: * @since SAAJ 1.3
545: */
546: public Iterator getFaultReasonLocales() throws SOAPException {
547: if (this .element.getOMFactory() instanceof SOAP11Factory) {
548: throw new UnsupportedOperationException(
549: "Message does not support the "
550: + "SOAP 1.2 concept of Fault Reason");
551: } else {
552: ArrayList faultReasonLocales = new ArrayList();
553: List soapTextList = this .fault.getReason()
554: .getAllSoapTexts();
555: if (soapTextList != null) {
556: Iterator faultReasons = soapTextList.iterator();
557: while (faultReasons.hasNext()) {
558: SOAPFaultText soapFaultText = (SOAPFaultText) faultReasons
559: .next();
560: String lang = soapFaultText.getLang();
561: if (lang == null) {
562: faultReasonLocales.add(Locale.getDefault());
563: } else {
564: if (lang.indexOf("_") != -1) {
565: String language = lang.substring(0, lang
566: .indexOf("_"));
567: String country = lang.substring(lang
568: .indexOf("_") + 1);
569: faultReasonLocales.add(new Locale(language,
570: country));
571: } else {
572: faultReasonLocales.add(new Locale(lang));
573: }
574: }
575: }
576: }
577: return faultReasonLocales.iterator();
578: }
579: }
580:
581: /**
582: * Returns the Reason Text associated with the given Locale. If more than one such Reason Text
583: * exists the first matching Text is returned
584: *
585: * @param locale - the Locale for which a localized Reason Text is desired
586: * @return the Reason Text associated with locale
587: * @throws SOAPException - if there was an error in retrieving the fault Reason text for the
588: * specified locale. java.lang.UnsupportedOperationException - if this
589: * message does not support the SOAP 1.2 concept of Fault Reason.
590: * @since SAAJ 1.3
591: */
592: public String getFaultReasonText(Locale locale)
593: throws SOAPException {
594: if (this .element.getOMFactory() instanceof SOAP11Factory) {
595: throw new UnsupportedOperationException(
596: "Message does not support the "
597: + "SOAP 1.2 concept of Fault Reason");
598: } else {
599: Iterator soapTextsItr = null;
600: SOAPFaultReason soapFaultReason = this .fault.getReason();
601: if (soapFaultReason != null) {
602: List soapTexts = soapFaultReason.getAllSoapTexts();
603: if (soapTexts != null) {
604: soapTextsItr = soapTexts.iterator();
605: while (soapTextsItr.hasNext()) {
606: SOAPFaultText soapFaultText = (SOAPFaultText) soapTextsItr
607: .next();
608: if (soapFaultText.getLang().equals(
609: locale.toString())) {
610: return soapFaultText.getText();
611: }
612: }
613: }
614: }
615: }
616: return null;
617: }
618:
619: /**
620: * Returns an Iterator over a sequence of String objects containing all of the Reason Text items
621: * for this SOAPFault.
622: *
623: * @throws SOAPException if there is an error in retrieving texts for Reason objects
624: * java.lang.UnsupportedOperationException - if this message does not
625: * support the SOAP 1.2 concept of Fault Reason.
626: */
627:
628: public Iterator getFaultReasonTexts() throws SOAPException {
629: if (this .element.getOMFactory() instanceof SOAP11Factory) {
630: throw new UnsupportedOperationException();
631: }
632:
633: Iterator soapTextsItr = this .fault.getReason()
634: .getAllSoapTexts().iterator();
635: ArrayList reasonTexts = new ArrayList();
636: while (soapTextsItr.hasNext()) {
637: SOAPFaultText soapFaultText = (SOAPFaultText) soapTextsItr
638: .next();
639: reasonTexts.add(soapFaultText.getText());
640: }
641: return reasonTexts.iterator();
642: }
643:
644: /**
645: * Returns the optional Role element value for this SOAPFault object. The Role element is
646: * optional in SOAP 1.2.
647: *
648: * @return Content of the env:Fault/env:Role element as a String or null if none
649: * @throws UnsupportedOperationException
650: * - if this message does not support the SOAP 1.2 concept of Fault Role.
651: * @since SAAJ 1.3
652: */
653: public String getFaultRole() {
654: if (this .element.getOMFactory() instanceof SOAP11Factory) {
655: throw new UnsupportedOperationException(
656: "Message does not support the "
657: + "SOAP 1.2 concept of Fault Reason");
658: } else {
659: if (this .fault.getRole() != null) {
660: return this .fault.getRole().getText();
661: } else {
662: return null;
663: }
664: }
665: }
666:
667: /**
668: * Gets the Subcodes for this SOAPFault as an iterator over QNames.
669: *
670: * @return an Iterator that accesses a sequence of QNames. This Iterator should not support the
671: * optional remove method. The order in which the Subcodes are returned reflects the
672: * hierarchy of Subcodes present in the fault from top to bottom.
673: * @throws UnsupportedOperationException
674: * - if this message does not support the SOAP 1.2 concept of Subcode.
675: */
676: public Iterator getFaultSubcodes() {
677: if (this .element.getOMFactory() instanceof SOAP11Factory) {
678: throw new UnsupportedOperationException();
679: }
680: ArrayList faultSubcodes = new ArrayList();
681: SOAPFaultSubCode subCodeElement = this .fault.getCode()
682: .getSubCode();
683: while (subCodeElement != null) {
684: QName qname = subCodeElement.getValue().getTextAsQName();
685: faultSubcodes.add(qname);
686: subCodeElement = subCodeElement.getSubCode();
687: }
688: return faultSubcodes.iterator();
689: }
690:
691: /** Returns true if this SOAPFault has a Detail subelement and false otherwise. */
692: public boolean hasDetail() {
693: if (this .fault.getDetail() != null) {
694: return true;
695: } else {
696: return false;
697: }
698:
699: }
700:
701: /**
702: * Removes any Subcodes that may be contained by this SOAPFault. Subsequent calls to
703: * getFaultSubcodes will return an empty iterator until a call to appendFaultSubcode is made.
704: *
705: * @throws UnsupportedOperationException
706: * - if this message does not support the SOAP 1.2 concept of Subcode.
707: */
708: public void removeAllFaultSubcodes() {
709: if (factory instanceof SOAP11Factory) {
710: throw new UnsupportedOperationException();
711: } else {
712: fault.getCode().getSubCode().detach();
713: }
714: }
715:
716: /**
717: * Sets this SOAPFault object with the given fault code. It is preferable to use this method
718: * over setFaultCode(Name)
719: *
720: * @param faultCodeQName - a QName object giving the fault code to be set. It must be namespace
721: * qualified.
722: * @throws SOAPException - if there was an error in adding the faultcode element to the
723: * underlying XML tree.
724: * @see getFaultCodeAsQName(), setFaultCode(Name), getFaultCodeAsQName()
725: * @since SAAJ 1.3
726: */
727: public void setFaultCode(QName qname) throws SOAPException {
728: if (qname.getNamespaceURI() == null
729: || qname.getNamespaceURI().trim().length() == 0) {
730: throw new SOAPException("Unqualified QName object : "
731: + qname);
732: }
733:
734: org.apache.axiom.soap.SOAPFactory soapFactory = null;
735: if (this .element.getOMFactory() instanceof SOAP11Factory) {
736: soapFactory = (SOAPFactory) this .element.getOMFactory();
737: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
738: if (!(qname.getNamespaceURI()
739: .equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE))) {
740: throw new SOAPException("Incorrect URI"
741: + qname.getNamespaceURI());
742: }
743: soapFactory = (SOAPFactory) this .element.getOMFactory();
744: } else {
745: throw new SOAPException("Invalid SOAP version");
746: }
747: SOAPFaultCode soapFaultCode = soapFactory
748: .createSOAPFaultCode(this .fault);
749:
750: String prefix = ((qname.getPrefix() != null) && !qname
751: .getPrefix().equals("")) ? qname.getPrefix()
752: : this .fault.getQName().getPrefix();
753:
754: if (this .element.getOMFactory() instanceof SOAP11Factory) {
755: soapFaultCode.setText(prefix + ":" + qname.getLocalPart());
756: OMNamespace omNamespace = new OMNamespaceImpl(qname
757: .getNamespaceURI(), qname.getPrefix());
758: soapFaultCode.declareNamespace(omNamespace);
759: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
760: SOAPFaultValue soapFaultValue = soapFactory
761: .createSOAPFaultValue(soapFaultCode);
762: // don't just use the default prefix, use the passed one or the parent's
763: soapFaultValue.setText(prefix + ":" + qname.getLocalPart());
764: OMNamespace omNamespace = new OMNamespaceImpl(qname
765: .getNamespaceURI(), qname.getPrefix());
766: soapFaultValue.declareNamespace(omNamespace);
767: soapFaultCode.setValue(soapFaultValue);
768: }
769:
770: this .fault.setCode(soapFaultCode);
771: }
772:
773: /**
774: * Creates or replaces any existing Node element value for this SOAPFault object. The Node
775: * element is optional in SOAP 1.2.
776: *
777: * @throws SOAPException - if there was an error in setting the Node for this SOAPFault object.
778: * java.lang.UnsupportedOperationException - if this message does not
779: * support the SOAP 1.2 concept of Fault Node.
780: * @since SAAJ 1.3
781: */
782:
783: public void setFaultNode(String s) throws SOAPException {
784: org.apache.axiom.soap.SOAPFactory soapFactory = null;
785: if (this .element.getOMFactory() instanceof SOAP11Factory) {
786: throw new UnsupportedOperationException(
787: "message does not support "
788: + "the SOAP 1.2 concept of Fault Node");
789: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
790: soapFactory = DOOMAbstractFactory.getSOAP12Factory();
791: }
792: SOAPFaultNode soapFaultNode = soapFactory
793: .createSOAPFaultNode(this .fault);
794: soapFaultNode.setText(s);
795: this .fault.setNode(soapFaultNode);
796: }
797:
798: /**
799: * Creates or replaces any existing Role element value for this SOAPFault object. The Role
800: * element is optional in SOAP 1.2.
801: *
802: * @param uri - the URI of the Role
803: * @throws SOAPException - if there was an error in setting the Role for this SOAPFault object
804: * java.lang.UnsupportedOperationException - if this message does not
805: * support the SOAP 1.2 concept of Fault Role.
806: */
807: public void setFaultRole(String uri) throws SOAPException {
808: org.apache.axiom.soap.SOAPFactory soapFactory = null;
809: if (this .element.getOMFactory() instanceof SOAP11Factory) {
810: throw new UnsupportedOperationException(
811: "message does not support the "
812: + "SOAP 1.2 concept of Fault Role");
813: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
814: soapFactory = DOOMAbstractFactory.getSOAP12Factory();
815: }
816: SOAPFaultRole soapFaultRole = soapFactory
817: .createSOAPFaultRole(this .fault);
818: soapFaultRole.setRoleValue(uri);
819: this .fault.setRole(soapFaultRole);
820: }
821:
822: public Iterator getChildElements(Name name) {
823: QName qName = new QName(name.getURI(), name.getLocalName());
824: return getChildren(element.getChildrenWithName(qName));
825: }
826:
827: public Iterator getChildElements() {
828: return getChildren(element.getChildren());
829: }
830:
831: private Iterator getChildren(Iterator childIter) {
832: Collection childElements = new ArrayList();
833: while (childIter.hasNext()) {
834: org.w3c.dom.Node domNode = (org.w3c.dom.Node) childIter
835: .next();
836: Node saajNode = toSAAJNode(domNode);
837: if (!(saajNode instanceof SOAPFaultElement)) {
838: // silently replace node, as per saaj 1.2 spec
839: SOAPFaultElement bodyEle = new SOAPFaultElementImpl(
840: (ElementImpl) domNode);
841: ((NodeImpl) domNode).setUserData(SAAJ_NODE, bodyEle,
842: null);
843: childElements.add(bodyEle);
844: } else {
845: childElements.add(saajNode);
846: }
847: }
848: return childElements.iterator();
849: }
850:
851: }
|