Quantcast
Channel: Abhisar's Blog
Viewing all articles
Browse latest Browse all 37

Soap request from JavaFX

$
0
0
I was searching on net to know how to send soap request from JavaFX. I could not get a straight answer. While looking at the JavaFX 1.0 API, I did find java.io.http.HttpRequest class to send HTTP asynchronous requests. The document says that you can invoke RESTful weservices through the class.

Since HTTPRequest supports POST as a method for sending the request, I though of sending the SOAP message over HTTP. I wrote a simple utility class to facilitate the same and it did help me achieve my goal. The example is a simple class keping in mind that usually soap request are known through WSDL but we do mistakes while preparing a complete XML soap message.

I would like to share some of the points for anyone trying the same.

1. Create a simple SoapRequest class as follows:
public class SoapRequest extends HttpRequest{

2. Define the variables to create xml tags, Soap envelop and body tags
// xml header 
var XML_HEADER:String = ""; 
// namespace for SOAP 1.1 message, this is also the default namespace 
public-read var SOAP1_1:String = "http://schemas.xmlsoap.org/soap/envelope/"; 
// namespace for SOAP 1.2 message 
public-read var SOAP1_2:String = "http://www.w3.org/2003/05/soap-envelope/";
// variable to set the namespace 
public var SOAP_SPEC:String = SOAP1_1; 
var SOAP_ENVELOP_START:String = " "; 
var SOAP_ENVELOP_END:String = ""; 
var SOAP_BODY_START:String = ""; 
var SOAP_BODY_END:String = "";
public var soap_request:String;
    var soap_request_size =  soap_request.getBytes().length;

3. Override the enqueue method to set content type
override function enqueue(): Integer {
        if (not (SOAP_SPEC == SOAP1_1 or SOAP_SPEC == SOAP1_2)){
            println("Soap specification should either be SOAP1_1 or SOAP1_2");
            return 0;
        }
        setHeader("Content-Type", "text/xml");
        setHeader("Content-Length", "{soap_request_size}");
        return super.enqueue();
    }

4. Create a methode to return complete soap message to be passed as part of outputstream
public function getSoapMessage():String{ 
return "{XML_HEADER}{SOAP_ENVELOP_START}{SOAP_BODY_START}{soap_request}{SOAP_BODY_END}{SOAP_ENVELOP_END}"; }

5. Create a client to send the request in JavaFX scripting

def postRequest: SoapRequest = SoapRequest {

    location: "http://localhost:8080/JITService/";

      soap_request: testContent;

onOutput: function(os: java.io.OutputStream) {
        try {
            println("onOutput - about to write {postRequest.getSoapMessage()} to output stream");
            println("onOutput - about to write {postRequest.getSoapMessage().getBytes()} bytes to output stream");
            os.write(postRequest.getSoapMessage().getBytes());

        } finally {
            println("onOutput - about to close output stream.");
            os.close();
        }
    }

onToRead: function(bytes: Integer) {
        if (bytes <>
            println("onToRead - Content length not specified by server; bytes: {bytes}");
        } else {
            println("onToRead - total number of content bytes to read: {bytes}");
        }
    }
    };
postRequest.enqueue();
    





Viewing all articles
Browse latest Browse all 37