KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email transport capabilities.
4  * @Author Vladan Obradovic
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 javax.mail.Transport JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.File JavaDoc;
15
16 /**
17  * Tests signing process. Signed text/html message with or withouth attachments
18  * can be sent by this test. This example is test for composing of email message
19  * content by html code which is given from the file system. To get help for
20  * this example type: "java org.enhydra.oyster.test.TestSignedHtml" in command
21  * line. It is assumed that oyster_tests.jar is in your classpath.<BR>
22  * <BR>
23  * Parameters passed to example are:<BR>
24  * &lt;mailHost&gt; &lt;mailAddress&gt; &lt;digestAlgorithm&gt; &lt;includingCert&gt;
25  * &lt;includingSignAttrib&gt; &lt;pfxFileName&gt; &lt;externalSigning&gt;
26  * [&lt;attachment&gt;]<BR>
27  * <BR>
28  * &lt;digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
29  * &lt;includingCert> could be: true/false<BR>
30  * &lt;includingSignAttrib> could be: true/false<BR>
31  * &lt;externalSigning> could be: true/false<BR>
32  * <BR>
33  * Note that for this example passwords for .pfx or .p12 files are fixed to
34  * "together". All .pfx files or .p12 files provided with this example have this
35  * password. Also, email address "FROM" is fixed to: "sender@together.at".
36  * You should change this values in source code of TestSignedHtml.java in order
37  * to use them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
38  */

39 public class TestSignedHtml
40 {
41
42   public static void main(String JavaDoc[] args)
43   {
44     String JavaDoc subject = "S/MIME signed message - Subject test: ";
45     String JavaDoc from = "sender@together.at";
46     String JavaDoc password = "together";
47
48     if (args.length < 7)
49     {
50       System.err.println(
51       System.getProperty("line.separator") +
52       "Usage of TestSignedHtml: " +
53       System.getProperty("line.separator") +
54       "java TestSignedHtml <mailHost> <mailAddress> <digestAlgorithm> " +
55       "<includingCert> <includingSignAttrib> <pfxFileName> <externalSigning>" +
56       "[<attachment>]" +
57       System.getProperty("line.separator") +
58       System.getProperty("line.separator") +
59       "Examples:" +
60       System.getProperty("line.separator") +
61       "java TestSignedHtml together.at recipient@together.at SHA1_WITH_RSA true " +
62       "true sender512.pfx true"+
63       System.getProperty("line.separator") +
64       "java TestSignedHtml together.at recipient@together.at SHA1_WITH_DSA true " +
65       "true senderDSA512.pfx false .\\test\\Zip8Test.zip");
66       System.exit(-1);
67     }
68
69     String JavaDoc smtpHost = args[0];
70     String JavaDoc addressTO = args[1];
71     String JavaDoc digestAlgorithm = args[2];
72
73     boolean includingCert = true;
74     if (args[3].equalsIgnoreCase("true"))
75       includingCert = true;
76     else
77       includingCert = false;
78
79     boolean includingSignAttrib = true;
80     if (args[4].equalsIgnoreCase("true"))
81       includingSignAttrib = true;
82     else
83       includingSignAttrib = false;
84
85     String JavaDoc pfxfileName = args[5];
86
87     boolean externalSigning = true;
88     if (args[6].equalsIgnoreCase("true"))
89       externalSigning = true;
90     else
91       externalSigning = false;
92
93     String JavaDoc fileName = null;
94     if (args.length > 7)
95       fileName = args[7];
96
97     String JavaDoc addressCC = "recipient@together.at";
98     String JavaDoc addressBCC = "recipient@together.at";
99
100     subject = digestAlgorithm + " " + args[3] + " " + args[4] + " " +
101               pfxfileName + " " + args[6] + " " + subject;
102
103     File JavaDoc htmlContent = new File JavaDoc("./test/HtmlTest.html");
104
105     SignedSMIME ss = null;
106
107     try {
108
109 // Construction of signed smime object
110
ss = new SignedSMIME(smtpHost, from, subject, "ISO-8859-1");
111
112 // sets html content withouth its resources
113
ss.setContent(htmlContent,"text/html");
114 // instead of previous line, next line could be used to add external alternative
115
// text/plain message instead of autogenerated text/plain message.
116
// ss.setContent( htmlContent,"text/html", "External text/plain message!" );
117

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

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

128       ss.addRecipient(addressTO, "TO"); // mandatory
129
// ss.addRecipient(addressCC, "CC"); // optional
130
// ss.addRecipient(addressBCC, "BCC"); // optional
131

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

136       ss.addSigner(pfxfileName, password, digestAlgorithm,
137                                 includingCert, includingSignAttrib); // mandatory
138

139       System.out.println("Creating signed message with " + digestAlgorithm + " algorithm ... ");
140       ss.signing(externalSigning);
141       System.out.print("Sending signed message ... ");
142       ss.send(); // instead of this next line could be used
143
// Transport.send(es.getSignedMessage());
144
System.out.println("done.");
145     }
146     catch (Exception JavaDoc e) {
147       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
148
if(e instanceof SMIMEException) {
149         SMIMEException eTmp = (SMIMEException)e;
150 // eTmp.loggingErrors(null); //logging
151
eTmp.displayErrors(null);
152        e = eTmp.getNonSMIMEException();
153        if(e != null)
154          e.printStackTrace();
155       }
156       else {
157         e.printStackTrace();
158       }
159     }
160   }
161 }
Popular Tags