001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.message.impl;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
023: import org.apache.axiom.soap.SOAPEnvelope;
024: import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
025: import org.apache.axis2.jaxws.ExceptionFactory;
026: import org.apache.axis2.jaxws.message.Block;
027: import org.apache.axis2.jaxws.message.Message;
028: import org.apache.axis2.jaxws.message.Protocol;
029: import org.apache.axis2.jaxws.message.databinding.SOAPEnvelopeBlock;
030: import org.apache.axis2.jaxws.message.factory.MessageFactory;
031: import org.apache.axis2.jaxws.message.util.MessageUtils;
032:
033: import javax.xml.soap.AttachmentPart;
034: import javax.xml.soap.SOAPMessage;
035: import javax.xml.stream.XMLStreamException;
036: import javax.xml.stream.XMLStreamReader;
037: import javax.xml.ws.WebServiceException;
038: import java.util.Iterator;
039:
040: /** MessageFactoryImpl */
041: public class MessageFactoryImpl implements MessageFactory {
042:
043: /** Default Constructor required for Factory */
044: public MessageFactoryImpl() {
045: super ();
046: }
047:
048: /* (non-Javadoc)
049: * @see org.apache.axis2.jaxws.message.factory.MessageFactory#createFrom(javax.xml.stream.XMLStreamReader)
050: */
051: public Message createFrom(XMLStreamReader reader, Protocol protocol)
052: throws XMLStreamException, WebServiceException {
053: StAXOMBuilder builder;
054: if (protocol == Protocol.rest) {
055: // Build a normal OM tree
056: builder = new StAXOMBuilder(reader);
057: } else {
058: // Build a SOAP OM tree
059: builder = new StAXSOAPModelBuilder(reader, null); // Pass null as the version to trigger autodetection
060: }
061: OMElement omElement = builder.getDocumentElement();
062: return createFrom(omElement, protocol);
063: }
064:
065: /* (non-Javadoc)
066: * @see org.apache.axis2.jaxws.message.MessageFactory#createFrom(org.apache.axiom.om.OMElement)
067: */
068: public Message createFrom(OMElement omElement, Protocol protocol)
069: throws XMLStreamException, WebServiceException {
070: return new MessageImpl(omElement, protocol);
071: }
072:
073: /* (non-Javadoc)
074: * @see org.apache.axis2.jaxws.message.MessageFactory#create(org.apache.axis2.jaxws.message.Protocol)
075: */
076: public Message create(Protocol protocol) throws XMLStreamException,
077: WebServiceException {
078: return new MessageImpl(protocol);
079: }
080:
081: /* (non-Javadoc)
082: * @see org.apache.axis2.jaxws.message.factory.MessageFactory#createFrom(javax.xml.soap.SOAPMessage)
083: */
084: public Message createFrom(SOAPMessage message)
085: throws XMLStreamException, WebServiceException {
086: try {
087: // Create a Message with an XMLPart from the SOAPEnvelope
088: Message m = new MessageImpl(message.getSOAPPart()
089: .getEnvelope());
090: if (message.countAttachments() > 0) {
091: Iterator it = message.getAttachments();
092: m.setDoingSWA(true);
093: while (it.hasNext()) {
094: AttachmentPart ap = (AttachmentPart) it.next();
095: m.addDataHandler(ap.getDataHandler(), ap
096: .getContentId());
097: }
098: }
099: return m;
100: } catch (Exception e) {
101: throw ExceptionFactory.makeWebServiceException(e);
102: }
103: }
104:
105: /* (non-Javadoc)
106: * @see org.apache.axis2.jaxws.message.factory.MessageFactory#createFrom(org.apache.axis2.jaxws.message.Block, java.lang.Object)
107: */
108: public Message createFrom(Block block, Object context,
109: Protocol protocol) throws XMLStreamException,
110: WebServiceException {
111:
112: // Small optimization to quickly consider the SOAPEnvelope case
113: if (block instanceof SOAPEnvelopeBlock) {
114: return new MessageImpl((SOAPEnvelope) block
115: .getBusinessObject(true), protocol);
116: }
117: return createFrom(block.getXMLStreamReader(true), protocol);
118: }
119:
120: }
|