Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SOAP, do not work...

843811Jan 3 2008 — edited Jan 10 2008
I am trying to send a SOAP message to web-service under SSL connection... but it does not work throwing an exception like this:
03/01/2008 09:21:11 com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0009: Message send failed
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Message send failed
        at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:146)
        at br.com.datamanager.nfex.util.soap.NFexSOAP.sendSOAPMessage(NFexSOAP.java:82)
        at br.com.datamanager.nfex.util.soap.NFexSOAP.acessWebService(NFexSOAP.java:36)
        at br.com.datamanager.nfex.util.soap.NFexSOAP.main(NFexSOAP.java:198)
Caused by: java.security.PrivilegedActionException: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Message send failed
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:140)
        ... 3 more
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Message send failed
        at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:344)
        at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection$PriviledgedPost.run(HttpSOAPConnection.java:169)
        ... 5 more
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate
        at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
        at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1657)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:932)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1096)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1123)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1107)
        at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:405)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:832)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
        at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:301)
        ... 6 more
Here is my class:
public class NFexSOAP {

    private static final boolean DEBUG_MODE = true;

    public static Document acessWebService(Document doc, URL url) throws SOAPException {
        SOAPMessage sm = createSOAPMessage(doc);
        SOAPMessage rs = sendSOAPMessage(sm, url);

        return rs.getSOAPPart();
    }

    private static SOAPMessage createSOAPMessage(Document doc) throws SOAPException {
        if(doc == null) {
            throw new IllegalArgumentException("O par�metro doc n�o pode ser nulo.");
        }

        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage sm = mf.createMessage();

        fillSOAPMessage(sm, doc);

        printSOAPMessage("CREATE - SOAP MESSAGE:", sm);

        return sm;
    }

    private static SOAPMessage sendSOAPMessage(SOAPMessage sm, URL url) throws SOAPException {
        if(sm == null || url == null) {
            throw new IllegalArgumentException("Os par�metros sm e url n�o podem ser nulos.");
        }

        SOAPConnectionFactory sfc = null;
        SOAPConnection connection = null;

        try {
            sfc = SOAPConnectionFactory.newInstance();
            connection = sfc.createConnection();

            SOAPMessage response = connection.call(sm, url);

            printSOAPMessage("SEND - SOAP MESSAGE:", sm);
            printSOAPMessage("SEND - SERVER RESPONSE:", response);

            return response;
        } finally {
            if(connection != null) {
                connection.close();
            }
        }
    }

    private static void fillSOAPBody(SOAPBody sb, Document doc) throws SOAPException {
        sb.addDocument(doc);        
    }

    private static void fillSOAPHeader(SOAPHeader sh) {
        sh.detachNode();
    }

    private static void fillSOAPPart(SOAPPart sp) throws SOAPException {
        SOAPEnvelope se = sp.getEnvelope();

        sp.setXmlVersion("1.0");

        se.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
        se.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        //se.addNamespaceDeclaration("enc", "http://schemas.xmlsoap.org/soap/encoding/");
        //se.addNamespaceDeclaration("env", "http://schemas.xmlsoap.org/soap/envelop/");
        //se.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
    }

    private static void fillSOAPMessage(SOAPMessage sm, Document doc) throws SOAPException {
        sm.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");

        SOAPPart sp   = sm.getSOAPPart();
        SOAPHeader sh = sm.getSOAPHeader();
        SOAPBody sb   = sm.getSOAPBody();

        // Preenche os elementos da mensagem.
        fillSOAPPart(sp);
        fillSOAPHeader(sh);
        fillSOAPBody(sb, doc);
    }

    public static void main(String[] args) {

        System.setProperty("javax.net.debug", "all");

        System.setProperty("javax.net.ssl.trustStore", "C:\\DM\\Java\\Trunk\\NFex\\tstore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "datamanager");

        System.setProperty("javax.net.ssl.keyStore", "C:\\DM\\Java\\Trunk\\NFex\\kstore.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "datamanager");

        try {
            //Cria um document vazio...
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

            // XML Consulta de Status do Servi�o TESTE!!
            Element consStatServ = doc.createElement("consStatServ");
            consStatServ.setAttribute("versao", "1.02");
            doc.appendChild(consStatServ);

            Element xServ = doc.createElement("xServ");
            xServ.setTextContent("STATUS");
            consStatServ.appendChild(xServ);
            // XML Consulta de Status do Servi�o TESTE!!

            //SOAPMessage sm = createSOAPMessage(doc);
            Document rs = acessWebService(doc, new URL("https://homologacao.sefaz.mt.gov.br/nfews/NfeStatusServico"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 7 2008
Added on Jan 3 2008
11 comments
11,041 views