This commit was generated by cvs2svn to compensate for changes in r5562,
[freeside.git] / rt / lib / RT / URI.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC 
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
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
16 # from www.gnu.org.
17
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.
22
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., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.html.
28
29
30 # CONTRIBUTION SUBMISSION POLICY:
31
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46
47 # END BPS TAGGED BLOCK }}}
48 package RT::URI;;
49
50 use strict;
51 use vars qw/@ISA/;
52 @ISA = qw(RT::Base);
53
54 use RT::URI::base;
55 use Carp;
56
57 =head1 NAME
58
59 RT::URI
60
61 =head1 DESCRIPTION
62
63 This class provides a base class for URIs, such as those handled
64 by RT::Link objects.  
65
66 =head1 API
67
68
69
70 =cut
71
72
73
74
75 =head2 new
76
77 Create a new RT::URI object.
78
79 =cut
80
81                          
82 sub new {
83     my $proto = shift;
84     my $class = ref($proto) || $proto;
85     my $self  = {};
86     bless( $self, $class );
87
88     $self->CurrentUser(@_);
89
90     return ($self);
91 }
92
93
94
95 # {{{ FromObject
96
97 =head2 FromObject <Object>
98
99 Given a local object, such as an RT::Ticket or an RT::FM::Article, this routine will return a URI for
100 the local object
101
102 =cut
103
104 sub FromObject {
105     my $self = shift;
106     my $obj = shift;
107
108     return undef unless  $obj->can('URI');
109     return $self->FromURI($obj->URI);
110 }
111
112 # }}}
113
114 # {{{ FromURI
115
116 =head2 FromURI <URI>
117
118 Returns a local object id for this content. You are expected to know
119 what sort of object this is the Id of
120
121 Returns true if everything is ok, otherwise false
122
123 =cut
124
125 sub FromURI {
126     my $self = shift;
127     my $uri = shift;    
128
129     return undef unless ($uri);
130
131     my $scheme;
132     # Special case: integers passed in as URIs must be ticket ids
133     if ($uri =~ /^(\d+)$/) {
134         $scheme = "fsck.com-rt";
135     } elsif ($uri =~ /^((?:\w|\.|-)+?):/) {
136         $scheme = $1;
137     }
138     else {
139         $RT::Logger->warning("$self Could not determine a URI scheme for $uri");
140                 return (undef);
141     }
142      
143     # load up a resolver object for this scheme  
144     $self->_GetResolver($scheme);
145     
146     unless ($self->Resolver->ParseURI($uri)) {
147         $RT::Logger->warning("Resolver ".ref($self->Resolver)." could not parse $uri");
148         $self->{resolver} = RT::URI::base->new( $self->CurrentUser ); # clear resolver
149         return (undef);
150     }
151
152 return(1);
153
154 }
155
156 # }}}
157
158 # {{{ _GetResolver
159
160 =private _GetResolver <scheme>
161
162 Gets an RT URI resolver for the scheme <scheme>. 
163 Falls back to a null resolver. RT::URI::base.
164
165 =cut
166
167 sub _GetResolver {
168     my $self = shift;
169     my $scheme = shift;
170
171     $scheme =~ s/(\.|-)/_/g;
172     my $resolver;
173
174     
175        eval " 
176             require RT::URI::$scheme;
177             \$resolver = RT::URI::$scheme->new(\$self->CurrentUser);
178        ";
179      
180         if ($resolver) {
181         $self->{'resolver'} = $resolver;
182         } else {
183         $self->{'resolver'} = RT::URI::base->new($self->CurrentUser); 
184         }
185
186 }
187
188 # }}}
189
190 # {{{ Scheme
191
192 =head2 Scheme
193
194 Returns a local object id for this content.  You are expected to know what sort of object this is the Id 
195 of 
196
197 =cut
198
199 sub Scheme {
200     my $self = shift;
201     return ($self->Resolver->Scheme);
202
203 }
204 # }}}
205 # {{{ URI
206
207 =head2 URI
208
209 Returns a local object id for this content.  You are expected to know what sort of object this is the Id 
210 of 
211
212 =cut
213
214 sub URI {
215     my $self = shift;
216     return ($self->Resolver->URI);
217
218 }
219 # }}}
220
221 # {{{ Object
222
223 =head2 Object
224
225 Returns a local object for this content. This will usually be an RT::Ticket or somesuch
226
227 =cut
228
229
230 sub Object {   
231     my $self = shift;
232     return($self->Resolver->Object);
233
234 }
235
236
237 # }}}
238
239 # {{{ IsLocal
240
241 =head2 IsLocal
242
243 Returns a local object for this content. This will usually be an RT::Ticket or somesuch
244
245 =cut
246
247 sub IsLocal {
248     my $self = shift;
249     return $self->Resolver->IsLocal;     
250 }
251
252
253 # }}}
254
255 =head2 AsHREF
256
257
258 =cut
259
260
261 sub AsHREF {
262     my $self = shift;
263     return $self->Resolver->HREF;
264
265 }
266 =head Resolver
267
268 Returns this URI's URI resolver object
269
270 =cut
271
272
273 sub Resolver {
274     my $self =shift;
275     return ($self->{'resolver'});
276 }
277
278 eval "require RT::URI_Vendor";
279 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI_Vendor.pm});
280 eval "require RT::URI_Local";
281 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI_Local.pm});
282
283 1;