| 1 | package Catalyst::Controller::Atompub::Base; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use Atompub::DateTime qw( datetime ); |
|---|
| 7 | use Atompub::MediaType qw( media_type ); |
|---|
| 8 | use Catalyst::Controller::Atompub; |
|---|
| 9 | use HTTP::Status; |
|---|
| 10 | use NEXT; |
|---|
| 11 | |
|---|
| 12 | use base qw( Catalyst::Controller ); |
|---|
| 13 | |
|---|
| 14 | __PACKAGE__->mk_accessors( qw( info ) ); |
|---|
| 15 | |
|---|
| 16 | sub new { |
|---|
| 17 | my $class = shift; |
|---|
| 18 | my $self = $class->NEXT::new(@_); |
|---|
| 19 | $self->info( Catalyst::Controller::Atompub::Info->instance( $self ) ); |
|---|
| 20 | $self; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | sub error { |
|---|
| 24 | my ( $self, $c, @args ) = @_; |
|---|
| 25 | |
|---|
| 26 | return if ! is_success $c->res->status && $c->res->body; |
|---|
| 27 | |
|---|
| 28 | my ( $status, $message ) |
|---|
| 29 | = @args > 1 ? @args |
|---|
| 30 | : @args == 1 && $args[0] =~ /^([1-5]\d\d)\s*(.*)/ ? ( $1, $2 ) |
|---|
| 31 | : @args == 1 && $args[0] =~ /^(.*)/ ? ( $2 ) |
|---|
| 32 | : (); |
|---|
| 33 | |
|---|
| 34 | $status ||= RC_INTERNAL_SERVER_ERROR; |
|---|
| 35 | $c->res->status( $status ); |
|---|
| 36 | |
|---|
| 37 | $message ||= status_message( $status ); |
|---|
| 38 | my $report = "$status $message"; |
|---|
| 39 | |
|---|
| 40 | my $entry = XML::Atom::Entry->new; |
|---|
| 41 | |
|---|
| 42 | my $link = XML::Atom::Link->new; |
|---|
| 43 | $link->rel('related'); # XXX via? |
|---|
| 44 | $link->href( $c->req->uri ); |
|---|
| 45 | $entry->add_link( $link ); |
|---|
| 46 | |
|---|
| 47 | $entry->updated( datetime->w3c ); |
|---|
| 48 | $entry->title( $report ); |
|---|
| 49 | $entry->content( $report ); # XXX @type=text is better |
|---|
| 50 | |
|---|
| 51 | $c->res->body( $entry->as_xml ); |
|---|
| 52 | $c->res->content_type( media_type('entry') ); |
|---|
| 53 | |
|---|
| 54 | $c->log->error( $report ); |
|---|
| 55 | |
|---|
| 56 | return; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | package Catalyst::Controller::Atompub::Info; |
|---|
| 60 | |
|---|
| 61 | use strict; |
|---|
| 62 | use warnings; |
|---|
| 63 | |
|---|
| 64 | use Catalyst::Utils; |
|---|
| 65 | use XML::Atom::Service; |
|---|
| 66 | use base qw( Class::Accessor::Lvalue::Fast ); |
|---|
| 67 | |
|---|
| 68 | __PACKAGE__->mk_accessors( qw( appclass ) ); |
|---|
| 69 | |
|---|
| 70 | my $Info; |
|---|
| 71 | |
|---|
| 72 | sub instance { |
|---|
| 73 | my $class = shift; |
|---|
| 74 | $Info ||= bless { appclass => Catalyst::Utils::class2appclass( shift ) }, $class; |
|---|
| 75 | $Info; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | sub get { |
|---|
| 79 | my ( $self, $c, $class ) = @_; |
|---|
| 80 | return unless $class = $self->_fullclass( $c, $class ); |
|---|
| 81 | return unless $self->_is_collection( $c, $class ); |
|---|
| 82 | $self->{info}{$class} ||= $self->_make_collection( $c, $class ); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | sub _fullclass { |
|---|
| 86 | my ( $self, $c, $class ) = @_; |
|---|
| 87 | $class = ref $class || $class || return; |
|---|
| 88 | my $appclass = $self->appclass; |
|---|
| 89 | return $class =~ /^$appclass\::/ ? $class : join( '::', $appclass, $class ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | sub _is_collection { |
|---|
| 93 | my ( $self, $c, $class ) = @_; |
|---|
| 94 | return UNIVERSAL::isa $class, 'Catalyst::Controller::Atompub::Collection'; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | sub _make_collection { |
|---|
| 98 | my ( $self, $c, $class ) = @_; |
|---|
| 99 | |
|---|
| 100 | my $suffix = Catalyst::Utils::class2classsuffix( $class ); |
|---|
| 101 | |
|---|
| 102 | my $config = $c->config->{$suffix}{collection}; |
|---|
| 103 | |
|---|
| 104 | my $coll = XML::Atom::Collection->new; |
|---|
| 105 | $coll->href( $self->_make_href( $c, $class ) ); |
|---|
| 106 | $coll->title( $config->{title} || $class =~ /Controller::(.+)/ ); |
|---|
| 107 | $coll->accept( @{ $config->{accept} } ) if $config->{accept}; |
|---|
| 108 | |
|---|
| 109 | $coll->add_categories( $self->_make_categories( $c, $_ ) ) |
|---|
| 110 | for @{ $config->{categories} }; |
|---|
| 111 | |
|---|
| 112 | return $coll; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | sub _make_href { |
|---|
| 116 | my ( $self, $c, $class ) = @_; |
|---|
| 117 | return unless $class = $self->_fullclass( $c, $class ); |
|---|
| 118 | return $c->req->base . $class->action_namespace( $c ); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | sub _make_categories { |
|---|
| 122 | my ( $self, $c, $config ) = @_; |
|---|
| 123 | |
|---|
| 124 | my $cats = XML::Atom::Categories->new; |
|---|
| 125 | $cats->href( $config->{href} ) if $config->{href}; |
|---|
| 126 | $cats->fixed( $config->{fixed} ) if $config->{fixed}; |
|---|
| 127 | $cats->scheme( $config->{scheme} ) if $config->{scheme}; |
|---|
| 128 | |
|---|
| 129 | my @cat = map { my $cat = XML::Atom::Category->new; |
|---|
| 130 | $cat->term( $_->{term} ); |
|---|
| 131 | $cat->scheme( $_->{scheme} ) if $_->{scheme}; |
|---|
| 132 | $cat->label( $_->{label} ) if $_->{label}; |
|---|
| 133 | $cat } |
|---|
| 134 | @{ $config->{category} }; |
|---|
| 135 | $cats->category( @cat ); |
|---|
| 136 | |
|---|
| 137 | return $cats; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | 1; |
|---|
| 141 | __END__ |
|---|
| 142 | |
|---|
| 143 | =head1 NAME |
|---|
| 144 | |
|---|
| 145 | Catalyst::Controller::Atompub::Base |
|---|
| 146 | - A Catalyst controller for the Publishing Protocol |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | =head1 DESCRIPTION |
|---|
| 150 | |
|---|
| 151 | L<Catalyst::Controller::Atompub::Base> is a base class of |
|---|
| 152 | L<Catalyst::Controller::Atompub::Service> and |
|---|
| 153 | L<Catalyst::Controller::Atompub::Collection>. |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | =head1 METHODS |
|---|
| 157 | |
|---|
| 158 | =head2 $controller->new |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | =head2 $controller->info |
|---|
| 162 | |
|---|
| 163 | An accessor for Collection information object. |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | =head2 $controller->error( $c, [ $status, $message ] ) |
|---|
| 167 | |
|---|
| 168 | Sets an Entry Document containing error message in $c->response->body, |
|---|
| 169 | and returns C<undef>. |
|---|
| 170 | |
|---|
| 171 | See L<ERROR HANDLING>. |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | =head1 ERROR HANDLING |
|---|
| 175 | |
|---|
| 176 | When something wrong happens, return with calling $controller->error method like: |
|---|
| 177 | |
|---|
| 178 | sub foo { |
|---|
| 179 | my ( $controller ,$c ) = @_; |
|---|
| 180 | |
|---|
| 181 | return $controller->error( $c, 404, "Entry does not exist" ) |
|---|
| 182 | if is_something_wrong; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | Then, Atompub server responds with an Entry Document including error message: |
|---|
| 186 | |
|---|
| 187 | HTTP/1.1 404 Not Found |
|---|
| 188 | Content-Type: application/atom+xml;type=entry |
|---|
| 189 | |
|---|
| 190 | <?xml version="1.0" encoding="UTF-8"?> |
|---|
| 191 | <entry xmlns="http://www.w3.org/2005/Atom"> |
|---|
| 192 | <updated>2007-01-01T00:00:00Z</updated> |
|---|
| 193 | <link rel="related" |
|---|
| 194 | href="http://localhost:3000/mycollection/entry_1.atom"/> |
|---|
| 195 | <title>404 Entry does not exist</title> |
|---|
| 196 | <content type="xhtml"> |
|---|
| 197 | <div xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 198 | 404 Entry does not exist |
|---|
| 199 | </div> |
|---|
| 200 | </content> |
|---|
| 201 | </entry> |
|---|
| 202 | |
|---|
| 203 | This default behavior can be changed by overriding the C<error> method. |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | =head1 SEE ALSO |
|---|
| 207 | |
|---|
| 208 | L<XML::Atom> |
|---|
| 209 | L<XML::Atom::Service> |
|---|
| 210 | L<Atompub> |
|---|
| 211 | L<Catalyst::Controller::Atompub> |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | =head1 AUTHOR |
|---|
| 215 | |
|---|
| 216 | Takeru INOUE C<< <takeru.inoue _ gmail.com> >> |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | =head1 LICENCE AND COPYRIGHT |
|---|
| 220 | |
|---|
| 221 | Copyright (c) 2007, Takeru INOUE C<< <takeru.inoue _ gmail.com> >>. All rights reserved. |
|---|
| 222 | |
|---|
| 223 | This module is free software; you can redistribute it and/or |
|---|
| 224 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | =head1 DISCLAIMER OF WARRANTY |
|---|
| 228 | |
|---|
| 229 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
|---|
| 230 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
|---|
| 231 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
|---|
| 232 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
|---|
| 233 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 234 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
|---|
| 235 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
|---|
| 236 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
|---|
| 237 | NECESSARY SERVICING, REPAIR, OR CORRECTION. |
|---|
| 238 | |
|---|
| 239 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
|---|
| 240 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
|---|
| 241 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
|---|
| 242 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
|---|
| 243 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
|---|
| 244 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
|---|
| 245 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
|---|
| 246 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
|---|
| 247 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
|---|
| 248 | SUCH DAMAGES. |
|---|