public class CompressedContent extends SMimeContent
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.p7zAfter 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 Jakarta Mail architecture, and how to handling MIME messages, consult the Jakarta Mail Specification/<.
For using the IAIK-CMS S/MIME library, you also will need the following packages:
iaik_jce(_full).jar
(IAIK-JCE Core Crypto Library).
iaik_eccelerate.jar
(IAIK ECC Library; if you want to use Elliptic Curve Cryptography).
# # 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 (<JDK-HOME>/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(); ...
CompressedDataStream
Constructor and Description |
---|
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(jakarta.activation.DataSource dataSource)
Constructs a CompressedContent object from the given data source.
|
Modifier and Type | Method and Description |
---|---|
iaik.asn1.structures.AlgorithmID |
getCompressionAlgorithm()
Returns the compression algorithm (including any associated parameters) of this
CompressedContent.
|
java.io.InputStream |
getContentInputStream()
Returns an InputStream with the unparsed content.
|
jakarta.activation.DataHandler |
getDataHandler()
Return a DataHandler holding the content.
|
java.lang.String |
getSMimeType()
Returns the smime-type parameter ("compressed-data").
|
void |
setCompressionAlgorithm(iaik.asn1.structures.AlgorithmID compressionAlgorithm)
Sets the compression algorithm to be used for content compressiom.
|
void |
setHeaders(jakarta.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.
|
getContent, getContentType, getInputStream, setBlockSize, setContent, setContent, setContentContentHeaders, setContentContentTransferEncoding, setDataHandler, setText
public CompressedContent()
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".
public CompressedContent(CryptoContent cryptoContent) throws jakarta.mail.MessagingException
cryptoContent
- the signed (or encrypted) content to be compressed
(however it may be preferable to first compress and
then sign or encrypt)jakarta.mail.MessagingException
- if the CompressedContent cannot be createdpublic CompressedContent(jakarta.activation.DataSource dataSource) throws java.io.IOException
content handler
supplying the data source.
For more information on data handling using the
jakarta.activation.DataSource
for "MIME type based" data
access, see the
JavaBeans Activation Framework (JAF) specification.
dataSource
- the DataSource supplying the compressed datajava.io.IOException
- if an I/O error occurs during reading the objectpublic void setHeaders(jakarta.mail.Part part)
Content-Disposition: attachment"; filename="smime.p7z" Content-Transfer-Encoding: base64It 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, Jakarta Mail may run method
writeTo
twice
to determine the content transfer encoding to be used.part
- the part for which the Content-Disposition and Content-Transfer-Encoding
headers should be setpublic jakarta.activation.DataHandler getDataHandler() throws jakarta.mail.MessagingException
getDataHandler
in class SMimeContent
jakarta.mail.MessagingException
- if an error occurs when fetching the data handlerpublic java.io.InputStream getContentInputStream() throws java.io.IOException
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.
java.io.IOException
- if an error occurs when reading the datapublic java.lang.String getSMimeType()
public void setCompressionAlgorithm(iaik.asn1.structures.AlgorithmID compressionAlgorithm) throws java.security.NoSuchAlgorithmException
compressionAlgorithm
- the algorithm to be used for content compressionjava.security.NoSuchAlgorithmException
- if the requested compression algorithm is not supportedpublic iaik.asn1.structures.AlgorithmID getCompressionAlgorithm()
public void writeTo(java.io.OutputStream os) throws java.io.IOException, jakarta.mail.MessagingException
os
- the output streamjava.io.IOException
- if an error occurs writing to the streamjakarta.mail.MessagingException
- if an error occurs when fetching the data to be written