001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.attachment;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Properties;
027:
028: import javax.activation.DataHandler;
029: import javax.mail.Session;
030: import javax.mail.internet.MimeBodyPart;
031: import javax.mail.internet.MimeMessage;
032: import javax.mail.internet.MimeMultipart;
033: import javax.mail.util.ByteArrayDataSource;
034:
035: import org.apache.cxf.helpers.IOUtils;
036: import org.apache.cxf.message.Attachment;
037: import org.apache.cxf.message.Message;
038: import org.apache.cxf.message.MessageImpl;
039: import org.junit.Assert;
040: import org.junit.Test;
041:
042: public class AttachmentSerializerTest extends Assert {
043:
044: @Test
045: public void testMessageWrite() throws Exception {
046: MessageImpl msg = new MessageImpl();
047:
048: Collection<Attachment> atts = new ArrayList<Attachment>();
049: AttachmentImpl a = new AttachmentImpl("test.xml");
050:
051: InputStream is = getClass().getResourceAsStream("my.wav");
052: ByteArrayDataSource ds = new ByteArrayDataSource(is,
053: "application/octet-stream");
054: a.setDataHandler(new DataHandler(ds));
055:
056: atts.add(a);
057:
058: msg.setAttachments(atts);
059:
060: // Set the SOAP content type
061: msg.put(Message.CONTENT_TYPE, "application/soap+xml");
062:
063: ByteArrayOutputStream out = new ByteArrayOutputStream();
064: msg.setContent(OutputStream.class, out);
065:
066: AttachmentSerializer serializer = new AttachmentSerializer(msg);
067:
068: serializer.writeProlog();
069:
070: String ct = (String) msg.get(Message.CONTENT_TYPE);
071: assertTrue(ct.indexOf("multipart/related;") == 0);
072: assertTrue(ct
073: .indexOf("start=\"<root.message@cxf.apache.org>\"") > -1);
074: assertTrue(ct.indexOf("start-info=\"application/soap+xml\"") > -1);
075: out.write("<soap:Body/>".getBytes());
076:
077: serializer.writeAttachments();
078:
079: out.flush();
080:
081: Session session = Session.getDefaultInstance(new Properties());
082: MimeMessage inMsg = new MimeMessage(session,
083: new ByteArrayInputStream(out.toByteArray()));
084: inMsg.addHeaderLine("Content-Type: " + ct);
085:
086: MimeMultipart multipart = (MimeMultipart) inMsg.getContent();
087:
088: MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(0);
089: assertEquals(
090: "application/xop+xml; charset=UTF-8; type=\"application/soap+xml; charset=UTF-8\"",
091: part.getHeader("Content-Type")[0]);
092: assertEquals("binary", part
093: .getHeader("Content-Transfer-Encoding")[0]);
094: assertEquals("<root.message@cxf.apache.org>", part
095: .getHeader("Content-ID")[0]);
096:
097: InputStream in = part.getDataHandler().getInputStream();
098: ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
099: IOUtils.copy(in, bodyOut);
100: out.close();
101: in.close();
102:
103: assertEquals("<soap:Body/>", bodyOut.toString());
104:
105: MimeBodyPart part2 = (MimeBodyPart) multipart.getBodyPart(1);
106: assertEquals("application/octet-stream", part2
107: .getHeader("Content-Type")[0]);
108: assertEquals("binary", part2
109: .getHeader("Content-Transfer-Encoding")[0]);
110: assertEquals("<test.xml>", part2.getHeader("Content-ID")[0]);
111:
112: }
113:
114: @Test
115: public void testMessageMTOM() throws Exception {
116: MessageImpl msg = new MessageImpl();
117:
118: Collection<Attachment> atts = new ArrayList<Attachment>();
119: AttachmentImpl a = new AttachmentImpl("test.xml");
120:
121: InputStream is = getClass().getResourceAsStream("my.wav");
122: ByteArrayDataSource ds = new ByteArrayDataSource(is,
123: "application/octet-stream");
124: a.setDataHandler(new DataHandler(ds));
125:
126: atts.add(a);
127:
128: msg.setAttachments(atts);
129:
130: // Set the SOAP content type
131: msg.put(Message.CONTENT_TYPE, "application/soap+xml");
132:
133: ByteArrayOutputStream out = new ByteArrayOutputStream();
134: msg.setContent(OutputStream.class, out);
135:
136: AttachmentSerializer serializer = new AttachmentSerializer(msg);
137:
138: serializer.writeProlog();
139:
140: String ct = (String) msg.get(Message.CONTENT_TYPE);
141: assertTrue(ct.indexOf("multipart/related;") == 0);
142: assertTrue(ct
143: .indexOf("start=\"<root.message@cxf.apache.org>\"") > -1);
144: assertTrue(ct.indexOf("start-info=\"application/soap+xml\"") > -1);
145:
146: out.write("<soap:Body/>".getBytes());
147:
148: serializer.writeAttachments();
149:
150: out.flush();
151:
152: Session session = Session.getDefaultInstance(new Properties());
153: MimeMessage inMsg = new MimeMessage(session,
154: new ByteArrayInputStream(out.toByteArray()));
155: inMsg.addHeaderLine("Content-Type: " + ct);
156:
157: MimeMultipart multipart = (MimeMultipart) inMsg.getContent();
158:
159: MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(0);
160: assertEquals(
161: "application/xop+xml; charset=UTF-8; type=\"application/soap+xml; charset=UTF-8\"",
162: part.getHeader("Content-Type")[0]);
163: assertEquals("binary", part
164: .getHeader("Content-Transfer-Encoding")[0]);
165: assertEquals("<root.message@cxf.apache.org>", part
166: .getHeader("Content-ID")[0]);
167:
168: InputStream in = part.getDataHandler().getInputStream();
169: ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
170: IOUtils.copy(in, bodyOut);
171: out.close();
172: in.close();
173:
174: assertEquals("<soap:Body/>", bodyOut.toString());
175:
176: MimeBodyPart part2 = (MimeBodyPart) multipart.getBodyPart(1);
177: assertEquals("application/octet-stream", part2
178: .getHeader("Content-Type")[0]);
179: assertEquals("binary", part2
180: .getHeader("Content-Transfer-Encoding")[0]);
181: assertEquals("<test.xml>", part2.getHeader("Content-ID")[0]);
182:
183: }
184: }
|