KickJava   Java API By Example, From Geeks To Geeks.

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


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

44 public class TestEncSigGeneratedHtml
45 {
46
47   public static void main(String JavaDoc[] args)
48   {
49
50     String JavaDoc subject = "S/MIME enveloped and signed message - Subject test: ÜüÄäÖöÜüß";
51     String JavaDoc from = "sender@together.at";
52     String JavaDoc password = "together";
53
54     if (args.length < 8)
55     {
56       System.err.println(
57       System.getProperty("line.separator") +
58       "Usage of TestEncSigGeneratedHtml: " +
59       System.getProperty("line.separator") +
60       "java TestEncSigGeneratedHtml <mailHost> <mailAddress> <cerFileName> " +
61       "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " +
62       "<pfxFileName> [<attachment>]"+
63       System.getProperty("line.separator") +
64       System.getProperty("line.separator") +
65       "Examples:" +
66       System.getProperty("line.separator") +
67       "java TestEncSigGeneratedHtml together.at recipient@together.at recipient512.cer " +
68       "RC240 SHA1_WITH_RSA true true sender512.pfx" +
69       System.getProperty("line.separator") +
70       "java TestEncSigGeneratedHtml together.at recipient@together.at recipient512.cer " +
71       "DES MD5_WITH_RSA true true sender512.pfx .\\test\\Zip8Test.zip");
72
73       System.exit(-1);
74     }
75
76     String JavaDoc smtpHost = args[0];
77     String JavaDoc addressTO = args[1];
78     String JavaDoc cerFileName = args[2];
79     String JavaDoc algorithmName = args[3];
80     String JavaDoc digestAlgorithm = args[4];
81
82     boolean includingCert = true;
83     if (args[5].equals("true"))
84       includingCert = true;
85     else
86       includingCert = false;
87
88     boolean includingSignAttrib = true;
89     if (args[6].equals("true"))
90       includingSignAttrib = true;
91     else
92       includingSignAttrib = false;
93
94     String JavaDoc pfxfileName = args[7];
95
96     String JavaDoc fileName = null;
97     if (args.length > 8)
98       fileName = args[8];
99
100     String JavaDoc addressCC = "recipient@together.at";
101     String JavaDoc addressBCC = "recipient@together.at";
102
103     subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " +
104               args[6] + " " + args[7] + " " + subject;
105
106     SignedAndEnvelopedSMIME ess = null;
107
108     try {
109       ExampleGenerator generator = new ExampleGenerator();
110
111 // Address for resource green.gif which will be obtained by program generation
112
// from instances of the ExampleGenerator class as InputSteam must be added to
113
// html code as virtual. For more information refer to setContent method of
114
// SignedSMIME class.
115
String JavaDoc green = "*****000generated.gif";
116
117 // Address for resource red.gif class will be defined as absolute address to
118
// location on the file system.
119
File JavaDoc redFile = new File JavaDoc("./test/pictures/red.gif");
120       String JavaDoc red = redFile.getAbsoluteFile().getCanonicalPath();
121
122 // Address for resource blue.gif class will be defined as file type of url
123
// address.
124
File JavaDoc blueFile = new File JavaDoc("./test/pictures/blue.gif");
125       String JavaDoc blue = "file:///" + blueFile.getAbsoluteFile().getCanonicalPath();
126       blue = blue.replace('\\','/');
127
128 // Addresses for resources orange.jpg and yellow.jpg are left relative.
129
// Default values of resources in ExampleGenerator are relative, as it can be
130
// seen in the original file of html example HtmlTest.html. To resolve this
131
// relative adresses common directory path must be obtained from the root.
132
// For more information refer to setContent method of
133
// SignedSMIME class.
134
String JavaDoc commonPath = new File JavaDoc("./test/HtmlTest.html").getParent();
135
136       generator.setResourceInExampleHtml("GREEN", green);
137       generator.setResourceInExampleHtml("RED", red);
138       generator.setResourceInExampleHtml("BLUE", blue);
139
140 // Simulation of resources (green.gif) obtained by program.
141
InputStream JavaDoc[] in = {generator.getGifImage()};
142
143 // Construction of enveloped and signed smime object
144
ess = new SignedAndEnvelopedSMIME(smtpHost, from, subject, "ISO-8859-1");
145
146 // sets html content with its resources
147
ess.setContent( generator.getHtmlString(), "text/html", commonPath, in );
148 // instead of previous line, next line could be used to add external alternative
149
// text/plain message instead of autogenerated text/plain message.
150
// ess.setContent( generator.getHtmlStream(), "text/html", commonPath, in, "External text/plain message!" );
151

152 // Attachments can be added to message from program (using Input stream). In
153
// the following line zip file will be generated and added as attachment. You must
154
// define some file names in order to give appropriate mime-type to your message.
155
ess.addAttachment(generator.getZipStream(), "test.zip");
156
157 // File can also be added from file system
158
File JavaDoc attachment2 = new File JavaDoc("./test/AdobeAcrobatTest.pdf");
159       ess.addAttachment(attachment2);
160       File JavaDoc attachment3 = new File JavaDoc("./test/Word2000Test.doc");
161       ess.addAttachment(attachment3);
162
163       if (fileName!=null) {
164         ess.addAttachment(fileName); // optional - use this if send attachment
165
}
166
167       ess.setReply(from); // optional
168

169       ess.addRecipient(addressTO, "TO", cerFileName); // mandatory
170
// ess.addRecipient(addressCC, "CC", cerFileName); // optional
171
// ess.addRecipient(addressBCC, "BCC", cerFileName); // optional
172

173       ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
174
ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
175
ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional
176

177       ess.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib);
178
179       if (algorithmName.equals("RC240"))
180       {
181         System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... ");
182         ess.signingAndEnveloping("ENCRYPT_FIRST"); // instead of this next line could be used
183
// ess.enveloping(ess.RC2_CBC, 40, "ENCRYPT_FIRST");
184
}
185       else if (algorithmName.equals("RC264"))
186       {
187         System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... ");
188         ess.signingAndEnveloping(ess.RC2_CBC, 64, "ENCRYPT_FIRST"); // send message with RC2 - 64 bits algorithm
189
}
190       else if (algorithmName.equals("RC2128"))
191       {
192         System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... ");
193         ess.signingAndEnveloping(ess.RC2_CBC, 128, "ENCRYPT_FIRST"); // send message with RC2 - 128 bits algorithm
194
}
195       else if (algorithmName.equals("DES"))
196       {
197         System.out.println("Creating enveloped and signed message with DES algorithm... ");
198         ess.signingAndEnveloping(ess.DES, 56, "ENCRYPT_FIRST"); // send message with DES - 56 bits algorithm
199
}
200       else if (algorithmName.equals("3DES"))
201       {
202         System.out.println("Creating enveloped and signed message with 3DES algorithm... ");
203         ess.signingAndEnveloping(ess.DES_EDE3_CBC, 192, "ENCRYPT_FIRST"); // send message with 3DES - 192 bits algorithm
204
}
205
206       System.out.print("Sending enveloped and signed message ... ");
207       ess.send(); // instead of this next line could be used
208
// Transport.send(ess.getSignedMessage());
209
System.out.println("done.");
210
211     }
212     catch (Exception JavaDoc e) {
213       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
214
if(e instanceof SMIMEException) {
215         SMIMEException eTmp = (SMIMEException)e;
216 // eTmp.loggingErrors(null); //logging
217
eTmp.displayErrors(null);
218        e = eTmp.getNonSMIMEException();
219        if(e != null)
220          e.printStackTrace();
221       }
222       else {
223         e.printStackTrace();
224       }
225     }
226   }
227 }
Popular Tags