br.com.topsys.cd.applet.AssinaturaApplet.java Source code

Java tutorial

Introduction

Here is the source code for br.com.topsys.cd.applet.AssinaturaApplet.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.topsys.cd.applet;

import br.com.bry.x509.certificado.X509Certificado;
import br.com.bry.x509.excecoes.ExcecaoX509;
import br.com.topsys.cd.exception.CertificadoDigitalException;
import br.com.topsys.cd.util.AssinaturaDigital;
import br.com.topsys.cd.util.CertificadoDigital;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import netscape.javascript.JSObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author andre
 */
public class AssinaturaApplet extends PrincipalApplet {

    private CertificadoDigital certificadoDigital;

    @Override
    public void complemento(CertificadoDigital certificadoDigital) {

        this.certificadoDigital = certificadoDigital;
        JSObject window = JSObject.getWindow(this);

        window.call("setHash", new Object[] { this.assinar(this.dados) });

        if (super.relatorio != null && !super.relatorio.equals("")) {
            window.call("setRelatorio", new Object[] { this.assinar(Base64.decodeBase64(super.relatorio)) });
        }

        this.closeSuccess();

    }

    public String assinar(String dados) {
        String retorno = null;

        try {

            X509Certificado certificadoAux = new X509Certificado(Base64.decodeBase64(this.certificado));

            if (certificadoAux.equals(this.certificadoDigital.getX509Certificado())) {

                if (super.flagListaCertificado) {
                    Gson gson = new Gson();
                    Map<Long, String> map = gson.fromJson(dados, HashMap.class);

                    for (Map.Entry<Long, String> entry : map.entrySet()) {
                        entry.setValue(new AssinaturaDigital().assinar(this.certificadoDigital, entry.getValue()));

                    }
                    retorno = gson.toJson(map);
                } else {
                    retorno = new AssinaturaDigital().assinar(this.certificadoDigital, dados);
                }

            } else {

                JOptionPane.showMessageDialog(null, "Certificado digital diferente do usurio do sistema!",
                        "Aviso", JOptionPane.ERROR_MESSAGE);
            }

        } catch (CertificadoDigitalException | ExcecaoX509 ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Aviso", JOptionPane.ERROR_MESSAGE);
            this.closeError();
        }

        return retorno;

    }

    public String assinar(byte[] dados) {
        String retorno = null;

        try {

            X509Certificado certificadoAux = new X509Certificado(Base64.decodeBase64(this.certificado));

            if (certificadoAux.equals(this.certificadoDigital.getX509Certificado())) {

                retorno = Base64.encodeBase64String(
                        new AssinaturaDigital().assinarDocumento(this.certificadoDigital, dados));

            } else {

                JOptionPane.showMessageDialog(null, "Certificado digital diferente do usurio do sistema!",
                        "Aviso", JOptionPane.ERROR_MESSAGE);
            }

        } catch (CertificadoDigitalException | ExcecaoX509 ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Aviso", JOptionPane.ERROR_MESSAGE);
            this.closeError();
        }

        return retorno;

    }

}