package net.catcert.trustedX.Creation.BigFiles.PDF.CAdES.BES;

import org.apache.axis.client.Stub;
import org.apache.axis.encoding.Base64;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.commons.configuration.Configuration;

import utils.TrustedXConfiguration;
import utils.Util;
import utils.UtilTrustedX;

import com.safelayer.trustedx.client.smartwrapper.Constants;
import com.safelayer.trustedx.client.smartwrapper.SmartHeader;
import com.safelayer.trustedx.client.smartwrapper.SmartSignRequest;
import com.safelayer.trustedx.client.smartwrapper.SmartSignResponse;

/**
 * PDF Signature Generation
 * 
 * @author aalcaide
 */
public class CreateSignaturePDF_CAdES_BES_Attached_BigFile {

	private static final String path_in = "in/docs_to_sign/BigFiles/";
	private static final String path_out = "out/Signatures/BigFiles/";

	private static final String filename = "demo-file-BigFile.pdf";
	
    private static final String pdfSignatureInfo = "<css:PdfSignatureInfo xmlns:css='http://www.safelayer.com/TWS'><css:PdfAttributes>"+
    											   "<css:validationMethod>PPKMS</css:validationMethod>"+
    											   "<css:signaturePosition>FIRST</css:signaturePosition>"+
      											   "<css:signatureAlg>SHA1</css:signatureAlg>"+ //attached
    											   "</css:PdfAttributes></css:PdfSignatureInfo>";
		
	public static void main(String[] args) {
		try {

			Configuration conf = TrustedXConfiguration.getConfiguration();
			String host = conf.getString("host");
			String distinguishedName = conf.getString("distinguishedname");
			String dipositari = conf.getString("dipositari");
			String rol  = conf.getString("rol");
			
			// Definition of Signature Request endpoint
			SmartSignRequest ssr = new SmartSignRequest(host);

			// Add customized header elements for SOAP
			Stub stub = (Stub) ssr.getStub();
			stub.setHeader(null, "Rol", rol);
			stub.setHeader(null, "Dipositari", dipositari);
			
			// PDF Signature
			ssr.setProfile(Constants.Profile.PDF);
			
			// Referčncia al fitxer que conté les dades a signar
			// Parŕmetres: path (codificat en Base64) del fitxer + tipus de dades del fitxer (Raw o Base64) 
			ssr.setInputBase64DataFile(Base64.encode((path_in+filename).getBytes()), Constants.SourceFormat.RAW);

			// Habilitem la gestió en arxiu de la signatura en la resposta
			ssr.enableDocumentWithSignaturePdfFile(Constants.SourceFormat.RAW);	
			
			// Key selection using Distinguished Name			
			ssr.setKeySubjectName(distinguishedName);
			
			// CAdES signature type
			ssr.setSignatureType(Constants.SignatureType.CADES);
			// CAdES signature form
			ssr.setSignatureFormat(Constants.SignatureFormat.BES);

			// Add PDF Signature Information
			ssr.setPdfSignatureInfo(pdfSignatureInfo);
			
			// Sending the request
			SmartSignResponse ssrs = ssr.send();

			// Retrieveing signed PDF
			if (UtilTrustedX.checkSW(ssrs.getResultMajor(), ssrs.getResultMinor(), ssrs.getResultMessage())) {
				// Obtenim el nom del fitxer que conté la signatura
				String referenceDestFilename = new String(Base64.decode(ssrs.getDocumentWithSignaturePdf()));
				// Opcionalment, podem canviar-li el nom
				String destFilename = filename.substring(0, filename.lastIndexOf("."))
				+ "_Signed_CAdES_BES_Attached.pdf";
				boolean renameOk = Util.renameFile(path_out + referenceDestFilename, path_out + destFilename);
				if(!renameOk) destFilename = referenceDestFilename;
				System.out.println("File saved successfully: " +path_out + destFilename);
				
				// Si volem obtenir el PDF signat per codi
				//byte[] signature = ssrs.getReferenceFileContent(destFilename, false);
				//System.out.println("Signature: "+Base64.encode(signature));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
