Memanggil Services WebMethods Melalui JavaFX


Ini contoh sederhana bagaimana menggunakan fitur webservice pada JavaFX. Dalam hal ini memanggil services webMethods yang biasa digunakan di perusahaan skala Enterprise. Komunikasi yang dilakukan menggunakan protokol SOAP-RPC, dan pada ada contoh dibawah ini hanya merubah karakter (case) dari inputan lowercase menjadi uppercase.

Mengenai SOAP-RPC jika ada yang belum jelas silahkan baca di wiki. Kalo tentang webMethods silahkan di gugling saja atau silahkan baca di dokumntasinya, khususnya mengenai SOAP. Tapi dalam kasus ini tidak perlu tahu banyak tentang webmethods, karena prinsipnya sama dengan webservices. Jika tidak menggunakan webMethods silahkan bisa menggunakan Axis atau server webbservices lainya, tinggal disesuiakan format envelopenya.

Pada kasus ini saya menggunakan webMethods sebagai server webservices nya. Dibawah ini adalah format envelope nya yang nanti akan dikirim ke engine SOAP-RPC yang ada di webMethods.

‘<?xml version=”1.0″ encoding=”UTF-8″?>’
‘<SOAP-ENV:Envelope’
‘ xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/”&#8216;
‘ xmlns:SOAP-ENC=”http://schemas.xmlsoap.org/soap/encoding/”&#8216;
‘ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&#8216;
‘ xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>&#8217;
‘<SOAP-ENV:Body>’
‘<riset:getUp xmlns:riset=”http://www.webmethods.com/riset”>&#8217;
‘<input>{masukan}</input>’
‘</riset:getUp>’
‘</SOAP-ENV:Body>’
‘</SOAP-ENV:Envelope>’;

Dan dibawah ini adalah kode JavaFX lengkapnya, silahkan di coba.

/*
* Main.fx
*
* Created on Nov 3, 2009, 4:38:15 PM
*/

package invokewm;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.Exception;
import javafx.io.http.HttpHeader;
import javafx.io.http.HttpRequest;
import invokewm.parser.RSSPullParser;
import javafx.data.pull.Event;
import javafx.data.pull.PullParser;
import javafx.scene.control.ProgressBar;
import javafx.ext.swing.SwingTextField;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;

/**
* @author bhangun
*/

//– login akses ke webmethods
var httpHeader = HttpHeader.basicAuth(“username”, “password”);

var par:RSSPullParser;
var isi:String;
var progress:Integer;
var masukan:String;

//– hardcode format amplop yg akan dikirim ke webmethods
var amplop: String=bind ‘<?xml version=”1.0″ encoding=”UTF-8″?>’
‘<SOAP-ENV:Envelope’
‘ xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/”&#8216;
‘ xmlns:SOAP-ENC=”http://schemas.xmlsoap.org/soap/encoding/”&#8216;
‘ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&#8216;
‘ xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>&#8217;
‘<SOAP-ENV:Body>’
‘<riset:getUp xmlns:riset=”http://www.webmethods.com/riset”>&#8217;
‘<input>{masukan}</input>’
‘</riset:getUp>’
‘</SOAP-ENV:Body>’
‘</SOAP-ENV:Envelope>’;

//– ukuran amplop yang akan dikirim
def testContentSize: Integer = amplop.getBytes().length;

//– fungsi untuk memanggil service webmethods
def panggil: HttpRequest = HttpRequest {
location: “http://localhost:5555/soap/rpc&#8221;;

method: HttpRequest.POST;

headers: [
HttpHeader {
name: HttpHeader.CONTENT_TYPE;
value: “text/xml”;
},
HttpHeader {
name: HttpHeader.CONTENT_LENGTH;
value: “{testContentSize}”;
},
HttpHeader {
name: HttpHeader.AUTHORIZATION;
value: “Basic”;
},
HttpHeader {
name: HttpHeader.USER_AGENT;
value: “Mozilla/4.0”;
},
httpHeader
];

onStarted: function() {
println(“onStarted – started performing method: {panggil.method} on location: {panggil.location}”);
}

onConnecting: function() { println(“onConnecting”) }
onDoneConnect: function() { println(“onDoneConnect”) }
onWriting: function() { println(“onWriting”) }

onOutput: function(os: java.io.OutputStream) {
try {
println(“onOutput – about to write {testContentSize} bytes to output stream”);
os.write(amplop.getBytes());
} finally {
println(“onOutput – about to close output stream.”);
os.close();
}
}

onToWrite: function(bytes: Long) { println(“onToWrite – entire content to be written: {bytes} bytes”) }
onWritten: function(bytes: Long) {

println(“onWritten – {bytes} bytes has now been written”)
}
onDoneWrite: function() { println(“doneWrite”) }
onReadingHeaders: function() { println(“onReadingHeaders”) }
onResponseCode: function(code:Integer) { println(“onResponseCode – responseCode: {code}”) }
onResponseMessage: function(msg:String) { println(“onResponseMessage – responseMessage: {msg}”) }

onResponseHeaders: function(headerNames: String[]) {
println(“onResponseHeaders – there are {headerNames.size()} response headers:”);
for (name in headerNames) {
println(”    {name}: {panggil.getResponseHeaderValue(name)}”);
}
}

onReading: function() { println(“onReading”) }

onToRead: function(bytes: Long) {
if (bytes < 0) {
println(“onToRead – Content length not specified by server; bytes: {bytes}”);
} else {
println(“onToRead – total number of content bytes to read: {bytes}”);
}
}

onRead: function(bytes: Long) {

progress =panggil.toread;

}

onInput: function(is: java.io.InputStream) {
try {
println(“onInput – bytes of content available: {is.available()}”);
def parser = PullParser {

input: is

onEvent: function(event: Event) {

//println(event.toString());
if (event.type == PullParser.START_ELEMENT) {
if(event.qname.name == “hasil” and event.level == 3) {

println(event.name);
}
}

if(event.type == PullParser.TEXT){
if(event.qname.name == “hasil” and event.level == 3) {
isi = event.text;
println(event.text);
}
}

}
}
parser.parse();
} finally {
is.close();
}
}

onException: function(ex: java.lang.Exception) {
println(“onException – exception: {ex.getClass()} {ex.getMessage()}”);
}

onDoneRead: function() { println(“onDoneRead”) }
onDone: function() { println(“onDone”) }
};

var field:SwingTextField = SwingTextField {
columns: 10
editable: true
}

//——————- GUI
Stage {
title: “Uppercase Menggunakan Service webMethods”
width: 380
height: 150
scene: Scene {
content: [
VBox {
content: [
HBox {
content: [
field,
Button {
text: “Kirim”
action: function() {
masukan=field.text;
panggil.start()
}
}

]},
VBox {
content: [ProgressBar {
progress: bind ProgressBar.computeProgress( 100, progress )
},
Text {
font : Font {
size : 20
}
x: 10
y: 50
content: bind isi;
}
]
}
]}
]
}
}

Jika berhasil di jalankan akan muncul seperti dibawah ini:

Untuk mencoba silahkan masukan kata-kata dalam huruf kecil (lowercase), karena nanti hasilnya dalam huruf besar (uppercase).

source dapat di download disini;

<a href=”http://api.ning.com/files/Lqh-Mcf*Rx7JV7mnbsQrxd8Cmazt4ldez1y5LX24xkBIZEQUAD1tCadjVfCAFmltS2e7VDRg1QgIQPNy4*XtkYYfLr3eDwZp/InvokeWM.zip”>InvokeWM.zip</a&gt;

Advertisements

One Response to Memanggil Services WebMethods Melalui JavaFX

  1. […] Memanggil Services WebMethods Melalui JavaFX November 2009 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: