title: Consuming a web service in java and SOAPUI
body: <p>I am consuming a webservice in java. This service is using UserNameToken, Timestamp and Signature for message level security. I have been given SoapUI project file(xml) to try and get the data from the service. Everything works fine in SoapUI.</p>
  
  <p>Now when I am trying to use the same soapUI file to generate the artifacts, I am getting a message <code>"Receiver Policy falsified"</code>. Why is that I can connect to the service in soapUI, but I can not in java? </p>
  
  <p>So I found out that I am not sending a keystore using Transport Layer Security. How do I send this in a SOAP request. I have done similar setting in SOAP UI in SSL settings and it works there. </p>
  
  <p>This is my code for SOAP request in java</p>
  
  <pre><code>SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
  SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  
      String url = "https://abc.org/test/ApplicantInformationService"; // Test System Web Service URL
  
  SSLSocketFactory sslSocketFactory = getSSLSocketFactory();
  
  conn = urlOnly.openConnection();
  if (conn instanceof HttpsURLConnection) {
       ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
       ((HttpsURLConnection) conn).setRequestMethod("POST");
   } 
  
  conn.setRequestProperty("Content-Type", "application/soap+xml;charset=UTF-8");    
  conn.setDoOutput(true);
  SOAPMessage message = createSOAPRequest(user);
  ByteArrayOutputStream os1 = new ByteArrayOutputStream();
  message.writeTo(os1);
  String requestXml = new String(os1.toByteArray()); 
  
  OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
  out.write(requestXml);
  out.flush();
  if(out != null){
      out.close();
  }               
  String line = "";                
  BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  String responseNewWay = "";
  while ((line = in.readLine()) != null) {
      responseNewWay = responseNewWay + line;
  }
  //SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(user), url);
  //soapConnection.close();
  //ByteArrayOutputStream os = new ByteArrayOutputStream();
  //soapResponse.writeTo(os);
  //String responseXml = new String(os.toByteArray());
  </code></pre>
  
  <p>And SSLFactory Code is like this</p>
  
  <pre><code>  private static SSLSocketFactory getSSLSocketFactory() 
      throws KeyStoreException, NoSuchAlgorithmException, CertificateException,  UnrecoverableKeyException, IOException, KeyManagementException{
      SSLSocketFactory sslsf = null;                  
      String keyStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";           
  
          String keyStorePath = ClassLoader.getSystemResource(keyStoreFileName).getPath();        
      String keyStoreType = "JKS";
          String keyStorePassword = "mypassword";
          String trustStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";        
  
          String trustStorePath = ClassLoader.getSystemResource(trustStoreFileName).getPath();
          String trustStorePassword = "mypassword";
          String alias = "clientcertificate";
  
          Properties systemProps = System.getProperties();
          systemProps.put("javax.net.ssl.keyStore", keyStorePath);
          systemProps.put("javax.net.ssl.keyStorePassword", keyStorePassword);
          systemProps.put("javax.net.ssl.keyStoreType", keyStoreType);
  
          systemProps.put("javax.net.ssl.trustStore", trustStorePath);
          systemProps.put("javax.net.ssl.trustStoreType", "JKS");
          systemProps.put("javax.net.ssl.trustStorePassword", trustStorePassword);
          System.setProperties(systemProps); 
  
          KeyManager[] keyManagers = createKeyManagers(keyStoreFileName,keyStorePassword, alias);
          TrustManager[] trustManagers = createTrustManagers(trustStoreFileName, trustStorePassword);
          sslsf = initItAll(keyManagers, trustManagers);
  
          return sslsf;
      }
  </code></pre>
  
  <p>And the error SOAP response i am getting is like this</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"&gt;
      &lt;soapenv:Body&gt;
          &lt;soapenv:Fault&gt;
              &lt;soapenv:Code&gt;
                  &lt;soapenv:Value&gt;soapenv:Receiver&lt;/soapenv:Value&gt;
              &lt;/soapenv:Code&gt;
              &lt;soapenv:Reason&gt;
                  &lt;soapenv:Text xml:lang="en-US"&gt;Policy Falsified&lt;/soapenv:Text&gt;
              &lt;/soapenv:Reason&gt;
              &lt;soapenv:Role&gt;https://abc.org/test/ApplicantInformationService&lt;/soapenv:Role&gt;
              &lt;soapenv:Detail&gt;
                  &lt;l7:policyResult
                      status="Service Not Found.  The request may have been sent to an invalid URL, or intended for an unsupported operation." xmlns:l7="http://www.layer7tech.com/ws/policy/fault"/&gt;
              &lt;/soapenv:Detail&gt;
          &lt;/soapenv:Fault&gt;
      &lt;/soapenv:Body&gt;
  </code></pre>
  
  <p></p>
  
  <p>Now I am getting this error </p>
  
  <pre><code>Server returned HTTP response code: 500 for URL: https://abc.org/test/ApplicantInformationService
  </code></pre>