Tatsuhiko Miyagawa > XML-Atom-0.41 > XML::Atom::Server

Download:
XML-Atom-0.41.tar.gz

Dependencies

Annotate this POD (1)

CPAN RT

New  21
Open  8
View/Report Bugs
Source  

NAME ^

XML::Atom::Server - A server for the Atom API

SYNOPSIS ^

    package My::Server;
    use base qw( XML::Atom::Server );
    sub handle_request {
        my $server = shift;
        $server->authenticate or return;
        my $method = $server->request_method;
        if ($method eq 'POST') {
            return $server->new_post;
        }
        ...
    }

    my %Passwords;
    sub password_for_user {
        my $server = shift;
        my($username) = @_;
        $Passwords{$username};
    }

    sub new_post {
        my $server = shift;
        my $entry = $server->atom_body or return;
        ## $entry is an XML::Atom::Entry object.
        ## ... Save the new entry ...
    }

    package main;
    my $server = My::Server->new;
    $server->run;

DESCRIPTION ^

XML::Atom::Server provides a base class for Atom API servers. It handles all core server processing, both the SOAP and REST formats of the protocol, and WSSE authentication. It can also run as either a mod_perl handler or as part of a CGI program.

It does not provide functions specific to any particular implementation, such as posting an entry, retrieving a list of entries, deleting an entry, etc. Implementations should subclass XML::Atom::Server, overriding the handle_request method, and handle all functions such as this themselves.

SUBCLASSING ^

Request Handling

Subclasses of XML::Atom::Server must override the handle_request method to perform all request processing. The implementation must set all response headers, including the response code and any relevant HTTP headers, and should return a scalar representing the response body to be sent back to the client.

For example:

    sub handle_request {
        my $server = shift;
        my $method = $server->request_method;
        if ($method eq 'POST') {
            return $server->new_post;
        }
        ## ... handle GET, PUT, etc
    }
    
    sub new_post {
        my $server = shift;
        my $entry = $server->atom_body or return;
        my $id = save_this_entry($entry);  ## Implementation-specific
        $server->response_header(Location => $server->uri . '/entry_id=' . $id);
        $server->response_code(201);
        $server->response_content_type('application/x.atom+xml');
        return serialize_entry($entry);    ## Implementation-specific
    }

Authentication

Servers that require authentication for posting or retrieving entries or feeds should override the password_for_user method. Given a username (from the WSSE header), password_for_user should return that user's password in plaintext. This will then be combined with the nonce and the creation time to generate the digest, which will be compared with the digest sent in the WSSE header. If the supplied username doesn't exist in your user database or alike, just return undef.

For example:

    my %Passwords = ( foo => 'bar' );   ## The password for "foo" is "bar".
    sub password_for_user {
        my $server = shift;
        my($username) = @_;
        $Passwords{$username};
    }

METHODS ^

XML::Atom::Server provides a variety of methods to be used by subclasses for retrieving headers, content, and other request information, and for setting the same on the response.

Client Request Parameters

Setting up the Response

Processing the Request

USAGE ^

Once you have defined your server subclass, you can set it up either as a CGI program or as a mod_perl handler.

A simple CGI program would look something like this:

    #!/usr/bin/perl -w
    use strict;

    use My::Server;
    my $server = My::Server->new;
    $server->run;

A simple mod_perl handler configuration would look something like this:

    PerlModule My::Server
    <Location /atom-server>
        SetHandler perl-script
        PerlHandler My::Server
    </Location>

ERROR HANDLING ^

If you wish to return an error from handle_request, you can use the built-in error method:

    sub handle_request {
        my $server = shift;
        ...
        return $server->error(500, "Something went wrong");
    }

This will be returned to the client with a response code of 500 and an error string of Something went wrong. Errors are automatically serialized into SOAP faults if the incoming request is enclosed in a SOAP envelope.

AUTHOR & COPYRIGHT ^

Please see the XML::Atom manpage for author, copyright, and license information.

syntax highlighting: