7.2 Sealing a File

The following code demonstrates how to seal a file using the seal method. The content to seal can be provided as any type of java.io.InputStream; this example uses a file input stream. Similarly, the sealed content can be written out to any output stream; this example writes the sealed content as a file whose file name is derived from the unsealed file name. When a file is sealed, a Classification must be specified. In this sample the file is sealed using the context classification system, specifying a context with a known UUID value and a fixed item code value.

Example 7-1

import static oracle.irm.engine.classifications.context.ContextConstants.CONTEXT_CLASSIFICATION_SYSTEM;
 import static oracle.irm.engine.classifications.context.ContextCookieFactory.createContextCookie;
 import static oracle.irm.engine.classifications.context.ContextFactory.createContext;
 import static oracle.irm.engine.classifications.item.ItemCodeFactory.createItemCode;
 import static oracle.irm.engine.content.sealing.SealingOperationsInstance.seal;
 import static oracle.irm.engine.content.sealing.SealingOptionsFactory.createSealingOptions;
 import static oracle.irm.engine.content.source.FileSourceFactory.createFileSource;
 import static oracle.irm.engine.content.type.ContentTypeOperationsInstance.getSealedFileName;
 import static oracle.irm.engine.core.classification.ClassificationFactory.createClassification;
 import static oracle.irm.engine.core.general.LabelCollectionFactory.EMPTY_LABELS;
 import java.io.FileOutputStream;
 import java.net.Authenticator;
 import java.net.PasswordAuthentication;
 import java.net.URI;
 import java.util.Date;
 import java.util.UUID;
 import oracle.irm.engine.classifications.context.Context;
 import oracle.irm.engine.classifications.context.ContextCookie;
 import oracle.irm.engine.classifications.item.ItemCode;
 import oracle.irm.engine.content.sealing.SealingOptions;
 import oracle.irm.engine.content.source.FileSource;
 import oracle.irm.engine.core.classification.Classification;
 public class SealFile {
     public static void main(String[] args) throws Exception {
         // The user name and password are provided on the command line. In a production
         // system these details should be provided in a more secure manner, such
         // as prompting from the console, or reading from a secure source.
         final String username = args[0];
         final String password = args[1];
         // Configure an authenticator to provide the credentials for any network access
         Authenticator.setDefault(new Authenticator() {
             protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(username, password.toCharArray());
         // Provide an explicit item code for the document
         ItemCode itemCode = createItemCode("sample document");
         // Context UUID is fixed for sample code
         Context context = createContext(UUID.fromString("46f910d9-dd30-476e-b060-4d01f88f8b05"));
         // Context cookie specifying the context and the item code
         ContextCookie cookie = createContextCookie(
         // The server address e.g. https://irm.example.com/irm_desktop
         URI serverURI = URI.create(args[2]);
         // Create the classification details used in the sealing options
         Classification classification = createClassification(
             null, // automatically fill in key set
             new Date(),
             EMPTY_LABELS, // automatically fill in labels
         // Create the sealing options
         SealingOptions sealingOptions = createSealingOptions(classification);
         // Create a file source from the file name
         String unsealedFilename = args[3];
         FileSource fileSource = createFileSource(unsealedFilename);
         // Get the sealed equivalent of the unsealed filename
         String sealedFilename = getSealedFileName(unsealedFilename);
         // Write the sealed stream out to a file
         FileOutputStream sealedOutputStream = new FileOutputStream(sealedFilename);
         // Seal the file
         seal(fileSource, sealedOutputStream, sealingOptions);
         // Close the streams