KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladan Obradovic
5  * @Version 2.1.5
6  */

7
8 package org.enhydra.oyster.test;
9
10 import javax.mail.Transport JavaDoc;
11 import org.enhydra.oyster.smime.SignedAndEnvelopedSMIME;
12 import org.enhydra.oyster.exception.SMIMEException;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.File JavaDoc;
15
16 /**
17  * Tests enveloping and signing process. Enveloped and signed
18  * text/html message with or withouth attachments can be sent by this test.
19  * This example is test for composing of email message content by html code which
20  * is given from the file system. To get help for this example type:
21  * "java org.enhydra.oyster.test.TestEncSigHtml" in command line.
22  * It is assumed that oyster_tests.jar is in your classpath.<BR>
23  * <BR>
24  * Parameters passed to example are:<BR>
25  * &lt;mailHost&gt; &lt;mailAddress&gt; &lt;cerFileName&gt; &lt;algorithmName&gt;
26  * &lt;digestAlgorithm&gt; &lt;includingCert&gt; &lt;includingSignAttrib&gt;
27  * &lt;pfxFileName&gt; [&lt;attachment&gt;] <BR>
28  * <BR>
29  * &lt;digestAlgorithm&gt; could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
30  * &lt;includingCert&gt; could be: true/false<BR>
31  * &lt;includingSignAttrib&gt; could be: true/false<BR>
32  * &lt;algorithmName&gt; could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
33  * <BR>
34  * Note that for this example passwords for .pfx or .p12 files are fixed to
35  * "together". All .pfx files or .p12 files provided with this example have this
36  * password. Also, email address "FROM" is fixed to: "sender@together.at".
37  * You should change this values in source code of TestEncSigHtml.java in order
38  * to use them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
39  */

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

116 // To send messages content withouth resources from FileInputStream (or any
117
// other input stream) comment previous line and use following commented line
118
// ess.setContent(new FileInputStream(htmlContent),"text/html");
119

120       if (fileName!=null) {
121         ess.addAttachment(fileName); // optional - use this if send attachment
122
}
123
124       ess.setReply(from); // optional
125

126       ess.addRecipient(addressTO, "TO", cerFileName); // mandatory
127
// ess.addRecipient(addressCC, "CC", cerFileName); // optional
128
// ess.addRecipient(addressBCC, "BCC", cerFileName); // optional
129

130       ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
131
ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
132
ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional
133

134       ess.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib);
135
136       if (algorithmName.equals("RC240"))
137       {
138         System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... ");
139         ess.signingAndEnveloping("ENCRYPT_FIRST"); // instead of this next line could be used
140
// ess.enveloping(ess.RC2_CBC, 40, "ENCRYPT_FIRST");
141
}
142       else if (algorithmName.equals("RC264"))
143       {
144         System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... ");
145         ess.signingAndEnveloping(ess.RC2_CBC, 64, "ENCRYPT_FIRST"); // send message with RC2 - 64 bits algorithm
146
}
147       else if (algorithmName.equals("RC2128"))
148       {
149         System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... ");
150         ess.signingAndEnveloping(ess.RC2_CBC, 128, "ENCRYPT_FIRST"); // send message with RC2 - 128 bits algorithm
151
}
152       else if (algorithmName.equals("DES"))
153       {
154         System.out.println("Creating enveloped and signed message with DES algorithm... ");
155         ess.signingAndEnveloping(ess.DES, 56, "ENCRYPT_FIRST"); // send message with DES - 56 bits algorithm
156
}
157       else if (algorithmName.equals("3DES"))
158       {
159         System.out.println("Creating enveloped and signed message with 3DES algorithm... ");
160         ess.signingAndEnveloping(ess.DES_EDE3_CBC, 192, "ENCRYPT_FIRST"); // send message with 3DES - 192 bits algorithm
161
}
162
163       System.out.print("Sending enveloped and signed message ... ");
164       ess.send(); // instead of this next line could be used
165
// Transport.send(ess.getSignedMessage());
166
System.out.println("done.");
167
168     }
169     catch (Exception JavaDoc e) {
170       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
171
if(e instanceof SMIMEException) {
172         SMIMEException eTmp = (SMIMEException)e;
173 // eTmp.loggingErrors(null); //logging
174
eTmp.displayErrors(null);
175        e = eTmp.getNonSMIMEException();
176        if(e != null)
177          e.printStackTrace();
178       }
179       else {
180         e.printStackTrace();
181       }
182     }
183   }
184 }
Popular Tags