KickJava   Java API By Example, From Geeks To Geeks.

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


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 signing and enveloping process. Signed and enveloped
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.TestSigEncHtml" 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 TestSigEncHtml.java in order
38  * to use them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
39  */

40 public class TestSigEncHtml
41 {
42
43   public static void main(String JavaDoc[] args)
44   {
45
46     String JavaDoc subject = "S/MIME signed and enveloped message - Subject test: ";
47     String JavaDoc content = "S/MIME signed and enveloped message example\r\nContent test: !";
48     String JavaDoc from = "sender@together.at";
49     String JavaDoc password = "together";
50
51     if (args.length < 8)
52     {
53       System.err.println(
54       System.getProperty("line.separator") +
55       "Usage of TestSigEncHtml: " +
56       System.getProperty("line.separator") +
57       "java TestSigEncHtml <mailHost> <mailAddress> <cerFileName> " +
58       "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " +
59       "<pfxFileName> [<attachment>]"+
60       System.getProperty("line.separator") +
61       System.getProperty("line.separator") +
62       "Examples:" +
63       System.getProperty("line.separator") +
64       "java TestSigEncHtml together.at recipient@together.at recipient512.cer " +
65       "RC240 SHA1_WITH_RSA true true sender512.pfx" +
66       System.getProperty("line.separator") +
67       "java TestSigEncHtml together.at recipient@together.at recipient512.cer " +
68       "DES MD5_WITH_RSA true true sender512.pfx .\\test\\Zip8Test.zip");
69       System.exit(-1);
70     }
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 see = null;
106
107     try {
108 // Construction of signed and enveloped smime object
109
see = new SignedAndEnvelopedSMIME(smtpHost, from, subject, "ISO-8859-1");
110
111 // sets html content withouth its resources
112
see.setContent(htmlContent,"text/html");
113 // instead of previous line, next line could be used to add external alternative
114
// text/plain message instead of autogenerated text/plain message.
115
// es.setContent( htmlContent,"text/html", "External text/plain message!" );
116

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

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

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

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

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