javax.xml.soap.SOAPConnection与java.mail之间的冲突

卢西亚诺

在我们的系统(应用程序)中,我们通过政府服务器托管的网络服务发送税务文件。我们通过以下方法发送此文档:

public SOAPMessage conecta(String xmlNfedados, URL url) throws SOAPException{
    SOAPMessage res = null; 

    try {
        MimeHeaders header = new MimeHeaders();
        header.addHeader("Content-Type", "application/soap+xml");

        MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

        SOAPMessage message;
        message = factory.createMessage(header, new ByteArrayInputStream(xmlNfedados.getBytes()));
        SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection();
        res = con.call(message, url);

        con.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return res;
}

在此连接之前,我们在jvm中设置以下属性:

public void setPropertiesA1(String caminhoCertificado, String senhaCertificado, String caminhoCacerts){
    //preparar as propriedades
    Properties properties = System.getProperties();
    properties.setProperty("java.protocol.handler.pkgs",  "com.sun.net.ssl.internal.www.protocol");
    properties.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
    properties.setProperty("javax.net.ssl.keyStore", caminhoCertificado);
    properties.setProperty("javax.net.ssl.keyStorePassword", senhaCertificado);
    properties.setProperty("javax.net.ssl.trustStoreType", "JKS");
    properties.setProperty("javax.net.ssl.trustStore", caminhoCacerts);
    properties.setProperty("javax.net.ssl.trustStorePassword", "changeit");
    properties.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true");
    //properties.setProperty("javax.net.debug", "all");

}

一切都很好。直到我们需要发送和接收供应商的电子邮件。在接收电子邮件的情况下,我们使用以下方法:

public static Store conectar(String login, String senha)
throws NoSuchProviderException, MessagingException
{
    logger.info("Conectando ao servidor de e-mail");

    logger.info("--------------Processo de leitura iniciado-----------------");
    String imap = "imaps";
    String host = "pop.gmail.com";
    int porta = 587;
    String diretorioServidor = "Inbox";

    Properties prop = new Properties();
    //System.out.println("numero antes " + System.getProperties().size());
    Session session = Session.getInstance(prop);

    //URLName url = new URLName(imap, host, porta, diretorioServidor, login, senha);

    Store store = session.getStore("pop3s");

    store.connect(host, login, senha);
    //System.out.println("numero depois " + System.getProperties().size());
    logger.info("Conexão estabelecida com servidor IMAP.");
    return store;
}

方法conectar返回商店对象由以下方法处理:

public static Folder recuperarCaixaEntrada(Store store)
throws MessagingException
{
    Folder folder = store.getFolder("Inbox");
    folder.open(2);

    return folder;
}

稍后在proccessMail()方法中处理对象文件夹包含的消息

public FileInputStream processMail()
throws MessagingException
{
    FileInputStream anexo = null;
    try
    {
      logger.info("Quantida de de e-mails encontrados na caixa de entrada: " + this.messages.length);

     if (this.messages.length <= 0) {
        this.folder.close(true);

        this.store.close();

        System.out.println("esta conectado " + store.isConnected());
        return null;
     }

     System.out.println("Existem na caixa de entrada: " + this.messages.length + " para serem tratados!");

     System.out.println("Tratando e-mail:1 de " + this.messages.length);
     logger.info("Tratando e-mail:1 de " + this.messages.length);

     this.message = this.messages[0];
     System.out.println("Content Type: " + this.message.getContentType());

     if (!this.message.getContentType().equals("text/plain; charset=br-ascii"))
     {
       anexo = getEmail(0);
       System.out.println("Baixou anexo");
     } else {
       System.out.println("Não baixou anexo");
     }

     this.folder.close(true);

     this.store.close();
     } catch (AuthenticationFailedException e) {
       this.store.close();
       logger.error("Falha na Autentica&#65533;&#65533;o: " + e.getMessage());
     } catch (FolderClosedException e) {
       this.store.close();
       logger.error("Falha no fechamento da pasta: " + e.getMessage());
     } catch (FolderNotFoundException e) {
       this.store.close();
       logger.error("Pasta n&#65533;o encontrada: " + e.getMessage());
     } catch (NoSuchProviderException e) {
       this.store.close();
       logger.error("NoSuchProviderException: " + e.getMessage());
     } catch (ReadOnlyFolderException e) {
       this.store.close();
       logger.error("Pasta com permiss&#65533;o de somente leitura: " + e.getMessage());
     } catch (StoreClosedException e) {
       this.store.close();
       logger.error("Erro ao fechar pasta auxiliar: " + e.getMessage());
     } catch (Exception e) {
       this.store.close();
       logger.error("Erro no m&#65533;todo Principal: " + e.getMessage());
       System.out.println(e.getMessage());
    }
  return anexo;
}

因此,在实施了接收电子邮件的过程之后,发送税务文件便开始返回以下错误:

com.sun.xml.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Message send failed
17:37:56,434 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at      com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConn     ection.java:191)
17:37:56,435 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at     br.com.nfe.business.ComunicacaoReceitaBusiness.conecta(ComunicacaoReceitaB    usiness.java:205)
17:37:56,436 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.nfe.business.ComunicacaoReceitaBusiness.criaConexao(ComunicacaoReceitaBusiness.java:39)
17:37:56,436 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.nfe.business.EmissaoReceitaBusiness.emissaoNfe(EmissaoReceitaBusiness.java:46)
17:37:56,437 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.coliseu.nfe.business.NfeBusiness.emitirNota(NfeBusiness.java:1739)
17:37:56,437 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.coliseu.controller.NotaSaidaController.emitir(NotaSaidaController.java:1023)
17:37:56,438 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
17:37:56,439 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
17:37:56,439 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
17:37:56,440 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at java.lang.reflect.Method.invoke(Method.java:606)
17:37:56,440 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:61)
17:37:56,441 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
17:37:56,441 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,442 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)
17:37:56,443 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,443 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61)
17:37:56,444 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,444 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.coliseu.interceptor.LoginInterceptor.intercept(LoginInterceptor.java:92)
17:37:56,445 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
17:37:56,446 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,446 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)
17:37:56,447 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,447 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:71)
17:37:56,448 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
17:37:56,448 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,449 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:83)
17:37:56,450 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
17:37:56,450 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,451 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor.intercept(ParametersInstantiatorInterceptor.java:93)
17:37:56,451 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:59)
17:37:56,452 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,452 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:48)
17:37:56,453 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
17:37:56,454 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,454 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:69)
17:37:56,455 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
17:37:56,455 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,456 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)
17:37:56,457 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
17:37:56,457 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.core.EnhancedRequestExecution.execute(EnhancedRequestExecution.java:44)
17:37:56,458 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:91)
17:37:56,458 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at     br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:58)
17:37:56,459 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:88)
17:37:56,459 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
17:37:56,460 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
17:37:56,460 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
17:37:56,461 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
17:37:56,462 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
17:37:56,462 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
17:37:56,463 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
17:37:56,463 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
17:37:56,464 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)
17:37:56,464 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
17:37:56,465 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
17:37:56,465 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
17:37:56,466 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
17:37:56,467 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
17:37:56,467 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
17:37:56,468 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
17:37:56,468 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
17:37:56,469 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at java.lang.Thread.run(Thread.java:745)
17:37:56,470 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) Caused by: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Message send failed
17:37:56,470 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at java.security.AccessController.doPrivileged(Native Method)
17:37:56,471 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:185)
17:37:56,471 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   ... 60 more
17:37:56,472 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) Caused by: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Message send failed
17:37:56,472 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:389)
17:37:56,473 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection$PriviledgedPost.run(HttpSOAPConnection.java:214)
17:37:56,474 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   ... 62 more
17:37:56,474 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
17:37:56,475 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
17:37:56,476 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)
17:37:56,476 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279)
17:37:56,477 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273)
17:37:56,477 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446)
17:37:56,478 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)
17:37:56,478 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.Handshaker.processLoop(Handshaker.java:913)
17:37:56,479 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.Handshaker.process_record(Handshaker.java:849)
17:37:56,479 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1023)
17:37:56,480 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
17:37:56,480 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
17:37:56,481 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
17:37:56,481 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
17:37:56,482 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
17:37:56,483 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1092)
17:37:56,483 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
17:37:56,484 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:346)
17:37:56,485 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   ... 63 more
17:37:56,485 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
17:37:56,486 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
17:37:56,487 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
17:37:56,488 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.validator.Validator.validate(Validator.java:260)
17:37:56,488 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
17:37:56,489 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
17:37:56,489 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
17:37:56,490 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1428)
17:37:56,490 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   ... 75 more
17:37:56,491 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
17:37:56,492 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
17:37:56,492 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
17:37:56,493 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
    17:37:56,493 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   ... 81 more

因此,错误消息很明确:无法找到请求的目标的有效证书路径。由于某种原因,我们的应用程序未找到证书路径。但是,如前所述,每次需要发送文档时,都会调用setPropertiesA1()方法除此之外,我在应用尝试发送文档之前检查了证书路径是否正常,是否可以。属性“ javax.net.ssl.keyStore”具有正确的值。

如果对这种连接的工作方式有深刻理解的人可以给我们提供解决方案的一瞥,或者指出我们做错了什么地方,那么我们将非常感激。

我希望这个问题足够清楚。

我们使用JBoss AS 7.1.1.Final作为服务器应用程序。

更新这里的问题不仅仅是“缺少证书”,因为如果我们注释负责发送电子邮件的代码,它也将无法工作。

另一个需要说明的地方是,两个连接不是同时完成的,而是一个接一个地发生。更准确地说,是在连接税收文件部分之前先连接电子邮件部分。

这里的主要问题是:在发送或接收电子邮件之后,为什么SOAPConnection无法从属性获取路径?

卢西亚诺

经过对该问题的广泛研究,我找到了解决方案,该解决方案是结合使用sslcontext来实现密钥管理器和信任管理器。这将在HttpsConnection对象中使用。

重构了方法“ SOAPMessage conecta(String xmlNfedados,URL url)”:

public String conecta(String xmlNfedados, URL url) throws SOAPException{
    String outputString = "";
    try {
        if(context != null){

            URLConnection connection = url.openConnection();
            HttpsURLConnection httpsconn = (HttpsURLConnection) connection;

            httpsconn.setSSLSocketFactory(this.context.getSocketFactory());

            byte[] buffer = new byte[xmlNfedados.length()];
            buffer = xmlNfedados.getBytes();

            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            bout.write(buffer);

            byte[] b = bout.toByteArray();

            httpsconn.setRequestProperty("Content-Type", "application/soap+xml");
            httpsconn.setRequestMethod("POST");

            httpsconn.setDoOutput(true);
            httpsconn.setDoInput(true);

            OutputStream out = httpsconn.getOutputStream();

            out.write(b);
            out.close();

            InputStreamReader isr = new InputStreamReader(httpsconn.getInputStream());

            BufferedReader in = new BufferedReader(isr);

            String responseString = "";

            while((responseString = in.readLine()) != null){
                outputString = outputString + responseString;
            }

            /*MimeHeaders header = new MimeHeaders();
            header.addHeader("Content-Type", "application/soap+xml");

            MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

            SOAPMessage message;
            message = factory.createMessage(header, new ByteArrayInputStream(xmlNfedados.getBytes()));
            SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection();
            res = con.call(message, url);*/

        } else {
            throw new IllegalStateException("SSContext não inicializado");
        }

        //con.close();
    } catch (IOException e) {
        e.printStackTrace();
    } 
    return outputString;
}

方法“ getsslContext”负责初始化sslContext:

@Override
public SSLContext getSslContext(String camingoCert, String caminhoCacerts, String senhaCertificado){
    SSLContext sc = null;
    try {
        sc = SSLContext.getInstance("SSL");
        KeyManager[] km = getKeyManager(camingoCert, senhaCertificado);
        TrustManager[] tm = getTrustManager(caminhoCacerts);

        sc.init(km, tm, null);

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    return sc;
}

以下是“ getKeyManager”:private KeyManager [] getKeyManager(String caminhoCert,String senha){KeyManagerFactory kmf = null;

    try {
        kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
        KeyStore ks = KeyStore.getInstance( "pkcs12" );
        ks.load(new FileInputStream( caminhoCert ), senha.toCharArray() );

        kmf.init( ks, senha.toCharArray() );

    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
    }
    return kmf.getKeyManagers();
}

和“ getTrustManager”:

private TrustManager[] getTrustManager(String caminhoCacerts){
    TrustManagerFactory tmf = null;
    try {

        tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        KeyStore ts = KeyStore.getInstance("JKS");
        ts.load(new FileInputStream(caminhoCacerts), SENHACACERTS.toCharArray());
        tmf.init(ts);

    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return tmf.getTrustManagers();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JDK 11:java.lang.NoClassDefFoundError:使用javax / XML / WS /处理器/ SOAP / SOAPHandler

来自分类Dev

Java的11包javax.xml.soap中不存在

来自分类Dev

使用Java SOAP服务会破坏javax.mail引用的可打印邮件封装

来自分类Dev

java.lang.ClassCastException:oracle.j2ee.ws.saaj.soap.TextImpl无法转换为javax.xml.soap.SOAPElement

来自分类Dev

java.lang.ClassCastException:oracle.j2ee.ws.saaj.soap.TextImpl无法转换为javax.xml.soap.SOAPElement

来自分类Dev

Scala / Play:javax.xml.soap请求标头Content-Type问题

来自分类Dev

ArcGIS GeoEvent Processor-javax.xml.ws.soap.SOAPFaultException:解组错误

来自分类Dev

如何在没有soap webService接口的情况下使用javax.xml.ws

来自分类Dev

解析对java对象的soap xml响应

来自分类Dev

com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl无法转换为javax.xml.soap.MessageFactory

来自分类Dev

XML over HTTP与SOAP over HTTP之间的区别

来自分类Dev

XML over HTTP与SOAP over HTTP之间的区别

来自分类Dev

Kafka 无法解析 javax/xml/bind/* ClassNotFoundExeption for java 12?

来自分类Dev

如何将SOAP XML解组到Java对象

来自分类Dev

Java soap调用将.XML文件传递给WebService

来自分类Dev

java xml验证soap-enc命名空间异常

来自分类Dev

PHP XML Soap请求

来自分类Dev

发布 SOAP XML 请求

来自分类Dev

SOAP XML 选择节点

来自分类Dev

BeSimple Soap捆绑软件与FOSRest捆绑软件之间的冲突

来自分类Dev

处理“ javax / xml / parsers / DocumentBuilder.class”时遇到麻烦:核心类(java。*或javax。*)的不良使用或错误使用

来自分类Dev

处理“ javax / xml / parsers / DocumentBuilder.class”时遇到麻烦:核心类(java。*或javax。*)的不良使用或错误使用

来自分类Dev

java.lang.ClassNotFoundException:javax.mail.MessagingException

来自分类Dev

java.lang.NoClassDefFoundError:javax / mail / MessagingException未解决

来自分类Dev

java.lang.NoClassDefFoundError:javax / mail / Address错误

来自分类Dev

javax.mail.AuthenticationFailedException:如何无法通过Java发送邮件?

来自分类Dev

javax.xml.transform.TransformerConfigurationException:

来自分类Dev

javax.mail.SendFailedException

来自分类Dev

更改WCF SOAP响应XML

Related 相关文章

  1. 1

    JDK 11:java.lang.NoClassDefFoundError:使用javax / XML / WS /处理器/ SOAP / SOAPHandler

  2. 2

    Java的11包javax.xml.soap中不存在

  3. 3

    使用Java SOAP服务会破坏javax.mail引用的可打印邮件封装

  4. 4

    java.lang.ClassCastException:oracle.j2ee.ws.saaj.soap.TextImpl无法转换为javax.xml.soap.SOAPElement

  5. 5

    java.lang.ClassCastException:oracle.j2ee.ws.saaj.soap.TextImpl无法转换为javax.xml.soap.SOAPElement

  6. 6

    Scala / Play:javax.xml.soap请求标头Content-Type问题

  7. 7

    ArcGIS GeoEvent Processor-javax.xml.ws.soap.SOAPFaultException:解组错误

  8. 8

    如何在没有soap webService接口的情况下使用javax.xml.ws

  9. 9

    解析对java对象的soap xml响应

  10. 10

    com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl无法转换为javax.xml.soap.MessageFactory

  11. 11

    XML over HTTP与SOAP over HTTP之间的区别

  12. 12

    XML over HTTP与SOAP over HTTP之间的区别

  13. 13

    Kafka 无法解析 javax/xml/bind/* ClassNotFoundExeption for java 12?

  14. 14

    如何将SOAP XML解组到Java对象

  15. 15

    Java soap调用将.XML文件传递给WebService

  16. 16

    java xml验证soap-enc命名空间异常

  17. 17

    PHP XML Soap请求

  18. 18

    发布 SOAP XML 请求

  19. 19

    SOAP XML 选择节点

  20. 20

    BeSimple Soap捆绑软件与FOSRest捆绑软件之间的冲突

  21. 21

    处理“ javax / xml / parsers / DocumentBuilder.class”时遇到麻烦:核心类(java。*或javax。*)的不良使用或错误使用

  22. 22

    处理“ javax / xml / parsers / DocumentBuilder.class”时遇到麻烦:核心类(java。*或javax。*)的不良使用或错误使用

  23. 23

    java.lang.ClassNotFoundException:javax.mail.MessagingException

  24. 24

    java.lang.NoClassDefFoundError:javax / mail / MessagingException未解决

  25. 25

    java.lang.NoClassDefFoundError:javax / mail / Address错误

  26. 26

    javax.mail.AuthenticationFailedException:如何无法通过Java发送邮件?

  27. 27

    javax.xml.transform.TransformerConfigurationException:

  28. 28

    javax.mail.SendFailedException

  29. 29

    更改WCF SOAP响应XML

热门标签

归档