Currently i'm implementing a usecase where i'm supposed to create a soap client which writes over HTTP protocol. To test the same usecase i have a mock service up and running. Now when i execute the SAAJ implementation of mine as a standalone jar my application works just well and doesn't have any issue.
My problem currently is that when i merge that code in an spring 4 based application and deploy it on Jboss AS 7.1.1, it gives me an error as below :
12:40:33,992 ERROR [stderr] (ActiveMQ Session Task-1) com.sun.xml.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
12:40:33,994 ERROR [stderr] (ActiveMQ Session Task-1) at com.sun.xml.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:670)
12:40:33,996 ERROR [stderr] (ActiveMQ Session Task-1) at com.sun.xml.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:100)
12:40:33,997 ERROR [stderr] (ActiveMQ Session Task-1) at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:342)
12:40:33,999 ERROR [stderr] (ActiveMQ Session Task-1) at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:159)
Initially i assumed it to be an issue due to other spring dependencies because previously i have struggled with same issues however that isn't the case here, as u can see in pom entries below i removed the conflicting spring -boot-autoconfigure APIS from my war build by making them optional.
my maven dependencies are as below :
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.saaj.client</groupId>
<artifactId>notification</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>notification</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.6.RELEASE</spring.version><!-- 3.0.5.RELEASE 4.1.6.RELEASE -->
<activemq.version>5.14.3</activemq.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.4.3.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>1.4.3.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>${activemq.version}</version>
</dependency>
<!--
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.3</version>
</dependency>
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-702.jdbc4</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.6</version>
</dependency>
</dependencies>
</project>
And my SAAJ implementation is as below :
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.xml.sax.InputSource;
import com.saaj.client.constants.NotificationConstants;
import com.saaj.client.exception.CustomException;
import com.saaj.client.logger.LoggerManager;
@Component("adapterServiceHelper")
public class AdapterServiceHelper {
private static Logger logger = LoggerManager.getInstance().getNotificationProcessingLogger();
public AdapterServiceHelper(){
}
public URL getEndpointConnection(String url) throws MalformedURLException, CustomException{
URL endpoint = null;
try{
endpoint = new URL (null, url, new URLStreamHandler () {
protected URLConnection openConnection (URL url) throws IOException {
// The url is the parent of this stream handler, so must create clone
URL clone = new URL (url.toString ());
URLConnection connection = clone.openConnection ();
connection.setRequestProperty("Content-Type",
"text/xml");
connection.setRequestProperty("Accept",
"application/soap+xml, text/*");
// If we cast to HttpURLConnection, we can set redirects
// connection.setInstanceFollowRedirects (false);
connection.setConnectTimeout (5 * 1000); // 15 sec
connection.setReadTimeout (5 * 1000); // 15 sec
// Custom header
return connection;
}});
}catch(MalformedURLException e){
throw e;
}catch(Exception exp){
exp.printStackTrace();
throw new CustomException(exp.getMessage(),"Error occured while getting endpoint connection to IP");
}
return endpoint;
}
//main SAAJ implementation
public SOAPMessage writeCommand(URL endpoint, String command) throws Exception{
SOAPMessage soapResponse = null;
SOAPConnection soapConnection = null;
InputStream is = null;
System.out.println("command written is "+command);
try{
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
.newInstance();
soapConnection = soapConnectionFactory
.createConnection();
//write to PG over soapConnection can be made generic
is = new ByteArrayInputStream(command.getBytes());
SOAPMessage request = MessageFactory.newInstance(
SOAPConstants.SOAP_1_1_PROTOCOL).createMessage(
new MimeHeaders(), is);
MimeHeaders headers = request.getMimeHeaders();
headers.addHeader("Content-Type", "text/xml");
request.saveChanges();
soapResponse = soapConnection.call(request, endpoint);
//soapConnection.close();
//is.close();
//process soap response
printSOAPResponse(soapResponse);
}
catch(SOAPException exp)
{
exp.printStackTrace();
logger.error("ERROR while connecting SOAP service" + exp.getMessage());
throw new Exception("ERROR : SOAPException encountered while writing ");
} catch (IOException e) {
e.printStackTrace();
throw new Exception("ERROR : SOAPException encountered while writing ");
} catch (TransformerException e) {
e.printStackTrace();
throw new Exception("ERROR : SOAPException encountered while writing ");
}
finally{
soapConnection.close();
is.close();
}
return soapResponse;
}
public static void printSOAPResponse(SOAPMessage soapResponse) throws TransformerException, SOAPException {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
}
Please do let me know if there is any suggestion for this problem.
I tried using the basic java.net HTTP connection to check what data i'm getting and found that i was really getting the html page for the mock service's port.(localhost:9997) Say my service is up on the URL : "http://localhost:9997/mockSoapBinding" So i was getting data for the URL : "http://localhost:9997" instead of the web service exposed.
To check same i used the code below :
public String handleRequest(String method, String url, String xmlPayload,String readTimeout,String connectTimeout,String soapAction) throws IOException {
HttpURLConnection connection = null;
OutputStreamWriter wr = null;
BufferedReader in = null;
String result = "";
try {
URL connectionUrl = new URL(url);
connection = (HttpURLConnection) connectionUrl.openConnection();
connection.setRequestMethod(method);
if ( xmlPayload != null && ! xmlPayload.equals("")) {
connection.setReadTimeout(Integer.parseInt(readTimeout));
connection.setConnectTimeout(Integer.parseInt(connectTimeout));
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
/*connection.setRequestProperty("SOAPAction", soapAction);*/
connection.setDoOutput(true);
wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(xmlPayload);
wr.flush();
}
try {
// getInputStream will throw exception when response code is
// greater than 400, so we need to getErrorStream
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
} catch (Exception e) {
in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
}
StringBuilder bodyBuilder = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
bodyBuilder.append(inputLine);
}
in.close();
result = String.valueOf( bodyBuilder.toString());
System.out.println(result);
} catch(Exception e){
logger.error("Error occured while send message for : " , e);
}finally {
if (wr != null) {
wr.close();
}
if (in != null) {
in.close();
}
if ( connection != null ) {
connection.disconnect();
}
}
return result;
}
Do note this is also an alternative to SAAJ implementation.