Showing posts with label WebMethods. Show all posts
Showing posts with label WebMethods. Show all posts

Monday, October 26, 2009

Memanggil Service WebMethods melalui PL/SQL

Pada contoh ini, ada beberapa fungsi penting yang digunakan untuk memanggil webservices yang terdapat pada Oracle, yaitu UTL_HTTP dan XMLTYPE, tapi fungsi-fungsi ini hanya bisa dijalankan untuk veri 9i keatas, untuk 8i kayaknya harus bikin fungsi pakai native Java-nya. Fungsi utama untuk memanggil webservice atau request (dengan metode POST/GET) pada PL/SQL terletak pada fungsi UTL_HTTP. Sedangkan XMLTYPE digunakan untuk parsing xml.

Berikut ini adalah contoh API untuk memanggil services pada webMethods, setelah disesuaikan format envelope-nya dengan API pada webmethods.


----------------------------code------------------------------------------------

CREATE OR REPLACE PACKAGE WEBMETHOD.soap_api AS
-- --------------------------------------------------------------------------
-- Name : http://forum.swamedia.co.id
-- Author : Bhangun
-- Description : API untuk memanggil web services pada webMethods.
-- Ammedments :
-- When Who What
-- =========== ======== =================================================
-- 21-OCT-2009 Bhangun Initial Creation
-- --------------------------------------------------------------------------

TYPE t_request IS RECORD (
method VARCHAR2(256),
service VARCHAR2(256),
namespace VARCHAR2(256),
body VARCHAR2(32767),
envelope_tag VARCHAR2(30)
);

TYPE t_response IS RECORD
(
doc XMLTYPE,
envelope_tag VARCHAR2(30)
);



PROCEDURE set_proxy_authentication(p_username IN VARCHAR2,
p_password IN VARCHAR2);

FUNCTION new_request(p_method IN VARCHAR2,
p_service IN VARCHAR2,
p_namespace IN VARCHAR2,
p_envelope_tag IN VARCHAR2 DEFAULT 'SOAP-ENV')
RETURN t_request;


PROCEDURE add_parameter(p_request IN OUT NOCOPY t_request,
p_name IN VARCHAR2,
p_type IN VARCHAR2,
p_value IN VARCHAR2);

FUNCTION invoke(p_request IN OUT NOCOPY t_request,
p_url IN VARCHAR2,
p_action IN VARCHAR2)
RETURN t_response;

FUNCTION get_return_value(p_response IN OUT NOCOPY t_response,
p_name IN VARCHAR2,
p_namespace IN VARCHAR2)
RETURN VARCHAR2;

END soap_api;
/
--------------------------------------------------------------------------


ini Body-nya
-------------------------code-------------------------------------------------
CREATE OR REPLACE PACKAGE BODY WEBMETHOD.soap_api AS
-- --------------------------------------------------------------------------
-- Name : http://www.oracle-base.com/dba/miscellaneous/soap_api
-- Author : DR Timothy S Hall
-- Description : SOAP related functions for consuming web services.
-- Ammedments :
-- When Who What
-- =========== ======== =================================================
-- 04-OCT-2003 Tim Hall Initial Creation
-- 23-FEB-2006 Tim Hall Parameterized the "soap" envelope tags.
-- 08-JUN-2006 Tim Hall Add proxy authentication functionality.
-- --------------------------------------------------------------------------

g_proxy_username VARCHAR2(50) := NULL;
g_proxy_password VARCHAR2(50) := NULL;


-- ---------------------------------------------------------------------
PROCEDURE set_proxy_authentication(p_username IN VARCHAR2,
p_password IN VARCHAR2) AS
-- ---------------------------------------------------------------------
BEGIN
g_proxy_username := p_username;
g_proxy_password := p_password;
END;
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
FUNCTION new_request(p_method IN VARCHAR2,
p_service IN VARCHAR2,
p_namespace IN VARCHAR2,
p_envelope_tag IN VARCHAR2 DEFAULT 'SOAP-ENV')
RETURN t_request AS
-- ---------------------------------------------------------------------
l_request t_request;
BEGIN
l_request.method := p_method;
l_request.service := p_service;
l_request.namespace := p_namespace;
l_request.envelope_tag := p_envelope_tag;
RETURN l_request;
END;
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
PROCEDURE add_parameter(p_request IN OUT NOCOPY t_request,
p_name IN VARCHAR2,
p_type IN VARCHAR2,
p_value IN VARCHAR2) AS
-- ---------------------------------------------------------------------
BEGIN
p_request.body := p_request.body||'<'||p_name||' xsi:type="'||p_type||'">'||p_value||'';
--DBMS_OUTPUT.PUT_LINE('<'||p_name||' xsi:type="'||p_type||'">'||p_value||'');
END;
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
PROCEDURE generate_envelope(p_request IN OUT NOCOPY t_request,
p_env IN OUT NOCOPY VARCHAR2) AS
-- ---------------------------------------------------------------------
BEGIN

p_env := '<'||p_request.envelope_tag||':Envelope xmlns:'||p_request.envelope_tag||'="http://schemas.xmlsoap.org/soap/envelope/" ' ||
' xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' ||
'<'||p_request.envelope_tag||':Body>' ||
'<'||p_request.method||':'||p_request.service||' xmlns:'||p_request.method||'="'||p_request.namespace||'">' ||
p_request.body ||
'' ||
'' ||
'';


END;
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
PROCEDURE show_envelope(p_env IN VARCHAR2) AS
-- ---------------------------------------------------------------------
i PLS_INTEGER;
l_len PLS_INTEGER;
BEGIN
i := 1; l_len := LENGTH(p_env);
WHILE (i <= l_len) LOOP
DBMS_OUTPUT.put_line(SUBSTR(p_env, i, 60));
i := i + 60;
END LOOP;
END;
-- ---------------------------------------------------------------------

-- ---------------------------------------------------------------------
PROCEDURE check_fault(p_response IN OUT NOCOPY t_response,
p_env IN VARCHAR2) AS
-- ---------------------------------------------------------------------
l_fault_node XMLTYPE;
l_fault_code VARCHAR2(256);
l_fault_string VARCHAR2(32767);
BEGIN
l_fault_node := p_response.doc.extract('/'||p_response.envelope_tag||':Fault',
'xmlns:'||p_response.envelope_tag||'="http://schemas.xmlsoap.org/soap/envelope/');
IF (l_fault_node IS NOT NULL) THEN
l_fault_code := l_fault_node.extract('/'||p_response.envelope_tag||':Fault/faultcode/child::text()',
'xmlns:'||p_response.envelope_tag||'="http://schemas.xmlsoap.org/soap/envelope/').getstringval();
l_fault_string := l_fault_node.extract('/'||p_response.envelope_tag||':Fault/faultstring/child::text()',
'xmlns:'||p_response.envelope_tag||'="http://schemas.xmlsoap.org/soap/envelope/').getstringval();
--RAISE_APPLICATION_ERROR(-20000, l_fault_code || ' - ' || l_fault_string||'-->>'||p_env);
RAISE_APPLICATION_ERROR(-20000, p_env);
END IF;
END;
-- ---------------------------------------------------------------------

-- ---------------------------------------------------------------------
FUNCTION invoke(p_request IN OUT NOCOPY t_request,
p_url IN VARCHAR2,
p_action IN VARCHAR2)
RETURN t_response AS
-- ---------------------------------------------------------------------
l_envelope VARCHAR2(32767);
l_http_request UTL_HTTP.req;
l_http_response UTL_HTTP.resp;
l_response t_response;
BEGIN
generate_envelope(p_request, l_envelope);
show_envelope(l_envelope);
l_http_request := UTL_HTTP.begin_request(p_url, 'POST','HTTP/1.0');
IF g_proxy_username IS NOT NULL THEN
UTL_HTTP.set_authentication(r => l_http_request,
username => g_proxy_username,
password => g_proxy_password,
scheme => 'Basic',
for_proxy => TRUE);
END IF;
UTL_HTTP.set_header(l_http_request, 'Content-Type', 'text/xml');
UTL_HTTP.set_header(l_http_request, 'Content-Length', LENGTH(l_envelope));
--UTL_HTTP.set_header(l_http_request, 'SOAPAction', p_action);
UTL_HTTP.write_text(l_http_request, l_envelope);
l_http_response := UTL_HTTP.get_response(l_http_request);
UTL_HTTP.read_text(l_http_response, l_envelope);
UTL_HTTP.end_response(l_http_response);
l_response.doc := XMLTYPE.createxml(l_envelope);
l_response.envelope_tag := p_request.envelope_tag;
l_response.doc := l_response.doc.extract('/'||l_response.envelope_tag||':Envelope/'||l_response.envelope_tag||':Body/child::node()',
'xmlns:'||l_response.envelope_tag||'="http://schemas.xmlsoap.org/soap/envelope/"');
------------------liat
show_envelope(l_response.doc.getstringval());
check_fault(l_response,l_envelope);
RETURN l_response;
END;
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
FUNCTION get_return_value(p_response IN OUT NOCOPY t_response,
p_name IN VARCHAR2,
p_namespace IN VARCHAR2)
RETURN VARCHAR2 AS
-- ---------------------------------------------------------------------
BEGIN
RETURN p_response.doc.extract('//'||p_name||'/child::text()',p_namespace).getstringval();
END;
-- ---------------------------------------------------------------------

END soap_api;
/
---------------------------------------------------------------------------


ini fungsi untuk memanggil soap_api
-----------------------------code----------------------------------------------
CREATE OR REPLACE FUNCTION WEBMETHOD.getdata (param IN VARCHAR2)
RETURN VARCHAR2
AS
l_request soap_api.t_request;
l_response soap_api.t_response;
l_return VARCHAR2(32767);
l_url VARCHAR2(32767);
l_namespace VARCHAR2(32767);
l_method VARCHAR2(32767);
l_service VARCHAR2(32767);
l_soap_action VARCHAR2(32767);
l_result_name VARCHAR2(32767);

BEGIN
-- Set proxy details if no direct net connection.
--UTL_HTTP.set_proxy('server', NULL);
--UTL_HTTP.set_persistent_conn_support(TRUE);

-- Set proxy authentication if necessary.
soap_api.set_proxy_authentication(p_username => 'Administrator',
p_password => 'manage');

l_url := 'http://localhost:5555/soap/rpc';
l_method := 'riset';
l_namespace := 'http://www.webmethods.com/'||l_method;
l_service := 'getUp';
l_soap_action := '';
l_result_name := 'hasil';

l_request := soap_api.new_request(p_method => l_method,
p_service => l_service,
p_namespace => l_namespace);

soap_api.add_parameter(p_request => l_request,
p_name => 'input',
p_type => 'xsd:string',
p_value => param);

l_response := soap_api.invoke(p_request => l_request,
p_url => l_url,
p_action => l_soap_action);

--l_return := soap_api.get_return_value(p_response => l_response,
-- p_name => l_result_name,
-- p_namespace => l_namespace);

RETURN null;--l_return;
END;
/

---------------------------------------------------------------------------



ini contoh untuk memanggil fungsinya:

----------------------------code------------------------------------------------
DECLARE
RetVal VARCHAR2(200);
PARAM VARCHAR2(200);

BEGIN
PARAM := 'bhangun';

RetVal := WEBMETHOD.GETDATA ( PARAM );
COMMIT;
END;
----------------------------------------------------------------------------


dan ini adalah hasilnya :

----------------------------hasil------------------------------------------------
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">


bhangun


xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENC:root="1">
BHANGUN


----------------------------hasil------------------------------------------------


Untuk sementara ini fungsi soap_api.get_return_value sengaja tidak digunakan karena belum nemu format XPath untuk response-nya di webMethod. Keluarannya juga baru dalam bentuk output di konsol tapi nanti bisa disesuaikan dengan kebutuhan, mau dimasukan kedalam tabel, langsung diambil value-nya ato terserah.... Laughing hehhe maklum soalnya baru nyoba PL/SQL lagi jadi kudu belajar dulu yg lain-lainya Wink

Silahkan dikembangkan lagi.... Selamat mencoba!!

Thursday, July 30, 2009

Calling WebMethods services Menggunakan SOAP-RPC pakai Java

ini adalah contoh class manggil service webmethods menggunakan SOAP-RPC.
Ini belum baca langsung dari file WSDL tapi ngambil infonya dari WSDL.
Untuk baca langsung dari file WSDL nanti nyusul.
Service yang dipake toUpper, mangga dicobian.... Very Happy

/* ini library yang dibutuhkan:
* axis-1_2/lib/axis.jar
* axis-1_2/lib/jaxrpc.jar
* axis-1_2/lib/saaj.jar
* axis-1_2/lib/commons-logging.jar
* axis-1_2/lib/commons-discovery.jar
* axis-1_2/lib/wsdl4j.jar
*
* bisa di download di:
* http://archive.apache.org/dist/ws/axis/1_2/
*/

package wsdl;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
/**
*
* @author bhangun
*
*/
public class TesWSDL {
public static void main(String[] args) {
String namespaceURI = "http://www.webmethods.com/pub.string";
String endpoint = "http://localhost:5555/soap/rpc";
String localPart = "toUpper";
String username = "Administrator";
String pass = "manage";
String paramName = "inString";
String nsReturnType= "http://www.w3.org/2001/XMLSchema";
String returnType = "String";
QName xmlType = org.apache.axis.Constants.XSD_STRING;
ParameterMode parameterMode = ParameterMode.IN;

try {

Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName(namespaceURI, localPart));
call.setUsername(username);
call.setPassword(pass);
call.addParameter(paramName, xmlType, parameterMode);
call.setReturnType(new QName(nsReturnType,returnType));
String ret = (String) call.invoke(new Object[] { "bismillah!" });

System.out.println("Coba ku urang di cobian, '" + ret + "'");
} catch (Exception e) {
System.err.println(e.toString());
}
}
}


Thursday, December 04, 2008

Manggil Service WebMethods pakai Servlet di build pake Maven2

Environment
Sebelum mulai ada beberapa environment yang perlu dipersiapkan, diantaranya tools/teknologi yang saya gunakan untuk percobaan ini adalah:
1. Eclipse Gannymede(ato versi lainnya)
2. Maven2
3. Servlet
4. JSP
5. WebMethods+Integration Server 6.5


yang lainnya lebih lengkapnya liat disini:
http://forum.swamedia.co.id/viewtopic.php?p=628#628

Kelas
Ini kelas servlet-nya
package test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wm.data.*;
import com.wm.app.b2b.server.Service;

public class UpperCase extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;

// Allows you to have finer control over the logging levels
private static Log log = LogFactory.getLog( UpperCase.class );


public void doPost( HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ServletContext ctx = getServletContext();

// Will show on IS level 8
ctx.log("logging a string in the IS server log via the Servlet API");
log.info("Servlet logging at the IS level 4");
log.debug("Servlet logging at the IS level 8");


//passing parameter to variabel
String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");

// INPUT -----------------------
IData input = IDataFactory.create();
IDataCursor inputCursor = input.getCursor();
IDataUtil.put( inputCursor, "inString", param2+param1 );
IDataUtil.put( inputCursor, "language", "language" );
IDataUtil.put( inputCursor, "country", "country" );
IDataUtil.put( inputCursor, "variant", "variant" );

// OUTPUT -----------------------
IData output = IDataFactory.create();
IData output2 = IDataFactory.create();

// invoke the WM service riset:getUpperName
/*try{
output = Service.doInvoke( "riset", "getUpperName", input );
}catch( Exception e){}
IDataCursor outputCursor2 = output2.getCursor();
String name = IDataUtil.getString( outputCursor2, "name" );
*/
// invoke the WM service pub.string:toUpper
try{
output = Service.doInvoke( "pub.string", "toUpper", input );
}catch( Exception e){}
IDataCursor outputCursor = output.getCursor();
String value = IDataUtil.getString( outputCursor, "value" );

//destroy the cursor
inputCursor.destroy();
//outputCursor2.destroy();
outputCursor.destroy();


}
}




PROSES
Setelah environment-nya dipersiapkan, selanjutnya adalah proses berikutnya
1. Build source di eclipse menggunakan Maven2. Atau jika tidak ada bisa menggunakan builder apapun. Intinya kita ingin mendapatkan paket WAR-nya (WAR=Web Archive). Klo menggunakan eclipse cukup mudah (tentunya klo plugin Maven2 sudah di install di eclipse). Jalankan Run As->Maven Packages.

2. Setelah itu masukan file war-nya (dalam hal ini CobaServlet.War) ke dalam folder /web/webapps.

3. Kemudian jalankan service pada packages WmTomcat, wm.tomcat:hotdeploy.

4. Jadi deh Packages CobaServlet-nya, selanjutnya tinggal dijalankan deh.

5. Ok, Selamat Mencoba!! Jika ada masukan silahkan hubungi saya ok! Terima kasih.

Wednesday, October 15, 2008

Bikin Java Service dari WebMethods Integration Server di Eclipse

Kalo ada yang mo nyoba silahkan, tapi ada beberapa hal yang perlu disiapkan:
1. Sediakan client.jar dan server.jar dari Integration servernya
2. Lakukan konfigurasi Library Eclipse, lalu tambahkan client.jar dan server.jar pada library.
3. Buat file xml, yaitu file untuk konfigurasi Input, berikut value-nya. Cotohnya seperti dibawah, kemudian simpan sebagai pipeline.xml. Lalu tempatkan sesuai dengan path yang akan dibaca kelas dibawah nanti:


"<"xml">"
\
\
\bhangun\
\Bandung\
\28\
\

\

4. Buat kelas seperti dibawah ini. Ingat path ayng ada dalam kelas merujuk kepada file pipeline.xml yang dibuat tadi:


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.wm.app.b2b.client.ServiceException;
import com.wm.data.IData;
import com.wm.data.IDataCursor;
import com.wm.data.IDataUtil;
import com.wm.util.coder.IDataXMLCoder;
import com.wm.util.coder.InvalidDatatypeException;


public class TestWm {

/**
* @param args
* @throws ServiceException
*/
public static void main(String[] args) throws ServiceException {

try {
IData pipeline = new IDataXMLCoder().decode(new FileInputStream("C:\\Users\\bhangun\\workspace\\webmethods\\src\\pipeline.xml"));
myTest(pipeline);
} catch (InvalidDatatypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static final void myTest(IData pipeline)
throws ServiceException
{
// --- <> ---
// @subtype unknown
// @sigtype java 3.5
// [i] field:0:required name
// [i] field:0:required address
// [i] field:0:required age

// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
@SuppressWarnings("unused")
String name = IDataUtil.getString( pipelineCursor, "name" );
@SuppressWarnings("unused")
String address = IDataUtil.getString( pipelineCursor, "address" );
@SuppressWarnings("unused")
String age = IDataUtil.getString( pipelineCursor, "age" );



pipelineCursor.first();
while (pipelineCursor.next()){

System.out.println(pipelineCursor.getValue());
}

pipelineCursor.destroy();



// pipeline

// --- <> ---


}

}


5. Coba jalankan, hasilnya kira-kira kayak gini deh:

Quote:
hartani
28


6. Selamat mencoba !!!