I'm writing an application using SAAJ that sends a soap message to a servlet which responds with some information, including HTML which the client application will display. The HTML has to go into a SOAP attachment so the tags won't get confused with XML.
I had the whole thing working with plain text sending within a normal node, and I only switched a few lines to make that text an attachment with HTML instead, but now I'm getting the exception "Content is not allowed in prolog" on the Client side when it tries to create the envelope from the SOAPMessage.
Code for building answering SOAPMessage with attachment (Servlet to Client)
public static SOAPMessage buildXML(Message newMessage){
SOAPMessage msg = null;
try {
MessageFactory msgFactory = MessageFactory.newInstance();
msg = msgFactory.createMessage();
// Create an envelope in the message
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
msg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
//Get hold of the the body
SOAPBody body = envelope.getBody();
SOAPElement soap_message = body.addChildElement("MESSAGE");
if (newMessage.message_type != null){
SOAPElement soap_message_type = soap_message.addChildElement("MESSAGETYPE");
soap_message_type.addTextNode(newMessage.message_type);
}
if (newMessage.questionID != 0){
SOAPElement soap_questionID = soap_message.addChildElement("QUESTIONID");
soap_questionID.addTextNode("" + newMessage.questionID);
}
if (newMessage.username != null){
SOAPElement soap_username = soap_message.addChildElement("USERNAME");
soap_username.addTextNode(newMessage.username);
}
if (newMessage.password != null){
SOAPElement soap_password = soap_message.addChildElement("PASSWORD");
soap_password.addTextNode(newMessage.password);
}
if (newMessage.message_text != null){
AttachmentPart soap_message_text = msg.createAttachmentPart("<html><body>" + newMessage.message_text + "</body></html>", "text/html");
soap_message_text.setContentId("MESSAGETEXT");
msg.addAttachmentPart(soap_message_text);
//SOAPElement soap_message_text = soap_message.addChildElement("MESSAGETEXT");
//soap_message_text.addTextNode(newMessage.message_text);
}
if (newMessage.sampleString != null){
SOAPElement soap_password = soap_message.addChildElement("SAMPLESTRING");
soap_password.addTextNode(newMessage.sampleString);
}
SOAPElement tmpEl;
for (int i=0; i<newMessage.numArrayUsed; i++){
tmpEl = soap_message.addChildElement("SAMPLEINT");
tmpEl.addTextNode("" + newMessage.sampleInt);
}
msg.saveChanges();
} catch (Exception e) {
e.printStackTrace();
}
return msg;
}
You can see in the area where it adds MESSAGETEXT there are two clients commented out which work. The three lines above it are what changed to make it an attachment instead.
Code in Client for recieving reply from Servlet
SOAPMessage reply = connection.call(soap_msg, endpoint);
System.out.println("\nReceived reply from: " + endpoint);
reply.writeTo(System.out);
System.out.println("");
reply_msg = ProcessXML.parse(reply);
This is the console output:
Edit: These forums seem to be inserting an extra > before the <?xml version="1.0" encoding="UTF-8"?> line, but it doesn't exist in the code I'm pasting
Received reply from: http://localhost:8080/kuj/CaseStudyServlet
------=_Part_2_371807.1137465625031
Content-Type: text/xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><MESSAGE><MESSAGETYPE>deliverquestion</MESSAGETYPE><QUESTIONID>1</QUESTIONID><USERNAME>zac</USERNAME></MESSAGE></SOAP-ENV:Body></SOAP-ENV:Envelope>
------=_Part_2_371807.1137465625031
Content-Type: text/html
Content-Id: MESSAGETEXT
<html><body>This is an Array sort Question. You will be given 10 random integers and you must sort and return them in the order of greatest to least.</body></html>
------=_Part_2_371807.1137465625031--
ERROR: 'Content is not allowed in prolog.'
17-Jan-2006 02:40:25 com.sun.xml.messaging.saaj.soap.EnvelopeFactory createEnvelope
SEVERE: SAAJ0511: Unable to create envelope from given source
com.sun.xml.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source:
at com.sun.xml.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:111)
at com.sun.xml.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(SOAPPart1_1Impl.java:39)
at com.sun.xml.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:98)
at com.zacwittedesign.xml.ProcessXML.realParse(ProcessXML.java:32)
at com.zacwittedesign.xml.ProcessXML.parse(ProcessXML.java:26)
(snip)
And this is the beginning of the code for extracting the info from the SOAPMessage on the Client side:
private static Message realParse(SOAPMessage msg){
Message newMessage = new Message();
try {
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
(snip)
It dies on SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
Does anyone know what I'm doing wrong?