| 1 | package Catalyst::Controller::Atompub::Service; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use Atompub::MediaType qw(media_type); |
|---|
| 7 | use XML::Atom::Service; |
|---|
| 8 | |
|---|
| 9 | use base qw(Catalyst::Controller::Atompub::Base); |
|---|
| 10 | |
|---|
| 11 | __PACKAGE__->mk_accessors(qw(service)); |
|---|
| 12 | |
|---|
| 13 | sub default :Private { |
|---|
| 14 | my($self, $c) = @_; |
|---|
| 15 | |
|---|
| 16 | $self->{service} ||= $self->_make_service($c); |
|---|
| 17 | $self->{service} = $self->modify_service($c, $self->service) |
|---|
| 18 | or return $self->error($c); |
|---|
| 19 | |
|---|
| 20 | $c->res->content_type(media_type('service')) unless $c->res->content_type; |
|---|
| 21 | |
|---|
| 22 | $c->res->body($self->service->as_xml) unless length $c->res->body; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | sub modify_service { |
|---|
| 26 | my($self, $c, $serv) = @_; |
|---|
| 27 | return $serv; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | sub _make_service { |
|---|
| 31 | my($self, $c) = @_; |
|---|
| 32 | |
|---|
| 33 | my $serv = XML::Atom::Service->new; |
|---|
| 34 | |
|---|
| 35 | my $suffix = Catalyst::Utils::class2classsuffix(ref $self); |
|---|
| 36 | |
|---|
| 37 | my @configs = @{ $c->config->{$suffix}{workspace} || [] }; |
|---|
| 38 | unless (@configs) { |
|---|
| 39 | my @colls = grep { $self->info->get($c, $_) } keys %{ $c->components }; |
|---|
| 40 | @configs = ({ |
|---|
| 41 | title => Catalyst::Utils::class2appclass($self), |
|---|
| 42 | collection => \@colls, |
|---|
| 43 | }); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | for my $config (@configs) { |
|---|
| 47 | my $work = XML::Atom::Workspace->new; |
|---|
| 48 | $work->title($config->{title}); |
|---|
| 49 | $work->add_collection($_) for grep { defined $_ } |
|---|
| 50 | map { $self->info->get($c, $_) } |
|---|
| 51 | @{ $config->{collection} || [] }; |
|---|
| 52 | $serv->add_workspace($work); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | $serv; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | 1; |
|---|
| 59 | __END__ |
|---|
| 60 | |
|---|
| 61 | =head1 NAME |
|---|
| 62 | |
|---|
| 63 | Catalyst::Controller::Atompub::Service |
|---|
| 64 | - A Catalyst controller for the Atom Service Document |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | =head1 SYNOPSIS |
|---|
| 68 | |
|---|
| 69 | # Use the Catalyst helper |
|---|
| 70 | $ perl script/myatom_create.pl controller MyService Atompub::Service |
|---|
| 71 | |
|---|
| 72 | # And edit lib/MyAtom/Controller/MyService.pm |
|---|
| 73 | package MyAtom::Controller::MyService; |
|---|
| 74 | use base 'Catalyst::Controller::Atompub::Service'; |
|---|
| 75 | |
|---|
| 76 | # Access to http://localhost:3000/myservice and get Service Document |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | =head1 DESCRIPTION |
|---|
| 80 | |
|---|
| 81 | L<Catalyst::Controller::Atompub::Service> generates a Service Document |
|---|
| 82 | based on collection configuration. |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | =head1 SUBCLASSING |
|---|
| 86 | |
|---|
| 87 | Just a single subclass is required in your Atompub server implementation. |
|---|
| 88 | No methods are needed to be overridden. |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | =head1 METHODS |
|---|
| 92 | |
|---|
| 93 | The following methods can be overridden to change the default behaviors. |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | =head2 $controller->modify_service |
|---|
| 97 | |
|---|
| 98 | By overriding C<modify_service>, modifies the default Service Documents: |
|---|
| 99 | |
|---|
| 100 | sub modify_service { |
|---|
| 101 | my($self, $c, $service) = @_; |
|---|
| 102 | |
|---|
| 103 | # Edit $service (XML::Atom::Service) if you'd like to modify the |
|---|
| 104 | # Service Document |
|---|
| 105 | |
|---|
| 106 | return $service; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | =head1 ACCESSORS |
|---|
| 111 | |
|---|
| 112 | =head2 $controller->service |
|---|
| 113 | |
|---|
| 114 | An accessor for a Service Document. |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | =head1 INTERNAL INTERFACES |
|---|
| 118 | |
|---|
| 119 | =head2 $controller->default |
|---|
| 120 | |
|---|
| 121 | =head2 $controller->_make_service |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | =head1 CONFIGURATION |
|---|
| 125 | |
|---|
| 126 | By default (no configuration), this module provides a Service Document |
|---|
| 127 | with a single I<atom:workspace>. |
|---|
| 128 | The order of I<atom:collection>s is not defined. |
|---|
| 129 | |
|---|
| 130 | You can specify the title and the order of I<atom:workspace>s and I<atom:collection>s like: |
|---|
| 131 | |
|---|
| 132 | Controller::Service: |
|---|
| 133 | workspace: |
|---|
| 134 | - title: My Blog |
|---|
| 135 | collection: |
|---|
| 136 | - Controller::EntryCollection |
|---|
| 137 | - Controller::MediaCollection |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | =head1 ERROR HANDLING |
|---|
| 141 | |
|---|
| 142 | See ERROR HANDLING in L<Catalyst::Controller::Atompub::Base>. |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | =head1 SAMPLES |
|---|
| 146 | |
|---|
| 147 | See SAMPLES in L<Catalyst::Controller::Atompub>. |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | =head1 SEE ALSO |
|---|
| 151 | |
|---|
| 152 | L<XML::Atom> |
|---|
| 153 | L<XML::Atom::Service> |
|---|
| 154 | L<Atompub> |
|---|
| 155 | L<Catalyst::Controller::Atompub> |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | =head1 AUTHOR |
|---|
| 159 | |
|---|
| 160 | Takeru INOUE C<< <takeru.inoue _ gmail.com> >> |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | =head1 LICENCE AND COPYRIGHT |
|---|
| 164 | |
|---|
| 165 | Copyright (c) 2007, Takeru INOUE C<< <takeru.inoue _ gmail.com> >>. All rights reserved. |
|---|
| 166 | |
|---|
| 167 | This module is free software; you can redistribute it and/or |
|---|
| 168 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | =head1 DISCLAIMER OF WARRANTY |
|---|
| 172 | |
|---|
| 173 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
|---|
| 174 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
|---|
| 175 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
|---|
| 176 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
|---|
| 177 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 178 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
|---|
| 179 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
|---|
| 180 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
|---|
| 181 | NECESSARY SERVICING, REPAIR, OR CORRECTION. |
|---|
| 182 | |
|---|
| 183 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
|---|
| 184 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
|---|
| 185 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
|---|
| 186 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
|---|
| 187 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
|---|
| 188 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
|---|
| 189 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
|---|
| 190 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
|---|
| 191 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
|---|
| 192 | SUCH DAMAGES. |
|---|