1 16 17 package org.apache.axis.message; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Constants; 21 import org.apache.axis.components.logger.LogFactory; 22 import org.apache.axis.encoding.DeserializationContext; 23 import org.apache.axis.encoding.SerializationContext; 24 import org.apache.axis.soap.SOAPConstants; 25 import org.apache.axis.utils.Messages; 26 import org.apache.commons.logging.Log; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.xml.sax.Attributes ; 30 31 import javax.xml.namespace.QName ; 32 import javax.xml.soap.Name ; 33 import javax.xml.soap.SOAPElement ; 34 import javax.xml.soap.SOAPException ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Locale ; 39 import java.util.Vector ; 40 41 46 public class SOAPBody extends MessageElement 47 implements javax.xml.soap.SOAPBody { 48 49 private static Log log = LogFactory.getLog(SOAPBody.class.getName()); 50 51 private SOAPConstants soapConstants; 52 53 private boolean disableFormatting = false; 54 private boolean doSAAJEncodingCompliance = false; 55 private static ArrayList knownEncodingStyles = new ArrayList (); 56 57 static { 58 knownEncodingStyles.add(Constants.URI_SOAP11_ENC); 59 knownEncodingStyles.add(Constants.URI_SOAP12_ENC); 60 knownEncodingStyles.add(""); 61 knownEncodingStyles.add(Constants.URI_SOAP12_NOENC); 62 } 63 64 SOAPBody(SOAPEnvelope env, SOAPConstants soapConsts) { 65 super(soapConsts.getEnvelopeURI(), Constants.ELEM_BODY); 66 soapConstants = soapConsts; 67 try { 68 setParentElement(env); 69 } catch (SOAPException ex) { 70 log.fatal(Messages.getMessage("exception00"), ex); 72 } 73 } 74 75 public SOAPBody(String namespace, String localPart, String prefix, 76 Attributes attributes, DeserializationContext context, 77 SOAPConstants soapConsts) throws AxisFault { 78 super(namespace, localPart, prefix, attributes, context); 79 soapConstants = soapConsts; 80 } 81 82 public void setParentElement(SOAPElement parent) throws SOAPException { 83 if(parent == null) { 84 throw new IllegalArgumentException (Messages.getMessage("nullParent00")); 85 } 86 try { 87 SOAPEnvelope env = (SOAPEnvelope)parent; 88 super.setParentElement(env); 89 setEnvelope(env); 90 } catch (Throwable t) { 91 throw new SOAPException (t); 92 } 93 } 94 95 public void disableFormatting() { 96 this.disableFormatting = true; 97 } 98 99 public void setEncodingStyle(String encodingStyle) throws SOAPException { 100 if (encodingStyle == null) { 101 encodingStyle = ""; 102 } 103 104 if (doSAAJEncodingCompliance) { 105 if (!knownEncodingStyles.contains(encodingStyle)) 107 throw new IllegalArgumentException (Messages.getMessage("badEncodingStyle1", encodingStyle)); 108 } 109 110 super.setEncodingStyle(encodingStyle); 111 } 112 113 protected void outputImpl(SerializationContext context) throws Exception { 114 boolean oldPretty = context.getPretty(); 115 if (!disableFormatting) { 116 context.setPretty(true); 117 } else { 118 context.setPretty(false); 119 } 120 121 List bodyElements = getChildren(); 122 123 if (bodyElements == null || bodyElements.isEmpty()) { 124 } 129 130 context.startElement(new QName (soapConstants.getEnvelopeURI(), 132 Constants.ELEM_BODY), 133 getAttributesEx()); 134 135 if (bodyElements != null) { 136 Iterator e = bodyElements.iterator(); 137 while (e.hasNext()) { 138 MessageElement body = (MessageElement)e.next(); 139 body.output(context); 140 } 142 } 143 144 context.outputMultiRefs(); 146 147 context.endElement(); 149 150 context.setPretty(oldPretty); 151 } 152 153 Vector getBodyElements() throws AxisFault { 154 initializeChildren(); 155 return new Vector (getChildren()); 156 } 157 158 SOAPBodyElement getFirstBody() throws AxisFault 159 { 160 if (!hasChildNodes()) 161 return null; 162 return (SOAPBodyElement)getChildren().get(0); 163 } 164 165 void addBodyElement(SOAPBodyElement element) 166 { 167 if (log.isDebugEnabled()) 168 log.debug(Messages.getMessage("addBody00")); 169 try { 170 addChildElement(element); 171 } catch (SOAPException ex) { 172 log.fatal(Messages.getMessage("exception00"), ex); 174 } 175 } 176 177 void removeBodyElement(SOAPBodyElement element) 178 { 179 if (log.isDebugEnabled()) 180 log.debug(Messages.getMessage("removeBody00")); 181 removeChild( (MessageElement)element ); 182 } 183 184 void clearBody() 185 { 186 removeContents(); 187 } 188 189 SOAPBodyElement getBodyByName(String namespace, String localPart) 190 throws AxisFault 191 { 192 QName name = new QName (namespace, localPart); 193 return (SOAPBodyElement)getChildElement(name); 194 } 195 196 198 public javax.xml.soap.SOAPBodyElement addBodyElement(Name name) 199 throws SOAPException { 200 SOAPBodyElement bodyElement = new SOAPBodyElement(name); 201 addChildElement(bodyElement); 202 return bodyElement; 203 } 204 205 public javax.xml.soap.SOAPFault addFault(Name name, String s, Locale locale) throws SOAPException { 206 AxisFault af = new AxisFault(new QName (name.getURI(), name.getLocalName()), s, "", new Element [0]); 207 SOAPFault fault = new SOAPFault(af); 208 addChildElement(fault); 209 return fault; 210 } 211 212 public javax.xml.soap.SOAPFault addFault(Name name, String s) throws SOAPException { 213 AxisFault af = new AxisFault(new QName (name.getURI(), name.getLocalName()), s, "", new Element [0]); 214 SOAPFault fault = new SOAPFault(af); 215 addChildElement(fault); 216 return fault; 217 } 218 219 public javax.xml.soap.SOAPBodyElement addDocument(Document document) throws SOAPException { 220 SOAPBodyElement bodyElement = new SOAPBodyElement(document.getDocumentElement()); 221 addChildElement(bodyElement); 222 return bodyElement; 223 } 224 225 public javax.xml.soap.SOAPFault addFault() throws SOAPException { 226 227 AxisFault af = new AxisFault(new QName (Constants.NS_URI_AXIS, Constants.FAULT_SERVER_GENERAL), "", "", new Element [0]); 228 SOAPFault fault = new SOAPFault(af); 229 addChildElement(fault); 230 return fault; 231 } 232 233 public javax.xml.soap.SOAPFault getFault() { 234 List bodyElements = getChildren(); 235 if (bodyElements != null) { 236 Iterator e = bodyElements.iterator(); 237 while (e.hasNext()) { 238 Object element = e.next(); 239 if(element instanceof javax.xml.soap.SOAPFault ) { 240 return (javax.xml.soap.SOAPFault ) element; 241 } 242 } 243 } 244 return null; 245 } 246 247 public boolean hasFault() { 248 return (getFault() != null); 249 } 250 251 public void addChild(MessageElement element) throws SOAPException { 253 element.setEnvelope(getEnvelope()); 258 super.addChild(element); 259 } 260 261 public SOAPElement addChildElement(SOAPElement element) 263 throws SOAPException { 264 SOAPElement child = super.addChildElement(element); 269 setDirty(true); 270 return child; 271 } 272 273 public SOAPElement addChildElement(Name name) throws SOAPException { 274 SOAPBodyElement child = new SOAPBodyElement(name); 275 addChildElement(child); 276 return child; 277 } 278 279 public SOAPElement addChildElement(String localName) throws SOAPException { 280 SOAPBodyElement child = new SOAPBodyElement(getNamespaceURI(), 282 localName); 283 addChildElement(child); 284 return child; 285 } 286 287 public SOAPElement addChildElement(String localName, 288 String prefix) throws SOAPException { 289 SOAPBodyElement child = 290 new SOAPBodyElement(getNamespaceURI(prefix), localName); 291 child.setPrefix(prefix); 292 addChildElement(child); 293 return child; 294 } 295 296 public SOAPElement addChildElement(String localName, 297 String prefix, 298 String uri) throws SOAPException { 299 SOAPBodyElement child = new SOAPBodyElement(uri, localName); 300 child.setPrefix(prefix); 301 child.addNamespaceDeclaration(prefix, uri); 302 addChildElement(child); 303 return child; 304 } 305 306 public void setSAAJEncodingCompliance(boolean comply) { 307 this.doSAAJEncodingCompliance = true; 308 } 309 } 310 | Popular Tags |