KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > pgp > MultipartSignedRenderer


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.pgp;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.columba.core.io.CloneStreamMaster;
23 import org.columba.ristretto.composer.MimePartRenderer;
24 import org.columba.ristretto.composer.MimeTreeRenderer;
25 import org.columba.ristretto.io.SequenceInputStream;
26 import org.columba.ristretto.message.InputStreamMimePart;
27 import org.columba.ristretto.message.MimeHeader;
28 import org.columba.ristretto.message.MimePart;
29 import org.columba.ristretto.message.StreamableMimePart;
30 import org.waffel.jscf.JSCFConnection;
31 import org.waffel.jscf.JSCFResultSet;
32 import org.waffel.jscf.JSCFStatement;
33
34 /**
35  * @author waffel
36  *
37  * This class renders a message to be a signed message. The class supports
38  * currently only pgp as method.
39  */

40 public class MultipartSignedRenderer extends MimePartRenderer {
41     private MimeHeader signatureHeader;
42
43     /**
44      * Creates a new Instance to render a message which should be signed. This
45      * constructor initialize the header for the signature with new mime header
46      * <code>application</code> and the value <code>pgp-signature</code>
47      */

48     public MultipartSignedRenderer() {
49         this.signatureHeader = new MimeHeader("application", "pgp-signature");
50     }
51
52     /*
53      * (non-Javadoc)
54      *
55      * @see org.columba.ristretto.composer.MimePartRenderer#getRegisterString()
56      */

57     public String JavaDoc getRegisterString() {
58         return "multipart/signed";
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.columba.ristretto.composer.MimePartRenderer#render(org.columba.ristretto.message.StreamableMimePart)
65      */

66     public InputStream JavaDoc render(MimePart part) throws Exception JavaDoc {
67         Vector JavaDoc streams = new Vector JavaDoc((2 * 2) + 3);
68
69         MimeHeader header = part.getHeader();
70
71         // Create boundary to separate the mime-parts
72
String JavaDoc boundary = createUniqueBoundary().toString();
73         header.putContentParameter("boundary", boundary);
74
75         byte[] startBoundary = ("\r\n--" + boundary + "\r\n").getBytes();
76         byte[] endBoundary = ("\r\n--" + boundary + "--\r\n").getBytes();
77
78         // Add pgp-specific content-parameters
79
// we take as default hash-algo SHA1
80
header.putContentParameter("micalg", "pgp-sha1");
81         header.putContentParameter("protocol", "application/pgp-signature");
82
83         // Create the header and body of the multipart
84
streams.add(header.getHeader().getInputStream());
85
86         // Add the MimePart that will be signed
87
streams.add(new ByteArrayInputStream JavaDoc(startBoundary));
88
89         CloneStreamMaster signedPartCloneModel = new CloneStreamMaster(
90                 MimeTreeRenderer.getInstance().renderMimePart(part.getChild(0)));
91
92         streams.add(signedPartCloneModel.getClone());
93
94         // Add the signature
95
streams.add(new ByteArrayInputStream JavaDoc(startBoundary));
96
97         StreamableMimePart signatureMimePart;
98
99         signatureMimePart = null;
100
101         JSCFController controller = JSCFController.getInstance();
102         JSCFConnection con = controller.getConnection();
103
104         PGPPassChecker passCheck = PGPPassChecker.getInstance();
105         boolean check = passCheck.checkPassphrase(con);
106
107         if (!check) {
108             throw new WrongPassphraseException("wrong passphrase");
109         }
110
111         JSCFStatement stmt = con.createStatement();
112         JSCFResultSet res = stmt.executeSign(signedPartCloneModel.getClone());
113
114         signatureMimePart = new InputStreamMimePart(this.signatureHeader, res
115                 .getResultStream());
116
117         streams.add(MimeTreeRenderer.getInstance().renderMimePart(
118                 signatureMimePart));
119
120         // Create the closing boundary
121
streams.add(new ByteArrayInputStream JavaDoc(endBoundary));
122
123         return new SequenceInputStream(streams);
124     }
125 }
126
Popular Tags