KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > test > TestExternalMessage


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email transport capabilities.
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8 package org.enhydra.oyster.test;
9
10 import javax.mail.Transport JavaDoc;
11 import javax.mail.internet.MimeBodyPart JavaDoc;
12 import javax.mail.internet.MimeMessage JavaDoc;
13 import javax.mail.internet.MimeMultipart JavaDoc;
14 import javax.mail.internet.InternetAddress JavaDoc;
15 import javax.mail.Session JavaDoc;
16 import org.enhydra.oyster.smime.SignedAndEnvelopedSMIME;
17 import org.enhydra.oyster.exception.SMIMEException;
18 import javax.activation.DataHandler JavaDoc;
19 import javax.activation.FileDataSource JavaDoc;
20 import java.util.Properties JavaDoc;
21 import java.io.File JavaDoc;
22 import java.security.Security JavaDoc;
23
24 /**
25  * Tests sending signed and enveloped message constructed with external prepared
26  * message. text/plain message with or withouth attachments can be sent by this
27  * test. To get help for this example type:
28  * "java org.enhydra.oyster.test.TestExternalMessage" in command line.
29  * It is assumed that oyster_tests.jar is in your classpath.<BR>
30  * <BR>
31  * Parameters passed to example are:<BR>
32  * &lt;mailHost&gt; &lt;mailAddress&gt; &lt;cerFileName&gt; &lt;algorithmName&gt;
33  * &lt;digestAlgorithm&gt; &lt;includingCert&gt; &lt;includingSignAttrib&gt;
34  * &lt;pfxFileName&gt; [&lt;attachment&gt;] <BR>
35  * <BR>
36  * &lt;digestAlgorithm&gt; could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
37  * &lt;includingCert&gt; could be: true/false<BR>
38  * &lt;includingSignAttrib&gt; could be: true/false<BR>
39  * &lt;algorithmName&gt; could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
40  * <BR>
41  * Note that for this example passwords for .pfx or .p12 files are fixed to
42  * "together". All .pfx files or .p12 files provided with this example have this
43  * password. Also, email address "FROM" is fixed to: "sender@together.at".
44  * You should change this values in source code of TestEncSig.java in order to use
45  * them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
46  */

47 public class TestExternalMessage
48 {
49
50   public static void main(String JavaDoc[] args)
51   {
52
53     String JavaDoc subject = "S/MIME signed and enveloped message - Subject test: ÜüÄäÖöÜüß";
54     String JavaDoc content = "S/MIME signed and enveloped message example\r\nContent test: ÜüÄäÖöÜüß!";
55     String JavaDoc from = "sender@together.at";
56     String JavaDoc password = "together";
57
58     if (args.length < 8)
59     {
60       System.err.println(
61       System.getProperty("line.separator") +
62       "Usage of TestExternalMessage: " +
63       System.getProperty("line.separator") +
64       "java TestExternalMessage <mailHost> <mailAddress> <cerFileName> " +
65       "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " +
66       "<pfxFileName> [<attachment>]"+
67       System.getProperty("line.separator") +
68       System.getProperty("line.separator") +
69       "Examples:" +
70       System.getProperty("line.separator") +
71       "java TestEncSig together.at recipient@together.at recipient512.cer " +
72       "RC240 SHA1_WITH_RSA true true sender512.pfx" +
73       System.getProperty("line.separator") +
74       "java TestEncSig together.at recipient@together.at recipient512.cer " +
75       "DES MD5_WITH_RSA true true sender512.pfx .\\test\\Zip8Test.zip");
76
77       System.exit(-1);
78     }
79
80     String JavaDoc smtpHost = args[0];
81     String JavaDoc addressTO = args[1];
82     String JavaDoc cerFileName = args[2];
83     String JavaDoc algorithmName = args[3];
84     String JavaDoc digestAlgorithm = args[4];
85
86     boolean includingCert = true;
87     if (args[5].equals("true"))
88       includingCert = true;
89     else
90       includingCert = false;
91
92     boolean includingSignAttrib = true;
93     if (args[6].equals("true"))
94       includingSignAttrib = true;
95     else
96       includingSignAttrib = false;
97
98     String JavaDoc pfxfileName = args[7];
99
100     String JavaDoc fileName = null;
101     if (args.length > 8)
102       fileName = args[8];
103
104     String JavaDoc addressCC = "recipient@together.at";
105     String JavaDoc addressBCC = "recipient@together.at";
106
107     subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " +
108               args[6] + " " + args[7] + " " + subject;
109
110
111     try {
112 // Construction of external message
113

114       Properties JavaDoc sesProp = new Properties JavaDoc();
115       sesProp.setProperty("mail.smtp.host", smtpHost);
116       Session JavaDoc ses = Session.getInstance(sesProp);
117       MimeMessage JavaDoc message = new MimeMessage JavaDoc(ses);
118       InternetAddress JavaDoc fromAddress = new InternetAddress JavaDoc(from);
119       message.setFrom(fromAddress);
120       message.setSubject(subject);
121
122       MimeBodyPart JavaDoc mbpText = new MimeBodyPart JavaDoc();
123       mbpText.setText(content);
124
125       if(fileName != null) {
126         MimeBodyPart JavaDoc mbpAttach = new MimeBodyPart JavaDoc();
127         FileDataSource JavaDoc fileSource = new FileDataSource JavaDoc(fileName);
128         mbpAttach.setDataHandler(new DataHandler JavaDoc(fileSource));
129         mbpAttach.setFileName(fileName);
130         mbpAttach.setDisposition(mbpAttach.ATTACHMENT);
131
132         MimeMultipart JavaDoc mm = new MimeMultipart JavaDoc();
133         mm.addBodyPart(mbpText);
134         mm.addBodyPart(mbpAttach);
135         message.setContent(mm);
136       }
137       else {
138         message.setContent((String JavaDoc)mbpText.getContent(), mbpText.getContentType());
139         message.setDisposition(mbpText.INLINE);
140       }
141       message.writeTo(System.out);
142 // Construction of enveloped and signed smime object
143
SignedAndEnvelopedSMIME ess = new SignedAndEnvelopedSMIME(message);
144     ess.addRecipient(addressTO, "TO", cerFileName); // mandatory
145
// ess.addRecipient(addressCC, "CC", cerFileName); // optional
146
// ess.addRecipient(addressBCC, "BCC", cerFileName); // optional
147

148       ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
149
ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
150
ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional
151

152       ess.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib);
153
154       if (algorithmName.equals("RC240"))
155       {
156         System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... ");
157         ess.signingAndEnveloping("SIGN_FIRST"); // instead of this next line could be used
158
// ess.enveloping(ess.RC2_CBC, 40, "SIGN_FIRST");
159
}
160       else if (algorithmName.equals("RC264"))
161       {
162         System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... ");
163         ess.signingAndEnveloping(ess.RC2_CBC, 64, "SIGN_FIRST"); // send message with RC2 - 64 bits algorithm
164
}
165       else if (algorithmName.equals("RC2128"))
166       {
167         System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... ");
168         ess.signingAndEnveloping(ess.RC2_CBC, 128, "SIGN_FIRST"); // send message with RC2 - 128 bits algorithm
169
}
170       else if (algorithmName.equals("DES"))
171       {
172         System.out.println("Creating enveloped and signed message with DES algorithm... ");
173         ess.signingAndEnveloping(ess.DES, 56, "SIGN_FIRST"); // send message with DES - 56 bits algorithm
174
}
175       else if (algorithmName.equals("3DES"))
176       {
177         System.out.println("Creating enveloped and signed message with 3DES algorithm... ");
178         ess.signingAndEnveloping(ess.DES_EDE3_CBC, 192, "SIGN_FIRST"); // send message with 3DES - 192 bits algorithm
179
}
180
181       System.out.print("Sending enveloped and signed message ... ");
182       ess.send(); // instead of this next line could be used
183
// Transport.send(ess.getSignedMessage());
184
System.out.println("done.");
185
186     }
187     catch (Exception JavaDoc e) {
188       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
189
if(e instanceof SMIMEException) {
190         SMIMEException eTmp = (SMIMEException)e;
191 // eTmp.loggingErrors(null); //logging
192
eTmp.displayErrors(null);
193        e = eTmp.getNonSMIMEException();
194        if(e != null)
195          e.printStackTrace();
196       }
197       else {
198         e.printStackTrace();
199       }
200     }
201   }
202 }
Popular Tags