酒日記

Wed, 29 May 2002

モルツ (5% × 1000 ml)

暇なのでソープのお勉強。と、カタカナで書くとなんですが、SOAPね。いかがわしいのじゃなくて。

Perl の SOAP::Lite (mod_perl + Apache::SOAP) で実験してみる。 何の機能を提供しようか、と考えてみるが、大したサービスができるわけでなし。 酒日記のトップページ の最終更新日を返す、というクソの役にも立たないものを作ってみる。まあとりあえず実験なので。

...

サーバ側の .htaccess を、

<FilesMatch "\.soap">
  SetHandler perl-script
  PerlHandler Apache::SOAP
  PerlSetVar dispatch_to "/home/httpd/html/soap/lib, SakeSOAP"
  PerlSetVar options "compress_threshold => 10000"
</FilesMatch>

とする。これで、クライアントから

end point URL : http://sake-nikki.dyndns.org/soap/index.soap
name space URI : urn:SakeSOAP

で接続すると、SakeSOAP.pm が叩ける(はず)。

SakeSOAP.pm は以下のような感じで。何の変哲もない、普通の Perl のモジュール。

package SakeSOAP;

use strict;
use HTTP::Date qw(time2iso);
use vars qw/ $VERSION /;
use constant SAKENIKKI_TOP => '/home/httpd/html/sake/index.html';
use constant LAST_UPDATED_TYPE_UNIX => 'unix';
use constant LAST_UPDATED_TYPE_ISO  => 'iso';

$VERSION = '0.01';

sub new
{
    my $class = shift;
    my $self = bless {}, $class;
    $self;
}

sub getSakeNikkiLastUpdated
{
    my $self = shift;
    my $type = shift;
    
    my @stats = stat SAKENIKKI_TOP;
    if($stats[9]){
        if($type eq LAST_UPDATED_TYPE_UNIX){
            return $stats[9];
        }elsif($type eq LAST_UPDATED_TYPE_ISO){
            return time2iso($stats[9]);
        }else{
            return $stats[9];
        }
    }else{
        return undef;
    }
}
1;

クライアントは、SOAP::Lite を使って以下のように。

use SOAP::Lite;
my $soap = SOAP::Lite
            ->uri('urn:SakeSOAP')
            ->proxy('http://sake-nikki.dyndns.org/soap/index.soap')
            ->new;
print $soap->getSakeNikkiLastUpdated('iso')->result, "\n";

getSakeNikkiLastUpdated の引数に "iso" を渡すと ISO 形式の文字列、"unix" を渡すと、UNIX time が返ります。

で、実行。

$ perl soap.pl
2002-05-29 05:00:20

... 地味だ。地味すぎる。が、まあ、一応このやりとりの裏ではメッセージが XML で行ったり来たりしているわけですよ。

クライアントもサーバも SOAP::Lite で実装しているので、うまくいくのはある意味あたり前。 クライアントを Java など他の実装で作って、それで叩いてみる必要もあるなあ。
# Java も勉強しないと・・・


powered by blosxom