Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
package routines;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class NAVWSInvoices {
public static void RequestWS(String destinationServiceURL, String soapAction,String inputXMLMessage,String outputXMLMessage) {
try {
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage messageRequest = messageFactory.createMessage();
// Add eventually a SoapAction header if necessary
MimeHeaders hd = messageRequest.getMimeHeaders();
hd.addHeader("SOAPAction", soapAction);
// Create objects for the message parts
SOAPPart soapPart = messageRequest.getSOAPPart();
StreamSource preparedMessageSource = new StreamSource( new FileInputStream(inputXMLMessage));
System.out.println(inputXMLMessage);
soapPart.setContent(preparedMessageSource);
messageRequest.saveChanges();
// Check the input
System.out.println("\nREQUEST:\n");
System.out.println(messageRequest.toString());
System.out.println("\nREQUEST:\n");
messageRequest.writeTo(System.out);
System.out.println();
// Send the message
SOAPMessage reply = connection.call(messageRequest, destinationServiceURL);
// Check the output
System.out.println("\nRESPONSE:\n");
// Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
// Set the output for the transformation
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(sourceContent, result);
StringBuffer sb = sw.getBuffer();
String SOAPResponse = sb.toString();
System.out.println(SOAPResponse);
System.out.println();
// Close the connection
connection.close();
Writer output = null;
// Write response to file
File file = new File(outputXMLMessage);
output = new BufferedWriter(new FileWriter(file));
output.write(SOAPResponse);
output.close();
} catch (Exception e) {
// Some error handling
System.out.println(e.getMessage());
}
}
}