1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import javax.xml.soap.AttachmentPart;
25import javax.xml.soap.MessageFactory;
26import javax.xml.soap.Name;
27import javax.xml.soap.SOAPEnvelope;
28import javax.xml.soap.SOAPException;
29import javax.xml.soap.SOAPMessage;
30import javax.xml.transform.stream.StreamSource;
31import java.io.ByteArrayInputStream;
32import java.io.ByteArrayOutputStream;
33import java.io.File;
34import java.io.FileInputStream;
35import java.io.FileOutputStream;
36import java.io.IOException;
37import java.util.Iterator;
38
39/*
40 * @test
41 * @modules java.xml.ws
42 * @run main XmlTest
43 * @summary tests JAF DataHandler can be instantiated; test serialize and
44 *   deserialize SOAP message containing xml attachment
45 */
46public class XmlTest {
47
48    public void test() throws Exception {
49
50        File file = new File("message.xml");
51        file.deleteOnExit();
52
53        MessageFactory mf = MessageFactory.newInstance();
54        SOAPMessage msg = createMessage(mf);
55
56        // Save the soap message to file
57        try (FileOutputStream sentFile = new FileOutputStream(file)) {
58            msg.writeTo(sentFile);
59        }
60
61        // See if we get the image object back
62        try (FileInputStream fin = new FileInputStream(file)) {
63            SOAPMessage newMsg = mf.createMessage(msg.getMimeHeaders(), fin);
64
65            newMsg.writeTo(new ByteArrayOutputStream());
66
67            Iterator<?> i = newMsg.getAttachments();
68            while (i.hasNext()) {
69                AttachmentPart att = (AttachmentPart) i.next();
70                Object obj = att.getContent();
71                if (!(obj instanceof StreamSource)) {
72                    fail("Got incorrect attachment type [" + obj.getClass() + "], " +
73                         "expected [javax.xml.transform.stream.StreamSource]");
74                }
75            }
76        }
77
78    }
79
80    private SOAPMessage createMessage(MessageFactory mf) throws SOAPException, IOException {
81        SOAPMessage msg = mf.createMessage();
82        SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
83        Name name = envelope.createName("hello", "ex", "http://example.com");
84        envelope.getBody().addChildElement(name).addTextNode("THERE!");
85
86        String s = "<root><hello>THERE!</hello></root>";
87
88        AttachmentPart ap = msg.createAttachmentPart(
89                new StreamSource(new ByteArrayInputStream(s.getBytes())),
90                "text/xml"
91        );
92        msg.addAttachmentPart(ap);
93        msg.saveChanges();
94
95        return msg;
96    }
97
98    private void fail(String s) {
99        throw new RuntimeException(s);
100    }
101
102    public static void main(String[] args) throws Exception {
103        new XmlTest().test();
104    }
105
106}
107