Nfe Homologação - Envia mas não cancela

0 respostas
E

Olá pessoal !!!

Tenho um problema estranho. Consigo enviar NFe em modo homologação mas dá erro ao cancelar.

Uso o seguinte código nos dois casos:

CertificaAmbienteNFe c = new CertificaAmbienteNFe(UtilNFe.getConfig(), UtilNFe.getCertificado());
        c.autentica();

        if (!ConexaoUtil.verifica()) {
            return;
        }
.....

Minha classe :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jnfe.nfe2.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import org.apache.commons.httpclient.protocol.Protocol;
import org.jnfe.modelos.Certificado;
import org.jnfe.modelos.NFeConfig;

/**
 *
 * @author ruviano
 */
public class CertificaAmbienteNFe {

    private NFeConfig config;
    private Certificado cert;

    public CertificaAmbienteNFe(NFeConfig config, Certificado cert) {
        this.config = config;
        this.cert = cert;
    }

    public void autentica() {
        // Prepara conexao segura
        //-------------------------------------------
        System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

        if (cert.getTipo().equalsIgnoreCase("a1")) {
            try {
                InputStream entrada = new FileInputStream(cert.getLocal());
                KeyStore ks = KeyStore.getInstance("pkcs12");
                try {
                    ks.load(entrada, cert.getSenha().toCharArray());
                } catch (IOException e) {
                    System.out.println(e);
                    return;
                }

                String alias = null;
                Enumeration<String> al = ks.aliases();
                while (al.hasMoreElements()) {
                    alias = al.nextElement();
                }

                Protocol protocol = new Protocol("https", new HSProtocoloSocketFactory((X509Certificate) ks.getCertificate(alias), (PrivateKey) ks.getKey(alias, cert.getSenha().toCharArray())), 443);
                Protocol.registerProtocol("https", protocol);
                
            } catch (Exception e) {
                System.out.println(e);
                return;
            }

        } else {

            Provider p = new sun.security.pkcs11.SunPKCS11(cert.getLocalToken());
            Security.addProvider(p);

            System.setProperty("javax.net.ssl.keyStoreType", "PKCS11");
            System.setProperty("javax.net.ssl.keyStore", "NONE");
            System.setProperty("javax.net.ssl.keyStoreProvider", "SunPKCS11-SmartCard");
            System.setProperty("javax.net.ssl.keyStorePassword", cert.getSenha());
        }

        if (config.getTipoAmbiente() == 1) {
            System.setProperty("javax.net.ssl.trustStore", config.getLocalRSNFe());
            System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
        } else {
            System.setProperty("javax.net.ssl.trustStore", config.getLocalRSNFeHom());
            System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
        }

        System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
        //-------------------------------------------
    }
}

verifica()

//Autentica com o ambiente
        Ambiente.configAmbiente(UtilNFe.getCertificado().getLocal(), UtilNFe.getCertificado().getSenha());

        //Monta xml da pesquisa
        String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<consStatServ versao=\"2.00\" xmlns=\"http://www.portalfiscal.inf.br/nfe\">"
                + "<tpAmb>" + ConfiguracaoGlobal.getConfiguracaoGlobal().getNfe_tpAmb() + "</tpAmb>"
                + "<cUF>43</cUF>"
                + "<xServ>STATUS</xServ>"
                + "</consStatServ>";


        try {
            //Trata o ws
            NfeStatusServico2Stub.setAmbiente(ConfiguracaoGlobal.getConfiguracaoGlobal().getNfe_tpAmb());
            NfeStatusServico2Stub stub = new NfeStatusServico2Stub();
            NfeStatusServico2Stub.NfeDadosMsg dados = new NfeStatusServico2Stub.NfeDadosMsg();

            OMElement el = AXIOMUtil.stringToOM(XML);
            dados.setExtraElement(el);

            NfeStatusServico2Stub.NfeCabecMsg cab = new NfeStatusServico2Stub.NfeCabecMsg();
            cab.setVersaoDados("2.00");
            cab.setCUF("43");
            NfeStatusServico2Stub.NfeCabecMsgE cabE = new NfeStatusServico2Stub.NfeCabecMsgE();
            cabE.setNfeCabecMsg(cab);

            //Executa consulta no ws
            String retorno = stub.nfeStatusServicoNF2(dados, cabE).getExtraElement().toString();

Dá erro na linha String retorno = stub.nfeStatusServicoNF2(dados, cabE).getExtraElement().toString();
com o erro org.apache.axis2.AxisFault: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)

Criado 18 de junho de 2012
Respostas 0
Participantes 1