if(doc.isAllSecurityToBeRemoved())
{
willEncrypt = false;
// also need to get rid of the "Encrypt" in the trailer so readers
// don't try to decrypt a document which is not encrypted
COSDocument cosDoc = doc.getDocument();
COSDictionary trailer = cosDoc.getTrailer();
trailer.removeItem(COSName.ENCRYPT);
}
else
{
if (document.getEncryption() != null)
{
SecurityHandler securityHandler = document.getEncryption().getSecurityHandler();
if(securityHandler != null)
{
securityHandler.prepareDocumentForEncryption(document);
willEncrypt = true;
}
else
{
willEncrypt = false;
}
}
else
{
willEncrypt = false;
}
}
COSDocument cosDoc = document.getDocument();
COSDictionary trailer = cosDoc.getTrailer();
COSArray idArray = (COSArray)trailer.getDictionaryObject( COSName.ID );
boolean missingID = true;
// check for an existing documentID
if (idArray != null && idArray.size() == 2)
{
missingID = false;
}
if( missingID || incrementalUpdate)
{
MessageDigest md5;
try
{
md5 = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
// should never happen
throw new RuntimeException(e);
}
// algorithm says to use time/path/size/values in doc to generate the id.
// we don't have path or size, so do the best we can
md5.update( Long.toString(idTime).getBytes("ISO-8859-1") );
COSDictionary info = (COSDictionary)trailer.getDictionaryObject( COSName.INFO );
if( info != null )
{
Iterator<COSBase> values = info.getValues().iterator();
while( values.hasNext() )
{
md5.update( values.next().toString().getBytes("ISO-8859-1") );
}
}
// reuse origin documentID if available as first value
COSString firstID = missingID ? new COSString( md5.digest() ) : (COSString)idArray.get(0);
COSString secondID = new COSString( md5.digest() );
idArray = new COSArray();
idArray.add( firstID );
idArray.add( secondID );
trailer.setItem( COSName.ID, idArray );
}
cosDoc.accept(this);
}