001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: SOAPFault.java,v 1.2 2008/06/25 05:47:23 qcheng Exp $ 026 * 027 */ 028 029 030package com.sun.identity.liberty.ws.soapbinding; 031 032import java.io.ByteArrayInputStream; 033 034import java.util.ArrayList; 035import java.util.Iterator; 036import java.util.List; 037 038import javax.xml.bind.JAXBException; 039 040import javax.xml.namespace.QName; 041 042import javax.xml.soap.SOAPMessage; 043import javax.xml.soap.MimeHeaders; 044 045import org.w3c.dom.Document; 046import org.w3c.dom.Element; 047import org.w3c.dom.Node; 048import org.w3c.dom.NodeList; 049 050import com.sun.identity.shared.xml.XMLUtils; 051 052/** 053 * The <code>SOAPFault</code> class represents a SOAP Fault element. 054 * 055 * @supported.all.api 056 */ 057 058public class SOAPFault { 059 060 private QName faultcode = null; 061 private String faultstring = null; 062 private String faultactor = null; 063 private SOAPFaultDetail detail = null; 064 065 066 /** 067 * Constructor. 068 * 069 * @param faultcode value of <code>faultcode</code> element 070 * @param faultstring value of <code>faultstring</code> element 071 */ 072 public SOAPFault(QName faultcode, String faultstring) { 073 this(faultcode, faultstring, null, null); 074 } 075 076 /** 077 * Constructor. 078 * 079 * @param faultcode value of <code>faultcode</code> element. 080 * @param faultstring value of <code>faultstring</code> element. 081 * @param faultactor value of <code>faultactor</code> element. 082 * @param detail a SOAP Fault Detail. 083 */ 084 public SOAPFault(QName faultcode, String faultstring, 085 String faultactor,SOAPFaultDetail detail) { 086 this.faultcode = faultcode; 087 this.faultstring = faultstring; 088 this.faultactor = faultactor; 089 this.detail = detail; 090 } 091 092 /** 093 * This constructor takes SOAP Fault element. 094 * 095 * @param faultElement a SOAP Fault element 096 * @throws SOAPBindingException if an error occurs while parsing 097 * SOAP Fault element 098 */ 099 SOAPFault(Element faultElement) throws SOAPBindingException { 100 101 NodeList nl = faultElement.getChildNodes(); 102 int length = nl.getLength(); 103 104 boolean foundInvalidChild = false; 105 Element detailE = null; 106 int numElements = 0; 107 for (int i = 0; i < length; i++) { 108 Node child = nl.item(i); 109 if (child.getNodeType() == Node.ELEMENT_NODE) { 110 Element element = (Element)child; 111 String localName = element.getLocalName(); 112 String namespaceURI = element.getNamespaceURI(); 113 numElements++; 114 if (numElements == 1) { 115 if (SOAPBindingConstants.TAG_FAULT_CODE.equals(localName)&& 116 namespaceURI == null) { 117 118 String value = XMLUtils.getElementValue(element); 119 if (value == null || value.length() ==0) { 120 String msg = 121 Utils.bundle.getString("missingFaultCode"); 122 Utils.debug.error("SOAPFaultException: " + msg); 123 throw new SOAPBindingException(msg); 124 } 125 faultcode = Utils.convertStringToQName(value, element); 126 } else { 127 String msg =Utils.bundle.getString("missingFaultCode"); 128 Utils.debug.error("SOAPFaultException: " + msg); 129 throw new SOAPBindingException(msg); 130 } 131 } else if (numElements == 2) { 132 if (SOAPBindingConstants.TAG_FAULT_STRING.equals(localName) 133 && namespaceURI == null) { 134 faultstring = XMLUtils.getElementValue(element); 135 } else { 136 String msg = 137 Utils.bundle.getString("missingFaultString"); 138 Utils.debug.error("SOAPFaultException: " + msg); 139 throw new SOAPBindingException(msg); 140 } 141 } else if (numElements == 3) { 142 if (SOAPBindingConstants.TAG_FAULT_ACTOR.equals(localName) 143 && namespaceURI == null) { 144 faultactor = XMLUtils.getElementValue(element); 145 } else if (SOAPBindingConstants.TAG_DETAIL 146 .equals(localName) && 147 namespaceURI == null) { 148 detailE = element; 149 } else { 150 String msg = Utils.bundle.getString("invalidChild"); 151 Utils.debug.error("SOAPFaultException: " + msg); 152 throw new SOAPBindingException(msg); 153 } 154 } else if (numElements == 4) { 155 if (detailE == null && 156 SOAPBindingConstants.TAG_DETAIL.equals(localName) && 157 namespaceURI == null) { 158 detailE = element; 159 } else { 160 String msg = Utils.bundle.getString("invalidChild"); 161 Utils.debug.error("SOAPFaultException: " + msg); 162 throw new SOAPBindingException(msg); 163 } 164 } else { 165 String msg = Utils.bundle.getString("invalidChild"); 166 Utils.debug.error("SOAPFaultException: " + msg); 167 throw new SOAPBindingException(msg); 168 } 169 } 170 } 171 172 if (detailE != null) { 173 detail = new SOAPFaultDetail(detailE); 174 } 175 } 176 177 /** 178 * Returns value of <code>faultcode</code> element. 179 * 180 * @return value of <code>faultcode</code> element. 181 */ 182 public QName getFaultCode() { 183 return faultcode; 184 } 185 186 /** 187 * Returns value of <code>faultstring</code> element. 188 * 189 * @return value of <code>faultstring</code> element. 190 */ 191 public String getFaultString() { 192 return faultstring; 193 } 194 195 /** 196 * Returns value of <code>faultactor</code> element. 197 * 198 * @return value of <code>faultactor</code> element. 199 */ 200 public String getFaultActor() { 201 return faultactor; 202 } 203 204 /** 205 * Returns a SOAP Fault Detail. 206 * 207 * @return a SOAP Fault Detail. 208 */ 209 public SOAPFaultDetail getDetail() { 210 return detail; 211 } 212 213 /** 214 * Sets value of <code>faultcode</code> element. 215 * 216 * @param faultcode value of <code>faultcode</code> element 217 */ 218 public void setFaultCode(QName faultcode) { 219 this.faultcode = faultcode; 220 } 221 222 /** 223 * Sets value of <code>faultstring</code> element. 224 * 225 * @param faultstring value of <code>faultstring</code> element. 226 */ 227 public void setFaultString(String faultstring) { 228 this.faultstring = faultstring; 229 } 230 231 /** 232 * Sets value of <code>faultactor</code> element. 233 * 234 * @param faultactor value of <code>faultactor</code> element. 235 */ 236 public void setFaultActor(String faultactor) { 237 this.faultactor = faultactor; 238 } 239 240 /** 241 * Sets a SOAP Fault Detail. 242 * 243 * @param detail a SOAP Fault Detail. 244 */ 245 public void setDetail(SOAPFaultDetail detail) { 246 this.detail = detail; 247 } 248 249 /** 250 * Appends the SOAPFault Header to the SOAP Element. 251 */ 252 void addToParent(Element bodyE) { 253 Document doc = bodyE.getOwnerDocument(); 254 Element faultE = doc.createElementNS(SOAPBindingConstants.NS_SOAP, 255 SOAPBindingConstants.PTAG_FAULT); 256 bodyE.appendChild(faultE); 257 258 Element faultcodeE = 259 doc.createElement(SOAPBindingConstants.TAG_FAULT_CODE); 260 String localPart = faultcode.getLocalPart(); 261 String ns = faultcode.getNamespaceURI(); 262 if (Utils.debug.messageEnabled()) { 263 Utils.debug.message("SOAPFault.addToParent: faultcode ns" + 264 " = " + ns + ", localPart = " + localPart); 265 } 266 if (ns != null && ns.length() > 0) { 267 String prefix; 268 if (ns.equals(SOAPBindingConstants.NS_SOAP)) { 269 prefix = SOAPBindingConstants.PREFIX_SOAP; 270 } else if (ns.equals(SOAPBindingConstants.NS_SOAP_BINDING)) { 271 prefix = SOAPBindingConstants.PREFIX_SOAP_BINDING; 272 } else { 273 prefix = SOAPBindingConstants.DEFAULT_PREFIX_FAULT_CODE_VALUE; 274 faultcodeE.setAttributeNS(SOAPBindingConstants.NS_XML, 275 "xmlns:" + prefix, ns); 276 } 277 faultcodeE.appendChild(doc.createTextNode(prefix +":" +localPart)); 278 } else { 279 faultcodeE.appendChild(doc.createTextNode(localPart)); 280 } 281 faultE.appendChild(faultcodeE); 282 283 Element faultstringE = 284 doc.createElement(SOAPBindingConstants.TAG_FAULT_STRING); 285 faultstringE.appendChild(doc.createTextNode(faultstring)); 286 faultE.appendChild(faultstringE); 287 288 if (faultactor != null) { 289 Element faultactorE = 290 doc.createElement(SOAPBindingConstants.TAG_FAULT_ACTOR); 291 faultactorE.appendChild(doc.createTextNode(faultactor)); 292 faultE.appendChild(faultactorE); 293 } 294 295 if (detail != null) { 296 detail.addToParent(faultE); 297 } 298 } 299}