KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > crypto > SignedAttachment


1 package net.suberic.pooka.crypto;
2
3 import net.suberic.pooka.*;
4 import net.suberic.crypto.*;
5
6 import javax.mail.internet.*;
7 import javax.mail.*;
8 import javax.activation.DataHandler JavaDoc;
9
10 import java.security.Key JavaDoc;
11
12 import java.io.*;
13
14 /**
15  * A signed attachment.
16  */

17 public class SignedAttachment extends Attachment {
18
19   boolean parsed = false;
20
21   /**
22    * Creates a SignedAttachment out of a MimePart.
23    */

24   public SignedAttachment(MimePart mp) throws MessagingException {
25     super(mp);
26   }
27   
28   /**
29    * Returns if the signature matches.
30    */

31   public boolean checkSignature(EncryptionUtils utils, Key JavaDoc key)
32     throws MessagingException, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
33
34     Object JavaDoc content = getDataHandler().getContent();
35     if (content instanceof MimeMultipart) {
36       return utils.checkSignature((MimeMultipart) content, key);
37     } else {
38       return false;
39     }
40   }
41
42   /**
43    * Returns the content part of the signed attachment.
44    */

45   public MimeBodyPart getSignedPart() throws javax.mail.MessagingException JavaDoc,
46   java.io.IOException JavaDoc {
47     Object JavaDoc content = getDataHandler().getContent();
48     if (content instanceof MimeMultipart) {
49       MimeMultipart mm = (MimeMultipart) content;
50
51       // this should be exactly two parts, one the content, the other the
52
// signature.
53
for (int i = 0; i < mm.getCount(); i++) {
54     // return the first one found.
55
MimeBodyPart mbp = (MimeBodyPart) mm.getBodyPart(i);
56     ContentType ct = new ContentType(mbp.getContentType());
57     if (! ct.getSubType().toLowerCase().endsWith("signature")) {
58       return mbp;
59     }
60       }
61     }
62
63     return null;
64   }
65
66   /**
67    * Returns the DataHandler for this Attachment.
68    */

69   public DataHandler JavaDoc getDataHandler() {
70     return super.getDataHandler();
71   }
72
73
74   /**
75    * Returns the MimeType.
76    */

77   /*
78   public ContentType getMimeType() {
79     try {
80       return new ContentType("text/plain");
81     } catch (javax.mail.internet.ParseException pe) {
82       return null;
83     }
84   }
85   */

86 }
87
Popular Tags