Thursday 8 October 2015

Overview

Bab  ini  aka menunjukkan  bagaimana  membuat  WSDL,  Web  Service Description  Language dari Web Service Java.

Membuat WSDL dengan wscompile Java

Contoh 1. Diinginkan membuat Web Service untuk mengkonversikan suhu dari Celcius menjadi Fahrenheit. Bagaimana membuat WSDL-nya?

Pertama, dibuat file interface TempConverterIntf.java sebagai berikut:
package temp_converter;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface TempConverterIntf extends Remote
{
public double getFahrenheit(double celcius) throws RemoteException;
}

Kedua, dibuat file implementasinya TempConverterImpl.java sebagai berikut:
package temp_converter;
import java.rmi.RemoteException;
public class TempConverterImpl implements TempConverterIntf
{
public double getFahrenheit(double celcius) throws RemoteException
{
double fahrenheit = 0;
fahrenheit = ((1.8) * celcius) + 32.0;
return fahrenheit;
}
}

Kedua file tersebut dikompilasi sebagai berikut:
C>....Folder>javac –d . TempConverter*.java

Ketiga,  buat  file  konfigurasi  awal  untuk  membuat  WSDL,  config.xml, sebagai berikut:

<?xml version="1.0" encoding="UTF-8"?>
<service
name="TempConverterWebService"



targetNamespace="urn:Foo" typeNamespace="urn:Foo" packageName="temp_converter">
<interface name="temp_converter.TempConverterIntf"/>
</service>
</configuration>

Gunakan perintah berikut di command prompt untuk membuat file wsdl, sekaligus mapping.xml :
C>..Folder>wscompile –define –mapping ./mapping.xml –d .
–nd . –classpath . config.xml

Berikut adalah file hasil WSDLnya, TempConverterWebService.wsdl :

<?xml version="1.0" encoding="UTF-8"?>

<definitions name="TempConverterWebService" targetNamespace="urn:Foo" xmlns:tns="urn:Foo" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<types/>
<message name="TempConverterInterface_getFahrenheit">
<part name="double_1" type="xsd:double"/></message>
<message name="TempConverterInterface_getFahrenheitResponse">
<part name="result" type="xsd:double"/></message>
<portType name="TempConverterInterface">
<operation name="getFahrenheit" parameterOrder="double_1">
<input message="tns:TempConverterInterface_getFahrenheit"/>
<output message="tns:TempConverterInterface_getFahrenheitResponse"/></operatio n></portType>
<binding name="TempConverterInterfaceBinding" type="tns:TempConverterInterface">
style="rpc"/>
<operation name="getFahrenheit">
<soap:operation soapAction=""/>
<input>



<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="urn:Foo"/></input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="urn:Foo"/></output></operation></binding>
<service name="TempConverterWebService">
<port name="TempConverterInterfacePort" binding="tns:TempConverterInterfaceBinding">
<soap:address location="REPLACE_WITH_ACTUAL_URL"/></port></service></definition s>

Perhatikan tag-tag penting elemen file WSDL yang ditandai bold di atas. Disamping file WSDL di atas, juga dihasilkan file mapping.xml berikut:

<?xml version="1.0" encoding="UTF-8"?>
<package-mapping>
<package-type>temp_converter</package-type>
<namespaceURI>urn:Foo</namespaceURI>
</package-mapping>
<package-mapping>
<package-type>temp_converter</package-type>
<namespaceURI>urn:Foo</namespaceURI>
</package-mapping>
<service-interface-mapping>
<service-interface>temp_converter.TempConverterWebService</service- interface>
<wsdl-service-name xmlns:serviceNS="urn:Foo">serviceNS:TempConverterWebService</wsdl- service-name>
<port-mapping>
<port-name>TempConverterInterfacePort</port-name>
<java-port-name>TempConverterInterfacePort</java-port-name>
</port-mapping>
</service-interface-mapping>
<service-endpoint-interface-mapping>



<service-endpoint- interface>temp_converter.TempConverterInterface</service-endpoint- interface>
<wsdl-port-type xmlns:portTypeNS="urn:Foo">portTypeNS:TempConverterInterface</wsdl- port-type>
<wsdl-binding xmlns:bindingNS="urn:Foo">bindingNS:TempConverterInterfaceBinding</ws dl-binding>
<service-endpoint-method-mapping>
<java-method-name>getFahrenheit</java-method-name>
<wsdl-operation>getFahrenheit</wsdl-operation>
<method-param-parts-mapping>
<param-position>0</param-position>
<param-type>double</param-type>
<wsdl-message-mapping>
<wsdl-message xmlns:wsdlMsgNS="urn:Foo">wsdlMsgNS:TempConverterInterface_getFahre nheit</wsdl-message>
<wsdl-message-part-name>double_1</wsdl-message-part-name>
<parameter-mode>IN</parameter-mode>
</wsdl-message-mapping>
</method-param-parts-mapping>
<wsdl-return-value-mapping>
<method-return-value>double</method-return-value>
<wsdl-message xmlns:wsdlMsgNS="urn:Foo">wsdlMsgNS:TempConverterInterface_getFahre nheitResponse</wsdl-message>
<wsdl-message-part-name>result</wsdl-message-part-name>
</wsdl-return-value-mapping>
</service-endpoint-method-mapping>
</service-endpoint-interface-mapping>
</java-wsdl-mapping>


Contoh  2.    Misal  akan  dibuat  web  service  ramalan  bintang.  Bagaimana membuat file WSDL-nya?

Pertama, perlu dibuat file interface-nya, HoroscopeInterface.java, sebagai berikut.



package horoscope;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HoroscopeInterface extends Remote
{
public String getSunSignTrait(int sunsign) throws RemoteException;
}


Berikutnya file implementasinya, HoroscopeImplementation.java, senagai berikut:

package horoscope;

import java.rmi.RemoteException;
import java.sql.*;

public class HoroscopeImplementation implements HoroscopeInterface
{
public String getSunSignTrait(int sunsign) throws RemoteException
{
String sunSignTrait[] = new String[12];
/* Aries sun sign trait */
sunSignTrait[0]  =  "Arians  are  adventurous,  ambitious, impulsive, enthusiastic, and full of energy.";
/* Taurus sun sign trait */
sunSignTrait[1]    =    "Taureans    are    practical,    stable, conservative, and lovers of peace.";
/* Gemini sun sign trait */
sunSignTrait[2]    "Geminis   ar dual-natured elusive, complex, and contradictory.";
/* Cancer sun sign trait */
sunSignTrait[3] = "Cancerians are timid and shy. They are also dull and conservative.";
/* Leo sun sign trait */
sunSignTrait[4] = "Leos are ambitious, courageous, strong- willed, independent, and self-confident.";
/* Virgo sun sign trait */



sunSignTrait[5] = "Virgos are characterized by preciseness, refinement, love for cleanliness, hygiene, and good order.";
/* Libra sun sign trait */
sunSignTrait[6] = "Librans are elegant, charming, naturally kind, gentle, and lovers of beauty, harmony and justice.";
/* Scorpio sun sign trait */
sunSignTrait[7] = "Scorpios are the most intense, profound, and powerful among the zodiacs.";
/* Sagittarius sun sign trait */
sunSignTrait[8] = "Sagittarians are full of energy, versatility, adventurousness, and eagerness to extend experience.";
/* Capricorn sun sign trait */
sunSignTrait[9] = "Capricorns are confident, strong-willed,

and calm.";

/* Aquarius sun sign trait */
sunSignTrait[10] = "Aquarians basically possess strong and

attractive personalities.";
/* Pisces sun sign trait */
sunSignTrait[11] = "Pisceans are gentle, patient, malleable, generous, friendly, creative, and sensitive to others' feelings.";

switch(sunsign)
{

case 0: case 1: case 2: case 3: case 4: case 5:

/* Returns sun sign trait of Aries */
return sunSignTrait[0];

/* Returns sun sign trait of Taurus */
return sunSignTrait[1];

/* Returns sun sign trait of Gemini */
return sunSignTrait[2];

/* Returns sun sign trait of Cancer */
return sunSignTrait[3];

/* Returns sun sign trait of Leo */
return sunSignTrait[4];

/* Returns sun sign trait of Virgo */





case 6:


case 7:

return sunSignTrait[5];

/* Returns sun sign trait of Libra */
return sunSignTrait[6];

/* Returns sun sign trait of Scorpio */

return sunSignTrait[7];
case 8:














default:

}
}
}



case 9: case 10: case 11:

/* Returns sun sign trait of Sagittarius */
return sunSignTrait[8];

/* Returns sun sign trait of Capricorn */
return sunSignTrait[9];

/* Returns sun sign trait of Aquarius */
return sunSignTrait[10];

/* Returns sun sign trait of Pisces */
return sunSignTrait[11];

return null;



File  konfigurasi  awal  untuk  membuat  WSDL,  config.xml,  dibuat  sebagai berikut:

<?xml version="1.0" encoding="UTF-8"?>
<service
name="HoroscopeWebService" targetNamespace="urn:Foo" typeNamespace="urn:Foo" packageName="horoscope">
<interface name="horoscope.HoroscopeInterface"/>
</service>
</configuration>



Gunakan perintah berikut di command prompt untuk membuat file wsdl, sekaligus mapping.xml :
C>..Folder>wscompile –define –mapping ./mapping.xml –d .
–nd . –classpath . config.xml

Berikut adalah file hasil WSDLnya, HoroscopeWebService.wsdl :

<?xml version="1.0" encoding="UTF-8"?>

<definitions name="HoroscopeWebService" targetNamespace="urn:Foo" xmlns:tns="urn:Foo" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<types/>
<message name="HoroscopeInterface_getSunSignTrait">
<part name="int_1" type="xsd:int"/></message>
<message name="HoroscopeInterface_getSunSignTraitResponse">
<part name="result" type="xsd:string"/></message>
<portType name="HoroscopeInterface">
<operation name="getSunSignTrait" parameterOrder="int_1">
<input message="tns:HoroscopeInterface_getSunSignTrait"/>
<output message="tns:HoroscopeInterface_getSunSignTraitResponse"/></operation>
</portType>
<binding name="HoroscopeInterfaceBinding" type="tns:HoroscopeInterface">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="getSunSignTrait">
<soap:operation soapAction=""/>
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="urn:Foo"/></input>
<output>
use="encoded" namespace="urn:Foo"/></output></operation></binding>
<service name="HoroscopeWebService">
<port name="HoroscopeInterfacePort" binding="tns:HoroscopeInterfaceBinding">



<soap:address location="REPLACE_WITH_ACTUAL_URL"/></port></service></definition s>
Selain file WSDL juga dihasilkan file mapping.xml sebagai berikut:

<?xml version="1.0" encoding="UTF-8"?>
<java-wsdl-mapping                          xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       version="1.1" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://www.ibm.com/webservices/xsd/j2ee_jaxrpc_mapping_1_1.xsd">
<package-mapping>
<package-type>horoscope</package-type>
<namespaceURI>urn:Foo</namespaceURI>
</package-mapping>
<package-mapping>
<package-type>horoscope</package-type>
<namespaceURI>urn:Foo</namespaceURI>
</package-mapping>
<service-interface-mapping>
<service-interface>horoscope.HoroscopeWebService</service-interface>
<wsdl-service-name xmlns:serviceNS="urn:Foo">serviceNS:HoroscopeWebService</wsdl- service-name>
<port-mapping>
<port-name>HoroscopeInterfacePort</port-name>
<java-port-name>HoroscopeInterfacePort</java-port-name>
</port-mapping>
</service-interface-mapping>
<service-endpoint-interface-mapping>
<service-endpoint-interface>horoscope.HoroscopeInterface</service- endpoint-interface>
<wsdl-port-type xmlns:portTypeNS="urn:Foo">portTypeNS:HoroscopeInterface</wsdl-port- type>
<wsdl-binding
xmlns:bindingNS="urn:Foo">bindingNS:HoroscopeInterfaceBinding</wsdl- binding>
<service-endpoint-method-mapping>
<java-method-name>getSunSignTrait</java-method-name>
<wsdl-operation>getSunSignTrait</wsdl-operation>



<method-param-parts-mapping>
<param-position>0</param-position>
<param-type>int</param-type>
<wsdl-message-mapping>
<wsdl-message xmlns:wsdlMsgNS="urn:Foo">wsdlMsgNS:HoroscopeInterface_getSunSignTr ait</wsdl-message>
<wsdl-message-part-name>int_1</wsdl-message-part-name>
<parameter-mode>IN</parameter-mode>
</wsdl-message-mapping>
</method-param-parts-mapping>
<wsdl-return-value-mapping>
<method-return-value>java.lang.String</method-return-value>
<wsdl-message xmlns:wsdlMsgNS="urn:Foo">wsdlMsgNS:HoroscopeInterface_getSunSignTr aitResponse</wsdl-message>
<wsdl-message-part-name>result</wsdl-message-part-name>
</wsdl-return-value-mapping>
</service-endpoint-method-mapping>
</service-endpoint-interface-mapping>
</java-wsdl-mapping>