iaik.smime
Class CompressedContent

java.lang.Object
  extended by iaik.smime.SMimeContent
      extended by iaik.smime.CompressedContent
All Implemented Interfaces:
CryptoContent

public class CompressedContent
extends SMimeContent

This class can be used to create compressed S/MIME emails in combination with the JavaMail API (javax.mail package).

The S/MIME (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 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);
 ...
 // created a CompressedContent object
 CompressedContent compressed = new CompressedContent();
 // supply the content, e.g.:
 DataHandler dh = ...;
 compressed.setDataHandler(dh);
 // set the compression algorithm to be used, e.g.:
 compressed.setCompressionAlgorithm((AlgorithmID)CMSAlgorithmID.zlib_compress.clone());
 // 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);
 
A recipient simply accesses the content of the compressed message, e.g.:
 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 the JavaMail specification.

For using the IAIK-CMS S/MIME library, you also will need the following packages:

The JAF assignment between MIME-types and content handlers is done by means of a RFC 1524 mailcap file which is included in the IAIK-CMS distribution. It defines the following classes as content handlers for the corresponding MIME types:
 #
 # 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_content
 
The content handlers are registered by copying the mailcap file into the lib directory of your JDK (/lib). Alternatively you may register the IAIK-S/MIME mailcap file dynamically by using the default command map:
 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();
 ...
 

See Also:
CompressedDataStream

Constructor Summary
CompressedContent()
          Creates a new CompressedContent object.
CompressedContent(CryptoContent cryptoContent)
          Creates a new S/MIME CompressedContent object for the a content to be signed (or encrypted) and then compressed.
CompressedContent(javax.activation.DataSource dataSource)
          Constructs a CompressedContent object from the given data source.
 
Method Summary
 AlgorithmID getCompressionAlgorithm()
          Returns the compression algorithm (including any associated parameters) of this CompressedContent.
 java.io.InputStream getContentInputStream()
          Returns an InputStream with the unparsed content.
 javax.activation.DataHandler getDataHandler()
          Return a DataHandler holding the content.
 java.lang.String getSMimeType()
          Returns the smime-type parameter ("compressed-data").
 void setCompressionAlgorithm(AlgorithmID compressionAlgorithm)
          Sets the compression algorithm to be used for content compressiom.
 void setHeaders(javax.mail.Part part)
          Sets additional headers of the part (message) containing this CompressedContent.
 void writeTo(java.io.OutputStream os)
          Writes this CompressedContent BER encoded to the given output stream.
 
Methods inherited from class iaik.smime.SMimeContent
getContent, getContentType, getInputStream, setBlockSize, setContent, setContent, setContentContentHeaders, setContentContentTransferEncoding, setDataHandler, setText
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompressedContent

public CompressedContent()
Creates a new CompressedContent object.

Use a proper setContent, setText, or setDataHandler method for supplying the content to be compressed. 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);
 ...
 // created a CompressedContent object
 CompressedContent compressed = new CompressedContent();
 // supply the content, e.g.:
 DataHandler dh = ...;
 compressed.setDataHandler(dh);
 // set the compression algorithm to be used, e.g.:
 compressed.setCompressionAlgorithm((AlgorithmID)CMSAlgorithmID.zlib_compressed.clone());
 // 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);
 
The smime-tpye parameter of the Content-Type header is set to "compressed-data", and the name parameter is set to "smime.p7z".


CompressedContent

public CompressedContent(CryptoContent cryptoContent)
                  throws javax.mail.MessagingException
Creates a new S/MIME CompressedContent object for the a content to be signed (or encrypted) and then compressed.

Parameters:
cryptoContent - the signed (or encrypted) content to be compressed (however it may be preferable to first compress and then sign or encrypt)
Throws:
javax.mail.MessagingException

CompressedContent

public CompressedContent(javax.activation.DataSource dataSource)
                  throws java.io.IOException
Constructs a CompressedContent object from the given data source. The given data source is expected to supply an BER encoded CMS ContentInfo object holding the compressedData. During a mail session this constructor usually will not be called directly. Rather it is called by a proper data content handler supplying the data source.

For more information on data handling using the javax.activation.DataSource for "MIME type based" data access, see the JavaBeans Activation Framework (JAF) specification.

Parameters:
dataSource - the DataSource supplying the compressed data
Throws:
java.io.IOException - if an I/O error occurs during reading the object
Method Detail

setHeaders

public void setHeaders(javax.mail.Part part)
Sets additional headers of the part (message) containing this CompressedContent. This method sets the Content-Disposition and Content-Transfer-Encoding headers:
 Content-Disposition: attachment";
     filename="smime.p7z"
 Content-Transfer-Encoding: base64
 
It is highly recommended to call this method to set the headers above for the part containing this CompressedContent to avoid processing overhead :
 MimeMessage msg = ...;
 CompressedContent compressed = ...;
 compressed.setHeaders(msg);
 
If this method is not called, JavaMail may run method writeTo twice to determine the content transfer encoding to be used.

Parameters:
part - the part for which the Content-Disposition and Content-Transfer-Encoding headers should be set

getDataHandler

public javax.activation.DataHandler getDataHandler()
                                            throws javax.mail.MessagingException
Return a DataHandler holding the content.

Specified by:
getDataHandler in class SMimeContent
Returns:
a DataHandler with the content
Throws:
javax.mail.MessagingException - if an error occurs when fetching the data handler

getContentInputStream

public java.io.InputStream getContentInputStream()
                                          throws java.io.IOException
Returns an InputStream with the unparsed content. No mail-specific transfer encodings will be decoded before the input stream is provided.

Attention!This method may be called only once since a stream only can be read once. However, if any of the content accessing methods like getContent or getDataHandler is called before calling getContentInputStream, getContentInputStream may be called repeadetly since in this case the content is internally kept by means of a DataHandler.

Returns:
an InputStream holding the unparsed content
Throws:
java.io.IOException - if an error occurs when reading the data

getSMimeType

public java.lang.String getSMimeType()
Returns the smime-type parameter ("compressed-data").

Returns:
the smime-type parameter ("compressed-data")

setCompressionAlgorithm

public void setCompressionAlgorithm(AlgorithmID compressionAlgorithm)
                             throws java.security.NoSuchAlgorithmException
Sets the compression algorithm to be used for content compressiom.

Parameters:
compressionAlgorithm - the algorithm to be used for content compression
Throws:
java.security.NoSuchAlgorithmException - if the requested compression algorithm is not supported

getCompressionAlgorithm

public AlgorithmID getCompressionAlgorithm()
Returns the compression algorithm (including any associated parameters) of this CompressedContent.

Returns:
the compression algorithm

writeTo

public void writeTo(java.io.OutputStream os)
             throws java.io.IOException,
                    javax.mail.MessagingException
Writes this CompressedContent BER encoded to the given output stream.

Throws:
java.io.IOException - if an error occurs writing to the stream
javax.mail.MessagingException - if an error occurs when fetching the data to be written

This Javadoc may contain text parts from text parts from IETF Internet Standard specifications (see copyright note).

IAIK-CMS 6.0, (c) 2002 IAIK, (c) 2003, 2023 SIC