import rt 3.4.6
[freeside.git] / rt / lib / RT / URI.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2005 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., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27
28 # CONTRIBUTION SUBMISSION POLICY:
29
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.)
35
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.
44
45 # END BPS TAGGED BLOCK }}}
46 package RT::URI;;
47
48 use strict;
49 use vars qw/@ISA/;
50 @ISA = qw(RT::Base);
51
52 use RT::URI::base;
53 use Carp;
54
55 =head1 NAME
56
57 RT::URI
58
59 =head1 DESCRIPTION
60
61 This class provides a base class for URIs, such as those handled
62 by RT::Link objects.  
63
64 =head1 API
65
66
67
68 =cut
69
70
71
72
73 =head2 new
74
75 Create a new RT::URI object.
76
77 =cut
78
79                          
80 sub new {
81     my $proto = shift;
82     my $class = ref($proto) || $proto;
83     my $self  = {};
84     bless( $self, $class );
85
86     $self->CurrentUser(@_);
87
88     return ($self);
89 }
90
91
92
93 # {{{ FromObject
94
95 =head2 FromObject <Object>
96
97 Given a local object, such as an RT::Ticket or an RT::FM::Article, this routine will return a URI for
98 the local object
99
100 =cut
101
102 sub FromObject {
103     my $self = shift;
104     my $obj = shift;
105
106     return undef unless  $obj->can('URI');
107     return $self->FromURI($obj->URI);
108 }
109
110 # }}}
111
112 # {{{ FromURI
113
114 =head2 FromURI <URI>
115
116 Returns a local object id for this content. You are expected to know
117 what sort of object this is the Id of
118
119 Returns true if everything is ok, otherwise false
120
121 =cut
122
123 sub FromURI {
124     my $self = shift;
125     my $uri = shift;    
126
127     return undef unless ($uri);
128
129     my $scheme;
130     # Special case: integers passed in as URIs must be ticket ids
131     if ($uri =~ /^(\d+)$/) {
132         $scheme = "fsck.com-rt";
133     } elsif ($uri =~ /^((?:\w|\.|-)+?):/) {
134         $scheme = $1;
135     }
136     else {
137         $RT::Logger->warning("$self Could not determine a URI scheme for $uri");
138                 return (undef);
139     }
140      
141     # load up a resolver object for this scheme  
142     $self->_GetResolver($scheme);
143     
144     unless ($self->Resolver->ParseURI($uri)) {
145         $RT::Logger->warning("Resolver ".ref($self->Resolver)." could not parse $uri");
146         $self->{resolver} = RT::URI::base->new( $self->CurrentUser ); # clear resolver
147         return (undef);
148     }
149
150 return(1);
151
152 }
153
154 # }}}
155
156 # {{{ _GetResolver
157
158 =private _GetResolver <scheme>
159
160 Gets an RT URI resolver for the scheme <scheme>. 
161 Falls back to a null resolver. RT::URI::base.
162
163 =cut
164
165 sub _GetResolver {
166     my $self = shift;
167     my $scheme = shift;
168
169     $scheme =~ s/(\.|-)/_/g;
170     my $resolver;
171
172     
173        eval " 
174             require RT::URI::$scheme;
175             \$resolver = RT::URI::$scheme->new(\$self->CurrentUser);
176        ";
177      
178         if ($resolver) {
179         $self->{'resolver'} = $resolver;
180         } else {
181         $self->{'resolver'} = RT::URI::base->new($self->CurrentUser); 
182         }
183
184 }
185
186 # }}}
187
188 # {{{ Scheme
189
190 =head2 Scheme
191
192 Returns a local object id for this content.  You are expected to know what sort of object this is the Id 
193 of 
194
195 =cut
196
197 sub Scheme {
198     my $self = shift;
199     return ($self->Resolver->Scheme);
200
201 }
202 # }}}
203 # {{{ URI
204
205 =head2 URI
206
207 Returns a local object id for this content.  You are expected to know what sort of object this is the Id 
208 of 
209
210 =cut
211
212 sub URI {
213     my $self = shift;
214     return ($self->Resolver->URI);
215
216 }
217 # }}}
218
219 # {{{ Object
220
221 =head2 Object
222
223 Returns a local object for this content. This will usually be an RT::Ticket or somesuch
224
225 =cut
226
227
228 sub Object {   
229     my $self = shift;
230     return($self->Resolver->Object);
231
232 }
233
234
235 # }}}
236
237 # {{{ IsLocal
238
239 =head2 IsLocal
240
241 Returns a local object for this content. This will usually be an RT::Ticket or somesuch
242
243 =cut
244
245 sub IsLocal {
246     my $self = shift;
247     return $self->Resolver->IsLocal;     
248 }
249
250
251 # }}}
252
253
254 =head Resolver
255
256 Returns this URI's URI resolver object
257
258 =cut
259
260
261 sub Resolver {
262     my $self =shift;
263     return ($self->{'resolver'});
264 }
265
266 eval "require RT::URI_Vendor";
267 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI_Vendor.pm});
268 eval "require RT::URI_Local";
269 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI_Local.pm});
270
271 1;