package soap;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class SOAPProcessor2 {
public static String invokeSrv(String endpoint) throws IOException {
try {
SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = connectionFactory.createConnection();
URL client = new URL("http://localhost:8088/soapTest");
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//Populate the Message
StreamSource preppedMsgSrc = new StreamSource(new FileInputStream("soapRequest.xml"));
soapPart.setContent(preppedMsgSrc);
//Save the message
message.saveChanges();
//Set the destination
SOAPMessage response = connection.call(message, client);
//Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = response.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
//System.out.println(result);
//Close the connection
connection.close();
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/**
* @param args
*/
public static void main(String[] args) {
try {
invokeSrv("");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}