001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.pgp;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.InputStream;
020: import java.util.Vector;
021:
022: import javax.swing.JOptionPane;
023:
024: import org.columba.core.gui.frame.FrameManager;
025: import org.columba.core.io.StreamUtils;
026: import org.columba.mail.config.SecurityItem;
027: import org.columba.mail.message.PGPMimePart;
028: import org.columba.ristretto.composer.MimePartRenderer;
029: import org.columba.ristretto.composer.MimeTreeRenderer;
030: import org.columba.ristretto.io.CharSequenceSource;
031: import org.columba.ristretto.io.SequenceInputStream;
032: import org.columba.ristretto.io.Source;
033: import org.columba.ristretto.message.InputStreamMimePart;
034: import org.columba.ristretto.message.LocalMimePart;
035: import org.columba.ristretto.message.MimeHeader;
036: import org.columba.ristretto.message.MimePart;
037: import org.columba.ristretto.message.StreamableMimePart;
038: import org.waffel.jscf.JSCFConnection;
039: import org.waffel.jscf.JSCFResultSet;
040: import org.waffel.jscf.JSCFStatement;
041:
042: public class MultipartEncryptedRenderer extends MimePartRenderer {
043: private StreamableMimePart controlPart;
044: private MimeHeader encryptedHeader;
045:
046: public MultipartEncryptedRenderer() {
047: MimeHeader controlHeader = new MimeHeader("application",
048: "pgp-encrypted");
049: Source controlBody = new CharSequenceSource("Version: 1\r\n");
050: controlPart = new LocalMimePart(controlHeader, controlBody);
051:
052: encryptedHeader = new MimeHeader("application", "octet-stream");
053: }
054:
055: /* (non-Javadoc)
056: * @see org.columba.ristretto.composer.MimePartRenderer#getRegisterString()
057: */
058: public String getRegisterString() {
059: return "multipart/encrypted";
060: }
061:
062: /* (non-Javadoc)
063: * @see org.columba.ristretto.composer.MimePartRenderer#render(org.columba.ristretto.message.StreamableMimePart)
064: */
065: public InputStream render(MimePart part) throws Exception {
066: Vector streams = new Vector((2 * 2) + 3);
067:
068: MimeHeader header = part.getHeader();
069:
070: // Create boundary to separate the mime-parts
071: String boundary = createUniqueBoundary().toString();
072: header.putContentParameter("boundary", boundary);
073:
074: byte[] startBoundary = ("\r\n--" + boundary + "\r\n")
075: .getBytes();
076: byte[] endBoundary = ("\r\n--" + boundary + "--\r\n")
077: .getBytes();
078:
079: // Add pgp-specific content-parameters
080: header.putContentParameter("protocol",
081: "application/pgp-encrypted");
082:
083: // Create the header of the multipart
084: streams.add(header.getHeader().getInputStream());
085:
086: SecurityItem pgpItem = ((PGPMimePart) part).getPgpItem();
087:
088: // Add the ControlMimePart
089: streams.add(new ByteArrayInputStream(startBoundary));
090: streams.add(MimeTreeRenderer.getInstance().renderMimePart(
091: controlPart));
092:
093: // Add the encrypted MimePart
094: streams.add(new ByteArrayInputStream(startBoundary));
095:
096: StreamableMimePart encryptedPart;
097: encryptedPart = null;
098:
099: /*
100: JSCFDriverManager.registerJSCFDriver(new GPGDriver());
101: JSCFConnection con = JSCFDriverManager.getConnection("jscf:gpg:"+pgpItem.get("path"));
102: */
103: JSCFController controller = JSCFController.getInstance();
104: JSCFConnection con = controller.getConnection();
105:
106: //con.getProperties().put("USERID", pgpItem.get("id"));
107: JSCFStatement stmt = con.createStatement();
108: JSCFResultSet res = stmt.executeEncrypt(MimeTreeRenderer
109: .getInstance().renderMimePart(part.getChild(0)),
110: pgpItem.get("recipients"));
111:
112: if (res.isError()) {
113: JOptionPane.showMessageDialog(FrameManager.getInstance()
114: .getActiveFrame(), StreamUtils.readCharacterStream(
115: res.getErrorStream()).toString());
116: }
117:
118: encryptedPart = new InputStreamMimePart(encryptedHeader, res
119: .getResultStream());
120: streams.add(MimeTreeRenderer.getInstance().renderMimePart(
121: encryptedPart));
122:
123: // Create the closing boundary
124: streams.add(new ByteArrayInputStream(endBoundary));
125:
126: return new SequenceInputStream(streams);
127: }
128: }
|