| 1 | package OpenInteract2::Request::Standalone; | ||
| 2 | |||
| 3 | # $Id: Standalone.pm,v 1.14 2005/03/18 04:09:51 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( OpenInteract2::Request ); | ||
| 7 | use Log::Log4perl qw( get_logger ); | ||
| 8 | use OpenInteract2::Constants qw( :log ); | ||
| 9 | use OpenInteract2::Context qw( CTX ); | ||
| 10 | use OpenInteract2::Exception qw( oi_error ); | ||
| 11 | use OpenInteract2::Upload; | ||
| 12 | use OpenInteract2::URL; | ||
| 13 | use Sys::Hostname; | ||
| 14 | |||
| 15 | $OpenInteract2::Request::Standalone::VERSION = sprintf("%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/); | ||
| 16 | |||
| 17 | my ( $log ); | ||
| 18 | |||
| 19 | sub init { | ||
| 20 | my ( $self, $props ) = @_; | ||
| 21 | $log ||= get_logger( LOG_REQUEST ); | ||
| 22 | $log->is_info && | ||
| 23 | $log->info( "Creating Standalone request" ); | ||
| 24 | |||
| 25 | $self->_set_property_defaults( $props ); | ||
| 26 | |||
| 27 | # This will die if any problems found | ||
| 28 | $self->_check_properties( $props ); | ||
| 29 | |||
| 30 | $log->is_debug && $log->debug( "Full URL from property: $props->{url}" ); | ||
| 31 | $self->assign_request_url( $props->{url} ); | ||
| 32 | |||
| 33 | # Then the various headers, properties, etc. | ||
| 34 | |||
| 35 | $self->referer( $props->{referer} ); | ||
| 36 | $self->user_agent( $props->{user_agent} ); | ||
| 37 | |||
| 38 | my @cookie_pieces = (); | ||
| 39 | foreach my $cookie_info ( @{ $props->{cookie} } ) { | ||
| 40 | if ( ref $cookie_info ) { | ||
| 41 | $self->cookie( $cookie_info->name, $cookie_info->value ); | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | push @cookie_pieces, $cookie_info; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | if ( scalar @cookie_pieces ) { | ||
| 48 | $self->cookie_header( join( '; ', @cookie_pieces ) ); | ||
| 49 | } | ||
| 50 | |||
| 51 | if ( $props->{accept_language} ) { | ||
| 52 | $self->language_header( $props->{accept_language} ); | ||
| 53 | } | ||
| 54 | elsif ( $props->{languages} ) { | ||
| 55 | $self->{_user_language} = $props->{languages}; | ||
| 56 | } | ||
| 57 | |||
| 58 | $self->server_name( $props->{server_name} ); | ||
| 59 | $self->remote_host( $props->{remote_host} ); | ||
| 60 | $log->is_debug && | ||
| 61 | $log->debug( "Set request and server properties ok" ); | ||
| 62 | |||
| 63 | my $num_param = 0; | ||
| 64 | foreach my $field ( keys %{ $props->{param} } ) { | ||
| 65 | $self->param( $field, $props->{param}{ $field } ); | ||
| 66 | $num_param++; | ||
| 67 | } | ||
| 68 | $log->is_debug && | ||
| 69 | $log->debug( "Set parameters ok ($num_param)" ); | ||
| 70 | |||
| 71 | my $num_upload = 0; | ||
| 72 | foreach my $field ( keys %{ $props->{upload} } ) { | ||
| 73 | $self->_set_upload( $field, $props->{upload}{ $field } ); | ||
| 74 | $num_upload++; | ||
| 75 | } | ||
| 76 | $log->is_debug && | ||
| 77 | $log->debug( "Set uploads ok ($num_upload)" ); | ||
| 78 | |||
| 79 | $log->is_info && | ||
| 80 | $log->info( "Finished creating Standalone request" ); | ||
| 81 | return $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _set_property_defaults { | ||
| 85 | my ( $self, $props ) = @_; | ||
| 86 | $props->{param} ||= {}; | ||
| 87 | $props->{upload} ||= {}; | ||
| 88 | $props->{server_name} ||= hostname; | ||
| 89 | $props->{cookie} ||= []; | ||
| 90 | if ( ref $props->{cookie} ne 'ARRAY' ) { | ||
| 91 | $props->{cookie} = [ $props->{cookie} ]; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _check_properties { | ||
| 96 | my ( $self, $props ) = @_; | ||
| 97 | my @errors = (); | ||
| 98 | unless ( $props->{url} ) { | ||
| 99 | push @errors, "Must be initialized with property 'url'"; | ||
| 100 | } | ||
| 101 | if ( ref $props->{param} eq 'HASH' ) { | ||
| 102 | while ( my ( $name, $value ) = each %{ $props->{param} } ) { | ||
| 103 | my $typeof_p = ref $value; | ||
| 104 | if ( $typeof_p and $typeof_p ne 'ARRAY' ) { | ||
| 105 | push @errors, "Parameter '$name' must be set to a simple " . | ||
| 106 | "scalar or an arrayref, not a $typeof_p"; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | push @errors, "If set, the property 'param' must be set to a hashref"; | ||
| 112 | } | ||
| 113 | foreach my $cookie ( @{ $props->{cookie} } ) { | ||
| 114 | if ( ref $cookie and ! UNIVERSAL::isa( $cookie, 'OpenInteract2::Cookie' ) ) { | ||
| 115 | push @errors, "A cookie is not a simple scalar or an " . | ||
| 116 | "acceptable cookie object"; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | if ( ref $props->{upload} eq 'HASH' ) { | ||
| 120 | while ( my ( $name, $upload ) = each %{ $props->{upload} } ) { | ||
| 121 | unless( UNIVERSAL::isa( $upload, 'OpenInteract2::Upload' ) ) { | ||
| 122 | push @errors, "Upload '$name' must be set to an acceptable " . | ||
| 123 | "upload object."; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | push @errors, "If set, the property 'upload' must be set to a hashref"; | ||
| 129 | } | ||
| 130 | if ( scalar @errors > 0 ) { | ||
| 131 | oi_error "Properties not valid:\n", join( "\n", @errors ); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 | |||
| 137 | __END__ |