| 1 | package OpenInteract::Request; | ||
| 2 | |||
| 3 | # $Id: Request.pm,v 1.16 2003/08/13 02:04:41 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( Class::Singleton ); | ||
| 7 | use Data::Dumper qw( Dumper ); | ||
| 8 | |||
| 9 | $OpenInteract::Request::VERSION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/); | ||
| 10 | |||
| 11 | $OpenInteract::Request::DEBUG = 0; | ||
| 12 | |||
| 13 | |||
| 14 | # This is only called the **first** time the object is created, which | ||
| 15 | # under mod_perl means when the first request comes into the httpd | ||
| 16 | # child; thereafter, Class::Singleton just returns the object. Be | ||
| 17 | # careful! | ||
| 18 | |||
| 19 | sub _new_instance { | ||
| 20 | my ( $pkg, %p ) = @_; | ||
| 21 | my $class = ref( $pkg ) || $pkg; | ||
| 22 | my $self = bless( {}, $pkg ); | ||
| 23 | $self->initialize(); | ||
| 24 | return $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | # Alias so people can do ->new() as well as ->instance() | ||
| 29 | |||
| 30 | sub new { my $class = shift; return $class->instance( @_ ); } | ||
| 31 | |||
| 32 | |||
| 33 | # Dummy method for subclasses to override | ||
| 34 | |||
| 35 | sub initialize { return 1; } | ||
| 36 | |||
| 37 | |||
| 38 | # Shortcuts pointing to the stash | ||
| 39 | |||
| 40 | sub cgi { return $_[0]->get_stash( 'cgi' ) } | ||
| 41 | sub apache { return $_[0]->get_stash( 'apache' ) } | ||
| 42 | sub cache { return $_[0]->get_stash( 'cache' ) } | ||
| 43 | sub config { return $_[0]->get_stash( 'config' ) } | ||
| 44 | sub CONFIG { return $_[0]->get_stash( 'config' ) } | ||
| 45 | sub uri { return $_[0]->get_stash( 'uri' ) } | ||
| 46 | sub error_handlers { return $_[0]->get_stash( 'error_handlers' ) } | ||
| 47 | |||
| 48 | |||
| 49 | sub template_object { | ||
| 50 | my ( $self ) = @_; | ||
| 51 | my $tt_object = $self->get_stash( 'template_object' ); | ||
| 52 | return $tt_object if ( $tt_object ); | ||
| 53 | $tt_object = $self->template->initialize; | ||
| 54 | $self->stash( 'template_object', $tt_object ); | ||
| 55 | return $tt_object; | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | sub db { | ||
| 60 | my ( $self, $connect_key ) = @_; | ||
| 61 | $connect_key ||= $self->CONFIG->{datasource}{default_connection_db} || | ||
| 62 | $self->CONFIG->{default_connection_db}; | ||
| 63 | my $db = $self->get_stash( "db-$connect_key" ); | ||
| 64 | return $db if ( $db ); | ||
| 65 | require OpenInteract::DBI; | ||
| 66 | $db = eval { OpenInteract::DBI->connect( $self->CONFIG->{db_info}{ $connect_key } ) }; | ||
| 67 | if ( $@ ) { | ||
| 68 | OpenInteract::Error->set({ system_msg => $@, | ||
| 69 | type => 'db', | ||
| 70 | extra => $self->CONFIG->{db_info}{ $connect_key } }); | ||
| 71 | $self->scrib( 0, "Cannot connect to database! Immediately exiting process!" ); | ||
| 72 | my $error_msg = $self->throw({ code => 11 }); | ||
| 73 | die $error_msg; | ||
| 74 | } | ||
| 75 | return $self->db_stash( $db, $connect_key ); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub db_stash { | ||
| 80 | my ( $self, $dbh, $connect_key ) = @_; | ||
| 81 | $connect_key ||= $self->CONFIG->{datasource}{default_connection_db} || | ||
| 82 | $self->CONFIG->{default_connection_db}; | ||
| 83 | return $self->stash( "db-$connect_key", $dbh ); | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub ldap { | ||
| 88 | my ( $self, $connect_key ) = @_; | ||
| 89 | $connect_key ||= $self->CONFIG->{datasource}{default_connection_ldap} || | ||
| 90 | $self->CONFIG->{default_connection_ldap}; | ||
| 91 | my $ldap = $self->get_stash( "ldap-$connect_key" ); | ||
| 92 | return $ldap if ( $ldap ); | ||
| 93 | require OpenInteract::LDAP; | ||
| 94 | $ldap = eval { OpenInteract::LDAP->connect_and_bind( | ||
| 95 | $self->CONFIG->{ldap_info}{ $connect_key } ) }; | ||
| 96 | if ( $@ ) { | ||
| 97 | OpenInteract::Error->set({ system_msg => $@, | ||
| 98 | type => 'db', | ||
| 99 | extra => $self->CONFIG->{db_info}{ $connect_key } }); | ||
| 100 | $self->scrib( 0, "Cannot connect to LDAP directory with connection ($connect_key)! Error: $@" ); | ||
| 101 | die $self->throw({ code => 11 }); | ||
| 102 | } | ||
| 103 | return $self->ldap_stash( $ldap, $connect_key ); | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | sub ldap_stash { | ||
| 109 | my ( $self, $ldap, $connect_key ) = @_; | ||
| 110 | $connect_key ||= $self->CONFIG->{datasource}{default_connection_ldap} || | ||
| 111 | $self->CONFIG->{default_connection_ldap}; | ||
| 112 | return $self->stash( "ldap-$connect_key", $ldap ); | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | # A shortcut, but we put the caller info in there so every error | ||
| 117 | # thrown doesn't appear to be coming from this location :) | ||
| 118 | |||
| 119 | sub throw { | ||
| 120 | my ( $self, $p ) = @_; | ||
| 121 | ( $p->{package}, $p->{filename}, $p->{line} ) = caller; | ||
| 122 | $p->{action} = $self->{current_context}{action}; | ||
| 123 | return $self->error->throw( $p ); | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | # Put an object in the stash | ||
| 128 | |||
| 129 | sub stash { | ||
| 130 | my ( $self, $name, $obj ) = @_; | ||
| 131 | return {} unless ( ref $self ); | ||
| 132 | my $stash_class = $self->{stash_class}; | ||
| 133 | die "No stash class defined in OpenInteract::Request object!\n" unless ( $stash_class ); | ||
| 134 | return $stash_class->set_stash( $name, $obj ); | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | # Get an object from the stash | ||
| 139 | |||
| 140 | sub get_stash { | ||
| 141 | my ( $self, $item ) = @_; | ||
| 142 | return {} unless ( ref $self ); | ||
| 143 | my $stash_class = $self->{stash_class}; | ||
| 144 | return $stash_class->get_stash( $item ) if ( $stash_class ); | ||
| 145 | return {}; | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | # Alias stuff | ||
| 150 | |||
| 151 | my %ALIAS = (); | ||
| 152 | my $ALIASES_SETUP = 0; | ||
| 153 | |||
| 154 | sub ALIAS { return \%ALIAS } | ||
| 155 | sub lookup_alias { return $ALIAS{ $_[1] }{ $_[0]->{stash_class} } } | ||
| 156 | |||
| 157 | |||
| 158 | # read in all the aliases in %ALIAS and setup subroutines | ||
| 159 | # based on them -- we only need to do this once per child init, | ||
| 160 | # so increment a counter that says we've setup the aliases. | ||
| 161 | # | ||
| 162 | # Should we let people do this more than once? If we do, there will | ||
| 163 | # be a bunch of 'subroutine xxx redefined blah blah blah' messages... | ||
| 164 | |||
| 165 | sub setup_aliases { | ||
| 166 | my ( $class ) = @_; | ||
| 167 | return if ( $ALIASES_SETUP ); | ||
| 168 | $class->scrib( 1, "Aliases not yet setup. Setting up aliases for process ($$)" ); | ||
| 169 | no strict 'refs'; | ||
| 170 | foreach my $alias ( keys %ALIAS ) { | ||
| 171 | *{ $class . '::' . $alias } = sub { | ||
| 172 | my $self = shift; $self = $self->instance unless ( ref $self ); | ||
| 173 | return $ALIAS{ $alias }{ $self->{stash_class} } | ||
| 174 | }; | ||
| 175 | } | ||
| 176 | $ALIASES_SETUP++; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | # I've finally relented and created a logging method. I'm still a | ||
| 181 | # little wary about the performance hit, but it's worth doing so that | ||
| 182 | # you don't have to restart mod_perl to get debugging info. All we | ||
| 183 | # need to do now is create a little admin gui so you can change the | ||
| 184 | # debug value in a session, which gets copied to all http children via | ||
| 185 | # a shared session | ||
| 186 | # | ||
| 187 | # Usage $R->DEBUG && $R->scrib( $level, $msg, $msg,... ); | ||
| 188 | # -- nothing done if $level < $CONFIG->{DEBUG} | ||
| 189 | # -- all $msg get join'd with a ' ' character to form the message | ||
| 190 | # -- msg sent to STDERR with "package::sub ($line) >> $msg" | ||
| 191 | |||
| 192 | sub scrib { | ||
| 193 | my ( $self, $level, @msg ) = @_; | ||
| 194 | return undef if ( $self->DEBUG < $level ); | ||
| 195 | my ( $pkg, $file, $line ) = caller; | ||
| 196 | my @ci = caller(1); | ||
| 197 | warn "$ci[3] ($line) >> ", join( ' ', @msg ), "\n"; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | # The last (?) piece to the debugging puzzle. You should now always | ||
| 202 | # call: | ||
| 203 | # | ||
| 204 | # $R->DEBUG && $R->DEBUG && $R->scrib( 1, "xxx" ); | ||
| 205 | # | ||
| 206 | # So that we don't have to do anything with the argument(s) to the | ||
| 207 | # scrib() method even if we're not debugging. | ||
| 208 | |||
| 209 | sub DEBUG { | ||
| 210 | my ( $self ) = @_; | ||
| 211 | my $app_debug = ( ref $self ) ? $self->CONFIG->{DEBUG} : 0; | ||
| 212 | return $app_debug || $OpenInteract::Request::DEBUG; | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | # Do some stuff with the configuration -- set defaults, etc. | ||
| 217 | # | ||
| 218 | # We should probably move this to OpenInteract::Config | ||
| 219 | # Determine that the conductor is for a particular module | ||
| 220 | |||
| 221 | sub lookup_conductor { | ||
| 222 | my ( $self, $action ) = @_; | ||
| 223 | $action ||= shift @{ $self->{path}{current} }; | ||
| 224 | $self->scrib( 1, "Find conductor for action ($action)" ); | ||
| 225 | my ( $action_info, $action_method ) = $self->lookup_action( $action, { return => 'info' } ); | ||
| 226 | $self->scrib( 2, "Info for action:\n", Dumper( $action_info ) ); | ||
| 227 | my $conductor = $action_info->{conductor}; | ||
| 228 | |||
| 229 | # skip conductor for component-only actions | ||
| 230 | |||
| 231 | return undef if ( $conductor eq 'null' ); | ||
| 232 | my $conductor_info = $self->CONFIG->{conductor}{ lc $conductor }; | ||
| 233 | my $method = $conductor_info->{method}; | ||
| 234 | return ( $conductor_info->{class}, $method ); | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | # Find the package/class corresponding to a particular | ||
| 239 | # action tag | ||
| 240 | # | ||
| 241 | # Possible $opt options: | ||
| 242 | # return => 'info' = return all action info | ||
| 243 | # skip_default => bool = if there is no action under $action_name, don't substitute the default | ||
| 244 | |||
| 245 | sub lookup_action { | ||
| 246 | my ( $self, $action_name, $opt ) = @_; | ||
| 247 | $action_name ||= shift @{ $self->{path}{current} }; | ||
| 248 | my $action_list = ( ref $action_name eq 'ARRAY' ) ? $action_name : [ $action_name ]; | ||
| 249 | |||
| 250 | my $CONFIG = $self->CONFIG; | ||
| 251 | ACTION: | ||
| 252 | foreach my $action ( @{ $action_list } ) { | ||
| 253 | $self->scrib( 1, "Find action corresponding to ($action)" ); | ||
| 254 | my $action_info = ( $action ) ? $CONFIG->{action}{ lc $action } | ||
| 255 | : $CONFIG->{action_info}{none}; | ||
| 256 | $self->scrib( 2, "Info for action:\n", Dumper( $action_info ) ); | ||
| 257 | |||
| 258 | # If we don't find a action, then we use the action from | ||
| 259 | # 'not_found'; since we put this before the looping to find | ||
| 260 | # 'action' references, this can simply be a pointer | ||
| 261 | |||
| 262 | unless ( $opt->{skip_default} or $action_info ) { | ||
| 263 | $action_info = $CONFIG->{action_info}{not_found}; | ||
| 264 | $self->scrib( 1, "Using 'notfound' action" ); | ||
| 265 | } | ||
| 266 | |||
| 267 | # Allow as many redirects as we need | ||
| 268 | |||
| 269 | while ( my $action_redir = $action_info->{redir} ) { | ||
| 270 | $action_info = $CONFIG->{action}{ lc $action_redir }; | ||
| 271 | $self->scrib( 3, "Info within redir ($action_redir):\n", Dumper( $action_info ) ); | ||
| 272 | } | ||
| 273 | next ACTION unless ( $action_info ); | ||
| 274 | $self->scrib( 1, "Found action info for ($action)" ); | ||
| 275 | $self->{current_context} = $action_info; | ||
| 276 | return \%{ $action_info } if ( $opt->{return} eq 'info' ); | ||
| 277 | my $method = $action_info->{method}; | ||
| 278 | return ( $action_info->{class}, $action_info->{method} ); | ||
| 279 | } | ||
| 280 | return undef; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | # Clear out everything that shouldn't be around | ||
| 285 | |||
| 286 | sub finish_request { | ||
| 287 | my ( $self ) = @_; | ||
| 288 | |||
| 289 | # Ask the stash to clean itself up | ||
| 290 | |||
| 291 | my $stash_class = $self->{stash_class}; | ||
| 292 | $stash_class->clean_stash; | ||
| 293 | |||
| 294 | # Clear out all the content in the object | ||
| 295 | |||
| 296 | foreach my $key ( keys %{ $self } ) { | ||
| 297 | # warn "Cleaning up key '$key' from request...\n"; | ||
| 298 | delete $self->{ $key }; | ||
| 299 | } | ||
| 300 | |||
| 301 | } | ||
| 302 | |||
| 303 | 1; | ||
| 304 | |||
| 305 | __END__ |