title: Sending SOAP message to service with Java and WS Security: Internal Server Error
body: <p>Our team is sending requests via soapUI to a remote service and collecting the responses. To automate this process, I made a simple java application being guided by the following tutorial.   </p>
  
  <p><a href="http://drumcoder.co.uk/blog/2011/oct/18/httpclient-client-side-certificates/" rel="nofollow">http://drumcoder.co.uk/blog/2011/oct/18/httpclient-client-side-certificates/</a></p>
  
  <p>The code is below. </p>
  
  <p><b>Background:</b></p>
  
  <p>Currently, we are using soapUI to send and receive messages. We specify a keystore for the requests and have to apply the outgoing WSS before sending. Once a response is received, we are manually copying and pasting it to a txt file. This gets very tedious and time consuming when there are hundreds of requests to process, so we are making a simple java application to run all of the requests and save off the corresponding responses. We used the above tutorial but it isn't working.</p>
  
  <p><b>What is Working?</b></p>
  
  <p>We currently save a SOAP request to a xml file manually, and then load, parse, and send the message to the service successfully using a java application. The response is also received and parsed successfully.</p>
  
  <p><b>The Problem</b></p>
  
  <p>The response received indicates a 500 internal server error, which is unexpected. Specifically, the custom error response indicates the service can not be found. I do recognize that a 500 internal server error is a pretty general problem and hard to debug without logs, though I do have some guesses. </p>
  
  <p><b>Guesses to What is Wrong</b></p>
  
  <p>One guess is that the endpoint / soap action is incorrect and the service cannot be found. I feel this is doubtful since we are using the exact credentials from the WSDL / soapUI. </p>
  
  <p>Guess two is that SSL security is not being handled correctly and this is causing an internal server error during the server's authentication process. This is what I believe is the problem though I am unsure.</p>
  
  <p><b>In General . . . </b> </p>
  
  <p>does anyone see the problem? Or if anything needs to be removed / added? Do you know of any other guides that may work better than this one for sending SOAP requests (I have tried java's javax.xml.soap API and that didn't work either, but resulted in the same error)? I have tried generating code from soapUI though I don't exactly know what to do with the resulting code (besides building with ant). I used <a href="http://java.dzone.com/tips/generating-client-java-code" rel="nofollow">http://java.dzone.com/tips/generating-client-java-code</a> to generate the code though I don't know if it is what I am looking for. </p>
  
  <p><b>Notes</b></p>
  
  <ul>
  <li><p>We are calling a remote service that we do not have access to the logs.</p></li>
  <li><p>The header in the SOAP request from the xml file is blank, unlike in soapUI where a security header is generated for the request when you apply an outgoing WSS. The bodies of the messages are equivalent. Even when the xml file containing the SOAP request loaded by the java application contains a non-expired security header (copied from soapUI after applying the outgoing WSS), the same invalid response is received.</p></li>
  <li><p>When we copy the generated soapUI message from java (with non-expired security header) to soapUI, a valid response is received. </p></li>
  <li><p>We do not have (and I suppose don't need) a truststore. </p></li>
  </ul>
  
  <p><b>Can anyone see what may be going wrong? Is there any more information that would help solve the problem (minus logs)?</p>
  
  <p>Thank You!</b></p>
  
  <hr>
  
  <p><b>Code : (paths, passwords, and urls are general)</b></p>
  
  <pre><code>public class SOAPController {
  
      static {
          org.apache.xml.security.Init.init();
      }
  
      final static String KEY_STORE_PATH = "PATH";
      final static String KEY_STORE_PASSWORD = "PASSWORD";
  
      public static void main(String[] args) throws Exception {
          KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
          InputStream keystoreInput = new FileInputStream(KEY_STORE_PATH);
          keystore.load(keystoreInput, KEY_STORE_PASSWORD.toCharArray());
          System.out.println("Keystore has " + keystore.size() + " keys");
  
          SchemeRegistry schemeRegistry = new SchemeRegistry();
          SSLSocketFactory lSchemeSocketFactory = new SSLSocketFactory(keystore,  KEY_STORE_PASSWORD);
          schemeRegistry.register(new Scheme("https", 443, lSchemeSocketFactory));
  
          final HttpParams httpParams = new BasicHttpParams();
          DefaultHttpClient lHttpClient = new DefaultHttpClient(new SingleClientConnManager(schemeRegistry), httpParams);
  
          String lUrl = "URL";
  
          String lXml = getStringFromDocument(new File("request1.xml"));
          System.out.println(lXml + "\n\n\n");
  
          HttpPost lMethod = new HttpPost(lUrl);
          HttpEntity lEntity = new StringEntity(lXml, "text/xml", "UTF-8");
          lMethod.setEntity(lEntity);
          lMethod.setHeader("SOAPAction", "soapaction");
  
          HttpResponse lHttpResponse = lHttpClient.execute(lMethod);
  
          System.out.println("Response status code: "
                  + lHttpResponse.getStatusLine().getStatusCode());
          System.out.println("Response body: ");
          System.out.println(EntityUtils.toString(lHttpResponse.getEntity()));
      }
  
      public static String getStringFromDocument(File file)
      {
          try
          {
              DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                      .newInstance();
              DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
              Document doc = dBuilder.parse(file);        
             DOMSource domSource = new DOMSource(doc);
             StringWriter writer = new StringWriter();
             StreamResult result = new StreamResult(writer);
             TransformerFactory tf = TransformerFactory.newInstance();
             Transformer transformer = tf.newTransformer();
             transformer.transform(domSource, result);
             return writer.toString();
          }
          catch(Exception ex)
          {
             ex.printStackTrace();
             return null;
          }
      } 
  }
  </code></pre>
  
  <p><b>pom.xml</b></p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
  &lt;groupId&gt;com.soap&lt;/groupId&gt;
  &lt;artifactId&gt;soap-util&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  
  &lt;name&gt;soap-util&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-codec&lt;/groupId&gt;
          &lt;artifactId&gt;commons-codec&lt;/artifactId&gt;
          &lt;version&gt;1.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-logging&lt;/groupId&gt;
          &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
          &lt;version&gt;1.1.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
          &lt;artifactId&gt;httpclient&lt;/artifactId&gt;
          &lt;version&gt;4.1.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
          &lt;artifactId&gt;httpclient-cache&lt;/artifactId&gt;
          &lt;version&gt;4.1.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
          &lt;artifactId&gt;httpcore&lt;/artifactId&gt;
          &lt;version&gt;4.1.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
          &lt;artifactId&gt;httpmime&lt;/artifactId&gt;
          &lt;version&gt;4.1.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.santuario&lt;/groupId&gt;
          &lt;artifactId&gt;xmlsec&lt;/artifactId&gt;
          &lt;version&gt;1.4.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jdom&lt;/groupId&gt;
          &lt;artifactId&gt;jdom2&lt;/artifactId&gt;
          &lt;version&gt;2.0.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.xml.soap&lt;/groupId&gt;
          &lt;artifactId&gt;javax.xml.soap-api&lt;/artifactId&gt;
          &lt;version&gt;1.3.7&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
   &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-eclipse-plugin&lt;/artifactId&gt;
              &lt;configuration&gt;
                  &lt;downloadSources&gt;true&lt;/downloadSources&gt;
                  &lt;downloadJavadocs&gt;true&lt;/downloadJavadocs&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p><b>Example SOAP Request (note empty header element):</b></p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"&gt;
      &lt;soap:Header/&gt;
      &lt;soap:Body&gt;
          ... same as soapUI request ...
      &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>