KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.enhydra.oyster.smime.EnvelopedSMIME;
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 process. Encrypted text/html message with
19  * attachments can be sent by this test. This example is test for composing
20  * of email message content by html code which is generated by application, and
21  * adding resources assocated in html code from the application, and from the
22  * file system. Also, attachments are added to message from the application,
23  * or from file system. To get help for this example type:
24  * "java org.enhydra.oyster.test.TestEncryptGeneratedHtml" 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;attachment&gt;]<BR>
30  * <BR>
31  * &lt;algorithmName&gt; could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
32  * <BR>
33  * Note that for this example email address "FROM" is fixed to: "sender@together.at".
34  * You should change this values in source code of TestEncryptGeneratedHtml.java
35  * in order to use them with other "FROM" address.
36  */

37 public class TestEncryptGeneratedHtml
38 {
39
40   public static void main (String JavaDoc[] args)
41   {
42
43     String JavaDoc subject = "S/MIME encrypted message - Subject test: ";
44     String JavaDoc from = "sender@together.at";
45
46     if (args.length < 4)
47     {
48       System.err.println(
49       System.getProperty("line.separator") +
50       "Usage of TestEncryptGeneratedHtml: " +
51       System.getProperty("line.separator") +
52       "java TestEncryptGeneratedHtml <mailHost> <mailAddress> <cerFileName> " +
53       "<algorithmName> [<attachment>]" +
54       System.getProperty("line.separator") +
55       System.getProperty("line.separator") +
56       "Examples:" +
57       System.getProperty("line.separator") +
58       "java TestEncryptGeneratedHtml together.at recipient@together.at " +
59       "recipient512.cer RC240 " +
60       System.getProperty("line.separator") +
61       "java TestEncryptGeneratedHtml together.at recipient@together.at " +
62       "recipient512.cer DES .\\test\\Zip8Test.zip");
63       System.exit(-1);
64     }
65     String JavaDoc smtpHost = args[0];
66     String JavaDoc addressTO = args[1];
67     String JavaDoc cerFileName = args[2];
68     String JavaDoc algorithmName = args[3];
69     String JavaDoc fileName = null;
70     if (args.length > 4)
71       fileName = args[4];
72
73     String JavaDoc addressCC = "recipient@together.at";
74     String JavaDoc addressBCC = "recipient@together.at";
75
76     subject = algorithmName + " " + cerFileName + " " + subject;
77
78     EnvelopedSMIME es = null;
79
80     try
81     {
82       ExampleGenerator generator = new ExampleGenerator();
83
84 // Address for resource green.gif which will be obtained by program generation
85
// from instances of the ExampleGenerator class as InputSteam must be added to
86
// html code as virtual. For more information refer to setContent method of
87
// SignedSMIME class.
88
String JavaDoc green = "*****000generated.gif";
89
90 // Address for resource red.gif class will be defined as absolute address to
91
// location in the file system.
92
File JavaDoc redFile = new File JavaDoc("./test/pictures/red.gif");
93       String JavaDoc red = redFile.getAbsoluteFile().getCanonicalPath();
94
95 // Address for resource blue.gif class will be defined as file type of url
96
// address.
97
File JavaDoc blueFile = new File JavaDoc("./test/pictures/blue.gif");
98       String JavaDoc blue = "file:///" + blueFile.getAbsoluteFile().getCanonicalPath();
99       blue = blue.replace('\\','/');
100
101 // Addresses for resources orange.jpg and yellow.jpg are left relative.
102
// Default values of resources in ExampleGenerator are relative, as it can be
103
// seen in the original file of html example HtmlTest.html. To resolve this
104
// relative adresses common directory path must be obtained from the root.
105
// For more information refer to setContent method of
106
// SignedSMIME class.
107
String JavaDoc commonPath = new File JavaDoc("./test/HtmlTest.html").getParent();
108
109       generator.setResourceInExampleHtml("GREEN", green);
110       generator.setResourceInExampleHtml("RED", red);
111       generator.setResourceInExampleHtml("BLUE", blue);
112
113 // Simulation of resources (green.gif) obtained by program.
114
InputStream JavaDoc[] in = {generator.getGifImage()};
115
116 // Construction of enveloped smime object
117
es = new EnvelopedSMIME(smtpHost, from, subject, "ISO-8859-1");
118
119 // sets html content with its resources
120
es.setContent( generator.getHtmlString(), "text/html", commonPath, in );
121 // instead of previous line, next line could be used to add external alternative
122
// text/plain message instead of autogenerated text/plain message.
123
// es.setContent( generator.getHtmlStream(), "text/html", commonPath, in, "External text/plain message!" );
124

125 // Attachments can be added to message from program (using Input stream). In
126
// the following line zip file will be generated and added as attachment. You must
127
// define some file names in order to give appropriate mime-type to your message.
128
es.addAttachment(generator.getZipStream(), "test.zip");
129
130 // File can also be added from file system
131
File JavaDoc attachment2 = new File JavaDoc("./test/AdobeAcrobatTest.pdf");
132       es.addAttachment(attachment2);
133       File JavaDoc attachment3 = new File JavaDoc("./test/Word2000Test.doc");
134       es.addAttachment(attachment3);
135
136       if (fileName!=null) {
137         es.addAttachment(fileName); // optional - use this if send attachment
138
}
139
140       es.setReply(from); // optional
141

142       es.addRecipient(addressTO, "TO", cerFileName); // mandatory
143
// es.addRecipient(addressCC, "CC", cerFileName); // optional
144
// es.addRecipient(addressBCC, "BCC", cerFileName); // optional
145

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