01: package org.bouncycastle.mail.smime.examples;
02:
03: import java.io.FileInputStream;
04: import java.io.FileOutputStream;
05: import java.io.IOException;
06: import java.io.InputStream;
07: import java.io.OutputStream;
08: import java.security.KeyStore;
09: import java.util.Enumeration;
10:
11: import javax.mail.MessagingException;
12: import javax.mail.internet.MimeBodyPart;
13:
14: public class ExampleUtils {
15: /**
16: * Dump the content of the passed in BodyPart to the file fileName.
17: *
18: * @throws MessagingException
19: * @throws IOException
20: */
21: public static void dumpContent(MimeBodyPart bodyPart,
22: String fileName) throws MessagingException, IOException {
23: //
24: // print mime type of compressed content
25: //
26: System.out
27: .println("content type: " + bodyPart.getContentType());
28:
29: //
30: // recover the compressed content
31: //
32: OutputStream out = new FileOutputStream(fileName);
33: InputStream in = bodyPart.getInputStream();
34:
35: byte[] buf = new byte[10000];
36: int len;
37:
38: while ((len = in.read(buf, 0, buf.length)) > 0) {
39: out.write(buf, 0, len);
40: }
41:
42: out.close();
43: }
44:
45: public static String findKeyAlias(KeyStore store, String storeName,
46: char[] password) throws Exception {
47: store.load(new FileInputStream(storeName), password);
48:
49: Enumeration e = store.aliases();
50: String keyAlias = null;
51:
52: while (e.hasMoreElements()) {
53: String alias = (String) e.nextElement();
54:
55: if (store.isKeyEntry(alias)) {
56: keyAlias = alias;
57: }
58: }
59:
60: if (keyAlias == null) {
61: throw new IllegalArgumentException(
62: "can't find a private key in keyStore: "
63: + storeName);
64: }
65:
66: return keyAlias;
67: }
68: }
|