aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.api.message.saaj; aoqi@0: aoqi@0: import java.util.Iterator; aoqi@0: aoqi@0: import javax.xml.soap.AttachmentPart; aoqi@0: import javax.xml.soap.MessageFactory; aoqi@0: import javax.xml.soap.SAAJMetaFactory; aoqi@0: import javax.xml.soap.SOAPException; aoqi@0: import javax.xml.soap.SOAPFactory; aoqi@0: import javax.xml.soap.SOAPMessage; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.marshaller.SAX2DOMEx; aoqi@0: import com.sun.xml.internal.ws.api.SOAPVersion; aoqi@0: import com.sun.xml.internal.ws.api.message.Attachment; aoqi@0: import com.sun.xml.internal.ws.api.message.AttachmentEx; aoqi@0: import com.sun.xml.internal.ws.api.message.Message; aoqi@0: import com.sun.xml.internal.ws.api.message.Packet; aoqi@0: import com.sun.xml.internal.ws.message.saaj.SAAJMessage; aoqi@0: import com.sun.xml.internal.ws.util.ServiceFinder; aoqi@0: import com.sun.xml.internal.ws.util.xml.XmlUtil; aoqi@0: aoqi@0: /** aoqi@0: * Factory SPI for SAAJ implementations aoqi@0: * aoqi@0: * @since 2.2.6 aoqi@0: */ aoqi@0: public class SAAJFactory { aoqi@0: private static final SAAJFactory instance = new SAAJFactory(); aoqi@0: aoqi@0: /** aoqi@0: * Creates a new MessageFactory object that is an instance aoqi@0: * of the specified implementation. May be a dynamic message factory, aoqi@0: * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic aoqi@0: * message factory creates messages based on the MIME headers specified aoqi@0: * as arguments to the createMessage method. aoqi@0: * aoqi@0: * This method uses the SAAJMetaFactory to locate the implementation class aoqi@0: * and create the MessageFactory instance. aoqi@0: * aoqi@0: * @return a new instance of a MessageFactory aoqi@0: * aoqi@0: * @param protocol a string constant representing the class of the aoqi@0: * specified message factory implementation. May be aoqi@0: * either DYNAMIC_SOAP_PROTOCOL, aoqi@0: * DEFAULT_SOAP_PROTOCOL (which is the same aoqi@0: * as) SOAP_1_1_PROTOCOL, or aoqi@0: * SOAP_1_2_PROTOCOL. aoqi@0: * aoqi@0: * @exception SOAPException if there was an error in creating the aoqi@0: * specified implementation of MessageFactory. aoqi@0: * @see SAAJMetaFactory aoqi@0: */ aoqi@0: public static MessageFactory getMessageFactory(String protocol) throws SOAPException { aoqi@0: for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { aoqi@0: MessageFactory mf = s.createMessageFactory(protocol); aoqi@0: if (mf != null) aoqi@0: return mf; aoqi@0: } aoqi@0: aoqi@0: return instance.createMessageFactory(protocol); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new SOAPFactory object that is an instance of aoqi@0: * the specified implementation, this method uses the SAAJMetaFactory to aoqi@0: * locate the implementation class and create the SOAPFactory instance. aoqi@0: * aoqi@0: * @return a new instance of a SOAPFactory aoqi@0: * aoqi@0: * @param protocol a string constant representing the protocol of the aoqi@0: * specified SOAP factory implementation. May be aoqi@0: * either DYNAMIC_SOAP_PROTOCOL, aoqi@0: * DEFAULT_SOAP_PROTOCOL (which is the same aoqi@0: * as) SOAP_1_1_PROTOCOL, or aoqi@0: * SOAP_1_2_PROTOCOL. aoqi@0: * aoqi@0: * @exception SOAPException if there was an error creating the aoqi@0: * specified SOAPFactory aoqi@0: * @see SAAJMetaFactory aoqi@0: */ aoqi@0: public static SOAPFactory getSOAPFactory(String protocol) throws SOAPException { aoqi@0: for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { aoqi@0: SOAPFactory sf = s.createSOAPFactory(protocol); aoqi@0: if (sf != null) aoqi@0: return sf; aoqi@0: } aoqi@0: aoqi@0: return instance.createSOAPFactory(protocol); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates Message from SOAPMessage aoqi@0: * @param saaj SOAPMessage aoqi@0: * @return created Message aoqi@0: */ aoqi@0: public static Message create(SOAPMessage saaj) { aoqi@0: for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { aoqi@0: Message m = s.createMessage(saaj); aoqi@0: if (m != null) aoqi@0: return m; aoqi@0: } aoqi@0: aoqi@0: return instance.createMessage(saaj); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads Message as SOAPMessage. After this call message is consumed. aoqi@0: * @param soapVersion SOAP version aoqi@0: * @param message Message aoqi@0: * @return Created SOAPMessage aoqi@0: * @throws SOAPException if SAAJ processing fails aoqi@0: */ aoqi@0: public static SOAPMessage read(SOAPVersion soapVersion, Message message) throws SOAPException { aoqi@0: for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { aoqi@0: SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message); aoqi@0: if (msg != null) aoqi@0: return msg; aoqi@0: } aoqi@0: aoqi@0: return instance.readAsSOAPMessage(soapVersion, message); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads Message as SOAPMessage. After this call message is consumed. aoqi@0: * @param soapVersion SOAP version aoqi@0: * @param message Message aoqi@0: * @param packet The packet that owns the Message aoqi@0: * @return Created SOAPMessage aoqi@0: * @throws SOAPException if SAAJ processing fails aoqi@0: */ aoqi@0: public static SOAPMessage read(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException { aoqi@0: for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { aoqi@0: SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message, packet); aoqi@0: if (msg != null) aoqi@0: return msg; aoqi@0: } aoqi@0: aoqi@0: return instance.readAsSOAPMessage(soapVersion, message, packet); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads the message within the Packet to a SAAJMessage. After this call message is consumed. aoqi@0: * @param packet Packet aoqi@0: * @return Created SAAJPMessage aoqi@0: * @throws SOAPException if SAAJ processing fails aoqi@0: */ aoqi@0: public static SAAJMessage read(Packet packet) throws SOAPException { aoqi@0: // Use the Component from the Packet if it exists. Note the logic aoqi@0: // in the ServiceFinder is such that find(Class) is not equivalent aoqi@0: // to find (Class, null), so the ternary operator is needed. aoqi@0: ServiceFinder factories = (packet.component != null ? aoqi@0: ServiceFinder.find(SAAJFactory.class, packet.component) : aoqi@0: ServiceFinder.find(SAAJFactory.class)); aoqi@0: for (SAAJFactory s : factories) { aoqi@0: SAAJMessage msg = s.readAsSAAJ(packet); aoqi@0: if (msg != null) return msg; aoqi@0: } aoqi@0: return instance.readAsSAAJ(packet); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads the message within the Packet to a SAAJMessage. After this call message is consumed. aoqi@0: * @param packet Packet aoqi@0: * @return Created SAAJPMessage aoqi@0: * @throws SOAPException if SAAJ processing fails aoqi@0: */ aoqi@0: public SAAJMessage readAsSAAJ(Packet packet) throws SOAPException { aoqi@0: SOAPVersion v = packet.getMessage().getSOAPVersion(); aoqi@0: SOAPMessage msg = readAsSOAPMessage(v, packet.getMessage()); aoqi@0: return new SAAJMessage(msg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new MessageFactory object that is an instance aoqi@0: * of the specified implementation. May be a dynamic message factory, aoqi@0: * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic aoqi@0: * message factory creates messages based on the MIME headers specified aoqi@0: * as arguments to the createMessage method. aoqi@0: * aoqi@0: * This method uses the SAAJMetaFactory to locate the implementation class aoqi@0: * and create the MessageFactory instance. aoqi@0: * aoqi@0: * @return a new instance of a MessageFactory aoqi@0: * aoqi@0: * @param protocol a string constant representing the class of the aoqi@0: * specified message factory implementation. May be aoqi@0: * either DYNAMIC_SOAP_PROTOCOL, aoqi@0: * DEFAULT_SOAP_PROTOCOL (which is the same aoqi@0: * as) SOAP_1_1_PROTOCOL, or aoqi@0: * SOAP_1_2_PROTOCOL. aoqi@0: * aoqi@0: * @exception SOAPException if there was an error in creating the aoqi@0: * specified implementation of MessageFactory. aoqi@0: * @see SAAJMetaFactory aoqi@0: */ aoqi@0: public MessageFactory createMessageFactory(String protocol) throws SOAPException { aoqi@0: return MessageFactory.newInstance(protocol); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new SOAPFactory object that is an instance of aoqi@0: * the specified implementation, this method uses the SAAJMetaFactory to aoqi@0: * locate the implementation class and create the SOAPFactory instance. aoqi@0: * aoqi@0: * @return a new instance of a SOAPFactory aoqi@0: * aoqi@0: * @param protocol a string constant representing the protocol of the aoqi@0: * specified SOAP factory implementation. May be aoqi@0: * either DYNAMIC_SOAP_PROTOCOL, aoqi@0: * DEFAULT_SOAP_PROTOCOL (which is the same aoqi@0: * as) SOAP_1_1_PROTOCOL, or aoqi@0: * SOAP_1_2_PROTOCOL. aoqi@0: * aoqi@0: * @exception SOAPException if there was an error creating the aoqi@0: * specified SOAPFactory aoqi@0: * @see SAAJMetaFactory aoqi@0: */ aoqi@0: public SOAPFactory createSOAPFactory(String protocol) throws SOAPException { aoqi@0: return SOAPFactory.newInstance(protocol); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates Message from SOAPMessage aoqi@0: * @param saaj SOAPMessage aoqi@0: * @return created Message aoqi@0: */ aoqi@0: public Message createMessage(SOAPMessage saaj) { aoqi@0: return new SAAJMessage(saaj); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads Message as SOAPMessage. After this call message is consumed. aoqi@0: * @param soapVersion SOAP version aoqi@0: * @param message Message aoqi@0: * @return Created SOAPMessage aoqi@0: * @throws SOAPException if SAAJ processing fails aoqi@0: */ aoqi@0: public SOAPMessage readAsSOAPMessage(final SOAPVersion soapVersion, final Message message) throws SOAPException { aoqi@0: SOAPMessage msg = soapVersion.getMessageFactory().createMessage(); aoqi@0: SaajStaxWriter writer = new SaajStaxWriter(msg); aoqi@0: try { aoqi@0: message.writeTo(writer); aoqi@0: } catch (XMLStreamException e) { aoqi@0: throw (e.getCause() instanceof SOAPException) ? (SOAPException) e.getCause() : new SOAPException(e); aoqi@0: } aoqi@0: msg = writer.getSOAPMessage(); aoqi@0: addAttachmentsToSOAPMessage(msg, message); aoqi@0: if (msg.saveRequired()) aoqi@0: msg.saveChanges(); aoqi@0: return msg; aoqi@0: } aoqi@0: aoqi@0: public SOAPMessage readAsSOAPMessageSax2Dom(final SOAPVersion soapVersion, final Message message) throws SOAPException { aoqi@0: SOAPMessage msg = soapVersion.getMessageFactory().createMessage(); aoqi@0: SAX2DOMEx s2d = new SAX2DOMEx(msg.getSOAPPart()); aoqi@0: try { aoqi@0: message.writeTo(s2d, XmlUtil.DRACONIAN_ERROR_HANDLER); aoqi@0: } catch (SAXException e) { aoqi@0: throw new SOAPException(e); aoqi@0: } aoqi@0: addAttachmentsToSOAPMessage(msg, message); aoqi@0: if (msg.saveRequired()) aoqi@0: msg.saveChanges(); aoqi@0: return msg; aoqi@0: } aoqi@0: aoqi@0: static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) { aoqi@0: for(Attachment att : message.getAttachments()) { aoqi@0: AttachmentPart part = msg.createAttachmentPart(); aoqi@0: part.setDataHandler(att.asDataHandler()); aoqi@0: aoqi@0: // Be safe and avoid double angle-brackets. aoqi@0: String cid = att.getContentId(); aoqi@0: if (cid != null) { aoqi@0: if (cid.startsWith("<") && cid.endsWith(">")) aoqi@0: part.setContentId(cid); aoqi@0: else aoqi@0: part.setContentId('<' + cid + '>'); aoqi@0: } aoqi@0: aoqi@0: // Add any MIME headers beside Content-ID, which is already aoqi@0: // accounted for above, and Content-Type, which is provided aoqi@0: // by the DataHandler above. aoqi@0: if (att instanceof AttachmentEx) { aoqi@0: AttachmentEx ax = (AttachmentEx) att; aoqi@0: Iterator imh = ax.getMimeHeaders(); aoqi@0: while (imh.hasNext()) { aoqi@0: AttachmentEx.MimeHeader ame = imh.next(); aoqi@0: if ((!"Content-ID".equals(ame.getName())) aoqi@0: && (!"Content-Type".equals(ame.getName()))) aoqi@0: part.addMimeHeader(ame.getName(), ame.getValue()); aoqi@0: } aoqi@0: } aoqi@0: msg.addAttachmentPart(part); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads Message as SOAPMessage. After this call message is consumed. aoqi@0: * The implementation in this class simply calls readAsSOAPMessage(SOAPVersion, Message), aoqi@0: * and ignores the other parameters aoqi@0: * Subclasses can override and choose to base SOAPMessage creation on Packet properties if needed aoqi@0: * @param soapVersion SOAP version aoqi@0: * @param message Message aoqi@0: * @return Created SOAPMessage aoqi@0: * @throws SOAPException if SAAJ processing fails aoqi@0: */ aoqi@0: public SOAPMessage readAsSOAPMessage(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException { aoqi@0: return readAsSOAPMessage(soapVersion, message); aoqi@0: } aoqi@0: }