1 16 package org.apache.axis.message; 17 18 import org.apache.axis.Constants; 19 import org.apache.axis.encoding.DeserializationContext; 20 import org.apache.axis.soap.SOAPConstants; 21 import org.apache.axis.utils.Messages; 22 import org.xml.sax.Attributes ; 23 import org.xml.sax.SAXException ; 24 import org.apache.axis.AxisFault; 25 import org.apache.axis.MessageContext; 26 27 import javax.xml.namespace.QName ; 28 29 37 public class EnvelopeBuilder extends SOAPHandler 38 { 39 private SOAPEnvelope envelope; 40 private SOAPConstants soapConstants = SOAPConstants.SOAP11_CONSTANTS; 41 42 private boolean gotHeader = false; 43 private boolean gotBody = false; 44 45 public EnvelopeBuilder(String messageType, SOAPConstants soapConstants) 46 { 47 envelope = new SOAPEnvelope(false, soapConstants); 48 envelope.setMessageType(messageType); 49 myElement = envelope; 50 } 51 52 public EnvelopeBuilder(SOAPEnvelope env, String messageType) 53 { 54 envelope = env ; 55 envelope.setMessageType(messageType); 56 myElement = envelope; 57 } 58 59 public SOAPEnvelope getEnvelope() 60 { 61 return envelope; 62 } 63 64 public void startElement(String namespace, String localName, 65 String prefix, Attributes attributes, 66 DeserializationContext context) 67 throws SAXException 68 { 69 if (!localName.equals(Constants.ELEM_ENVELOPE)) 70 throw new SAXException ( 71 Messages.getMessage("badTag00", localName)); 72 73 MessageContext msgContext = context.getMessageContext(); 75 SOAPConstants singleVersion = null; 76 if (msgContext != null) { 77 singleVersion = (SOAPConstants)msgContext.getProperty( 78 Constants.MC_SINGLE_SOAP_VERSION); 79 } 80 81 if (namespace.equals(Constants.URI_SOAP11_ENV)) { 82 soapConstants = SOAPConstants.SOAP11_CONSTANTS; 84 } else if (namespace.equals(Constants.URI_SOAP12_ENV)) { 85 soapConstants = SOAPConstants.SOAP12_CONSTANTS; 87 } else { 88 soapConstants = null; 89 } 90 91 if ((soapConstants == null) || 92 (singleVersion != null && soapConstants != singleVersion)) { 93 96 soapConstants = SOAPConstants.SOAP11_CONSTANTS; 99 if (singleVersion == null) singleVersion = soapConstants; 100 101 try { 102 AxisFault fault = new AxisFault(soapConstants.getVerMismatchFaultCodeQName(), 103 null, Messages.getMessage("versionMissmatch00"), null, null, null); 104 105 SOAPHeaderElement newHeader = new 106 SOAPHeaderElement(soapConstants.getEnvelopeURI(), 107 Constants.ELEM_UPGRADE); 108 109 MessageElement innerHeader = new 112 MessageElement(soapConstants.getEnvelopeURI(), 113 Constants.ELEM_SUPPORTEDENVELOPE); 114 innerHeader.addAttribute(null, Constants.ATTR_QNAME, 115 new QName (singleVersion.getEnvelopeURI(), Constants.ELEM_ENVELOPE)); 116 117 newHeader.addChildElement(innerHeader); 118 fault.addHeader(newHeader); 119 120 throw new SAXException (fault); 121 122 } catch (javax.xml.soap.SOAPException e) { 123 throw new SAXException (e); 124 } 125 } 126 127 if(context.getMessageContext() != null) 130 context.getMessageContext().setSOAPConstants(soapConstants); 131 132 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS && 133 attributes.getValue(Constants.URI_SOAP12_ENV, Constants.ATTR_ENCODING_STYLE) != null) { 134 135 AxisFault fault = new AxisFault(Constants.FAULT_SOAP12_SENDER, 136 null, Messages.getMessage("noEncodingStyleAttrAppear", "Envelope"), null, null, null); 137 138 throw new SAXException (fault); 139 } 140 141 envelope.setPrefix(prefix); 142 envelope.setNamespaceURI(namespace); 143 envelope.setNSMappings(context.getCurrentNSMappings()); 144 envelope.setSoapConstants(soapConstants); 145 context.pushNewElement(envelope); 146 } 147 148 public SOAPHandler onStartChild(String namespace, 149 String localName, 150 String prefix, 151 Attributes attributes, 152 DeserializationContext context) 153 throws SAXException 154 { 155 QName thisQName = new QName (namespace, localName); 156 if (thisQName.equals(soapConstants.getHeaderQName())) { 157 if (gotHeader) 158 throw new SAXException (Messages.getMessage("only1Header00")); 159 160 gotHeader = true; 161 return new HeaderBuilder(envelope); 162 } 163 164 if (thisQName.equals(soapConstants.getBodyQName())) { 165 if (gotBody) 166 throw new SAXException (Messages.getMessage("only1Body00")); 167 168 gotBody = true; 169 return new BodyBuilder(envelope); 170 } 171 172 if (!gotBody) 173 throw new SAXException (Messages.getMessage("noCustomElems00")); 174 175 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { 176 throw new SAXException (Messages.getMessage("noElemAfterBody12")); 177 } 178 179 try { 180 MessageElement element = new MessageElement(namespace, localName, prefix, 181 attributes, context); 182 183 if (element.getFixupDeserializer() != null) 184 return (SOAPHandler)element.getFixupDeserializer(); 185 } catch (AxisFault axisFault) { 186 throw new SAXException (axisFault); 187 } 188 189 return null; 190 } 191 192 public void onEndChild(String namespace, String localName, 193 DeserializationContext context) 194 { 195 } 196 197 public void endElement(String namespace, String localName, 198 DeserializationContext context) 199 throws SAXException 200 { 201 envelope.setDirty(false); 203 } 204 } 205 | Popular Tags |