|
Programming Web Services with Scripting Languages, by Kam Lee
WSJ Vol 02 Issue 01 - pg.11
Listing 1:
Advogato_Query.pm
package Advogato_Query;
sub GetCurrentArticles {
my @article_titles = [];
.....
return \@article_titles;
}
sub GetArticleDetails {
my
$article_title = shift;
my
$article_details = "";
.....
return
$article_details;
}
sub GetArticleList {
my $start_date = shift;
my $end_date = shift;
my @article_list = [];
.....
return \@article_list;
}
1;
Listing 2:
Advogato_Query.py
def GetCurrentArticles():
article_titles = []
.....
return article_titles
def GetArticleDetails( article_title ):
article_details = ""
.....
return article_details
def GetArticleList( start_date, end_date ):
article_list = []
.....
return article_list
Listing 3:
Advogato_Query.wsdl
<xml version="1.0"?>
<definitions name="SOAP"
targetNamespace="http://websservices.activestate.com/PerlEx/soap.plex"
xmlns:tns="http://websservices.activestate.com/PerlEx/soap.plex"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<schema targetNamespace='http://soapinterop.org/'
xmlns='http://www.w3.org/1999/XMLSchema'>
<complexType name='dateStruct'>
<all>
<element
name="year" type="int"/>
<element
name="month" type="int"/>
<element
name="day" type="int"/>
</all>
</complexType>
</schema>
</types>
<message
name="GetCurrentArticles">
</message>
<message
name="GetCurrentArticlesResponse">
<part name="result" type="t:ArrayOfString"
xmlns:t="SOAP-ENC"/>
</message>
<message
name="GetArticleDetails">
<part name="article_title" type="t:string"
xmlns:t="http://www.w3.org/1999/XMLSchema"/>
</message>
<message
name="GetArticleDetailsResponse">
<part name="result" type="t:string"
xmlns:t="http://www.w3.org/1999/XMLSchema"/>
</message>
<message
name="GetArticleList">
<part name="start_date" type="t:dateStruct"
xmlns:t="http://soapinterop.org/"/>
<part name="end_date" type="t:dateStruct"
xmlns:t="http://soapinterop.org/"/>
</message>
<message
name="GetArticleListResponse">
<part name="result" type="t:ArrayOfString"
xmlns:t="SOAP-ENC"/>
</message>
<portType
name="Advogato_Query_PortType">
<operation name="GetCurrentArticles">
<input name="GetCurrentArticles"
message="tns:GetCurrentArticles"/>
<output name="GetCurrentArticlesResponse"
message="tns:GetCurrentArticlesResponse"/>
</operation>
<operation name="GetArticleDetails">
<input name="GetArticleDetails"
message="tns:GetArticleDetails"/>
<output name="GetArticleDetailsResponse"
message="tns:GetArticleDetailsResponse"/>
</operation>
<operation name="GetArticleList">
<input name="GetArticleList" message="tns:GetArticleList"/>
<output name="GetArticleListResponse"
message="tns:GetArticleListResponse"/>
</operation>
</portType>
<binding
name="Advogato_Query_Binding" type="tns:Advogato_Query_PortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetCurrentArticles">
<soap:operation soapAction="urn:webservices"/>
<input>
<soap:body use="encoded"
namespace="http://webservices.activestate.com/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded"
namespace="http://webservices.activestate.com/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="GetArticleDetails">
<soap:operation soapAction="urn:webservices"/>
<input>
<soap:body use="encoded"
namespace="http://webservices.activestate.com/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded"
namespace="http://webservices.activestate.com/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="GetArticleList">
<soap:operation soapAction="urn: webservices"/>
<input>
<soap:body use="encoded" namespace="http://
webservices.activestate.com/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="http://
webservices.activestate.com/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service
name="Advogato_Query">
<port name="Advogato_Query_Port"
binding="tns:Advogato_Query_Binding">
<soap:address
location="http://websservices.activestate.com/PerlEx/soap.plex"/>
</port>
</service>
</definitions>
Listing 4
package Translate;
sub translate
{
my $mode = shift;
my $text = shift;
my $translated_text = "";
my $uri = "http://services.xmethods.net:80/perl/soaplite.cgi";
my $msg = <<"EOT";
<SOAP-ENV:Envelope
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<m:BabelFish xmlns:m="urn:xmethodsBabelFish">
<translationmode
xsi:type="xsd:string">$mode</translationmode>
<sourcedata xsi:type="xsd:string">$text</sourcedata>
</m:BabelFish>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
EOT style="mso-tab-count: 1">
require LWP::UserAgent;
my $request = HTTP::Request->new(POST => $uri);
$request->content_type("text/xml");
$request->header(SOAPAction =>
'"urn:xmethodsBabelFish#BabelFish"');
$request->content($msg);
my $res = LWP::UserAgent->new->request($request);
if ($res->content =~ /<return.*>(.*)<\/return>/) {
style="mso-tab-count: 1">
$translated_text = $1;
}
return $translated_text;
}
1;
Listing 5
def translate( mode, text ):
translated_text = ""
msg = """
<SOAP-ENV:Envelope
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<m:BabelFish xmlns:m="urn:xmethodsBabelFish">
<translationmode xsi:type="xsd:string">""" + mode +
"""</translationmode>
<sourcedata xsi:type="xsd:string">""" + text +
"""</sourcedata>
</m:BabelFish>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
"""
import httplib
h = httplib.HTTP("services.xmethods.net:80")
h.putrequest("POST", "/perl/soaplite.cgi")
h.putheader("Content-type", "text/xml")
h.putheader("Content-length", "%d" % len(msg))
h.putheader("SoapAction", "urn:xmethodsBabelFish#BabelFish")
h.endheaders()
h.send(msg)
reply, message, hdrs = h.getreply()
data = h.getfile().read()
import re
p = re.compile('<return.*>(.*)</return>')
s = p.search(data)
if s:
translated_text = s.group(1)
else:
translated_text = "ERROR!"
return translated_text
Listing 6:
the translate() subroutine in Perl
sub translate
{
my $mode = shift;
my $text = shift;
use SOAP::Lite
on_action => sub {sprintf '"urn:xmethodsBabelFish#BabelFish"'},
uri => 'urn:xmethodsBabelFish',
proxy => 'http://services.xmethods.net:80/perl/soaplite.cgi';
my $dat1 = SOAP::Data ->name('translationmode' => $mode);
my $dat2 = SOAP::Data ->name('sourcedata' => $text);
my $interface = new SOAP::Lite;
my $translated_text = $interface->BabelFish($dat1,
$dat2)->result;
return $translated_text;
}
Listing 7:
The translate() subroutine in Perl sub translate
{
my mode = shift;
my text = shift;
use WebService;
my $babel_service = WebService->new(
"http://www.xmethods.net/sd/2001/BabelFishService.wsdl" );
my $translated_text = $babel_service->getTranslation( $mode, $text
);
return $translated_text;
}
Listing 8:
The code in Python def translate( mode, text ):
import WebService
babel_service = WebService.new(
"http://www.xmethods.net/sd/2001/BabelFishService.wsdl" )
translated_text = babel_service.getTranslation( mode, text )
return translated_text
Listing 9:
News_In_French.pm =for interface
def str_array = ustring[];
interface Advogato_In_French {
static str_array GetArticleList();
};
<soap soapaction="urn:webservices"/>
<soap
namespace="http://webservices.activestate.com/"/>
=cut
package Advogato_In_French;
sub GetArticleList
{
use WebService;
my @articlelist = [];
my @translatedlist = [];
my $advogato_service = WebService->new(
"http://webservices.activestate.com/" );
$articlelist = $advogato_service->GetCurrentArticles();
my $articles_str = join( ":::::",
@articlelist );
my $babel_service = WebService->new(
"http://www.xmethods.net/sd/2001/BabelFishService.wsdl" );
my $translated_str = $babel_service->BabelFish( "en_fr",
$articles_str );
@translatedlist = split( ":::::", $translated_str );
return \@translatedlist;
}
|