7.6 Resealing a File with Different Custom Data

The following code demonstrates how to reseal a sealed file using the reseal method. This sample adds XML-based custom data to the sealed file.

Example 7-5

 import static oracle.irm.engine.content.sealing.CustomDataCollectionFactory.createCustomData;
 import static oracle.irm.engine.content.sealing.CustomDataFactory.createCustomData;
 import static oracle.irm.engine.content.sealing.SealingOperationsInstance.reseal;
 
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.net.Authenticator;
 import java.net.PasswordAuthentication;
 import java.util.Collection;
 import java.util.UUID;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 
 import oracle.irm.engine.content.sealing.CustomData;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
 public class ResealFile {
 
     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 reseal
         String filename = args[2];
 
         // The output file name
         String resealedFilename = args[3];
         
         // Custom data is provided as XML
         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
         Document document = documentBuilder.newDocument();
 
         Element element = document.createElement("SampleCustomData");
         element.setTextContent("Some example custom data provided as an XML text element");
 
         // UUID identifies the custom data, in this example just use a random UUID value
         UUID uuid = UUID.randomUUID();
 
         // Create the custom data, UUID + value
         CustomData data = createCustomData(uuid, element);
         
         Collection customData = createCustomData(data); 
         
         // Reclassify the sealed file with the new classification
         FileInputStream sealedFileStream = new FileInputStream(filename);
 
         FileOutputStream resealedFileStream = new FileOutputStream(resealedFilename);
 
         reseal(sealedFileStream, resealedFileStream, customData);
 
         // Close the streams
         sealedFileStream.close();
         resealedFileStream.close();
     }
 }