1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
6 %# <jesse@bestpractical.com>
8 %# (Except where explicitly superseded by other copyright notices)
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 %# General Public License for more details.
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 %# CONTRIBUTION SUBMISSION POLICY:
30 %# (The following paragraph is not intended to limit the rights granted
31 %# to you to modify and distribute this software under the terms of
32 %# the GNU General Public License and is only of importance to you if
33 %# you choose to contribute your changes and enhancements to the
34 %# community by submitting them to Best Practical Solutions, LLC.)
36 %# By intentionally submitting any modifications, corrections or
37 %# derivatives to this work, or any other work intended for use with
38 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 %# you are the copyright holder for those contributions and you grant
40 %# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
41 %# royalty-free, perpetual, license to use, copy, create derivative
42 %# works based on those contributions, and sublicense and distribute
43 %# those contributions and any derivatives thereof.
45 %# END BPS TAGGED BLOCK }}}
55 use RT::Interface::REST;
58 my $status = "200 Ok";
59 my $object = $m->dhandler_arg;
61 my $name = qr{[\w.-]+};
62 my $list = '(?:(?:\d+-)?\d+,)*(?:\d+-)?\d+';
63 my $label = '[a-zA-Z0-9@_.+-]+';
64 my $field = '[a-zA-Z][a-zA-Z0-9_-]*';
65 my $labels = "(?:$label,)*$label";
67 # We must handle requests such as the following:
69 # 1. http://.../REST/1.0/show (with a list of object specifications).
70 # 2. http://.../REST/1.0/edit (with a self-contained list of forms).
71 # 3. http://.../REST/1.0/ticket/show (implicit type specification).
72 # http://.../REST/1.0/ticket/edit
73 # 4. http://.../REST/1.0/ticket/nn (all possibly with a single form).
74 # http://.../REST/1.0/ticket/nn/history
75 # http://.../REST/1.0/ticket/nn/attachment/1
77 # Objects are specified by their type, and either a unique numeric ID,
78 # or a unique name (e.g. ticket/1, queue/foo). Multiple objects of the
79 # same type may be specified by a comma-separated list of identifiers
80 # (e.g., user/ams,rai or ticket/1-3,5-7).
82 # Ultimately, we want a list of object specifications to operate upon.
83 # The URLs in (4) provide enough information to identify an object. We
84 # will assemble submitted information into that format in other cases.
86 my (@objects, $forms);
89 if ($object eq 'show' || # $REST/show
90 (($utype) = ($object =~ m{^($name)/show$}))) # $REST/ticket/show
92 # We'll convert type/range specifications ("ticket/1-3,7-9/history")
93 # into a list of singular object specifications ("ticket/1/history").
94 # If the URL specifies a type, we'll accept only that one.
95 foreach my $id (@id) {
96 $id =~ s|^(?:$utype/)?|$utype/| if $utype;
97 if (my ($type, $oids, $extra) =
98 ($id =~ m#^($name)/($list|$labels)(?:(/.*))?$#o))
100 foreach my $oid (expand_list($oids)) {
101 if ($extra =~ m{^(?:/($name)(?:/(.*))?)?$}o) {
102 my ($attr, $args) = ($1, $2);
103 # expand transaction and attachment range specifications
106 if ($attr eq 'history' && $args =~ m#id/(\d.*)#o) {
110 push(@objects, "$type/$oid/$attr/id/$_") for expand_list($tids);
112 push(@objects, "$type/$oid$extra");
118 $status = "400 Bad Request";
119 $output = "Invalid object ID specified: '$id'";
124 elsif ($object eq 'edit' || # $REST/edit
125 (($utype) = ($object =~ m{^($name)/edit$}))) # $REST/ticket/edit
127 # We'll make sure each of the submitted forms is syntactically valid
128 # and sufficiently identifies an object to operate upon, then add to
129 # the object list as above.
132 $forms = form_parse($content);
133 foreach my $form (@$forms) {
134 my ($c, $o, $k, $e) = @$form;
137 push @output, [ "# Syntax error.", $o, $k, $e ];
142 # Look for matching types in the ID, form, and URL.
143 $type = exists $k->{type} ? $k->{type} : $utype;
144 $type =~ s|^(?:$utype)?|$utype/| if $utype;
147 if (exists $k->{id}) {
149 $id =~ s|^(?:$type/)?|$type/| if $type;
151 if ($id =~ m#^$name/(?:$label|\d+)(?:/.*)?#o) {
155 push @output, [ "# Invalid object ID: '$id'", $o, $k, $e ];
159 push @output, [ "# No object ID specified.", $o, $k, $e ];
163 # If we saw any errors at this stage, we won't process any part of
164 # the submitted data.
166 unshift @output, [ "# Please resubmit with errors corrected." ];
167 $status = "409 Syntax Error";
168 $output = form_compose(\@output);
173 # We'll assume that this is in the correct format already. Otherwise
174 # it will be caught by the loop below.
175 push @objects, $object;
178 $forms = form_parse($content);
181 $status = "400 Bad Request";
182 $output = "You may submit only one form to this object.";
186 my ($c, $o, $k, $e) = @{ $forms->[0] };
188 $status = "409 Syntax Error";
189 $output = form_compose([ ["# Syntax error.", $o, $k, $e] ]);
195 # Make sure we have something to do.
197 $status = "400 Bad Request";
198 $output = "No objects specified.";
202 # Parse and validate any field specifications.
203 my (%fields, @fields);
205 unless ($fields =~ /^(?:$field,)*$field$/) {
206 $status = "400 Bad Request";
207 $output = "Invalid field specification: $fields";
210 @fields = map lc, split /,/, $fields;
211 @fields{@fields} = ();
212 unless (exists $fields{id}) {
213 unshift @fields, "id";
218 my (@comments, @output);
220 foreach $object (@objects) {
221 my ($handler, $type, $id, $attr, $args);
222 my ($c, $o, $k, $e) = ("", ["id"], {id => $object}, 0);
225 if ($object =~ m{^($name)/(\d+|$label)(?:/($name)(?:/(.*))?)?$}o ||
226 $object =~ m{^($name)/(new)$}o)
228 ($type, $id, $attr, $args) = ($1, $2, ($3 || 'default'), $4);
229 $handler = "Forms/$type/$attr";
231 unless ($m->comp_exists($handler)) {
232 $args = "$attr/$args";
233 $handler = "Forms/$type/default";
235 unless ($m->comp_exists($handler)) {
237 $c = "# Unknown object type: $type";
240 elsif ($id ne 'new' && $id !~ /^\d+$/) {
241 my $ns = "Forms/$type/ns";
243 # Can we resolve named objects?
244 unless ($m->comp_exists($ns)) {
246 $c = "# Objects of type $type must be specified by numeric id.";
249 my ($n, $s) = $m->comp("Forms/$type/ns", id => $id);
250 if ($n <= 0) { $i = 4; $c = "# $s"; }
251 else { $i = 0; $id = $n; }
260 $c = "# Invalid object specification: '$object'";
265 (undef, $o, $k, $e) = @{ shift @$forms };
267 push @output, [ $c, $o, $k ];
272 my $d = $m->comp($handler, id => $id, args => $args, format => $format, fields => \%fields);
273 my ($c, $o, $k, $e) = @$d;
275 if (!$e && @$o && keys %fields) {
276 my %lk = map { lc $_ => $_ } keys %$k;
277 @$o = map { $lk{$_} } @fields;
278 foreach my $key (keys %$k) {
279 delete $k->{$key} unless exists $fields{lc $key};
282 push(@output, [ $c, $o, $k ]) if ($c || @$o || keys %$k);
285 my ($c, $o, $k, $e) = @{ shift @$forms };
286 my $d = $m->comp($handler, id => $id, args => $args, format => $format,
288 ($c, $o, $k, $e) = @$d;
290 # We won't pass $e through to compose, trusting instead that the
291 # handler added suitable comments for the user.
293 $status = "409 Syntax Error" if @$o;
294 push @output, [ $c, $o, $k ];
302 unshift(@output, [ join "\n", @comments ]) if @comments;
303 $output = form_compose(\@output);
307 RT/<% $RT::VERSION %> <% $status %>