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 \s-1API\s0 servers. It handles all core server processing, both the \s-1SOAP\s0 and \s-1REST\s0 formats of the protocol, and \s-1WSSE\s0 authentication. It can also run as either a mod_perl handler or as part of a \s-1CGI\s0 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 \s-1HTTP\s0 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 \s-1WSSE\s0 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 \s-1WSSE\s0 header. If the supplied username doesn't exist in your user database or alike, just return \*(C`undef\*(C'.

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

  • $server->uri Returns the \s-1URI\s0 of the Atom server implementation.

  • $server->request_method Returns the name of the request method sent to the server from the client (for example, \*(C`GET\*(C', \*(C`POST\*(C', etc). Note that if the client sent the request in a \s-1SOAP\s0 envelope, the method is obtained from the SOAPAction \s-1HTTP\s0 header.

  • $server->request_header($header) Retrieves the value of the \s-1HTTP\s0 request header $header.

  • $server->request_content Returns a scalar containing the contents of a \s-1POST\s0 or \s-1PUT\s0 request from the client.

  • $server->request_param($param) XML::Atom::Server automatically parses the \s-1PATH_INFO\s0 sent in the request and breaks it up into key-value pairs. This can be used to pass parameters. For example, in the \s-1URI\s0 http://localhost/atom-server/entry_id=1 the entry_id parameter would be set to 1. request_param returns the value of the value of the parameter $param.

Setting up the Response

  • $server->response_header($header, $value) Sets the value of the \s-1HTTP\s0 response header $header to $value.

  • $server->response_code([ $code ]) Returns the current response code to be sent back to the client, and if $code is given, sets the response code.

  • $server->response_content_type([ $type ]) Returns the current Content-Type header to be sent back to the client, and $type is given, sets the value for that header.

Processing the Request

  • $server->authenticate Attempts to authenticate the request based on the authentication information present in the request (currently just \s-1WSSE\s0). This will call the password_for_user method in the subclass to obtain the cleartext password for the username given in the request.

  • $server->atom_body Returns an XML::Atom::Entry object containing the entry sent in the request.

USAGE

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

A simple \s-1CGI\s0 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 \*(C`Something went wrong\*(C'. Errors are automatically serialized into \s-1SOAP\s0 faults if the incoming request is enclosed in a \s-1SOAP\s0 envelope.

AUTHOR & COPYRIGHT

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