title: Consuming secure web service from JAVA
body: <p>I've been banging my head against the wall for the last month trying to get a simple WebService client to work.</p>
  
  <p>Basically, the service that I am accessing requires both signing XML elements and the whole document. I have successfully got the first step to work both manually (implementing the algorithm) and following IBM tutorials.</p>
  
  <p>I have been searching information on how to get the second step to work and all I have been able to find was that the following properties should be set, either via parameters or by setting them in the code like:</p>
  
  <pre><code>System.setProperty("javax.net.ssl.keyStoreType","pkcs12");
  System.setProperty("javax.net.ssl.keyStorePassword","changeit");
  System.setProperty("javax.net.ssl.keyStore","/path/to/keystore");
  </code></pre>
  
  <p>I am not sure if this is enough but I know it is indeed necessary since commenting out these three lines results in a <code>java.lang.NullPointerException at sun.security.pkcs12.PKCS12KeyStore.engineGetKey</code>.</p>
  
  <p>I have a XML document I know is valid for testing purposes. However, I can't get the WebService to accept my request whenever I send it via my own application. I know the request is fine since it does work on SoapUI.</p>
  
  <p>I have tried to keep my code as short as possible and it's currently looking like the following:</p>
  
  <pre><code>String xml = getXMLToSend(); // Loads the XML I know works
  
  uy.gub.dgi.WSEFacturaEFACRECEPCIONSOBRE params;
  params = new WSEFacturaEFACRECEPCIONSOBRE();
  Data datain = new Data();
  datain.setXmlData(xml);        
  params.setDatain(datain);
  
  // Do the external call
  WSEFacturaEFACRECEPCIONSOBREResponse resp = efacrecepcionsobre(params);
  </code></pre>
  
  <p>Note: All these elements are NetBeans-generated from the WSDL.</p>
  
  <p>And this is the result:</p>
  
  <pre><code>%% Cached client session: [Session-1, TLS_RSA_WITH_AES_256_CBC_SHA]
  main, WRITE: TLSv1 Application Data, length = 320
  main, WRITE: TLSv1 Application Data, length = 32
  main, WRITE: TLSv1 Application Data, length = 288
  main, READ: TLSv1 Application Data, length = 144
  main, READ: TLSv1 Application Data, length = 64
  main, READ: TLSv1 Application Data, length = 4016
  main, READ: TLSv1 Application Data, length = 368
  main, READ: TLSv1 Alert, length = 32
  main, RECV TLSv1 ALERT:  warning, close_notify
  main, called closeInternal(false)
  main, SEND TLSv1 ALERT:  warning, description = close_notify
  main, WRITE: TLSv1 Alert, length = 32
  main, called closeSocket(selfInitiated)
  main, called close()
  main, called closeInternal(true)
  Server : handleFault(): com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl@42c3bd
  Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Error al consumir el Servicio Web
      at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:189)
      at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:130)
      at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
      at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)
      at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:140)
      at com.sun.proxy.$Proxy37.efacrecepcionsobre(Unknown Source)
      at xmlsign.XmlSign.efacrecepcionsobre(XmlSign.java:196)
      at xmlsign.XmlSign.main(XmlSign.java:110)
  </code></pre>
  
  <p>I am unsure whether my XML code should or not include <code>[CDATA</code> tags. SoapUI needs them but I couldn't get my code to work either way. I have tried:</p>
  
  <ul>
  <li>Wrapping my XML in CDATA tags</li>
  <li>Sending it directly</li>
  <li>Sending the whole SoapUI request (which starts like <code>&lt;soapenv:Envelope xmlns:soapenv=...</code>).</li>
  </ul>
  
  <p>SoapUI works flawlessly, all I need to do is set it to use my keystore and provide a password for it. I am trying to implement the same behavior in my code but cannot find a way to do so. This is the settings dialog on SoapUI.</p>
  
  <p><img src="https://i.stack.imgur.com/exvDk.png" alt="enter image description here"></p>
  
  <p>I am honestly clueless as to what to do next since everything that I have tried seems to fail.</p>