KickJava   Java API By Example, From Geeks To Geeks.

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


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/plain message with or
18  * withouth attachments can be sent by this test. To get help for this
19  * example type: "java org.enhydra.oyster.test.TestSigned" in command line.
20  * It is assumed that oyster_tests.jar is in your classpath.<BR>
21  * <BR>
22  * Parameters passed to example are:<BR>
23  * &lt;mailHost&gt; &lt;mailAddress&gt; &lt;digestAlgorithm&gt; &lt;includingCert&gt;
24  * &lt;includingSignAttrib&gt; &lt;pfxFileName&gt; &lt;externalSigning&gt;
25  * [&lt;attachment&gt;]<BR>
26  * <BR>
27  * &lt;digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
28  * &lt;includingCert> could be: true/false<BR>
29  * &lt;includingSignAttrib> could be: true/false<BR>
30  * &lt;externalSigning> could be: true/false<BR>
31  * <BR>
32  * Note that for this example passwords for .pfx or .p12 files are fixed to
33  * "together". All .pfx files or .p12 files provided with this example have this
34  * password. Also, email address "FROM" is fixed to: "sender@together.at".
35  * You should change this values in source code of TestSigned.java in order to use
36  * them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
37  */

38 public class TestSigned
39 {
40
41   public static void main(String JavaDoc[] args)
42   {
43     String JavaDoc subject = "S/MIME signed message - Subject test: ";
44     String JavaDoc content = "S/MIME signed message example\r\nContent 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 TestSigned: " +
53       System.getProperty("line.separator") +
54       "java TestSigned <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 TestSigned together.at recipient@together.at SHA1_WITH_RSA true " +
62       "true sender512.pfx true"+
63       System.getProperty("line.separator") +
64       "java TestSigned 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     SignedSMIME ss = null;
104
105     try {
106
107 // Construction of signed smime object
108
ss = new SignedSMIME(smtpHost, from, subject, content, "ISO-8859-1");
109
110       if (fileName!=null) {
111         ss.addAttachment(fileName); // optional - use this if send attachment
112
}
113
114       ss.setReply(from); // optional
115

116       ss.addRecipient(addressTO, "TO"); // mandatory
117
// ss.addRecipient(addressCC, "CC"); // optional
118
// ss.addRecipient(addressBCC, "BCC"); // optional
119

120       ss.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
121
ss.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
122
ss.setCapabilities("SIGNATURE", 1, 2, 3, 4, 0); // optional
123

124       ss.addSigner(pfxfileName, password, digestAlgorithm,
125                                 includingCert, includingSignAttrib); // mandatory
126

127       System.out.println("Creating signed message with " + digestAlgorithm + " algorithm ... ");
128       ss.signing(externalSigning);
129       System.out.print("Sending signed message ... ");
130       ss.send(); // instead of this next line could be used
131
// Transport.send(es.getSignedMessage());
132
System.out.println("done.");
133     }
134     catch (Exception JavaDoc e) {
135       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
136
if(e instanceof SMIMEException) {
137         SMIMEException eTmp = (SMIMEException)e;
138 // eTmp.loggingErrors(null); //logging
139
eTmp.displayErrors(null);
140        e = eTmp.getNonSMIMEException();
141        if(e != null)
142          e.printStackTrace();
143       }
144       else {
145         e.printStackTrace();
146       }
147     }
148   }
149 }
Popular Tags