aboutsummaryrefslogtreecommitdiff
path: root/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java
blob: a1bd1da3fc9e31acbd433134a33a85ce1af90078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.xml.internal.ws.api.message.saaj;

import java.util.Iterator;

import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SAAJMetaFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.stream.XMLStreamException;

import org.xml.sax.SAXException;

import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
import com.sun.xml.internal.ws.api.SOAPVersion;
import com.sun.xml.internal.ws.api.message.Attachment;
import com.sun.xml.internal.ws.api.message.AttachmentEx;
import com.sun.xml.internal.ws.api.message.Message;
import com.sun.xml.internal.ws.api.message.Packet;
import com.sun.xml.internal.ws.message.saaj.SAAJMessage;
import com.sun.xml.internal.ws.util.ServiceFinder;
import com.sun.xml.internal.ws.util.xml.XmlUtil;

/**
 * Factory SPI for SAAJ implementations
 *
 * @since 2.2.6
 */
public class SAAJFactory {
        private static final SAAJFactory instance = new SAAJFactory();

    /**
     * Creates a new <code>MessageFactory</code> object that is an instance
     * of the specified implementation.  May be a dynamic message factory,
     * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
     * message factory creates messages based on the MIME headers specified
     * as arguments to the <code>createMessage</code> method.
     *
     * This method uses the SAAJMetaFactory to locate the implementation class
     * and create the MessageFactory instance.
     *
     * @return a new instance of a <code>MessageFactory</code>
     *
     * @param protocol  a string constant representing the class of the
     *                   specified message factory implementation. May be
     *                   either <code>DYNAMIC_SOAP_PROTOCOL</code>,
     *                   <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
     *                   as) <code>SOAP_1_1_PROTOCOL</code>, or
     *                   <code>SOAP_1_2_PROTOCOL</code>.
     *
     * @exception SOAPException if there was an error in creating the
     *            specified implementation of  <code>MessageFactory</code>.
     * @see SAAJMetaFactory
     */
        public static MessageFactory getMessageFactory(String protocol) throws SOAPException {
                for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
                        MessageFactory mf = s.createMessageFactory(protocol);
                        if (mf != null)
                                return mf;
                }

        return instance.createMessageFactory(protocol);
        }

    /**
     * Creates a new <code>SOAPFactory</code> object that is an instance of
     * the specified implementation, this method uses the SAAJMetaFactory to
     * locate the implementation class and create the SOAPFactory instance.
     *
     * @return a new instance of a <code>SOAPFactory</code>
     *
     * @param protocol  a string constant representing the protocol of the
     *                   specified SOAP factory implementation. May be
     *                   either <code>DYNAMIC_SOAP_PROTOCOL</code>,
     *                   <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
     *                   as) <code>SOAP_1_1_PROTOCOL</code>, or
     *                   <code>SOAP_1_2_PROTOCOL</code>.
     *
     * @exception SOAPException if there was an error creating the
     *            specified <code>SOAPFactory</code>
     * @see SAAJMetaFactory
     */
        public static SOAPFactory getSOAPFactory(String protocol) throws SOAPException {
                for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
                        SOAPFactory sf = s.createSOAPFactory(protocol);
                        if (sf != null)
                                return sf;
                }

        return instance.createSOAPFactory(protocol);
        }

        /**
         * Creates Message from SOAPMessage
         * @param saaj SOAPMessage
         * @return created Message
         */
        public static Message create(SOAPMessage saaj) {
                for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
                        Message m = s.createMessage(saaj);
                        if (m != null)
                                return m;
                }

        return instance.createMessage(saaj);
        }

        /**
         * Reads Message as SOAPMessage.  After this call message is consumed.
         * @param soapVersion SOAP version
         * @param message Message
         * @return Created SOAPMessage
         * @throws SOAPException if SAAJ processing fails
         */
        public static SOAPMessage read(SOAPVersion soapVersion, Message message) throws SOAPException {
                for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
                        SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message);
                        if (msg != null)
                                return msg;
                }

        return instance.readAsSOAPMessage(soapVersion, message);
        }

        /**
     * Reads Message as SOAPMessage.  After this call message is consumed.
     * @param soapVersion SOAP version
     * @param message Message
     * @param packet The packet that owns the Message
     * @return Created SOAPMessage
     * @throws SOAPException if SAAJ processing fails
     */
    public static SOAPMessage read(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException {
        for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
            SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message, packet);
            if (msg != null)
                return msg;
        }

        return instance.readAsSOAPMessage(soapVersion, message, packet);
    }

    /**
     * Reads the message within the Packet to a SAAJMessage.  After this call message is consumed.
     * @param packet Packet
     * @return Created SAAJPMessage
     * @throws SOAPException if SAAJ processing fails
     */
    public static SAAJMessage read(Packet packet) throws SOAPException {
        // Use the Component from the Packet if it exists.  Note the logic
        // in the ServiceFinder is such that find(Class) is not equivalent
        // to find (Class, null), so the ternary operator is needed.
        ServiceFinder<SAAJFactory> factories = (packet.component != null ?
                ServiceFinder.find(SAAJFactory.class, packet.component) :
                ServiceFinder.find(SAAJFactory.class));
        for (SAAJFactory s : factories) {
            SAAJMessage msg = s.readAsSAAJ(packet);
            if (msg != null) return msg;
        }
        return instance.readAsSAAJ(packet);
    }

    /**
     * Reads the message within the Packet to a SAAJMessage.  After this call message is consumed.
     * @param packet Packet
     * @return Created SAAJPMessage
     * @throws SOAPException if SAAJ processing fails
     */
    public SAAJMessage readAsSAAJ(Packet packet) throws SOAPException {
        SOAPVersion v = packet.getMessage().getSOAPVersion();
        SOAPMessage msg = readAsSOAPMessage(v, packet.getMessage());
        return new SAAJMessage(msg);
    }

    /**
     * Creates a new <code>MessageFactory</code> object that is an instance
     * of the specified implementation.  May be a dynamic message factory,
     * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
     * message factory creates messages based on the MIME headers specified
     * as arguments to the <code>createMessage</code> method.
     *
     * This method uses the SAAJMetaFactory to locate the implementation class
     * and create the MessageFactory instance.
     *
     * @return a new instance of a <code>MessageFactory</code>
     *
     * @param protocol  a string constant representing the class of the
     *                   specified message factory implementation. May be
     *                   either <code>DYNAMIC_SOAP_PROTOCOL</code>,
     *                   <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
     *                   as) <code>SOAP_1_1_PROTOCOL</code>, or
     *                   <code>SOAP_1_2_PROTOCOL</code>.
     *
     * @exception SOAPException if there was an error in creating the
     *            specified implementation of  <code>MessageFactory</code>.
     * @see SAAJMetaFactory
     */
        public MessageFactory createMessageFactory(String protocol) throws SOAPException {
                return MessageFactory.newInstance(protocol);
        }

    /**
     * Creates a new <code>SOAPFactory</code> object that is an instance of
     * the specified implementation, this method uses the SAAJMetaFactory to
     * locate the implementation class and create the SOAPFactory instance.
     *
     * @return a new instance of a <code>SOAPFactory</code>
     *
     * @param protocol  a string constant representing the protocol of the
     *                   specified SOAP factory implementation. May be
     *                   either <code>DYNAMIC_SOAP_PROTOCOL</code>,
     *                   <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
     *                   as) <code>SOAP_1_1_PROTOCOL</code>, or
     *                   <code>SOAP_1_2_PROTOCOL</code>.
     *
     * @exception SOAPException if there was an error creating the
     *            specified <code>SOAPFactory</code>
     * @see SAAJMetaFactory
     */
        public SOAPFactory createSOAPFactory(String protocol) throws SOAPException {
                return SOAPFactory.newInstance(protocol);
        }

        /**
         * Creates Message from SOAPMessage
         * @param saaj SOAPMessage
         * @return created Message
         */
        public Message createMessage(SOAPMessage saaj) {
                return new SAAJMessage(saaj);
        }

        /**
         * Reads Message as SOAPMessage.  After this call message is consumed.
         * @param soapVersion SOAP version
         * @param message Message
         * @return Created SOAPMessage
         * @throws SOAPException if SAAJ processing fails
         */
        public SOAPMessage readAsSOAPMessage(final SOAPVersion soapVersion, final Message message) throws SOAPException {
        SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
        SaajStaxWriter writer = new SaajStaxWriter(msg);
        try {
            message.writeTo(writer);
        } catch (XMLStreamException e) {
            throw (e.getCause() instanceof SOAPException) ? (SOAPException) e.getCause() : new SOAPException(e);
        }
        msg = writer.getSOAPMessage();
        addAttachmentsToSOAPMessage(msg, message);
        if (msg.saveRequired())
                msg.saveChanges();
        return msg;
        }

    public SOAPMessage readAsSOAPMessageSax2Dom(final SOAPVersion soapVersion, final Message message) throws SOAPException {
        SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
        SAX2DOMEx s2d = new SAX2DOMEx(msg.getSOAPPart());
        try {
            message.writeTo(s2d, XmlUtil.DRACONIAN_ERROR_HANDLER);
        } catch (SAXException e) {
            throw new SOAPException(e);
        }
        addAttachmentsToSOAPMessage(msg, message);
        if (msg.saveRequired())
            msg.saveChanges();
        return msg;
    }

        static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
        for(Attachment att : message.getAttachments()) {
            AttachmentPart part = msg.createAttachmentPart();
            part.setDataHandler(att.asDataHandler());

            // Be safe and avoid double angle-brackets.
            String cid = att.getContentId();
            if (cid != null) {
                if (cid.startsWith("<") && cid.endsWith(">"))
                    part.setContentId(cid);
                else
                    part.setContentId('<' + cid + '>');
            }

            // Add any MIME headers beside Content-ID, which is already
            // accounted for above, and Content-Type, which is provided
            // by the DataHandler above.
            if (att instanceof AttachmentEx) {
                AttachmentEx ax = (AttachmentEx) att;
                Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
                while (imh.hasNext()) {
                    AttachmentEx.MimeHeader ame = imh.next();
                    if ((!"Content-ID".equals(ame.getName()))
                            && (!"Content-Type".equals(ame.getName())))
                        part.addMimeHeader(ame.getName(), ame.getValue());
                }
            }
            msg.addAttachmentPart(part);
        }
    }

    /**
     * Reads Message as SOAPMessage.  After this call message is consumed.
     * The implementation in this class simply calls readAsSOAPMessage(SOAPVersion, Message),
     * and ignores the other parameters
     * Subclasses can override and choose to base SOAPMessage creation on Packet properties if needed
     * @param soapVersion SOAP version
     * @param message Message
     * @return Created SOAPMessage
     * @throws SOAPException if SAAJ processing fails
     */
        public SOAPMessage readAsSOAPMessage(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException {
            return readAsSOAPMessage(soapVersion, message);
        }
}