starting to work...
[freeside.git] / rt / lib / RT / URI.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 #                                          <sales@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/licenses/old-licenses/gpl-2.0.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
49 package RT::URI;
50
51 use strict;
52 use warnings;
53 use base 'RT::Base';
54
55 use RT::URI::base;
56 use Carp;
57
58 =head1 NAME
59
60 RT::URI
61
62 =head1 DESCRIPTION
63
64 This class provides a base class for URIs, such as those handled
65 by RT::Link objects.  
66
67 =head1 API
68
69
70
71 =cut
72
73
74
75
76 =head2 new
77
78 Create a new RT::URI object.
79
80 =cut
81
82                          
83 sub new {
84     my $proto = shift;
85     my $class = ref($proto) || $proto;
86     my $self  = {};
87     bless( $self, $class );
88
89     $self->CurrentUser(@_);
90
91     return ($self);
92 }
93
94
95
96
97 =head2 FromObject <Object>
98
99 Given a local object, such as an RT::Ticket or an RT::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 =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         $self->{resolver} = RT::URI::base->new( $self->CurrentUser ); # clear resolver
138         $RT::Logger->warning("Could not determine a URI scheme for $uri");
139         return (undef);
140     }
141      
142     # load up a resolver object for this scheme  
143     $self->_GetResolver($scheme);
144     
145     unless ($self->Resolver->ParseURI($uri)) {
146         $RT::Logger->warning( "Resolver "
147               . ref( $self->Resolver )
148               . " could not parse $uri, maybe Organization config was changed?"
149         );
150         $self->{resolver} = RT::URI::base->new( $self->CurrentUser ); # clear resolver
151         return (undef);
152     }
153
154     return(1);
155
156 }
157
158
159
160 =head2 _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 =head2 Scheme
191
192 Returns a local object id for this content.  You are expected to know
193 what sort of object this is the Id of
194
195 =cut
196
197 sub Scheme {
198     my $self = shift;
199     return ($self->Resolver->Scheme);
200
201 }
202
203 =head2 URI
204
205 Returns a local object id for this content.  You are expected to know what sort of object this is the Id 
206 of 
207
208 =cut
209
210 sub URI {
211     my $self = shift;
212     return ($self->Resolver->URI);
213
214 }
215
216
217 =head2 Object
218
219 Returns a local object for this content. This will usually be an RT::Ticket or somesuch
220
221 =cut
222
223
224 sub Object {   
225     my $self = shift;
226     return($self->Resolver->Object);
227
228 }
229
230
231
232
233 =head2 IsLocal
234
235 Returns a local object for this content. This will usually be an RT::Ticket or somesuch
236
237 =cut
238
239 sub IsLocal {
240     my $self = shift;
241     return $self->Resolver->IsLocal;     
242 }
243
244
245
246 =head2 AsHREF
247
248
249 =cut
250
251
252 sub AsHREF {
253     my $self = shift;
254     return $self->Resolver->HREF;
255 }
256
257 =head2 Resolver
258
259 Returns this URI's URI resolver object
260
261 =cut
262
263
264 sub Resolver {
265     my $self =shift;
266     return ($self->{'resolver'});
267 }
268
269 RT::Base->_ImportOverlays();
270
271 1;