KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.enhydra.oyster.smime.SignedSMIME;
11 import org.enhydra.oyster.exception.SMIMEException;
12 import java.io.File JavaDoc;
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16
17 /**
18  * Tests signing process. Signed 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.TestSignedGeneratedHtml" 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;digestAlgorithm&gt; &lt;includingCert&gt;
29  * &lt;includingSignAttrib&gt; &lt;pfxFileName&gt; &lt;externalSigning&gt;
30  * [&lt;attachment&gt;]<BR>
31  * <BR>
32  * &lt;digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
33  * &lt;includingCert> could be: true/false<BR>
34  * &lt;includingSignAttrib> could be: true/false<BR>
35  * &lt;externalSigning> could be: true/false<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 TestSignedGeneratedHtml.java
41  * in order to use them with other .pfx or .p12 files and corresponding "FROM"
42  * addresses and passwords.
43  */

44 public class TestSignedGeneratedHtml
45 {
46
47   public static void main(String JavaDoc[] args)
48   {
49
50     String JavaDoc subject = "S/MIME signed message - Subject test: ÜüÄäÖöÜüß";
51     String JavaDoc from = "sender@together.at";
52     String JavaDoc password = "together";
53
54     if (args.length < 7)
55     {
56       System.err.println(
57       System.getProperty("line.separator") +
58       "Usage of TestSignedGeneratedHtml: " +
59       System.getProperty("line.separator") +
60       "java TestSignedGeneratedHtml <mailHost> <mailAddress> <digestAlgorithm> " +
61       "<includingCert> <includingSignAttrib> <pfxFileName> <externalSigning>" +
62       "[<attachment>]" +
63       System.getProperty("line.separator") +
64       System.getProperty("line.separator") +
65       "Examples:" +
66       System.getProperty("line.separator") +
67       "java TestSignedGeneratedHtml together.at recipient@together.at SHA1_WITH_RSA true " +
68       "true sender512.pfx true"+
69       System.getProperty("line.separator") +
70       "java TestSignedGeneratedHtml together.at recipient@together.at SHA1_WITH_DSA true " +
71       "true senderDSA512.pfx false .\\test\\Zip8Test.zip");
72       System.exit(-1);
73     }
74
75     String JavaDoc smtpHost = args[0];
76     String JavaDoc addressTO = args[1];
77     String JavaDoc digestAlgorithm = args[2];
78
79     boolean includingCert = true;
80     if (args[3].equalsIgnoreCase("true"))
81       includingCert = true;
82     else
83       includingCert = false;
84
85     boolean includingSignAttrib = true;
86     if (args[4].equalsIgnoreCase("true"))
87       includingSignAttrib = true;
88     else
89       includingSignAttrib = false;
90
91     String JavaDoc pfxfileName = args[5];
92
93     boolean externalSigning = true;
94     if (args[6].equalsIgnoreCase("true"))
95       externalSigning = true;
96     else
97       externalSigning = false;
98
99     String JavaDoc fileName = null;
100     if (args.length > 7)
101       fileName = args[7];
102
103     String JavaDoc addressCC = "recipient@together.at";
104     String JavaDoc addressBCC = "recipient@together.at";
105
106     subject = digestAlgorithm + " " + args[3] + " " + args[4] + " " +
107               pfxfileName + " " + args[6] + " " + subject;
108
109     SignedSMIME ss = 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 smime object
147
ss = new SignedSMIME(smtpHost, from, subject, "ISO-8859-1");
148
149 // sets html content with its resources
150
ss.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
// ss.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
ss.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       ss.addAttachment(attachment2);
163       File JavaDoc attachment3 = new File JavaDoc("./test/Word2000Test.doc");
164       ss.addAttachment(attachment3);
165
166       if (fileName!=null) {
167         ss.addAttachment(fileName); // optional - use this if send attachment
168
}
169
170       ss.setReply(from); // optional
171

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

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

180       ss.addSigner(pfxfileName, password, digestAlgorithm,
181                                 includingCert, includingSignAttrib); // mandatory
182

183       System.out.println("Creating signed message with " + digestAlgorithm + " algorithm ... ");
184       ss.signing(externalSigning);
185       System.out.print("Sending signed message ... ");
186       ss.send(); // instead of this next line could be used
187
// Transport.send(es.getSignedMessage());
188
System.out.println("done.");
189     }
190     catch (Exception JavaDoc e) {
191       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
192
if(e instanceof SMIMEException) {
193         SMIMEException eTmp = (SMIMEException)e;
194 // eTmp.loggingErrors(null); //logging
195
eTmp.displayErrors(null);
196        e = eTmp.getNonSMIMEException();
197        if(e != null)
198          e.printStackTrace();
199       }
200       else {
201         e.printStackTrace();
202       }
203     }
204   }
205
206
207 }
Popular Tags