AnsweredAssumed Answered

¿Como firmar un acp?

Question asked by magarcia_sm on Apr 12, 2017

Hola a todos, 

Os comento la situación. Tengo el siguiente código heredado con el que se creaba un acp de un espacio indicado de alfresco. Una vez creado se firmaba de forma similar a un .jar. En teoría ese código en la versión 3 de Alfresco funcionaba correctamente, pero ahora en la 4.2.f no funciona. Al intentar ejecutar el comando de firmado nos da el error "jarsigner: unable to open jar file". ¿Existe alguna manera alternativa de firmar un acp?

 

El código es el siguiente. 

 

    exportACP(exportNodeRef, params);
    firmarACP();

    private void exportACP(NodeRef nodeRefPeriodo, Map<String, Serializable> paramsExport) throws FileNotFoundException {
        ExporterCrawlerParameters parameters = new ExporterCrawlerParameters();
        parameters.setExportFrom(new Location(nodeRefPeriodo));
        parameters.setCrawlChildNodes(true);
        parameters.setCrawlSelf(true);
        parameters.setCrawlContent(true);
        String packageName = (String) paramsExport.get(ExporterActionExecuter.PARAM_PACKAGE_NAME);
        File dataFile = new File(packageName);
        File contentDir = new File(packageName);
        File zipFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, ACPExportPackageHandler.ACP_EXTENSION);
        NodeRef zip = createExportZip(paramsExport);
        ContentWriter writeTo = this.services.getContentService().getWriter(zip, ContentModel.PROP_CONTENT, true);
        ACPExportPackageHandler handler = new ACPExportPackageHandler(new FileOutputStream(zipFile), dataFile, contentDir, this.services.getMimetypeService());
        this.services.getExporterService().exportView(handler, parameters, null);
    }

    private NodeRef createExportZip(Map<String, Serializable> paramsExport) {
        logger.info("createExportZip");
// create a node in the repository to represent the export package
        NodeRef exportDest = (NodeRef) paramsExport.get(ExporterActionExecuter.PARAM_DESTINATION_FOLDER);
        String packageName = (String) paramsExport.get(ExporterActionExecuter.PARAM_PACKAGE_NAME);
// add the default Alfresco content package alfresco.extension if an alfresco.extension hasn't been given
        if (!packageName.endsWith("." + ACPExportPackageHandler.ACP_EXTENSION)) {
            packageName += (packageName.charAt(packageName.length() - 1) == '.') ? ACPExportPackageHandler.ACP_EXTENSION : "." + ACPExportPackageHandler.ACP_EXTENSION;
        }
        NodeRef zipNodeRef = getFileACP();
        String desc = "";
        String storeRef = (String) paramsExport.get(ExporterActionExecuter.PARAM_STORE);
        NodeRef rootNode = this.nodeService.getRootNode(new StoreRef(storeRef));
        if (rootNode.equals(this.nodoPeriodo.getNodeRef())) {
            desc = I18NUtil.getMessage("export.root.package.description");
        } else {
            String spaceName = (String) this.nodeService.getProperty(this.nodoPeriodo.getNodeRef(), ContentModel.PROP_NAME);
            String pattern = I18NUtil.getMessage("export.package.description");
            if (pattern != null && spaceName != null) {
                desc = MessageFormat.format(pattern, spaceName);
            }
        }
// apply the titled aspect to behave in the web client
        Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>(3, 1.0f);
        titledProps.put(ContentModel.PROP_TITLE, packageName);
        titledProps.put(ContentModel.PROP_DESCRIPTION, desc);
        this.nodeService.addAspect(zipNodeRef, ContentModel.ASPECT_TITLED, titledProps);
        return zipNodeRef;
    }


    private NodeRef getFileACP() {
        NodeRef fileACP = this.fileFolderService.searchSimple(this.getEspacioRepositorio(),
                this.getNombreFicheroPeriodo() + "." + ACPExportPackageHandler.ACP_EXTENSION);
        return fileACP;
    }

    public void firmarACP() throws Exception {
          
        NodeRef certificado = this.getCertificado();
        NodeRef node_acp = getFileACP();
          
        if ((certificado == null) || (!this.getNodeService().hasAspect(certificado, FacturaModel.ASPECT_CERTIFICADO))
                || (this.getNodeService().getProperty(certificado, FacturaModel.PROP_CLAVE) == null)) {
                         
            throw new CertifNotExistException();
               
        } else {
               
            File filecert = File.createTempFile("prefCert", "sufCert");
            File fileACP = File.createTempFile("ACP_", ".bootstrap");
            File signedFileACP = File.createTempFile("ACPsigned_", ".bootstrap");
            this.getFileFolderService().getReader(certificado).getReader().getContent(filecert);
            this.getFileFolderService().getReader(node_acp).getReader().getContent(fileACP);
            String clave = (String) this.getNodeService().getProperty(certificado, FacturaModel.PROP_CLAVE);
            KeyStore ks = null;
            ks = KeyStore.getInstance("pkcs12");
            logger.info("clave " + clave);
            ks.load(new FileInputStream(filecert), clave.toCharArray());
            Enumeration<String> enumAlias = ks.aliases();
            String alias = null;
               
            do {
                String aliasks = (String) enumAlias.nextElement();
                logger.info("aliasks " + aliasks);

                boolean b = ks.isKeyEntry(aliasks);

                boolean c = ks.isCertificateEntry(aliasks);
                if (b) {
                         
                    Certificate cert = ks.getCertificate(aliasks);
                    PublicKey pk = cert.getPublicKey();
                    Certificate[] chain = ks.getCertificateChain(aliasks);
                         
                    if (chain != null) {
                              
                        alias = aliasks;
                        break;
                              
                    }
                }
                    
            } while (enumAlias.hasMoreElements());
               
            String[] commandArray = new String[]{
                    "jarsigner",
                    "-keystore",
                    filecert.getPath(),
                    "-storetype",
                    "PKCS12",
                    "-storepass",
                    clave,
                    "-signedjar",
                    signedFileACP.getPath(),
                    fileACP.getPath(),
                    alias};
                         
            StringBuffer output = new StringBuffer();
            Process p;
               
            try {
                    
                p = Runtime.getRuntime().exec(commandArray);
                p.waitFor();
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                    
                while ((line = reader.readLine()) != null) {
                         
                    output.append("<OUTPUT>" + line + "\n");
                         
                }
                    
                ContentWriter writer = this.services.getContentService().getWriter(
                        node_acp, ContentModel.PROP_CONTENT, true);
                writer.putContent(signedFileACP);

            } catch (Exception e) {
                    
                e.printStackTrace();
                    
            }
        }
    }

Outcomes