001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
022: *
023: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
024: *
025: * The contents of this file are subject to the terms of either the GNU
026: * General Public License Version 2 only ("GPL") or the Common Development
027: * and Distribution License("CDDL") (collectively, the "License"). You
028: * may not use this file except in compliance with the License. You can obtain
029: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
030: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
031: * language governing permissions and limitations under the License.
032: *
033: * When distributing the software, include this License Header Notice in each
034: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
035: * Sun designates this particular file as subject to the "Classpath" exception
036: * as provided by Sun in the GPL Version 2 section of the License file that
037: * accompanied this code. If applicable, add the following below the License
038: * Header, with the fields enclosed by brackets [] replaced by your own
039: * identifying information: "Portions Copyrighted [year]
040: * [name of copyright owner]"
041: *
042: * Contributor(s):
043: *
044: * If you wish your version of this file to be governed by only the CDDL or
045: * only the GPL Version 2, indicate your decision by adding "[Contributor]
046: * elects to include this software in this distribution under the [CDDL or GPL
047: * Version 2] license." If you don't indicate a single choice of license, a
048: * recipient has the option to distribute your version of this file under
049: * either the CDDL, the GPL Version 2 or to extend the choice of license to
050: * its licensees as provided above. However, if you add GPL Version 2 code
051: * and therefore, elected the GPL Version 2 license, then the option applies
052: * only if the new code is made subject to such option by the copyright
053: * holder.
054: */
055:
056: package mime;
057:
058: import java.io.*;
059: import java.net.URL;
060: import java.util.*;
061:
062: import javax.activation.DataHandler;
063: import javax.xml.soap.*;
064:
065: import junit.framework.TestCase;
066:
067: public class StartParameterTest extends TestCase {
068:
069: public StartParameterTest(String name) {
070: super (name);
071: }
072:
073: public void changeAndSaveMimeHeaders(SOAPMessage msg,
074: String fileName) throws IOException {
075:
076: FileOutputStream fos = new FileOutputStream(fileName);
077: ObjectOutputStream oos = new ObjectOutputStream(fos);
078:
079: Hashtable hashTable = new Hashtable();
080: MimeHeaders mimeHeaders = msg.getMimeHeaders();
081: Iterator iterator = mimeHeaders.getAllHeaders();
082:
083: while (iterator.hasNext()) {
084: MimeHeader mimeHeader = (MimeHeader) iterator.next();
085: if (mimeHeader.getName().equals("Content-Type"))
086: hashTable.put(mimeHeader.getName(), mimeHeader
087: .getValue()
088: + "; start=attachmentPart");
089: else
090: hashTable.put(mimeHeader.getName(), mimeHeader
091: .getValue());
092: }
093:
094: oos.writeObject(hashTable);
095: oos.flush();
096: oos.close();
097:
098: fos.flush();
099: fos.close();
100: }
101:
102: public SOAPMessage getModifiedMessage(String mimeHdrsFile,
103: String msgFile) throws Exception {
104: SOAPMessage message;
105:
106: MimeHeaders mimeHeaders = new MimeHeaders();
107: FileInputStream fis = new FileInputStream(msgFile);
108:
109: ObjectInputStream ois = new ObjectInputStream(
110: new FileInputStream(mimeHdrsFile));
111: Hashtable hashTable = (Hashtable) ois.readObject();
112: ois.close();
113:
114: if (hashTable.isEmpty())
115: fail("MimeHeaders Hashtable is empty");
116: else {
117: for (int i = 0; i < hashTable.size(); i++) {
118: Enumeration keys = hashTable.keys();
119: Enumeration values = hashTable.elements();
120: while (keys.hasMoreElements()
121: && values.hasMoreElements()) {
122: String name = (String) keys.nextElement();
123: String value = (String) values.nextElement();
124: mimeHeaders.addHeader(name, value);
125: }
126: }
127: }
128:
129: MessageFactory messageFactory = MessageFactory.newInstance();
130: message = messageFactory.createMessage(mimeHeaders, fis);
131:
132: message.saveChanges();
133:
134: return message;
135: }
136:
137: public void testStartParameter() throws Exception {
138: MessageFactory mf = MessageFactory.newInstance();
139: SOAPMessage msg = mf.createMessage();
140: SOAPPart sp = msg.getSOAPPart();
141: sp.setContentId("soapPart");
142:
143: SOAPEnvelope envelope = sp.getEnvelope();
144:
145: SOAPHeader hdr = envelope.getHeader();
146: SOAPBody bdy = envelope.getBody();
147:
148: // Add to body
149: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
150: "GetLastTradePrice", "ztrade",
151: "http://wombat.ztrade.com"));
152:
153: gltp.addChildElement(
154: envelope.createName("symbol", "ztrade",
155: "http://wombat.ztrade.com"))
156: .addTextNode("SUNW");
157:
158: // Attach an xml file containing empty Body message
159: URL url = new URL("file", null,
160: "src/test/mime/data/message.xml");
161:
162: AttachmentPart ap = msg.createAttachmentPart(new DataHandler(
163: url));
164:
165: ap.setContentType("text/xml");
166: ap.setContentId("attachmentPart");
167:
168: msg.addAttachmentPart(ap);
169:
170: msg.saveChanges();
171:
172: FileOutputStream sentFile = new FileOutputStream(
173: "src/test/mime/data/message.txt");
174: msg.writeTo(sentFile);
175: sentFile.close();
176: changeAndSaveMimeHeaders(msg, "src/test/mime/data/headers.txt");
177:
178: SOAPMessage newMsg = getModifiedMessage(
179: "src/test/mime/data/headers.txt",
180: "src/test/mime/data/message.txt");
181: assertFalse("newMsg has an empty body", newMsg.getSOAPBody()
182: .getChildElements().hasNext());
183: assertTrue("Soap part has the Content-Id: attachmentPart",
184: newMsg.getSOAPPart().getContentId().equals(
185: "attachmentPart"));
186: assertTrue("Attachment part has the Content-Id: soapPart",
187: ((AttachmentPart) newMsg.getAttachments().next())
188: .getContentId().equals("soapPart"));
189: }
190:
191: public void testSampleMessageOne() throws Exception {
192: MimeHeaders hdrs = new MimeHeaders();
193: hdrs.addHeader("Server", "WebSphere Application Server/5.1");
194: hdrs
195: .addHeader(
196: "Content-Type",
197: "multipart/related; "
198: + "type=\"text/xml\"; "
199: + "start=\"<139912840220.1065629194743.IBM.WEBSERVICES@ibm-7pr28r4m35k>\"; "
200: + "boundary=\"----=_Part_4_910054940.1065629194743\"");
201: hdrs.addHeader("Content-Language", "en-US");
202: hdrs.addHeader("Connection", "close");
203:
204: FileInputStream fis = new FileInputStream(
205: "src/test/mime/data/msg.txt");
206: MessageFactory factory = MessageFactory.newInstance();
207: SOAPMessage msg = factory.createMessage(hdrs, fis);
208: String[] s = msg.getSOAPPart().getMimeHeader(
209: "Content-Description");
210: }
211:
212: public void testContentDescription() throws Exception {
213: MimeHeaders hdrs = new MimeHeaders();
214: hdrs.addHeader("Server", "WebSphere Application Server/5.1");
215: hdrs
216: .addHeader(
217: "Content-Type",
218: "multipart/related; "
219: + "type=\"text/xml\"; "
220: + "start=\"<139912840220.1065629194743.IBM.WEBSERVICES@ibm-7pr28r4m35k>\"; "
221: + "boundary=\"----=_Part_4_910054940.1065629194743\"");
222: hdrs.addHeader("Content-Language", "en-US");
223: hdrs.addHeader("Connection", "close");
224:
225: FileInputStream fis = new FileInputStream(
226: "src/test/mime/data/msg.txt");
227: MessageFactory factory = MessageFactory.newInstance();
228: try {
229: factory.createMessage(hdrs, fis);
230: } catch (Exception e) {
231: fail("Exception should not be thrown "
232: + "while internalizing the message");
233: }
234: }
235:
236: public void testSampleMessageOneWithoutTypeParameter()
237: throws Exception {
238: MimeHeaders hdrs = new MimeHeaders();
239: hdrs.addHeader("Server", "WebSphere Application Server/5.1");
240: hdrs
241: .addHeader(
242: "Content-Type",
243: "multipart/related; "
244: + "start=\"<139912840220.1065629194743.IBM.WEBSERVICES@ibm-7pr28r4m35k>\"; "
245: + "boundary=\"----=_Part_4_910054940.1065629194743\"");
246: hdrs.addHeader("Content-Language", "en-US");
247: hdrs.addHeader("Connection", "close");
248:
249: FileInputStream fis = new FileInputStream(
250: "src/test/mime/data/msg.txt");
251: MessageFactory factory = MessageFactory.newInstance();
252: try {
253: factory.createMessage(hdrs, fis);
254: } catch (Exception e) {
255: fail("Exception should not be thrown "
256: + "while internalizing the message");
257: }
258: }
259:
260: public void testSampleMessageTwo() throws Exception {
261: MimeHeaders hdrs = new MimeHeaders();
262: hdrs.addHeader("Server", "WebSphere Application Server/5.1");
263: hdrs
264: .addHeader(
265: "Content-Type",
266: "multipart/related; "
267: + "type=\"text/xml\"; "
268: + "start=\"<1071294019496.1066069460327.IBM.WEBSERVICES@ibm-7pr28r4m35k>\"; "
269: + "boundary=\"----=_Part_1_807283631.1066069460327\"");
270: hdrs.addHeader("Content-Language", "en-US");
271: hdrs.addHeader("Connection", "close");
272:
273: FileInputStream fis = new FileInputStream(
274: "src/test/mime/data/msg2.txt");
275: MessageFactory factory = MessageFactory.newInstance();
276: factory.createMessage(hdrs, fis);
277: }
278: }
|