public class CompressedContent extends java.lang.Object implements SMimeContentType
This class can be used to create compressed S/MIME emails in combination with the JavaMail API (javax.mail package).
The S/MIMEv3.1
(Secure/Multipurpose Internet Mail Extensions) protocol specifies
the compressed-data content type for compressing MIME entities
by using the CMS type CompressedData.
After preparing the MIME entity in the usual way it is packed it into a
CMS CompressedData object, wrapped by the CMS ContentInfo object. Finally the
ContentInfo is put into a S/MIME entity of content type application/pkcs7-mime
with the following MIME headers:
Content-Type: application/pkcs7-mime; smime-type=compressed-data;
name=smime.p7z
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=smime.p7z
After having created a new CompressedContent object,
the content to be compressed has to be supplied by means of a setContent,
setText, or setDataHandler
method. Before actually sending the message with the compressed content, the
compression algorithm has to be set, and
method setHeaders shall be called for properly updating
the message headers, e.g.:
// the mail session Session = ...; // create a new MIME message and set Subject, From, To,... headers MimeMessage msg = new MimeMessage(session); ... // This step could be signing, encrypting, etc... ... // create a CompressedContent object CompressedContent compressed = new CompressedContent(); // supply the content, e.g.: DataHandler dh = ...; compressed.setDataHandler(dh); // set the compression algorithm to be used, default is zlib compression // if a different algorithm is desired, aA recipient simply accesses the content of the compressed message, e.g.:CompressionProviderwhich provides // a different set of algorithms can be set using thesetCompressionProvidermethod compressed.setCompressionAlgorithm(CompressionProvider.zlib_compressed); // update message headers compressed.setHeaders(msg); // set the CompressedContent as content of the message msg.setContent(compressed, compressed.getContentType()); // send the message Transport.send(msg);
CompressedContent compressed = (CompressedContent)msg.getContent(); // get the (decompressed, original) content Object content = compressed.getContent(); ...For more information about the JavaMail architecture, and how to handling MIME messages, consult Sun´s JavaMail specification.
For using the IAIK-JCE S/MIME classes, an application also will need the following packages:
# # IAIK 'mailcap' file entries # multipart/signed;; x-java-content-handler=iaik.smime.signed_content application/x-pkcs7-signature;; x-java-content-handler=iaik.smime.signed_content application/x-pkcs7-mime;; x-java-content-handler=iaik.smime.encrypted_content application/x-pkcs10;; x-java-content-handler=iaik.smime.pkcs10_content application/pkcs7-signature;; x-java-content-handler=iaik.smime.signed_content application/pkcs7-mime;; x-java-content-handler=iaik.smime.encrypted_content application/pkcs10;; x-java-content-handler=iaik.smime.pkcs10_contentThe content handlers are registered by copying the mailcap file into the lib directory of your JDK (
String mailcapFileName = ...; MailcapCommandMap mc = new MailcapCommandMap(mailcapFileName); CommandMap.setDefaultCommandMap(mc);For a more detailed description of mailcap handling consult the Javadoc of the Activation Framework.
When creating a new CompressedContent to be sent per default the new S/MIME content types
(application/pkcs7-mime) are used. For using the old types (application/x-pkcs7-mime) call
the static useNewContentTypes
method of the SMimeParameters class before creating
a new CompressedContent
object, e.g.:
//switch to old content types SMimeParameters.useNewContentTypes(false); //create a SignedContent CompressedContent sc = new CompressedContent(); ...
CompressedDataStreamLOG| Constructor and Description |
|---|
CompressedContent()
Creates a new CompressedContent object.
|
CompressedContent(javax.activation.DataSource datasource)
Constructs a CompressedContent object from the given data source.
|
CompressedContent(SMimeContentType smimeCT) |
| Modifier and Type | Method and Description |
|---|---|
AlgorithmID |
getCompressionAlgorithm()
Return the Compression Algorithm
|
CompressionProvider |
getCompressionProvider() |
java.lang.Object |
getContent()
This method returns the content contained in the CompressedContent object.
|
java.io.InputStream |
getContentInputStream()
Returns an InputStream for this part's unparsed content.
|
java.lang.String |
getContentType()
Returns the ContentType and any attached parameters of this
CompressedContent.
|
javax.activation.DataHandler |
getDataHandler()
Return a DataHandler for the content within this part.
|
java.lang.String |
getSMimeType()
Returns the SMimeType string.
|
void |
setCompressionAlgorithm(AlgorithmID algorithmid)
Sets the Compression Algorithm which is used to Compress the Data.
|
void |
setCompressionProvider(CompressionProvider compressionprovider)
Sets the Compression Provider to be used for Compression.
|
void |
setContent(javax.mail.Multipart multipart)
This method sets the given Multipart object as this compressed object's
content.
|
void |
setContent(java.lang.Object content,
java.lang.String type)
A convenience method for setting this compresesed object's content.
|
void |
setContentContentHeaders(javax.mail.Header[] headers)
Set some headers for the entity to be compressed.
|
void |
setContentContentTransferEncoding(java.lang.String s)
Sets the content transfer encoding of the entity to be compressed.
|
void |
setDataHandler(javax.activation.DataHandler datahandler)
Sets the content wrapped by a
javax.activation.DataHandler. |
void |
setHeaders(javax.mail.Part part)
Sets additional headers of the message containing this CompressedContent.
|
void |
setSession(javax.mail.Session session)
Set the mail session to use when sending this message.
|
void |
setSMimeType()
Sets the smime-type parameter to "compressed-data".
|
void |
setText(java.lang.String text)
A convenience method that sets the given String as this
CompressedObject's content with a MIME type of "text/plain".
|
void |
writeTo(java.io.OutputStream outputstream)
Writes this CompressedContent DER encoded to the given output stream.
|
public CompressedContent(javax.activation.DataSource datasource)
throws java.io.IOException
encrypted_content supplying the data
source.
For more information on data handling using the
javax.activation.DataSource for "MIME type based" data
access, see Sun´s
JavaBeans Activation Framework (JAF) sepecification.
datasource - the DataSource supplying the enveloped datajava.io.IOException - if an I/O error occurs during reading the objectpublic CompressedContent(SMimeContentType smimeCT) throws javax.mail.MessagingException
javax.mail.MessagingExceptionpublic CompressedContent()
Use a proper setContent,setText,
or setDataHandlermethod for
supplying the content to be enveloped.
Before actually sending the Message with the compressed content, the
setHeadersmethod shall be called for
properly updating the message headers.
Typical usage (Wrapping an explicit signed message (content-type
"multipart/signed")):
MimeMessage msg = new MimeMessage(session); ... boolean implicit = false; SignedContent sc = new SignedContent(implicit); set the content to be signed sc.setContent(...); sc.setCertificates(certificates); sc.addSigner(privateKey, signerCertificate); ... CompressedContent cc = new CompressedContent(sc); cc.setCompressionAlgorithm(AlgorithmID.zlib_compress); cc.setContent(...); cc.setHeaders(msg); msg.setContent(cc, cc.getContentType()); Transport.send(msg);When using this constructor the smime-type parameter will not be set. Use the
setSMimeTypeparameter for setting the
"compressed-data" smime-type parameter.public void writeTo(java.io.OutputStream outputstream)
throws javax.mail.MessagingException,
java.io.IOException
java.io.IOException - if an error occurs writing to the streamjavax.mail.MessagingException - if an error occurs when fetching the data to be writtenpublic void setText(java.lang.String text)
throws javax.mail.MessagingException
text - the text that is the CompressedObject's contentjavax.mail.MessagingExceptionpublic void setDataHandler(javax.activation.DataHandler datahandler)
throws javax.mail.MessagingException
javax.activation.DataHandler.
This method provides the mechanism to set this CompressedContent's
content, e.g:
DataHandler dataHandler = new DataHandler(...); CompressedContent cc = new CompressedContent(); cc.setDataHandler(dataHandler);The DataHandler wraps around the actual content. It allows clients to discover the operations available on the content, and to instantiate the appropriate component to perform those operations. For more information consult Sun´s JavaBeans Activation Framework (JAF) sepecification.
datahandler - the DataHandler for the contentjavax.mail.MessagingExceptionpublic java.lang.Object getContent()
throws javax.mail.MessagingException,
java.io.IOException
javax.mail.MessagingException - If a messaging error occursjava.io.IOException - If there was an error decoding the datapublic java.lang.String getContentType()
Message msg = ...; CompressedContent cc = new CompressedContent(); msg.setContent(cc, cc.getContentType()); ...
getContentType in interface SMimeContentTypepublic void setHeaders(javax.mail.Part part)
Content-Disposition: attachment";
filename="smime.p7z"
Content-Transfer-Encoding: base64
setHeaders in interface SMimeContentTypepart - Part to add the headerspublic void setSession(javax.mail.Session session)
session - public void setSMimeType()
public void setCompressionAlgorithm(AlgorithmID algorithmid) throws java.security.NoSuchAlgorithmException
algorithmid - -
The AlgorithmID to be used for compressionjava.security.NoSuchAlgorithmException - -
If algorithm does not existpublic java.lang.String getSMimeType()
getSMimeType in interface SMimeContentTypepublic javax.activation.DataHandler getDataHandler()
throws javax.mail.MessagingException
javax.mail.MessagingException - if an error occurs when fetching the data handlerpublic java.io.InputStream getContentInputStream()
throws java.io.IOException
This method can be used to get the content of large messages.
java.io.IOExceptionpublic AlgorithmID getCompressionAlgorithm()
public void setContent(javax.mail.Multipart multipart)
throws javax.mail.MessagingException
multipart - the multipart object that is the compressed object's contentjavax.mail.MessagingExceptionpublic void setContent(java.lang.Object content,
java.lang.String type)
throws javax.mail.MessagingException
Note that a DataContentHandler class for the specified type should be available to the JavaMail implementation for this to work right; i.e., to do setContent(foobar, "application/x-foobar"), a DataContentHandler for "application/x-foobar" should be installed. Refer to the Java Activation Framework for more information.
content - a java objecttype - MIME type of this objectjavax.mail.MessagingExceptionpublic void setContentContentTransferEncoding(java.lang.String s)
throws javax.mail.MessagingException
setText or
setDataHandler is used for
supplying the content. However, the supplied content transfer
encoding will be ignored when the inner entity is a structured
entity.s - the content transfer encoding to be applied to the
inner entity to be signedjavax.mail.MessagingExceptionpublic void setContentContentHeaders(javax.mail.Header[] headers)
throws javax.mail.MessagingException
setText or
setDataHandler is used for
supplying the content. However, the supplied header will be ignored
when the inner entity is a structured entity. The supplied headers
are not cloned!headers - the headers to be applied to the
inner entity to be signedjavax.mail.MessagingExceptionpublic void setCompressionProvider(CompressionProvider compressionprovider)
CompressionProvider to be set. For
example, if you want to use a different algorithm than the default ZLIB
compression, you could create your own CompressionProvider class which
provides a different set of algorithms.compressionprovider - the compression provider to be usedpublic CompressionProvider getCompressionProvider()