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 com.sun.xml.messaging.soap;
057:
058: import java.util.*;
059:
060: import java.io.*;
061:
062: import javax.xml.soap.*;
063: import javax.xml.transform.stream.StreamSource;
064:
065: /**
066: * This class provides a method <code>save</code> for saving a
067: * <code>SOAPMessage</code> to a file and a method <code>load</code> for
068: * loading a saved <code>SOAPMessage</code> from a file (in which the
069: * <code>SOAPMessage</code> was saved using the <code>save</code> method of
070: * this class).
071: * The format of the saved <code>SOAPMessage</code> is human readable.
072: */
073: public class SOAPMessagePersister {
074:
075: /**
076: * Saves a given <code>SOAPMessage</code> to a given file location in a
077: * human readable format.
078: */
079: public void save(SOAPMessage msg, String location)
080: throws IOException, SOAPException {
081:
082: FileWriter writer = new FileWriter(location);
083:
084: // Write all the message Mime headers
085: msg.saveChanges();
086: Iterator iterator = msg.getMimeHeaders().getAllHeaders();
087: while (iterator.hasNext()) {
088: MimeHeader mimeHeader = (MimeHeader) iterator.next();
089: writer.write(mimeHeader.getName() + ": "
090: + mimeHeader.getValue() + "\n");
091: }
092: writer.write("\n");
093:
094: // If there are no attachments
095: if (msg.countAttachments() == 0) {
096:
097: // Write a boundary to mark the beginning of the soap-part
098: writer.write("=--soap-part--=\n");
099:
100: // Write the Mime headers of the soap-part
101: Iterator it = msg.getSOAPPart().getAllMimeHeaders();
102: while (it.hasNext()) {
103: MimeHeader mimeHeader = (MimeHeader) it.next();
104: writer.write(mimeHeader.getName() + ": "
105: + mimeHeader.getValue() + "\n");
106: }
107: writer.write("\n");
108: }
109:
110: // Do a writeTo() of the message
111: ByteArrayOutputStream baos = new ByteArrayOutputStream();
112: msg.writeTo(baos);
113: writer.write(baos.toString());
114:
115: // If there were no attachments write the boundary to mark the end of
116: // the soap-part
117: if (msg.countAttachments() == 0)
118: writer.write("\n=--soap-part--=\n");
119:
120: writer.flush();
121: writer.close();
122: }
123:
124: /**
125: * Loads a <code>SOAPMessage</code> from a given file location.
126: * The message in the given file location should have been stored using
127: * the <code>save</code> method of this class.
128: */
129: public SOAPMessage load(String location) throws IOException,
130: SOAPException {
131:
132: FileReader reader = new FileReader(location);
133:
134: // Read the Mime headers of the message
135: MimeHeaders hdrs = new MimeHeaders();
136: String headerLine = readLine(reader);
137: while (!headerLine.equals("")) {
138:
139: int colonIndex = headerLine.indexOf(':');
140: String headerName = headerLine.substring(0, colonIndex);
141: String headerValue = headerLine.substring(colonIndex + 1);
142: hdrs.addHeader(headerName, headerValue);
143: headerLine = readLine(reader);
144: }
145:
146: // If there are no attachments
147: if (readLine(reader).equals("=--soap-part--=")) {
148:
149: // Read the Mime headers of soap-part
150: MimeHeaders soapPartHdrs = new MimeHeaders();
151: headerLine = readLine(reader);
152: while (!headerLine.equals("")) {
153: int colonIndex = headerLine.indexOf(':');
154: String headerName = headerLine.substring(0, colonIndex);
155: String headerValue = headerLine
156: .substring(colonIndex + 1);
157: soapPartHdrs.addHeader(headerName, headerValue);
158: headerLine = readLine(reader);
159: }
160:
161: // Read the content of the soap-part in a StringBuffer
162: StringBuffer soapPartBuffer = new StringBuffer();
163: String soapPartLine = readLine(reader);
164: while (!soapPartLine.equals("=--soap-part--=")) {
165: soapPartBuffer.append(soapPartLine);
166: soapPartBuffer.append('\n');
167: soapPartLine = readLine(reader);
168: }
169: soapPartBuffer.deleteCharAt(soapPartBuffer.length() - 1);
170:
171: // Create a new SOAPMessage
172: SOAPMessage msg = MessageFactory.newInstance()
173: .createMessage(
174: hdrs,
175: new StringBufferInputStream(soapPartBuffer
176: .toString()));
177:
178: // Set the Mime headers of its soap-part
179: SOAPPart soapPart = msg.getSOAPPart();
180: soapPart.removeAllMimeHeaders();
181: for (Iterator it = soapPartHdrs.getAllHeaders(); it
182: .hasNext();) {
183: MimeHeader hdr = (MimeHeader) it.next();
184: soapPart.addMimeHeader(hdr.getName(), hdr.getValue());
185: }
186:
187: msg.saveChanges();
188:
189: reader.close();
190:
191: // Return message
192: return msg;
193: }
194:
195: reader.close();
196:
197: // When there are no attachments, pass the FileInputStream of the
198: // saved message to CreateMessage method. This works because the parser
199: // ignores everything before the first part boundary
200: return MessageFactory.newInstance().createMessage(hdrs,
201: new FileInputStream(location));
202: }
203:
204: private String readLine(FileReader reader) throws IOException {
205:
206: StringBuffer buffer = new StringBuffer();
207: char ch = (char) reader.read();
208: while (ch != '\n') {
209: buffer.append(ch);
210: ch = (char) reader.read();
211: }
212: return buffer.toString();
213: }
214: }
|