root/lang/perl/Catalyst-Controller-Atompub/trunk/lib/Catalyst/Controller/Atompub/Service.pm @ 8062

Revision 8062, 5.0 kB (checked in by takemaru, 6 years ago)

lang/perl/Catalyst-Controller-Atompub: 0.4.0 rc1. major refactoring etc., see Changes in details

Line 
1package Catalyst::Controller::Atompub::Service;
2
3use strict;
4use warnings;
5
6use Atompub::MediaType qw(media_type);
7use XML::Atom::Service;
8
9use base qw(Catalyst::Controller::Atompub::Base);
10
11__PACKAGE__->mk_accessors(qw(service));
12
13sub 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
25sub modify_service {
26    my($self, $c, $serv) = @_;
27    return $serv;
28}
29
30sub _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
581;
59__END__
60
61=head1 NAME
62
63Catalyst::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
81L<Catalyst::Controller::Atompub::Service> generates a Service Document
82based on collection configuration.
83
84
85=head1 SUBCLASSING
86
87Just a single subclass is required in your Atompub server implementation.
88No methods are needed to be overridden.
89
90
91=head1 METHODS
92
93The following methods can be overridden to change the default behaviors.
94
95
96=head2 $controller->modify_service
97
98By 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
114An 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
126By default (no configuration), this module provides a Service Document
127with a single I<atom:workspace>.
128The order of I<atom:collection>s is not defined.
129
130You can specify the title and the order of I<atom:workspace>s and I<atom:collection>s like:
131
132Controller::Service:
133    workspace:
134      - title: My Blog
135        collection:
136          - Controller::EntryCollection
137          - Controller::MediaCollection
138
139
140=head1 ERROR HANDLING
141
142See ERROR HANDLING in L<Catalyst::Controller::Atompub::Base>.
143
144
145=head1 SAMPLES
146
147See SAMPLES in L<Catalyst::Controller::Atompub>.
148
149
150=head1 SEE ALSO
151
152L<XML::Atom>
153L<XML::Atom::Service>
154L<Atompub>
155L<Catalyst::Controller::Atompub>
156
157
158=head1 AUTHOR
159
160Takeru INOUE  C<< <takeru.inoue _ gmail.com> >>
161
162
163=head1 LICENCE AND COPYRIGHT
164
165Copyright (c) 2007, Takeru INOUE C<< <takeru.inoue _ gmail.com> >>. All rights reserved.
166
167This module is free software; you can redistribute it and/or
168modify it under the same terms as Perl itself. See L<perlartistic>.
169
170
171=head1 DISCLAIMER OF WARRANTY
172
173BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
174FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
175OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
176PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
177EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
178WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
179ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
180YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
181NECESSARY SERVICING, REPAIR, OR CORRECTION.
182
183IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
184WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
185REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
186LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
187OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
188THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
189RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
190FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
191SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
192SUCH DAMAGES.
Note: See TracBrowser for help on using the browser.