7.5 Reclassifying a File

The following code demonstrates how to reclassify a sealed file using the reclassify method. The content to reclassify can be provided as any type of java.io.InputStream; this example uses a file input stream. The sample code changes the labels of the classification and then writes the resulting reclassified stream out as a file.

Example 7-4

import static oracle.irm.engine.content.sealing.SealingOperationsInstance.peek;
 import static oracle.irm.engine.content.sealing.SealingOperationsInstance.reclassify;
 import static oracle.irm.engine.core.classification.ClassificationFactory.createClassification;
 import static oracle.irm.engine.core.general.LabelFactory.createLabel;
 
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.net.Authenticator;
 import java.net.PasswordAuthentication;
 import java.util.Collections;
 import java.util.Locale;
 
 import oracle.irm.engine.content.sealing.ContentDescription;
 import oracle.irm.engine.core.classification.Classification;
 import oracle.irm.engine.core.general.Label;
 
 public class ReclassifyFile {
 
     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() {
             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(username, password.toCharArray());
             }
         });
         
         // The file to reclassify
         String filename = args[2];
 
         // The output file name
         String reclassifiedFilename = args[3];
         
         // The label to apply to the classification
         String labelName = args[4];
         
         // Peek the contents of the file to obtain the current classification
         FileInputStream originalFileStream = new FileInputStream(filename);
 
         ContentDescription contentDescription = peek(originalFileStream);
 
         // Close the file stream
         originalFileStream.close();
 
         // Extract the classification from the content description
         Classification classification = contentDescription.getClassification();
 
         // Replace the labels with the one specified
         Label label = createLabel(Locale.ENGLISH, labelName, null);
 
         classification = createClassification(
                classification.getId(),
             classification.getSystem(), 
             classification.getKeySet(),
             classification.getUri(),
             classification.getClassificationTime(), 
             Collections.singleton(label), 
             classification.getCookie());
 
         // Reclassify the sealed file with the new classification
         originalFileStream = new FileInputStream(filename);
 
         FileOutputStream reclassifiedFileStream = new FileOutputStream(reclassifiedFilename);
 
         reclassify(originalFileStream, reclassifiedFileStream, classification);
 
         // Close the streams
         originalFileStream.close();
         reclassifiedFileStream.close();
     }
 }