1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/message/stream/StreamMessage.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,766 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.message.stream; 1.30 + 1.31 +import com.sun.istack.internal.NotNull; 1.32 +import com.sun.istack.internal.Nullable; 1.33 +import com.sun.istack.internal.XMLStreamReaderToContentHandler; 1.34 +import com.sun.xml.internal.bind.api.Bridge; 1.35 +import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; 1.36 +import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; 1.37 +import com.sun.xml.internal.stream.buffer.XMLStreamBufferMark; 1.38 +import com.sun.xml.internal.stream.buffer.stax.StreamReaderBufferCreator; 1.39 +import com.sun.xml.internal.ws.api.SOAPVersion; 1.40 +import com.sun.xml.internal.ws.api.message.AttachmentSet; 1.41 +import com.sun.xml.internal.ws.api.message.Header; 1.42 +import com.sun.xml.internal.ws.api.message.HeaderList; 1.43 +import com.sun.xml.internal.ws.api.message.Message; 1.44 +import com.sun.xml.internal.ws.api.message.MessageHeaders; 1.45 +import com.sun.xml.internal.ws.api.message.StreamingSOAP; 1.46 +import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; 1.47 +import com.sun.xml.internal.ws.encoding.TagInfoset; 1.48 +import com.sun.xml.internal.ws.message.AbstractMessageImpl; 1.49 +import com.sun.xml.internal.ws.message.AttachmentUnmarshallerImpl; 1.50 +import com.sun.xml.internal.ws.protocol.soap.VersionMismatchException; 1.51 +import com.sun.xml.internal.ws.spi.db.XMLBridge; 1.52 +import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil; 1.53 +import com.sun.xml.internal.ws.util.xml.DummyLocation; 1.54 +import com.sun.xml.internal.ws.util.xml.StAXSource; 1.55 +import com.sun.xml.internal.ws.util.xml.XMLReaderComposite; 1.56 +import com.sun.xml.internal.ws.util.xml.XMLStreamReaderToXMLStreamWriter; 1.57 +import com.sun.xml.internal.ws.util.xml.XMLReaderComposite.ElemInfo; 1.58 + 1.59 +import org.xml.sax.ContentHandler; 1.60 +import org.xml.sax.ErrorHandler; 1.61 +import org.xml.sax.SAXException; 1.62 +import org.xml.sax.SAXParseException; 1.63 +import org.xml.sax.helpers.NamespaceSupport; 1.64 + 1.65 +import javax.xml.bind.JAXBException; 1.66 +import javax.xml.bind.Unmarshaller; 1.67 +import javax.xml.stream.*; 1.68 + 1.69 +import static javax.xml.stream.XMLStreamConstants.START_DOCUMENT; 1.70 +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; 1.71 +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; 1.72 +import javax.xml.transform.Source; 1.73 +import javax.xml.ws.WebServiceException; 1.74 +import java.util.ArrayList; 1.75 +import java.util.Enumeration; 1.76 +import java.util.HashMap; 1.77 +import java.util.List; 1.78 +import java.util.Map; 1.79 + 1.80 +/** 1.81 + * {@link Message} implementation backed by {@link XMLStreamReader}. 1.82 + * 1.83 + * TODO: we need another message class that keeps {@link XMLStreamReader} that points 1.84 + * at the start of the envelope element. 1.85 + */ 1.86 +public class StreamMessage extends AbstractMessageImpl implements StreamingSOAP { 1.87 + /** 1.88 + * The reader will be positioned at 1.89 + * the first child of the SOAP body 1.90 + */ 1.91 + private @NotNull XMLStreamReader reader; 1.92 + 1.93 + // lazily created 1.94 + private @Nullable MessageHeaders headers; 1.95 + 1.96 + /** 1.97 + * Because the StreamMessage leaves out the white spaces around payload 1.98 + * when being instantiated the space characters between soap:Body opening and 1.99 + * payload is stored in this field to be reused later (necessary for message security); 1.100 + * Instantiated after StreamMessage creation 1.101 + */ 1.102 + private String bodyPrologue = null; 1.103 + 1.104 + /** 1.105 + * instantiated after writing message to XMLStreamWriter 1.106 + */ 1.107 + private String bodyEpilogue = null; 1.108 + 1.109 + private String payloadLocalName; 1.110 + 1.111 + private String payloadNamespaceURI; 1.112 + 1.113 + /** 1.114 + * Used only for debugging. This records where the message was consumed. 1.115 + */ 1.116 + private Throwable consumedAt; 1.117 + 1.118 + private XMLStreamReader envelopeReader; 1.119 + 1.120 + public StreamMessage(SOAPVersion v) { 1.121 + super(v); 1.122 + payloadLocalName = null; 1.123 + payloadNamespaceURI = null; 1.124 + } 1.125 + 1.126 + public StreamMessage(SOAPVersion v, @NotNull XMLStreamReader envelope, @NotNull AttachmentSet attachments) { 1.127 + super(v); 1.128 + envelopeReader = envelope; 1.129 + attachmentSet = attachments; 1.130 + } 1.131 + 1.132 + public XMLStreamReader readEnvelope() { 1.133 + if (envelopeReader == null) { 1.134 + List<XMLStreamReader> hReaders = new java.util.ArrayList<XMLStreamReader>(); 1.135 + ElemInfo envElem = new ElemInfo(envelopeTag, null); 1.136 + ElemInfo hdrElem = (headerTag != null) ? new ElemInfo(headerTag, envElem) : null; 1.137 + ElemInfo bdyElem = new ElemInfo(bodyTag, envElem); 1.138 + for (Header h : getHeaders().asList()) { 1.139 + try { 1.140 + hReaders.add(h.readHeader()); 1.141 + } catch (XMLStreamException e) { 1.142 + throw new RuntimeException(e); 1.143 + } 1.144 + } 1.145 + XMLStreamReader soapHeader = (hdrElem != null) ? new XMLReaderComposite(hdrElem, hReaders.toArray(new XMLStreamReader[hReaders.size()])) : null; 1.146 + XMLStreamReader[] payload = {readPayload()}; 1.147 + XMLStreamReader soapBody = new XMLReaderComposite(bdyElem, payload); 1.148 + XMLStreamReader[] soapContent = (soapHeader != null) ? new XMLStreamReader[]{soapHeader, soapBody} : new XMLStreamReader[]{soapBody}; 1.149 + return new XMLReaderComposite(envElem, soapContent); 1.150 + } 1.151 + return envelopeReader; 1.152 + } 1.153 + 1.154 + /** 1.155 + * Creates a {@link StreamMessage} from a {@link XMLStreamReader} 1.156 + * that points at the start element of the payload, and headers. 1.157 + * 1.158 + * <p> 1.159 + * This method creates a {@link Message} from a payload. 1.160 + * 1.161 + * @param headers 1.162 + * if null, it means no headers. if non-null, 1.163 + * it will be owned by this message. 1.164 + * @param reader 1.165 + * points at the start element/document of the payload (or the end element of the <s:Body> 1.166 + * if there's no payload) 1.167 + */ 1.168 + public StreamMessage(@Nullable MessageHeaders headers, @NotNull AttachmentSet attachmentSet, @NotNull XMLStreamReader reader, @NotNull SOAPVersion soapVersion) { 1.169 + super(soapVersion); 1.170 + init(headers, attachmentSet, reader, soapVersion); 1.171 + } 1.172 + 1.173 + private void init(@Nullable MessageHeaders headers, @NotNull AttachmentSet attachmentSet, @NotNull XMLStreamReader reader, @NotNull SOAPVersion soapVersion) { 1.174 + this.headers = headers; 1.175 + this.attachmentSet = attachmentSet; 1.176 + this.reader = reader; 1.177 + 1.178 + if(reader.getEventType()== START_DOCUMENT) 1.179 + XMLStreamReaderUtil.nextElementContent(reader); 1.180 + 1.181 + //if the reader is pointing to the end element </soapenv:Body> then its empty message 1.182 + // or no payload 1.183 + if(reader.getEventType() == XMLStreamConstants.END_ELEMENT){ 1.184 + String body = reader.getLocalName(); 1.185 + String nsUri = reader.getNamespaceURI(); 1.186 + assert body != null; 1.187 + assert nsUri != null; 1.188 + //if its not soapenv:Body then throw exception, we received malformed stream 1.189 + if(body.equals("Body") && nsUri.equals(soapVersion.nsUri)){ 1.190 + this.payloadLocalName = null; 1.191 + this.payloadNamespaceURI = null; 1.192 + }else{ //TODO: i18n and also we should be throwing better message that this 1.193 + throw new WebServiceException("Malformed stream: {"+nsUri+"}"+body); 1.194 + } 1.195 + }else{ 1.196 + this.payloadLocalName = reader.getLocalName(); 1.197 + this.payloadNamespaceURI = reader.getNamespaceURI(); 1.198 + } 1.199 + 1.200 + // use the default infoset representation for headers 1.201 + int base = soapVersion.ordinal()*3; 1.202 + this.envelopeTag = DEFAULT_TAGS.get(base); 1.203 + this.headerTag = DEFAULT_TAGS.get(base+1); 1.204 + this.bodyTag = DEFAULT_TAGS.get(base+2); 1.205 + } 1.206 + 1.207 + /** 1.208 + * Creates a {@link StreamMessage} from a {@link XMLStreamReader} 1.209 + * and the complete infoset of the SOAP envelope. 1.210 + * 1.211 + * <p> 1.212 + * See {@link #StreamMessage(MessageHeaders, AttachmentSet, XMLStreamReader, SOAPVersion)} for 1.213 + * the description of the basic parameters. 1.214 + * 1.215 + * @param headerTag 1.216 + * Null if the message didn't have a header tag. 1.217 + * 1.218 + */ 1.219 + public StreamMessage(@NotNull TagInfoset envelopeTag, @Nullable TagInfoset headerTag, @NotNull AttachmentSet attachmentSet, @Nullable MessageHeaders headers, @NotNull TagInfoset bodyTag, @NotNull XMLStreamReader reader, @NotNull SOAPVersion soapVersion) { 1.220 + this(envelopeTag, headerTag, attachmentSet, headers, null, bodyTag, null, reader, soapVersion); 1.221 + } 1.222 + 1.223 + public StreamMessage(@NotNull TagInfoset envelopeTag, @Nullable TagInfoset headerTag, @NotNull AttachmentSet attachmentSet, @Nullable MessageHeaders headers, @Nullable String bodyPrologue, @NotNull TagInfoset bodyTag, @Nullable String bodyEpilogue, @NotNull XMLStreamReader reader, @NotNull SOAPVersion soapVersion) { 1.224 + super(soapVersion); 1.225 + init(envelopeTag, headerTag, attachmentSet, headers, bodyPrologue, bodyTag, bodyEpilogue, reader, soapVersion); 1.226 + } 1.227 + 1.228 + private void init(@NotNull TagInfoset envelopeTag, @Nullable TagInfoset headerTag, @NotNull AttachmentSet attachmentSet, @Nullable MessageHeaders headers, @Nullable String bodyPrologue, @NotNull TagInfoset bodyTag, @Nullable String bodyEpilogue, @NotNull XMLStreamReader reader, @NotNull SOAPVersion soapVersion) { 1.229 + init(headers,attachmentSet,reader,soapVersion); 1.230 + if(envelopeTag == null ) { 1.231 + throw new IllegalArgumentException("EnvelopeTag TagInfoset cannot be null"); 1.232 + } 1.233 + if(bodyTag == null ) { 1.234 + throw new IllegalArgumentException("BodyTag TagInfoset cannot be null"); 1.235 + } 1.236 + this.envelopeTag = envelopeTag; 1.237 + this.headerTag = headerTag; 1.238 + this.bodyTag = bodyTag; 1.239 + this.bodyPrologue = bodyPrologue; 1.240 + this.bodyEpilogue = bodyEpilogue; 1.241 + } 1.242 + 1.243 + public boolean hasHeaders() { 1.244 + if ( envelopeReader != null ) readEnvelope(this); 1.245 + return headers!=null && headers.hasHeaders(); 1.246 + } 1.247 + 1.248 + public MessageHeaders getHeaders() { 1.249 + if ( envelopeReader != null ) readEnvelope(this); 1.250 + if (headers == null) { 1.251 + headers = new HeaderList(getSOAPVersion()); 1.252 + } 1.253 + return headers; 1.254 + } 1.255 + 1.256 + public String getPayloadLocalPart() { 1.257 + if ( envelopeReader != null ) readEnvelope(this); 1.258 + return payloadLocalName; 1.259 + } 1.260 + 1.261 + public String getPayloadNamespaceURI() { 1.262 + if ( envelopeReader != null ) readEnvelope(this); 1.263 + return payloadNamespaceURI; 1.264 + } 1.265 + 1.266 + public boolean hasPayload() { 1.267 + if ( envelopeReader != null ) readEnvelope(this); 1.268 + return payloadLocalName!=null; 1.269 + } 1.270 + 1.271 + public Source readPayloadAsSource() { 1.272 + if(hasPayload()) { 1.273 + assert unconsumed(); 1.274 + return new StAXSource(reader, true, getInscopeNamespaces()); 1.275 + } else 1.276 + return null; 1.277 + } 1.278 + 1.279 + /** 1.280 + * There is no way to enumerate inscope namespaces for XMLStreamReader. That means 1.281 + * namespaces declared in envelope, and body tags need to be computed using their 1.282 + * {@link TagInfoset}s. 1.283 + * 1.284 + * @return array of the even length of the form { prefix0, uri0, prefix1, uri1, ... } 1.285 + */ 1.286 + private String[] getInscopeNamespaces() { 1.287 + NamespaceSupport nss = new NamespaceSupport(); 1.288 + 1.289 + nss.pushContext(); 1.290 + for(int i=0; i < envelopeTag.ns.length; i+=2) { 1.291 + nss.declarePrefix(envelopeTag.ns[i], envelopeTag.ns[i+1]); 1.292 + } 1.293 + 1.294 + nss.pushContext(); 1.295 + for(int i=0; i < bodyTag.ns.length; i+=2) { 1.296 + nss.declarePrefix(bodyTag.ns[i], bodyTag.ns[i+1]); 1.297 + } 1.298 + 1.299 + List<String> inscope = new ArrayList<String>(); 1.300 + for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) { 1.301 + String prefix = (String)en.nextElement(); 1.302 + inscope.add(prefix); 1.303 + inscope.add(nss.getURI(prefix)); 1.304 + } 1.305 + return inscope.toArray(new String[inscope.size()]); 1.306 + } 1.307 + 1.308 + public Object readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException { 1.309 + if(!hasPayload()) 1.310 + return null; 1.311 + assert unconsumed(); 1.312 + // TODO: How can the unmarshaller process this as a fragment? 1.313 + if(hasAttachments()) 1.314 + unmarshaller.setAttachmentUnmarshaller(new AttachmentUnmarshallerImpl(getAttachments())); 1.315 + try { 1.316 + return unmarshaller.unmarshal(reader); 1.317 + } finally{ 1.318 + unmarshaller.setAttachmentUnmarshaller(null); 1.319 + XMLStreamReaderUtil.readRest(reader); 1.320 + XMLStreamReaderUtil.close(reader); 1.321 + XMLStreamReaderFactory.recycle(reader); 1.322 + } 1.323 + } 1.324 + /** @deprecated */ 1.325 + public <T> T readPayloadAsJAXB(Bridge<T> bridge) throws JAXBException { 1.326 + if(!hasPayload()) 1.327 + return null; 1.328 + assert unconsumed(); 1.329 + T r = bridge.unmarshal(reader, 1.330 + hasAttachments() ? new AttachmentUnmarshallerImpl(getAttachments()) : null); 1.331 + XMLStreamReaderUtil.readRest(reader); 1.332 + XMLStreamReaderUtil.close(reader); 1.333 + XMLStreamReaderFactory.recycle(reader); 1.334 + return r; 1.335 + } 1.336 + 1.337 + public <T> T readPayloadAsJAXB(XMLBridge<T> bridge) throws JAXBException { 1.338 + if(!hasPayload()) 1.339 + return null; 1.340 + assert unconsumed(); 1.341 + T r = bridge.unmarshal(reader, 1.342 + hasAttachments() ? new AttachmentUnmarshallerImpl(getAttachments()) : null); 1.343 + XMLStreamReaderUtil.readRest(reader); 1.344 + XMLStreamReaderUtil.close(reader); 1.345 + XMLStreamReaderFactory.recycle(reader); 1.346 + return r; 1.347 + } 1.348 + 1.349 + @Override 1.350 + public void consume() { 1.351 + assert unconsumed(); 1.352 + XMLStreamReaderUtil.readRest(reader); 1.353 + XMLStreamReaderUtil.close(reader); 1.354 + XMLStreamReaderFactory.recycle(reader); 1.355 + } 1.356 + 1.357 + public XMLStreamReader readPayload() { 1.358 + if(!hasPayload()) 1.359 + return null; 1.360 + // TODO: What about access at and beyond </soap:Body> 1.361 + assert unconsumed(); 1.362 + return this.reader; 1.363 + } 1.364 + 1.365 + public void writePayloadTo(XMLStreamWriter writer)throws XMLStreamException { 1.366 + if ( envelopeReader != null ) readEnvelope(this); 1.367 + assert unconsumed(); 1.368 + 1.369 + if(payloadLocalName==null) { 1.370 + return; // no body 1.371 + } 1.372 + 1.373 + if (bodyPrologue != null) { 1.374 + writer.writeCharacters(bodyPrologue); 1.375 + } 1.376 + 1.377 + XMLStreamReaderToXMLStreamWriter conv = new XMLStreamReaderToXMLStreamWriter(); 1.378 + 1.379 + while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT){ 1.380 + String name = reader.getLocalName(); 1.381 + String nsUri = reader.getNamespaceURI(); 1.382 + 1.383 + // After previous conv.bridge() call the cursor will be at END_ELEMENT. 1.384 + // Check if its not soapenv:Body then move to next ELEMENT 1.385 + if(reader.getEventType() == XMLStreamConstants.END_ELEMENT){ 1.386 + 1.387 + if (!isBodyElement(name, nsUri)){ 1.388 + // closing payload element: store epilogue for further signing, if applicable 1.389 + // however if there more than one payloads exist - the last one is stored 1.390 + String whiteSpaces = XMLStreamReaderUtil.nextWhiteSpaceContent(reader); 1.391 + if (whiteSpaces != null) { 1.392 + this.bodyEpilogue = whiteSpaces; 1.393 + // write it to the message too 1.394 + writer.writeCharacters(whiteSpaces); 1.395 + } 1.396 + } else { 1.397 + // body closed > exit 1.398 + break; 1.399 + } 1.400 + 1.401 + } else { 1.402 + // payload opening element: copy payload to writer 1.403 + conv.bridge(reader,writer); 1.404 + } 1.405 + } 1.406 + 1.407 + XMLStreamReaderUtil.readRest(reader); 1.408 + XMLStreamReaderUtil.close(reader); 1.409 + XMLStreamReaderFactory.recycle(reader); 1.410 + } 1.411 + 1.412 + private boolean isBodyElement(String name, String nsUri) { 1.413 + return name.equals("Body") && nsUri.equals(soapVersion.nsUri); 1.414 + } 1.415 + 1.416 + public void writeTo(XMLStreamWriter sw) throws XMLStreamException{ 1.417 + if ( envelopeReader != null ) readEnvelope(this); 1.418 + writeEnvelope(sw); 1.419 + } 1.420 + 1.421 + /** 1.422 + * This method should be called when the StreamMessage is created with a payload 1.423 + * @param writer 1.424 + */ 1.425 + private void writeEnvelope(XMLStreamWriter writer) throws XMLStreamException { 1.426 + if ( envelopeReader != null ) readEnvelope(this); 1.427 + writer.writeStartDocument(); 1.428 + envelopeTag.writeStart(writer); 1.429 + 1.430 + //write headers 1.431 + MessageHeaders hl = getHeaders(); 1.432 + if (hl.hasHeaders() && headerTag == null) headerTag = new TagInfoset(envelopeTag.nsUri,"Header",envelopeTag.prefix,EMPTY_ATTS); 1.433 + if (headerTag != null) { 1.434 + headerTag.writeStart(writer); 1.435 + if (hl.hasHeaders()){ 1.436 + for(Header h : hl.asList()){ 1.437 + h.writeTo(writer); 1.438 + } 1.439 + } 1.440 + writer.writeEndElement(); 1.441 + } 1.442 + bodyTag.writeStart(writer); 1.443 + if(hasPayload()) 1.444 + writePayloadTo(writer); 1.445 + writer.writeEndElement(); 1.446 + writer.writeEndElement(); 1.447 + writer.writeEndDocument(); 1.448 + } 1.449 + 1.450 + public void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException { 1.451 + if ( envelopeReader != null ) readEnvelope(this); 1.452 + assert unconsumed(); 1.453 + 1.454 + try { 1.455 + if(payloadLocalName==null) 1.456 + return; // no body 1.457 + 1.458 + if (bodyPrologue != null) { 1.459 + char[] chars = bodyPrologue.toCharArray(); 1.460 + contentHandler.characters(chars, 0, chars.length); 1.461 + } 1.462 + 1.463 + XMLStreamReaderToContentHandler conv = new XMLStreamReaderToContentHandler(reader,contentHandler,true,fragment,getInscopeNamespaces()); 1.464 + 1.465 + while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT){ 1.466 + String name = reader.getLocalName(); 1.467 + String nsUri = reader.getNamespaceURI(); 1.468 + 1.469 + // After previous conv.bridge() call the cursor will be at END_ELEMENT. 1.470 + // Check if its not soapenv:Body then move to next ELEMENT 1.471 + if(reader.getEventType() == XMLStreamConstants.END_ELEMENT){ 1.472 + 1.473 + if (!isBodyElement(name, nsUri)){ 1.474 + // closing payload element: store epilogue for further signing, if applicable 1.475 + // however if there more than one payloads exist - the last one is stored 1.476 + String whiteSpaces = XMLStreamReaderUtil.nextWhiteSpaceContent(reader); 1.477 + if (whiteSpaces != null) { 1.478 + this.bodyEpilogue = whiteSpaces; 1.479 + // write it to the message too 1.480 + char[] chars = whiteSpaces.toCharArray(); 1.481 + contentHandler.characters(chars, 0, chars.length); 1.482 + } 1.483 + } else { 1.484 + // body closed > exit 1.485 + break; 1.486 + } 1.487 + 1.488 + } else { 1.489 + // payload opening element: copy payload to writer 1.490 + conv.bridge(); 1.491 + } 1.492 + } 1.493 + XMLStreamReaderUtil.readRest(reader); 1.494 + XMLStreamReaderUtil.close(reader); 1.495 + XMLStreamReaderFactory.recycle(reader); 1.496 + } catch (XMLStreamException e) { 1.497 + Location loc = e.getLocation(); 1.498 + if(loc==null) loc = DummyLocation.INSTANCE; 1.499 + 1.500 + SAXParseException x = new SAXParseException( 1.501 + e.getMessage(),loc.getPublicId(),loc.getSystemId(),loc.getLineNumber(),loc.getColumnNumber(),e); 1.502 + errorHandler.error(x); 1.503 + } 1.504 + } 1.505 + 1.506 + // TODO: this method should be probably rewritten to respect spaces between elements; is it used at all? 1.507 + @Override 1.508 + public Message copy() { 1.509 + if ( envelopeReader != null ) readEnvelope(this); 1.510 + try { 1.511 + assert unconsumed(); 1.512 + consumedAt = null; // but we don't want to mark it as consumed 1.513 + MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer(); 1.514 + StreamReaderBufferCreator c = new StreamReaderBufferCreator(xsb); 1.515 + 1.516 + // preserving inscope namespaces from envelope, and body. Other option 1.517 + // would be to create a filtering XMLStreamReader from reader+envelopeTag+bodyTag 1.518 + c.storeElement(envelopeTag.nsUri, envelopeTag.localName, envelopeTag.prefix, envelopeTag.ns); 1.519 + c.storeElement(bodyTag.nsUri, bodyTag.localName, bodyTag.prefix, bodyTag.ns); 1.520 + 1.521 + if (hasPayload()) { 1.522 + // Loop all the way for multi payload case 1.523 + while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT){ 1.524 + String name = reader.getLocalName(); 1.525 + String nsUri = reader.getNamespaceURI(); 1.526 + if(isBodyElement(name, nsUri) || (reader.getEventType() == XMLStreamConstants.END_DOCUMENT)) 1.527 + break; 1.528 + c.create(reader); 1.529 + 1.530 + // Skip whitespaces in between payload and </Body> or between elements 1.531 + // those won't be in the message itself, but we store them in field bodyEpilogue 1.532 + if (reader.isWhiteSpace()) { 1.533 + bodyEpilogue = XMLStreamReaderUtil.currentWhiteSpaceContent(reader); 1.534 + } else { 1.535 + // clear it in case the existing was not the last one 1.536 + // (we are interested only in the last one?) 1.537 + bodyEpilogue = null; 1.538 + } 1.539 + } 1.540 + } 1.541 + c.storeEndElement(); // create structure element for </Body> 1.542 + c.storeEndElement(); // create structure element for </Envelope> 1.543 + c.storeEndElement(); // create structure element for END_DOCUMENT 1.544 + 1.545 + XMLStreamReaderUtil.readRest(reader); 1.546 + XMLStreamReaderUtil.close(reader); 1.547 + XMLStreamReaderFactory.recycle(reader); 1.548 + 1.549 + reader = xsb.readAsXMLStreamReader(); 1.550 + XMLStreamReader clone = xsb.readAsXMLStreamReader(); 1.551 + 1.552 + // advance to the start tag of the <Body> first child element 1.553 + proceedToRootElement(reader); 1.554 + proceedToRootElement(clone); 1.555 + 1.556 + return new StreamMessage(envelopeTag, headerTag, attachmentSet, HeaderList.copy(headers), bodyPrologue, bodyTag, bodyEpilogue, clone, soapVersion); 1.557 + } catch (XMLStreamException e) { 1.558 + throw new WebServiceException("Failed to copy a message",e); 1.559 + } 1.560 + } 1.561 + 1.562 + private void proceedToRootElement(XMLStreamReader xsr) throws XMLStreamException { 1.563 + assert xsr.getEventType()==START_DOCUMENT; 1.564 + xsr.nextTag(); 1.565 + xsr.nextTag(); 1.566 + xsr.nextTag(); 1.567 + assert xsr.getEventType()==START_ELEMENT || xsr.getEventType()==END_ELEMENT; 1.568 + } 1.569 + 1.570 + public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler ) throws SAXException { 1.571 + if ( envelopeReader != null ) readEnvelope(this); 1.572 + contentHandler.setDocumentLocator(NULL_LOCATOR); 1.573 + contentHandler.startDocument(); 1.574 + envelopeTag.writeStart(contentHandler); 1.575 + if (hasHeaders() && headerTag == null) headerTag = new TagInfoset(envelopeTag.nsUri,"Header",envelopeTag.prefix,EMPTY_ATTS); 1.576 + if (headerTag != null) { 1.577 + headerTag.writeStart(contentHandler); 1.578 + if (hasHeaders()) { 1.579 + MessageHeaders headers = getHeaders(); 1.580 + for (Header h : headers.asList()) { 1.581 + // shouldn't JDK be smart enough to use array-style indexing for this foreach!? 1.582 + h.writeTo(contentHandler,errorHandler); 1.583 + } 1.584 + } 1.585 + headerTag.writeEnd(contentHandler); 1.586 + } 1.587 + bodyTag.writeStart(contentHandler); 1.588 + writePayloadTo(contentHandler,errorHandler, true); 1.589 + bodyTag.writeEnd(contentHandler); 1.590 + envelopeTag.writeEnd(contentHandler); 1.591 + contentHandler.endDocument(); 1.592 + } 1.593 + 1.594 + /** 1.595 + * Used for an assertion. Returns true when the message is unconsumed, 1.596 + * or otherwise throw an exception. 1.597 + * 1.598 + * <p> 1.599 + * Calling this method also marks the stream as 'consumed' 1.600 + */ 1.601 + private boolean unconsumed() { 1.602 + if(payloadLocalName==null) 1.603 + return true; // no payload. can be consumed multiple times. 1.604 + 1.605 + if(reader.getEventType()!=XMLStreamReader.START_ELEMENT) { 1.606 + AssertionError error = new AssertionError("StreamMessage has been already consumed. See the nested exception for where it's consumed"); 1.607 + error.initCause(consumedAt); 1.608 + throw error; 1.609 + } 1.610 + consumedAt = new Exception().fillInStackTrace(); 1.611 + return true; 1.612 + } 1.613 + 1.614 + public String getBodyPrologue() { 1.615 + if ( envelopeReader != null ) readEnvelope(this); 1.616 + return bodyPrologue; 1.617 + } 1.618 + 1.619 + public String getBodyEpilogue() { 1.620 + if ( envelopeReader != null ) readEnvelope(this); 1.621 + return bodyEpilogue; 1.622 + } 1.623 + 1.624 + public XMLStreamReader getReader() { 1.625 + if ( envelopeReader != null ) readEnvelope(this); 1.626 + assert unconsumed(); 1.627 + return reader; 1.628 + } 1.629 + 1.630 + 1.631 + private static final String SOAP_ENVELOPE = "Envelope"; 1.632 + private static final String SOAP_HEADER = "Header"; 1.633 + private static final String SOAP_BODY = "Body"; 1.634 + 1.635 + protected interface StreamHeaderDecoder { 1.636 + public Header decodeHeader(XMLStreamReader reader, XMLStreamBuffer mark); 1.637 + } 1.638 + 1.639 + static final StreamHeaderDecoder SOAP12StreamHeaderDecoder = new StreamHeaderDecoder() { 1.640 + @Override 1.641 + public Header decodeHeader(XMLStreamReader reader, XMLStreamBuffer mark) { 1.642 + return new StreamHeader12(reader, mark); 1.643 + } 1.644 + }; 1.645 + 1.646 + static final StreamHeaderDecoder SOAP11StreamHeaderDecoder = new StreamHeaderDecoder() { 1.647 + @Override 1.648 + public Header decodeHeader(XMLStreamReader reader, XMLStreamBuffer mark) { 1.649 + return new StreamHeader11(reader, mark); 1.650 + } 1.651 + }; 1.652 + 1.653 + static private void readEnvelope(StreamMessage message) { 1.654 + if ( message.envelopeReader == null ) return; 1.655 + XMLStreamReader reader = message.envelopeReader; 1.656 + message.envelopeReader = null; 1.657 + SOAPVersion soapVersion = message.soapVersion; 1.658 + // Move to soap:Envelope and verify 1.659 + if(reader.getEventType()!=XMLStreamConstants.START_ELEMENT) 1.660 + XMLStreamReaderUtil.nextElementContent(reader); 1.661 + XMLStreamReaderUtil.verifyReaderState(reader,XMLStreamConstants.START_ELEMENT); 1.662 + if (SOAP_ENVELOPE.equals(reader.getLocalName()) && !soapVersion.nsUri.equals(reader.getNamespaceURI())) { 1.663 + throw new VersionMismatchException(soapVersion, soapVersion.nsUri, reader.getNamespaceURI()); 1.664 + } 1.665 + XMLStreamReaderUtil.verifyTag(reader, soapVersion.nsUri, SOAP_ENVELOPE); 1.666 + 1.667 + TagInfoset envelopeTag = new TagInfoset(reader); 1.668 + 1.669 + // Collect namespaces on soap:Envelope 1.670 + Map<String,String> namespaces = new HashMap<String,String>(); 1.671 + for(int i=0; i< reader.getNamespaceCount();i++){ 1.672 + namespaces.put(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); 1.673 + } 1.674 + 1.675 + // Move to next element 1.676 + XMLStreamReaderUtil.nextElementContent(reader); 1.677 + XMLStreamReaderUtil.verifyReaderState(reader, 1.678 + javax.xml.stream.XMLStreamConstants.START_ELEMENT); 1.679 + 1.680 + HeaderList headers = null; 1.681 + TagInfoset headerTag = null; 1.682 + 1.683 + if (reader.getLocalName().equals(SOAP_HEADER) 1.684 + && reader.getNamespaceURI().equals(soapVersion.nsUri)) { 1.685 + headerTag = new TagInfoset(reader); 1.686 + 1.687 + // Collect namespaces on soap:Header 1.688 + for(int i=0; i< reader.getNamespaceCount();i++){ 1.689 + namespaces.put(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); 1.690 + } 1.691 + // skip <soap:Header> 1.692 + XMLStreamReaderUtil.nextElementContent(reader); 1.693 + 1.694 + // If SOAP header blocks are present (i.e. not <soap:Header/>) 1.695 + if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) { 1.696 + headers = new HeaderList(soapVersion); 1.697 + 1.698 + try { 1.699 + // Cache SOAP header blocks 1.700 + StreamHeaderDecoder headerDecoder = SOAPVersion.SOAP_11.equals(soapVersion) ? SOAP11StreamHeaderDecoder : SOAP12StreamHeaderDecoder; 1.701 + cacheHeaders(reader, namespaces, headers, headerDecoder); 1.702 + } catch (XMLStreamException e) { 1.703 + // TODO need to throw more meaningful exception 1.704 + throw new WebServiceException(e); 1.705 + } 1.706 + } 1.707 + 1.708 + // Move to soap:Body 1.709 + XMLStreamReaderUtil.nextElementContent(reader); 1.710 + } 1.711 + 1.712 + // Verify that <soap:Body> is present 1.713 + XMLStreamReaderUtil.verifyTag(reader, soapVersion.nsUri, SOAP_BODY); 1.714 + TagInfoset bodyTag = new TagInfoset(reader); 1.715 + 1.716 + String bodyPrologue = XMLStreamReaderUtil.nextWhiteSpaceContent(reader); 1.717 + message.init(envelopeTag,headerTag,message.attachmentSet,headers,bodyPrologue,bodyTag,null,reader,soapVersion); 1.718 + // when there's no payload, 1.719 + // it's tempting to use EmptyMessageImpl, but it doesn't preserve the infoset 1.720 + // of <envelope>,<header>, and <body>, so we need to stick to StreamMessage. 1.721 + } 1.722 + 1.723 + 1.724 + private static XMLStreamBuffer cacheHeaders(XMLStreamReader reader, 1.725 + Map<String, String> namespaces, HeaderList headers, 1.726 + StreamHeaderDecoder headerDecoder) throws XMLStreamException { 1.727 + MutableXMLStreamBuffer buffer = createXMLStreamBuffer(); 1.728 + StreamReaderBufferCreator creator = new StreamReaderBufferCreator(); 1.729 + creator.setXMLStreamBuffer(buffer); 1.730 + 1.731 + // Reader is positioned at the first header block 1.732 + while(reader.getEventType() == javax.xml.stream.XMLStreamConstants.START_ELEMENT) { 1.733 + Map<String,String> headerBlockNamespaces = namespaces; 1.734 + 1.735 + // Collect namespaces on SOAP header block 1.736 + if (reader.getNamespaceCount() > 0) { 1.737 + headerBlockNamespaces = new HashMap<String,String>(namespaces); 1.738 + for (int i = 0; i < reader.getNamespaceCount(); i++) { 1.739 + headerBlockNamespaces.put(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); 1.740 + } 1.741 + } 1.742 + 1.743 + // Mark 1.744 + XMLStreamBuffer mark = new XMLStreamBufferMark(headerBlockNamespaces, creator); 1.745 + // Create Header 1.746 + headers.add(headerDecoder.decodeHeader(reader, mark)); 1.747 + 1.748 + 1.749 + // Cache the header block 1.750 + // After caching Reader will be positioned at next header block or 1.751 + // the end of the </soap:header> 1.752 + creator.createElementFragment(reader, false); 1.753 + if (reader.getEventType() != XMLStreamConstants.START_ELEMENT && 1.754 + reader.getEventType() != XMLStreamConstants.END_ELEMENT) { 1.755 + XMLStreamReaderUtil.nextElementContent(reader); 1.756 + } 1.757 + } 1.758 + 1.759 + return buffer; 1.760 + } 1.761 + 1.762 + private static MutableXMLStreamBuffer createXMLStreamBuffer() { 1.763 + // TODO: Decode should own one MutableXMLStreamBuffer for reuse 1.764 + // since it is more efficient. ISSUE: possible issue with 1.765 + // lifetime of information in the buffer if accessed beyond 1.766 + // the pipe line. 1.767 + return new MutableXMLStreamBuffer(); 1.768 + } 1.769 +}