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: /*
057: * MessageBuilder.java
058: *
059: * Created on April 4, 2003, 12:03 PM
060: */
061:
062: package mime;
063:
064: import java.io.*;
065: import java.util.*;
066:
067: import javax.xml.soap.*;
068: import javax.xml.transform.Transformer;
069: import javax.xml.transform.TransformerFactory;
070: import javax.xml.transform.dom.DOMSource;
071: import javax.xml.transform.sax.SAXSource;
072: import javax.xml.transform.stream.StreamResult;
073: import javax.xml.transform.stream.StreamSource;
074:
075: import org.xml.sax.InputSource;
076:
077: /**
078: * @author Krishna Meduri
079: */
080:
081: public class MessageBuilder {
082:
083: public void saveMimeHeaders(SOAPMessage msg, String fileName)
084: throws IOException {
085:
086: FileOutputStream fos = new FileOutputStream(fileName);
087: ObjectOutputStream oos = new ObjectOutputStream(fos);
088:
089: Hashtable hashTable = new Hashtable();
090: MimeHeaders mimeHeaders = msg.getMimeHeaders();
091: Iterator iterator = mimeHeaders.getAllHeaders();
092:
093: while (iterator.hasNext()) {
094: MimeHeader mimeHeader = (MimeHeader) iterator.next();
095: hashTable.put(mimeHeader.getName(), mimeHeader.getValue());
096: }
097:
098: oos.writeObject(hashTable);
099: oos.flush();
100: oos.close();
101:
102: fos.flush();
103: fos.close();
104: }
105:
106: public SOAPMessage constructMessage(String mimeHdrsFile,
107: String msgFile) throws Exception {
108: SOAPMessage message;
109:
110: MimeHeaders mimeHeaders = new MimeHeaders();
111: FileInputStream fis = new FileInputStream(msgFile);
112:
113: ObjectInputStream ois = new ObjectInputStream(
114: new FileInputStream(mimeHdrsFile));
115: Hashtable hashTable = (Hashtable) ois.readObject();
116: ois.close();
117:
118: if (hashTable.isEmpty())
119: System.out.println("MimeHeaders Hashtable is empty");
120: else {
121: for (int i = 0; i < hashTable.size(); i++) {
122: Enumeration keys = hashTable.keys();
123: Enumeration values = hashTable.elements();
124: while (keys.hasMoreElements()
125: && values.hasMoreElements()) {
126: String name = (String) keys.nextElement();
127: String value = (String) values.nextElement();
128: mimeHeaders.addHeader(name, value);
129: }
130: }
131: }
132:
133: MessageFactory messageFactory = MessageFactory.newInstance();
134: message = messageFactory.createMessage(mimeHeaders, fis);
135:
136: message.saveChanges();
137:
138: return message;
139: }
140:
141: public boolean verifyMessage(SOAPMessage msg, SOAPMessage gMsg)
142: throws Exception {
143: boolean result = false;
144: result = verifySOAPPart(msg, gMsg);
145: if (result == false) {
146: System.out.println("Mismatch in SOAPParts");
147: return false;
148: }
149: result = verifyAttachmentPart(msg, gMsg);
150: if (result == false) {
151: System.out.println("Mismatch in AttachmentParts");
152: return false;
153: }
154: return true;
155: }
156:
157: public boolean verifyAttachmentPart(SOAPMessage msg,
158: SOAPMessage gMsg) throws Exception {
159:
160: StreamSource streamSource = null;
161: StreamSource gStreamSource = null;
162:
163: BufferedReader reader = null;
164: BufferedReader gReader = null;
165:
166: AttachmentPart ap = null;
167: AttachmentPart gAp = null;
168:
169: String str = null;
170: String gStr = null;
171:
172: int count = msg.countAttachments();
173: int gCount = gMsg.countAttachments();
174:
175: // System.out.println("****attachment count**" + gCount);
176:
177: if (count != gCount)
178: return false;
179:
180: if (count < 1)
181: return true;
182:
183: Iterator attachmentIterator = msg.getAttachments();
184: Iterator gAttachmentIterator = gMsg.getAttachments();
185:
186: for (int i = 0; i < count; i++) {
187: ap = (AttachmentPart) attachmentIterator.next();
188: gAp = (AttachmentPart) gAttachmentIterator.next();
189:
190: String contentType = ap.getContentType();
191: String gContentType = gAp.getContentType();
192:
193: //System.out.println("**contentType**" + contentType);
194: //System.out.println("**gContentType**" + gContentType);
195:
196: if (!contentType.equals(gContentType))
197: return false;
198:
199: //Code for contentType="text/plain yet to write
200: if (gContentType.equals("text/xml")) {
201: // System.out.println("getContent() returns <" +
202: // ap.getContent().getClass() + ">");
203:
204: streamSource = (StreamSource) ap.getContent();
205: gStreamSource = (StreamSource) gAp.getContent();
206:
207: reader = new BufferedReader(new InputStreamReader(
208: streamSource.getInputStream()));
209: gReader = new BufferedReader(new InputStreamReader(
210: gStreamSource.getInputStream()));
211:
212: } else {
213: InputStream is = (InputStream) ap.getContent();
214: InputStream gIs = (InputStream) gAp.getContent();
215:
216: reader = new BufferedReader(new InputStreamReader(is));
217: gReader = new BufferedReader(new InputStreamReader(gIs));
218: }
219:
220: while (true) {
221: boolean gReaderMore = false;
222: boolean readerMore = false;
223:
224: if ((gStr = gReader.readLine()) != null) {
225: gReaderMore = true;
226: }
227: if ((str = reader.readLine()) != null) {
228: readerMore = true;
229: }
230:
231: if (gReaderMore != readerMore) {
232: System.out
233: .println("The number of lines in reader differs from golden");
234: return false;
235: } else if (gReaderMore == true && readerMore == true) {
236: if (!gStr.equals(str)) {
237: System.out.println("gStr is <" + gStr + ">");
238: System.out.println("str is <" + str + ">");
239: System.out
240: .println("Contents in a line differ from golden");
241: return false;
242: }
243: } else
244: break;
245: }
246:
247: }
248:
249: return true;
250: }
251:
252: public boolean verifySOAPPart(SOAPMessage msg, SOAPMessage gMsg)
253: throws Exception {
254:
255: String str = null;
256: String gStr = null;
257:
258: BufferedReader reader = null;
259:
260: SOAPPart soapPart = msg.getSOAPPart();
261:
262: //System.out.println("**soapPart.getContent().getClass() returns " +
263: //"<" + soapPart.getContent().getClass() + ">");
264:
265: if (soapPart.getContent() instanceof SAXSource) {
266:
267: SAXSource saxSource = (SAXSource) soapPart.getContent();
268: InputSource iSource = saxSource.getInputSource();
269: reader = new BufferedReader(iSource.getCharacterStream());
270:
271: } else if (soapPart.getContent() instanceof StreamSource) {
272:
273: StreamSource streamSource = (StreamSource) soapPart
274: .getContent();
275: InputStream instream = streamSource.getInputStream();
276: InputStreamReader isReader = new InputStreamReader(instream);
277:
278: reader = new BufferedReader(isReader);
279:
280: } else if (soapPart.getContent() instanceof DOMSource) {
281:
282: //With SAAJ1.2 we get DOMSOurce for SOAPPart - 02-14-2003 kmeduri
283: DOMSource domSource = (DOMSource) soapPart.getContent();
284:
285: TransformerFactory tFactory = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();
286: Transformer transformer = tFactory.newTransformer();
287: ByteArrayOutputStream baos = new ByteArrayOutputStream();
288: StreamResult streamResult = new StreamResult(baos);
289:
290: transformer.transform(domSource, streamResult);
291: baos = (ByteArrayOutputStream) streamResult
292: .getOutputStream();
293:
294: byte[] byteArray = new byte[baos.size()];
295: byteArray = baos.toByteArray();
296: ByteArrayInputStream bais = new ByteArrayInputStream(
297: byteArray);
298:
299: InputStreamReader isReader = new InputStreamReader(bais);
300: reader = new BufferedReader(isReader);
301: }
302:
303: SOAPPart gSoapPart = gMsg.getSOAPPart();
304: //System.out.println("gSoapPart.getContent().getClass() returns " +
305: //"<" + gSoapPart.getContent().getClass() + ">");
306:
307: if (gSoapPart.getContent() instanceof SAXSource) {
308:
309: SAXSource saxSource2 = (SAXSource) gSoapPart.getContent();
310: InputSource iSource2 = saxSource2.getInputSource();
311: BufferedReader gReader = new BufferedReader(iSource2
312: .getCharacterStream());
313:
314: while (true) {
315: boolean gReaderMore = false;
316: boolean readerMore = false;
317:
318: if ((gStr = gReader.readLine()) != null) {
319: //Added 09-06-2002 - kmeduri
320: if (gStr.startsWith("<?xml"))
321: gStr = gReader.readLine();
322: //System.out.println(gStr);
323:
324: gReaderMore = true;
325: }
326: if ((str = reader.readLine()) != null) {
327: //Added 09-06-2002 - kmeduri
328: if (str.startsWith("<?xml"))
329: str = reader.readLine();
330: //System.out.println(str);
331:
332: readerMore = true;
333: }
334:
335: if (gReaderMore != readerMore) {
336: return false;
337: } else if (gReaderMore == true && readerMore == true) {
338: if (!gStr.equals(str))
339: return false;
340: } else
341: break;
342: }
343: }
344:
345: if (gSoapPart.getContent() instanceof StreamSource) {
346:
347: StreamSource streamSource = (StreamSource) gSoapPart
348: .getContent();
349: InputStream instream = streamSource.getInputStream();
350: InputStreamReader isReader = new InputStreamReader(instream);
351:
352: BufferedReader gReader = new BufferedReader(isReader);
353:
354: while (true) {
355: boolean gReaderMore = false;
356: boolean readerMore = false;
357:
358: if ((gStr = gReader.readLine()) != null) {
359:
360: if (gStr.startsWith("<?xml"))
361: gStr = gReader.readLine();
362: //System.out.println(gStr);
363:
364: gReaderMore = true;
365: }
366:
367: if ((str = reader.readLine()) != null) {
368:
369: /* Anil fixed this for java_xml_pack promoted build b07.
370: Had to comment this... kmeduri...04/15/2002
371:
372: The fix broke in J2EE1.4 integration build
373: (saaj jar picked from jaxrpc build on Aug 19)
374: Uncommenting on 09-06-2002 ...
375:
376: **/
377:
378: //SAXSource puts prolog in the beginning which is not the
379: //case with StreamSource. This stmt ignores xml prolog
380: if (str.startsWith("<?xml"))
381: str = reader.readLine();
382: // System.out.println(str);
383: readerMore = true;
384: }
385:
386: if (gReaderMore != readerMore) {
387: return false;
388: } else if (gReaderMore == true && readerMore == true) {
389: if (!gStr.equals(str))
390: return false;
391: } else
392: break;
393: }
394: }
395:
396: return true;
397: }
398:
399: public void copyStreamToFile(InputStream is, String fileName)
400: throws Exception {
401: FileOutputStream fos = new FileOutputStream(fileName);
402:
403: int byteRead;
404: while ((byteRead = is.read()) != -1) {
405: fos.write(byteRead);
406: }
407:
408: // System.out.println("File copied to " + fileName );
409: fos.flush();
410: fos.close();
411: }
412:
413: }
|