src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 384
8f2986ff0235
     1.1 --- a/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java	Thu Apr 04 19:05:24 2013 -0700
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java	Tue Apr 09 14:51:13 2013 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -33,6 +33,7 @@
    1.11  import javax.xml.soap.SOAPException;
    1.12  import javax.xml.soap.SOAPFactory;
    1.13  import javax.xml.soap.SOAPMessage;
    1.14 +import javax.xml.stream.XMLStreamException;
    1.15  
    1.16  import org.xml.sax.SAXException;
    1.17  
    1.18 @@ -41,6 +42,7 @@
    1.19  import com.sun.xml.internal.ws.api.message.Attachment;
    1.20  import com.sun.xml.internal.ws.api.message.AttachmentEx;
    1.21  import com.sun.xml.internal.ws.api.message.Message;
    1.22 +import com.sun.xml.internal.ws.api.message.Packet;
    1.23  import com.sun.xml.internal.ws.message.saaj.SAAJMessage;
    1.24  import com.sun.xml.internal.ws.util.ServiceFinder;
    1.25  import com.sun.xml.internal.ws.util.xml.XmlUtil;
    1.26 @@ -76,14 +78,14 @@
    1.27       *            specified implementation of  <code>MessageFactory</code>.
    1.28       * @see SAAJMetaFactory
    1.29       */
    1.30 -        public static MessageFactory getMessageFactory(String saajFactoryString) throws SOAPException {
    1.31 +        public static MessageFactory getMessageFactory(String protocol) throws SOAPException {
    1.32                  for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
    1.33 -                        MessageFactory mf = s.createMessageFactory(saajFactoryString);
    1.34 +                        MessageFactory mf = s.createMessageFactory(protocol);
    1.35                          if (mf != null)
    1.36                                  return mf;
    1.37                  }
    1.38  
    1.39 -        return instance.createMessageFactory(saajFactoryString);
    1.40 +        return instance.createMessageFactory(protocol);
    1.41          }
    1.42  
    1.43      /**
    1.44 @@ -104,14 +106,14 @@
    1.45       *            specified <code>SOAPFactory</code>
    1.46       * @see SAAJMetaFactory
    1.47       */
    1.48 -        public static SOAPFactory getSOAPFactory(String saajFactoryString) throws SOAPException {
    1.49 +        public static SOAPFactory getSOAPFactory(String protocol) throws SOAPException {
    1.50                  for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
    1.51 -                        SOAPFactory sf = s.createSOAPFactory(saajFactoryString);
    1.52 +                        SOAPFactory sf = s.createSOAPFactory(protocol);
    1.53                          if (sf != null)
    1.54                                  return sf;
    1.55                  }
    1.56  
    1.57 -        return instance.createSOAPFactory(saajFactoryString);
    1.58 +        return instance.createSOAPFactory(protocol);
    1.59          }
    1.60  
    1.61          /**
    1.62 @@ -146,6 +148,50 @@
    1.63          return instance.readAsSOAPMessage(soapVersion, message);
    1.64          }
    1.65  
    1.66 +        /**
    1.67 +     * Reads Message as SOAPMessage.  After this call message is consumed.
    1.68 +     * @param soapVersion SOAP version
    1.69 +     * @param message Message
    1.70 +     * @param packet The packet that owns the Message
    1.71 +     * @return Created SOAPMessage
    1.72 +     * @throws SOAPException if SAAJ processing fails
    1.73 +     */
    1.74 +    public static SOAPMessage read(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException {
    1.75 +        for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
    1.76 +            SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message, packet);
    1.77 +            if (msg != null)
    1.78 +                return msg;
    1.79 +        }
    1.80 +
    1.81 +        return instance.readAsSOAPMessage(soapVersion, message, packet);
    1.82 +    }
    1.83 +
    1.84 +    /**
    1.85 +     * Reads the message within the Packet to a SAAJMessage.  After this call message is consumed.
    1.86 +     * @param packet Packet
    1.87 +     * @return Created SAAJPMessage
    1.88 +     * @throws SOAPException if SAAJ processing fails
    1.89 +     */
    1.90 +    public static SAAJMessage read(Packet packet) throws SOAPException {
    1.91 +        for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
    1.92 +            SAAJMessage msg = s.readAsSAAJ(packet);
    1.93 +            if (msg != null) return msg;
    1.94 +        }
    1.95 +        return instance.readAsSAAJ(packet);
    1.96 +    }
    1.97 +
    1.98 +    /**
    1.99 +     * Reads the message within the Packet to a SAAJMessage.  After this call message is consumed.
   1.100 +     * @param packet Packet
   1.101 +     * @return Created SAAJPMessage
   1.102 +     * @throws SOAPException if SAAJ processing fails
   1.103 +     */
   1.104 +    public SAAJMessage readAsSAAJ(Packet packet) throws SOAPException {
   1.105 +        SOAPVersion v = packet.getMessage().getSOAPVersion();
   1.106 +        SOAPMessage msg = readAsSOAPMessage(v, packet.getMessage());
   1.107 +        return new SAAJMessage(msg);
   1.108 +    }
   1.109 +
   1.110      /**
   1.111       * Creates a new <code>MessageFactory</code> object that is an instance
   1.112       * of the specified implementation.  May be a dynamic message factory,
   1.113 @@ -169,8 +215,8 @@
   1.114       *            specified implementation of  <code>MessageFactory</code>.
   1.115       * @see SAAJMetaFactory
   1.116       */
   1.117 -        public MessageFactory createMessageFactory(String saajFactoryString) throws SOAPException {
   1.118 -                return MessageFactory.newInstance(saajFactoryString);
   1.119 +        public MessageFactory createMessageFactory(String protocol) throws SOAPException {
   1.120 +                return MessageFactory.newInstance(protocol);
   1.121          }
   1.122  
   1.123      /**
   1.124 @@ -191,8 +237,8 @@
   1.125       *            specified <code>SOAPFactory</code>
   1.126       * @see SAAJMetaFactory
   1.127       */
   1.128 -        public SOAPFactory createSOAPFactory(String saajFactoryString) throws SOAPException {
   1.129 -                return SOAPFactory.newInstance(saajFactoryString);
   1.130 +        public SOAPFactory createSOAPFactory(String protocol) throws SOAPException {
   1.131 +                return SOAPFactory.newInstance(protocol);
   1.132          }
   1.133  
   1.134          /**
   1.135 @@ -211,16 +257,36 @@
   1.136           * @return Created SOAPMessage
   1.137           * @throws SOAPException if SAAJ processing fails
   1.138           */
   1.139 -        public SOAPMessage readAsSOAPMessage(SOAPVersion soapVersion, Message message) throws SOAPException {
   1.140 +        public SOAPMessage readAsSOAPMessage(final SOAPVersion soapVersion, final Message message) throws SOAPException {
   1.141          SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
   1.142 +        SaajStaxWriter writer = new SaajStaxWriter(msg);
   1.143 +        try {
   1.144 +            message.writeTo(writer);
   1.145 +        } catch (XMLStreamException e) {
   1.146 +            throw (e.getCause() instanceof SOAPException) ? (SOAPException) e.getCause() : new SOAPException(e);
   1.147 +        }
   1.148 +        msg = writer.getSOAPMessage();
   1.149 +        addAttachmentsToSOAPMessage(msg, message);
   1.150 +        if (msg.saveRequired())
   1.151 +                msg.saveChanges();
   1.152 +        return msg;
   1.153 +        }
   1.154  
   1.155 +    public SOAPMessage readAsSOAPMessageSax2Dom(final SOAPVersion soapVersion, final Message message) throws SOAPException {
   1.156 +        SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
   1.157          SAX2DOMEx s2d = new SAX2DOMEx(msg.getSOAPPart());
   1.158          try {
   1.159              message.writeTo(s2d, XmlUtil.DRACONIAN_ERROR_HANDLER);
   1.160          } catch (SAXException e) {
   1.161              throw new SOAPException(e);
   1.162          }
   1.163 +        addAttachmentsToSOAPMessage(msg, message);
   1.164 +        if (msg.saveRequired())
   1.165 +            msg.saveChanges();
   1.166 +        return msg;
   1.167 +    }
   1.168  
   1.169 +        static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
   1.170          for(Attachment att : message.getAttachments()) {
   1.171              AttachmentPart part = msg.createAttachmentPart();
   1.172              part.setDataHandler(att.asDataHandler());
   1.173 @@ -249,9 +315,19 @@
   1.174              }
   1.175              msg.addAttachmentPart(part);
   1.176          }
   1.177 +    }
   1.178  
   1.179 -        if (msg.saveRequired())
   1.180 -                msg.saveChanges();
   1.181 -        return msg;
   1.182 +    /**
   1.183 +     * Reads Message as SOAPMessage.  After this call message is consumed.
   1.184 +     * The implementation in this class simply calls readAsSOAPMessage(SOAPVersion, Message),
   1.185 +     * and ignores the other parameters
   1.186 +     * Subclasses can override and choose to base SOAPMessage creation on Packet properties if needed
   1.187 +     * @param soapVersion SOAP version
   1.188 +     * @param message Message
   1.189 +     * @return Created SOAPMessage
   1.190 +     * @throws SOAPException if SAAJ processing fails
   1.191 +     */
   1.192 +        public SOAPMessage readAsSOAPMessage(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException {
   1.193 +            return readAsSOAPMessage(soapVersion, message);
   1.194          }
   1.195  }

mercurial