author | mchung |
Wed, 10 Aug 2016 15:51:25 -0700 | |
changeset 40261 | 86a49ba76f52 |
parent 37779 | 7c84df693837 |
permissions | -rw-r--r-- |
36511 | 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 |
||
24 |
import javax.xml.soap.AttachmentPart; |
|
25 |
import javax.xml.soap.MessageFactory; |
|
26 |
import javax.xml.soap.Name; |
|
27 |
import javax.xml.soap.SOAPEnvelope; |
|
28 |
import javax.xml.soap.SOAPException; |
|
29 |
import javax.xml.soap.SOAPMessage; |
|
30 |
import javax.xml.transform.stream.StreamSource; |
|
31 |
import java.io.ByteArrayInputStream; |
|
32 |
import java.io.ByteArrayOutputStream; |
|
33 |
import java.io.File; |
|
34 |
import java.io.FileInputStream; |
|
35 |
import java.io.FileOutputStream; |
|
36 |
import java.io.IOException; |
|
37 |
import java.util.Iterator; |
|
38 |
||
39 |
/* |
|
40 |
* @test |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
37779
diff
changeset
|
41 |
* @modules java.xml.ws |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
37779
diff
changeset
|
42 |
* @run main XmlTest |
36511 | 43 |
* @summary tests JAF DataHandler can be instantiated; test serialize and |
44 |
* deserialize SOAP message containing xml attachment |
|
45 |
*/ |
|
46 |
public 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 |
} |