Hi , i have signed with CADES signature some pdf file and convert them to p7m file e.g. test.pdf to test.p7m.
For anyone don't know the p7m extension it's just like a zip file where you can extract the single file in it, by using a specific key.
What i want it's to extract the original pdf in the p7m and make it visualize on share like a normal pdf on runtime while using share, so i can use the standard preview and standard thumbnail of the orginal pdf file.
NOTE: by some expert suggestion on a similar question , i need to setup a composite / pipeline transformer for P7M to PNG, since there is likely not going to be an existing one that works with PDF as an intermediary.
In the documentation of alfresco 5.2 the png not support the preview so instead, i'm tring to use the tiff format to show the original signed document of the p7m file (pdf or text o what else).
So in the end i'm tring to make a trasnformer P7M to TIFF:
===============================================================
/src/main/resources/alfresco/extension/mimetype/custom-mimetype-map.xml
===============================================================
<alfresco-config area="mimetype-map">
<config evaluator="string-compare" condition="Mimetype Map">
<mimetypes>
<mimetype mimetype="application/pkcs7-mime" display="P7M">
<extension>p7m</extension>
</mimetype>
</mimetypes>
</config>
</alfresco-config>
=================================================================
/src/main/resources/alfresco/module/webdesktop-amp/alfresco-global.properties
=================================================================
.......
#TRANSFORMER
content.transformer.P7MToTIFF.priority=200
content.transformer.P7MToTIFF.extensions.p7m.tiff.supported=true
................
=================================================================
/src/main/resources/alfresco/module/webdesktop-amp/mimetype-map-custom.xml
=================================================================
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
" rel="nofollow" target="_blank">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- IS EMPTY -->
</beans>
=================================================================
/src/main/resources/alfresco/module/webdesktop-amp/module-context.xml
=================================================================
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="classpath:alfresco/module/${project.artifactId}/context/*-context.xml"/>
</beans>
=================================================================
/src/main/resources/alfresco/module/webdesktop-amp/context/transformer-context.xml
=================================================================
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="transformer.P7MToTIFF"
class="org.alfresco.transformers.P7MToTIFFTransformer"
parent="baseContentTransformer" >
</bean>
</beans>
=================================================================
/src/main/java/org/alfresco/transformers/P7MToTIFFTransformer.java
=================================================================
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import org.alfresco.repo.content.transform.AbstractContentTransformer2;
import org.alfresco.repo.content.transform.UnsupportedTransformationException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.TransformationOptions;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class P7MToTIFFTransformer extends AbstractContentTransformer2
{
private static final Log logger = LogFactory.getLog(P7MToTIFFTransformer.class);
/**
* Select only the file to transform
*/
public boolean isTransformable(String sourceMimetype, String targetMimetype, TransformationOptions options)
{
String ext = getExtensionOrAny("application/pkcs7-mime");
if ("application/pkcs7-mime".equalsIgnoreCase(sourceMimetype) || ext.equalsIgnoreCase("p7m")){
logger.debug("P/MToTIFFTransformer IsTranformable '"+ sourceMimetype + "' to '" + targetMimetype+"'");
return true;
}else{
return false;
}
}
/**
* Transofmer a p7m to the tiff image of single file in it
*/
@Override
protected void transformInternal(ContentReader contentReader, ContentWriter contentWriter, TransformationOptions options) throws Exception
{
logger.debug("USE P7MToTIFFTransformer");
BufferedReader reader = null;
Writer out = null;
InputStream inStream = null;
InputStream is = null;
try
{
// Check the transformability
String myMimeType = myStrictMimetypeCheck(contentReader, contentWriter, options);
if(myMimeType !=null && !mimeType.isEmpty())contentWriter.setMimetype(myMimeType);
//The p7m file on share
inStream = contentReader.getContentInputStream();
out = new BufferedWriter(new OutputStreamWriter(contentWriter.getContentOutputStream()));
//Extract Original Signed Document from the p7m file (ignore these line they are work just fine)
byte[] originalFile = Manager.getOriginalDocumentBinaries(inStream);
logger.debug("OriginalFile: " + String.valueOf(originalFile!=null && originalFile.length >0));
is = new BufferedInputStream(Manager.getOriginalDocumentStream(inStream));
//Try to guess the mimetype of the orginal signed document (ignore these line they are work just fine)
String guessMimeType = URLConnection.guessContentTypeFromStream(is);
logger.debug("GuessMimetype: " + guessMimeType); //this is for example "application/pdf"
//Convert the pdf file extract from p7m file to a tiff image because the
//pdf transformer don't work like a intermediary
File fileTiffOriginal = Manager.toTiff(originalFile);
IOUtils.write(fileTiffOriginal, out,StandardCharsets.UTF_8.displayName());
//change the current mimetype to the tiff mimetype for alfresco
String guessMimeType2 = "image/tiff ";
if(guessMimeType!=null){
contentWriter.setMimetype(guessMimeType2);
}
}catch(IOException e){
e.printStackTrace();
}
finally
{
if (reader != null)
{
try{reader.close(); }
catch (Throwable e){e.printStackTrace();}
}
if (out != null) {
try{out.close();}
catch (Throwable e){e.printStackTrace();}
}
if(inStream != null){
try{inStream.close();}
catch (Throwable e){e.printStackTrace();}
}
if(is != null){
try{is.close();}
catch (Throwable e){e.printStackTrace();}
}
}
}
/**
* Manage the inner warnings of alfresco 5.2
* @param reader the reader of the current document
* @param writer the writer of the current document
* @param options the transform options of the current document
* @return the mimetype suggested from share
* @throws UnsupportedTransformationException
*/
private String myStrictMimetypeCheck(ContentReader reader,ContentWriter writer, TransformationOptions options)throws UnsupportedTransformationException
{
String mimeTypeOfAlfresco = "";
String sourceMimetype = reader.getMimetype();
String targetMimetype = writer.getMimetype();
String differentType = getMimetypeService().getMimetypeIfNotMatches(reader.getReader());
if (!transformerConfig.strictMimetypeCheck(sourceMimetype, differentType))
{
String fileName = transformerDebug.getFileName(options, true, 0);
String readerSourceMimetype = reader.getMimetype();
String message = "Transformation of ("+fileName+
") has not taken place because the declared mimetype ("+
readerSourceMimetype+") does not match the detected mimetype ("+
differentType+").";
logger.warn(message);
logger.debug("Cambiamo il mimetype con quello voluto da alfresco per evitare stackstrace del warning");
mimeTypeOfAlfresco = differentType;
}else{
logger.debug("Mimetype ok : mimeTypeOfAlfresco = "+mimeTypeOfAlfresco+ " , sourceMimetype = "+sourceMimetype);
mimeTypeOfAlfresco = sourceMimetype;
}
return mimeTypeOfAlfresco;
}
}
========================================================================================
The Alfresco version i use , is the 5.2 community.
It's seem i make some grave mistake with the code.
ty in adavcne for any help.
Greetings.