KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

172       see.addRecipient(addressTO, "TO", cerFileName); // mandatory
173
// see.addRecipient(addressCC, "CC", cerFileName); // optional
174
// see.addRecipient(addressBCC, "BCC", cerFileName); // optional
175

176       see.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
177
see.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
178
see.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional
179

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