001: package org.bouncycastle.mail.smime;
002:
003: import java.io.IOException;
004: import java.io.OutputStream;
005:
006: import javax.activation.CommandMap;
007: import javax.activation.MailcapCommandMap;
008: import javax.mail.MessagingException;
009: import javax.mail.internet.MimeBodyPart;
010: import javax.mail.internet.MimeMessage;
011:
012: import org.bouncycastle.cms.CMSCompressedDataGenerator;
013: import org.bouncycastle.cms.CMSCompressedDataStreamGenerator;
014:
015: /**
016: * General class for generating a pkcs7-mime compressed message.
017: *
018: * A simple example of usage.
019: *
020: * <pre>
021: * SMIMECompressedGenerator fact = new SMIMECompressedGenerator();
022: *
023: * MimeBodyPart smime = fact.generate(content, algorithm);
024: * </pre>
025: *
026: * <b>Note:<b> Most clients expect the MimeBodyPart to be in a MimeMultipart
027: * when it's sent.
028: */
029: public class SMIMECompressedGenerator extends SMIMEGenerator {
030: public static final String ZLIB = CMSCompressedDataGenerator.ZLIB;
031:
032: private static final String COMPRESSED_CONTENT_TYPE = "application/pkcs7-mime; name=\"smime.p7z\"; smime-type=compressed-data";
033:
034: static {
035: MailcapCommandMap mc = (MailcapCommandMap) CommandMap
036: .getDefaultCommandMap();
037:
038: mc
039: .addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
040: mc
041: .addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
042:
043: CommandMap.setDefaultCommandMap(mc);
044: }
045:
046: /**
047: * generate an compressed object that contains an SMIME Compressed
048: * object using the given compression algorithm.
049: */
050: private MimeBodyPart make(MimeBodyPart content,
051: String compressionOID) throws SMIMEException {
052: try {
053: MimeBodyPart data = new MimeBodyPart();
054:
055: data.setContent(new ContentCompressor(content,
056: compressionOID), COMPRESSED_CONTENT_TYPE);
057: data.addHeader("Content-Type", COMPRESSED_CONTENT_TYPE);
058: data.addHeader("Content-Disposition",
059: "attachment; filename=\"smime.p7z\"");
060: data.addHeader("Content-Description",
061: "S/MIME Compressed Message");
062: data.addHeader("Content-Transfer-Encoding", encoding);
063:
064: return data;
065: } catch (MessagingException e) {
066: throw new SMIMEException(
067: "exception putting multi-part together.", e);
068: }
069: }
070:
071: /**
072: * generate an compressed object that contains an SMIME Compressed
073: * object using the given provider from the contents of the passed in
074: * message
075: */
076: public MimeBodyPart generate(MimeBodyPart content,
077: String compressionOID) throws SMIMEException {
078: return make(makeContentBodyPart(content), compressionOID);
079: }
080:
081: /**
082: * generate an compressed object that contains an SMIME Compressed
083: * object using the given provider from the contents of the passed in
084: * message
085: */
086: public MimeBodyPart generate(MimeMessage message,
087: String compressionOID) throws SMIMEException {
088: try {
089: message.saveChanges(); // make sure we're up to date.
090: } catch (MessagingException e) {
091: throw new SMIMEException("unable to save message", e);
092: }
093:
094: return make(makeContentBodyPart(message), compressionOID);
095: }
096:
097: private class ContentCompressor implements SMIMEStreamingProcessor {
098: private final MimeBodyPart _content;
099: private final String _compressionOid;
100:
101: ContentCompressor(MimeBodyPart content, String compressionOid) {
102: _content = content;
103: _compressionOid = compressionOid;
104: }
105:
106: public void write(OutputStream out) throws IOException {
107: CMSCompressedDataStreamGenerator cGen = new CMSCompressedDataStreamGenerator();
108:
109: OutputStream compressed = cGen.open(out, _compressionOid);
110:
111: try {
112: _content.writeTo(compressed);
113:
114: compressed.close();
115: } catch (MessagingException e) {
116: throw new IOException(e.toString());
117: }
118: }
119: }
120: }
|