1 16 17 package org.jboss.axis.message; 18 19 import org.jboss.axis.AxisFault; 20 import org.jboss.axis.Constants; 21 import org.jboss.axis.encoding.DeserializationContext; 22 import org.jboss.axis.encoding.SerializationContext; 23 import org.jboss.axis.soap.SOAPConstants; 24 import org.jboss.axis.utils.Messages; 25 import org.jboss.logging.Logger; 26 import org.w3c.dom.Attr ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.NamedNodeMap ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 import org.xml.sax.Attributes ; 33 34 import javax.xml.namespace.QName ; 35 import javax.xml.rpc.JAXRPCException ; 36 import javax.xml.soap.Name ; 37 import javax.xml.soap.SOAPBodyElement ; 38 import javax.xml.soap.SOAPElement ; 39 import javax.xml.soap.SOAPException ; 40 import javax.xml.soap.SOAPFault ; 41 import java.util.ArrayList ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Locale ; 45 46 51 public class SOAPBodyAxisImpl extends SOAPBodyImpl 52 { 53 54 private static Logger log = Logger.getLogger(SOAPBodyAxisImpl.class.getName()); 55 56 private SOAPConstants soapConstants; 57 58 private boolean disableFormatting; 59 private boolean doSAAJEncodingCompliance; 60 private static ArrayList knownEncodingStyles = new ArrayList (); 61 62 static 63 { 64 knownEncodingStyles.add(Constants.URI_SOAP11_ENC); 65 knownEncodingStyles.add(Constants.URI_SOAP12_ENC); 66 knownEncodingStyles.add(""); 67 knownEncodingStyles.add(Constants.URI_SOAP12_NOENC); 68 } 69 70 SOAPBodyAxisImpl(SOAPEnvelopeAxisImpl env, SOAPConstants soapConsts) 71 { 72 super(soapConsts.getEnvelopeURI(), Constants.ELEM_BODY); 73 soapConstants = soapConsts; 74 try 75 { 76 setParentElement(env); 77 } 78 catch (SOAPException ex) 79 { 80 log.fatal(Messages.getMessage("exception00"), ex); 82 } 83 } 84 85 public SOAPBodyAxisImpl(String namespace, String localPart, String prefix, Attributes attributes, 86 DeserializationContext context, SOAPConstants soapConsts) throws AxisFault 87 { 88 super(namespace, localPart, prefix, attributes, context); 89 soapConstants = soapConsts; 90 } 91 92 public void setParentElement(SOAPElement parent) throws SOAPException 93 { 94 if (parent == null) 95 throw new IllegalArgumentException (Messages.getMessage("nullParent00")); 96 97 try 98 { 99 SOAPEnvelopeAxisImpl env = (SOAPEnvelopeAxisImpl)parent; 100 super.setParentElement(env); 101 setEnvelope(env); 102 } 103 catch (Throwable t) 104 { 105 throw new SOAPException (t); 106 } 107 } 108 109 public void disableFormatting() 110 { 111 this.disableFormatting = true; 112 } 113 114 public void setEncodingStyle(String encodingStyle) throws SOAPException 115 { 116 if (encodingStyle == null) 117 { 118 encodingStyle = ""; 119 } 120 121 if (doSAAJEncodingCompliance) 122 { 123 if (!knownEncodingStyles.contains(encodingStyle)) 125 throw new IllegalArgumentException (Messages.getMessage("badEncodingStyle1", encodingStyle)); 126 } 127 128 super.setEncodingStyle(encodingStyle); 129 } 130 131 protected void outputImpl(SerializationContext context) throws Exception 132 { 133 boolean oldPretty = context.getPretty(); 134 context.setPretty(!disableFormatting); 135 136 if (getChildren().isEmpty()) 137 { 138 } 143 144 context.startElement(new QName (soapConstants.getEnvelopeURI(), 146 Constants.ELEM_BODY), getAttributesEx()); 147 153 for (Iterator it = getChildElements(); it.hasNext();) 154 { 155 Node childNode = (Node )it.next(); 156 if (childNode instanceof SOAPElementAxisImpl) 157 ((SOAPElementAxisImpl)childNode).output(context); 158 else if (childNode instanceof TextImpl) 159 context.writeString(childNode.getNodeValue()); 160 } 161 162 context.outputMultiRefs(); 164 165 context.endElement(); 167 168 context.setPretty(oldPretty); 169 } 170 171 List getBodyElements() throws AxisFault 172 { 173 return getChildren(); 174 } 175 176 void addBodyElement(SOAPBodyElementAxisImpl element) 177 { 178 if (log.isDebugEnabled()) 179 log.debug(Messages.getMessage("addBody00")); 180 try 181 { 182 element.setParentElement(this); 183 } 184 catch (SOAPException ex) 185 { 186 log.fatal(Messages.getMessage("exception00"), ex); 188 } 189 } 190 191 void clearBody() 192 { 193 removeContents(); 194 } 195 196 void removeBodyElement(SOAPBodyElementAxisImpl element) 197 { 198 if (log.isDebugEnabled()) 199 log.debug(Messages.getMessage("removeBody00")); 200 removeChild(element); 201 } 202 203 SOAPBodyElementAxisImpl getBodyByName(String namespace, String localPart) 204 throws AxisFault 205 { 206 return (SOAPBodyElementAxisImpl)findElement(getChildren(), 207 namespace, 208 localPart); 209 } 210 211 protected SOAPElementAxisImpl findElement(List list, String namespace, String localPart) 212 { 213 if (list.isEmpty()) 214 return null; 215 216 QName qname = new QName (namespace, localPart); 217 Iterator it = list.iterator(); 218 while (it.hasNext()) 219 { 220 SOAPElementAxisImpl element = (SOAPElementAxisImpl)it.next(); 221 if (element.getQName().equals(qname)) 222 return element; 223 } 224 225 return null; 226 } 227 228 SOAPBodyElementAxisImpl getFirstBody() throws AxisFault 229 { 230 if (getChildren().isEmpty()) 231 return null; 232 235 return (SOAPBodyElementAxisImpl)getChildren().get(0); 236 } 237 238 240 public SOAPBodyElement addBodyElement(Name name) 241 throws SOAPException 242 { 243 SOAPBodyElementAxisImpl bodyElement = new SOAPBodyElementAxisImpl(name); 244 addBodyElement(bodyElement); 245 return bodyElement; 246 } 247 248 public SOAPFault addFault(Name name, String s, Locale locale) throws SOAPException 249 { 250 AxisFault af = new AxisFault(new QName (name.getURI(), name.getLocalName()), s, "", new Element [0]); 251 SOAPFaultImpl fault = new SOAPFaultImpl(af); 252 addBodyElement(fault); 253 return fault; 254 } 255 256 public SOAPFault addFault(Name name, String s) throws SOAPException 257 { 258 AxisFault af = new AxisFault(new QName (name.getURI(), name.getLocalName()), s, "", new Element [0]); 259 SOAPFaultImpl fault = new SOAPFaultImpl(af); 260 addBodyElement(fault); 261 return fault; 262 } 263 264 public SOAPBodyElement addDocument(Document document) throws SOAPException 265 { 266 return (SOAPBodyElement )importDOMElement(this, document.getDocumentElement()); 267 } 268 269 public SOAPFault addFault() throws SOAPException 270 { 271 272 AxisFault af = new AxisFault(new QName (Constants.NS_URI_AXIS, Constants.FAULT_SERVER_GENERAL), "", "", new Element [0]); 273 SOAPFaultImpl fault = new SOAPFaultImpl(af); 274 addBodyElement(fault); 275 return fault; 276 } 277 278 public SOAPFault getFault() 279 { 280 Iterator it = getChildren().iterator(); 281 while (it.hasNext()) 282 { 283 Object element = it.next(); 284 if (element instanceof SOAPFault ) 285 { 286 return (SOAPFault )element; 287 } 288 } 289 return null; 290 } 291 292 public boolean hasFault() 293 { 294 Iterator it = getChildren().iterator(); 295 while (it.hasNext()) 296 { 297 if (it.next() instanceof SOAPFault ) 298 { 299 return true; 300 } 301 } 302 return false; 303 } 304 305 public void setSAAJEncodingCompliance(boolean comply) 306 { 307 this.doSAAJEncodingCompliance = true; 308 } 309 310 public SOAPElement addChildElement(Name name) throws SOAPException 311 { 312 SOAPBodyElementAxisImpl soapBodyElement = new SOAPBodyElementAxisImpl(name); 313 super.addChildElement(soapBodyElement); 314 return soapBodyElement; 315 } 316 317 319 private SOAPElementAxisImpl importDOMElement(SOAPElementAxisImpl soapParent, Element domElement) 320 { 321 try 322 { 323 NameImpl name; 324 if (domElement.getNamespaceURI() != null) 325 name = new NameImpl(domElement.getLocalName(), domElement.getPrefix(), domElement.getNamespaceURI()); 326 else 327 name = new NameImpl(domElement.getLocalName()); 328 329 SOAPElementAxisImpl soapChild = (SOAPElementAxisImpl)soapParent.addChildElement(name); 330 331 NamedNodeMap attrs = ((Element )domElement).getAttributes(); 332 for (int i = 0; i < attrs.getLength(); i++) 333 { 334 Attr att = (Attr )attrs.item(i); 335 String nsURI = att.getNamespaceURI(); 336 String localName = att.getLocalName(); 337 String value = att.getValue(); 338 339 if (nsURI != null) 340 soapChild.setAttributeNS(nsURI, localName, value); 341 else 342 soapChild.setAttribute(localName, value); 343 } 344 345 StringBuffer content = new StringBuffer (); 346 347 NodeList children = domElement.getChildNodes(); 348 for (int i = 0; i < children.getLength(); i++) 349 { 350 Node domChild = children.item(i); 351 if (domChild.getNodeType() == Node.ELEMENT_NODE) 352 importDOMElement(soapChild, (Element )domChild); 353 354 if (domChild.getNodeType() == Node.TEXT_NODE && domChild.getNodeValue().trim().length() > 0) 355 content.append(domChild.getNodeValue()); 356 } 357 358 if (content.length() > 0) 359 { 360 String value = content.toString(); 361 soapChild.addTextNode(value); 362 } 363 364 return soapChild; 365 } 366 catch (RuntimeException e) 367 { 368 throw e; 369 } 370 catch (Exception e) 371 { 372 throw new JAXRPCException (e); 373 } 374 } 375 } 376 | Popular Tags |