use LWP::UserAgent;
use HTTP::Request::Common;
my $request = << REQ;
# message here
REQ
# client certificate support
$ENV{HTTPS_CERT_FILE} = 'test.crt';
$ENV{HTTPS_KEY_FILE} = 'test.key';
# CA cert peer verification
$ENV{HTTPS_CA_FILE} = 'ca.crt';
my $ua = new LWP::UserAgent;
my $res = $ua->request(POST 'https://test.com',
SOAPAction => 'http://test.com/operationA',
Content_Type => 'application/xml',
Content => $request);
print $res->code."\n";
print $res->content."\n";
print "Work is done. \n\n";
Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts
2007-04-11
ssl mutual authentication in perl
Here is a perl script to invoke web service using SSL mutual authentication.
2006-01-26
Make Perl speak JMS (3)
Finally I will implement a Perl web service client. The tool I pick is SOAP::Lite (v0.55).
SOAP::Lite comes with a handy tool similar to wsdl2java called stubmaker. Run our WSDL through the tool and it generates a Perl module MessageRoutingService.pm. This module wrapps the call to the single RouteMessage API defined in the WSDL. Following code shows how to use this module:
Sounds easy? There is a small catch here. The generated stub module needs a little tweak. Somehow it misses the URI namespace for the top method wrapper element .
Without the URI, the method in the request looks like
which is obviously wrong. After I manually add the URI in the stub to make it look like
Now the request becomes
and it is happily accpted by the Java Axis server.
SOAP::Lite comes with a handy tool similar to wsdl2java called stubmaker. Run our WSDL through the tool and it generates a Perl module MessageRoutingService.pm. This module wrapps the call to the single RouteMessage API defined in the WSDL. Following code shows how to use this module:
my @dest = (
SOAP::Data->name(protocol=>'jms'),
SOAP::Data->name(category=>'queue'),
SOAP::Data->name(server=>'my_jms_server'),
SOAP::Data->name(port=>'7222'),
SOAP::Data->name(target=>'my_queue'),
SOAP::Data->name(user=>'user_a'),
SOAP::Data->name(password=>'password_a') );
my @content = (SOAP::Data->name(content=>'Hello World'));
my @route_request = (SOAP::Data->name(destination=>\SOAP::Data->value(@dest)),
SOAP::Data->name(request=>\SOAP::Data->value(@content)));
my $result = MessageRoutingService->RouteMessage(@route_request);
print $result->{content};
Sounds easy? There is a small catch here. The generated stub module needs a little tweak. Somehow it misses the URI namespace for the top method wrapper element .
my %methods = (
RouteMessage => {
endpoint => 'http://localhost:8080/common_router',
soapaction => 'http://www.simple.code/router/action/RouteMessage',
uri => '',
parameters => [
SOAP::Data->new(name => 'document', type => '', attr => {}),
],
},
);
Without the URI, the method in the request looks like
<RouteMessage xmlns="">
which is obviously wrong. After I manually add the URI in the stub to make it look like
my %methods = (
RouteMessage => {
endpoint => 'http://localhost:8080/common_router',
soapaction => 'http://www.simple.code/router/action/RouteMessage',
uri => 'http://www.simple.code/router/action/RouteMessage',
parameters => [
SOAP::Data->new(name => 'document', type => '', attr => {}),
],
},
);
Now the request becomes
<RouteMessage xmlns="http://www.simple.code/router/action/RouteMessage">
and it is happily accpted by the Java Axis server.
2006-01-25
PLUM is ripe
My little tool PerL based Utility for Messaging is almost done. It needs to be improved in several places, but it is actually usable now. It has following functions:
* send single request to server using different protocols, currently supports HTTP(Post) and JMS(Topic and Queue)
* compare response message with expected response
* Perl RE can be used for matching XML type response
* send a batch of requests and compare responses with expected responses
The utility is a web based application. I am thinking about adding a comand line tool which is more suitable for batch processing.
* send single request to server using different protocols, currently supports HTTP(Post) and JMS(Topic and Queue)
* compare response message with expected response
* Perl RE can be used for matching XML type response
* send a batch of requests and compare responses with expected responses
The utility is a web based application. I am thinking about adding a comand line tool which is more suitable for batch processing.
Added login for the Catalyst application
I tried to add login for my testing utility which is a Catalyst based web application last night. I finished the code by carefully following the examples provided with document. Not too bad, just a couple of lines and I was done. But the application complained:
It seems that the later import (authentication related) overrode the previous import(session related). Thus the session method is missing because the module Session is not imported.
To fix this, just change the code to:
Can't locate object method "session" via package "myapp" I went back and reviewed the code several times and could not figure out what was wrong. After hours of struggle, changing configurations, switching to different plugin modules, I finally got the reason. In my code, I used following statement to import several Catalyst plugins:use Catalyst qw/Static::Simple Session Session::State::Cookie Session::Store::File/;
use Catalyst qw/Authentication Authentication::Store::Minimal Authentication::Credential::Password/;
It seems that the later import (authentication related) overrode the previous import(session related). Thus the session method is missing because the module Session is not imported.
To fix this, just change the code to:
use Catalyst qw/-Debug Static::Simple Session
Session::State::Cookie Session::Store::File
Authentication Authentication::Store::Minimal
Authentication::Credential::Password
/;
2006-01-23
SemanticXmlDiff failed on Unicode
I am using CPAN module SemanticXmlDiff to compare two set of XML files. It complains about not being able to handle "wide character". After looking at the source code, it turns out the problem is the MD5 function used inside the module. The MD5 hash is used to spead up the comparson of text node in the XML file. However, MD5 only supports computing hash of ASCII characters, thus it will fail on wide characters which has multiple bytes.
The solution? According to the Digest::Digest module, the text should be encoded before it is passed to MD5 function. To apply this, I change the following line from
to
Now it works!
The solution? According to the Digest::Digest module, the text should be encoded before it is passed to MD5 function. To apply this, I change the following line from
$doc->{"$test_context"}->{TextChecksum} = md5_base64("$text");
to
$doc->{"$test_context"}->{TextChecksum} = md5_base64(encode_utf8("$text"));
Now it works!
2006-01-19
Configure Catalyst 5.61 with Apache2 on Windows
I reconfigured my Catalyst app using mod_perl today. It was configured using CGI before as I was making a lot of changes and would like to page to always reflect the last change. The original configuration is:
It works but the performance is horrorable. The new configuration is:
<VirtualHost *:80>
ServerAdmin my@server.com
DocumentRoot c:/site/myapp/root
ServerName myserver2
ErrorLog c:/site/myapp/myapp-error.log
CustomLog c:/site/myapp/myapp-access.log common
ScriptAliasMatch ^/(.+) c:/site/myapp/script/myapp_cgi.pl/$1
Alias /static c:/site/myapp/root/static
<Location "/static">
SetHandler none
</Location>
</VirtualHost>
It works but the performance is horrorable. The new configuration is:
<VirtualHost *:80>
ServerAdmin my@server.com
DocumentRoot c:/site/myapp/root
ServerName myserver2
ErrorLog c:/site/myapp/myapp-error.log
CustomLog c:/site/myapp/myapp-access.log common
PerlOptions +Parent
PerlSwitches -IC:/site/myapp/lib
<Location />
SetHandler modperl
PerlHandler myapp
</Location>
<LocationMatch "/static">
SetHandler none
</LocationMatch>
</VirtualHost>
2006-01-18
Use DBD: :Oracle 1.16 to access Oracle 9.2 database
I try to connect to an Oracle 9.2 database using following code:
It failed with error:
I have to explicitly set the ENV variable NLS_LANG to make it work. The new code looks like:
and it works.
The initial thought is that the default NLS_LANG from DBD::Oracle has to match the server configuration which is "AMERICAN_AMERICA.UTF8". But the follwoing code works as well:
So maybe the DBD:Oracle is sending some invalid NLS_LANG string though I havn't figured out what it is.
my $dbh = DBI->connect($config->{dburl}, $config->{dbuser}, $config->{dbpass});
It failed with error:
DBI connect('host=host;sid=dbsid','user',...) failed: ORA-12705: invalid or unknown NLS parameter value specified (DBD ERROR: OCISessionBegin) at ..
I have to explicitly set the ENV variable NLS_LANG to make it work. The new code looks like:
$ENV{NLS_LANG}="AMERICAN_AMERICA.UTF8";
my $dbh = DBI->connect($config->{dburl}, $config->{dbuser}, $config->{dbpass});
and it works.
The initial thought is that the default NLS_LANG from DBD::Oracle has to match the server configuration which is "AMERICAN_AMERICA.UTF8". But the follwoing code works as well:
$ENV{NLS_LANG}="AMERICAN_AMERICA.US7ASCII";
my $dbh = DBI->connect($config->{dburl}, $config->{dbuser}, $config->{dbpass});
So maybe the DBD:Oracle is sending some invalid NLS_LANG string though I havn't figured out what it is.
2006-01-14
Perl object is just a blessed hash
To work on a small testing utility, I have to pick up perl again. Fortunately my memory still served me well until I got into this problem. How to trun a hash into object. After a little reading of my Learning Perl book. I figured out how it works. Once you understand perl object is just a blessed hash, then the solution is clear.
============================
============================
package MyTest;
use strict;
my %hash = (color=>"red", shape=>"round");
my $hashref = \%hash;
sub not_typical_new {
my ($proto, $arg) = @_;
my $class = ref($proto) || $proto;
my $self = $arg;
bless ($self, $class);
return $self;
}
my $obj = mytest->not_typical_new($hashref);
print $obj->{color}, "\n";
Subscribe to:
Posts (Atom)