KickJava   Java API By Example, From Geeks To Geeks.

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


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  * An encrypted attachment.
16  */

17 public class CryptoAttachment extends Attachment {
18
19   boolean parsed = false;
20
21   boolean encrypted = false;
22
23   boolean signed = false;
24
25   BodyPart decryptedBodyPart = null;
26   
27   DataHandler JavaDoc msgDataHandler = null;
28
29   /**
30    * Creates a CryptoAttachment out of a MimePart.
31    */

32   public CryptoAttachment(MimePart mp) throws MessagingException {
33     super(mp);
34     ContentType ct = new ContentType(mp.getContentType());
35     if (ct.getSubType().equalsIgnoreCase("encrypted"))
36       encrypted = true;
37     else if (ct.getSubType().equalsIgnoreCase("signed"))
38       signed = true;
39     else if (ct.getPrimaryType().equalsIgnoreCase("application") && ct.getSubType().equalsIgnoreCase("pkcs7-mime")) {
40       encrypted = true;
41     }
42   }
43
44   /**
45    * Tries to decrypt this Attachment.
46    */

47   public BodyPart decryptAttachment(EncryptionUtils utils, Key JavaDoc key)
48     throws MessagingException, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
49     
50     if (decryptedBodyPart != null)
51       return decryptedBodyPart;
52     else {
53       MimeBodyPart mbp = new MimeBodyPart();
54       mbp.setDataHandler(super.getDataHandler());
55       mbp.setHeader("Content-Type", super.getDataHandler().getContentType());
56
57       decryptedBodyPart = utils.decryptBodyPart(mbp, key);
58
59       return decryptedBodyPart;
60     }
61     
62   }
63
64   // accessor methods.
65

66   /**
67    * Returns the text of the Attachment, up to maxLength bytes. If
68    * the content is truncated, then append the truncationMessage at the
69    * end of the content displayed.
70    *
71    * If withHeaders is set, then show the Headers to go with this message.
72    * If showFullHeaders is also set, then show all the headers.
73    */

74   public String JavaDoc getText(boolean withHeaders, boolean showFullHeaders, int maxLength, String JavaDoc truncationMessage) throws java.io.IOException JavaDoc {
75     StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
76     if (withHeaders)
77       retVal.append(getHeaderInformation(showFullHeaders));
78     
79     retVal.append(Pooka.getProperty("Pooka.crypto.encryptedMessage", "****** This is an encrypted message. Click on the 'encryption' button or go to Encrypt->Decrypt message to read it. ******"));
80     
81     return retVal.toString();
82   }
83
84   /**
85    * Returns if we have already decrypted this attachment successfully.
86    */

87   public boolean decryptedSuccessfully() {
88     return (decryptedBodyPart != null);
89   }
90   
91 }
92
Popular Tags