| 1 | package RWDE::Web::CommandProxy; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # Proxy object for getting the specific functionality of a particular object | ||
| 7 | |||
| 8 | use vars qw($VERSION); | ||
| 9 | $VERSION = sprintf "%d", q$Revision: 550 $ =~ /(\d+)/; | ||
| 10 | |||
| 11 | use Error qw(:try); | ||
| 12 | |||
| 13 | use RWDE::AbstractFactory; | ||
| 14 | use RWDE::Configuration; | ||
| 15 | use RWDE::Exceptions; | ||
| 16 | use RWDE::Web::Helper; | ||
| 17 | use RWDE::Web::TemplateAdapter; | ||
| 18 | |||
| 19 | use base qw(RWDE::Proxy); | ||
| 20 | |||
| 21 | sub execute { | ||
| 22 | my ($self, $params) = @_; | ||
| 23 | |||
| 24 | my $req = $$params{req} | ||
| 25 | or throw RWDE::DevelException({ info => 'No request to process specified' }); | ||
| 26 | |||
| 27 | # create auxiliary object to facilitate passing of the data between layers | ||
| 28 | my $helper = new RWDE::Web::Helper; | ||
| 29 | |||
| 30 | # The commands might need the request object for logging/redirection purposes | ||
| 31 | $helper->set_stash({ req => $req }); | ||
| 32 | |||
| 33 | # put elements in the helper for easy access by Command layer | ||
| 34 | my $formdata = $req->get_formdata(); | ||
| 35 | $helper->set_stash({ formdata => $formdata }); | ||
| 36 | |||
| 37 | # get the params from the request that will decide on method invocation and params | ||
| 38 | # the uri (the part after the host name) will determine the class called | ||
| 39 | my $uri = $req->get_uri(); | ||
| 40 | |||
| 41 | $helper->set_stash({ uri => $uri }); | ||
| 42 | |||
| 43 | # store the current state of the https connection in the stash | ||
| 44 | if ($req->is_https()) { | ||
| 45 | $helper->set_stash({ https => 1 }); | ||
| 46 | $helper->set_stash({ FullServiceAddress => RWDE::Configuration->ServiceSSLAddress }); | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | $helper->set_stash({ FullServiceAddress => RWDE::Configuration->ServiceAddress }); | ||
| 50 | } | ||
| 51 | |||
| 52 | # Placing the Service configuration hash for Templates to access. This leaves a few redundant | ||
| 53 | # fields that are injected manually now, will cleanup as the rest is refactored as the templates | ||
| 54 | # will have to use the format Configuration.Debug rather then Debug | ||
| 55 | $helper->set_stash({ Configuration => RWDE::Configuration->get_instance() }); | ||
| 56 | |||
| 57 | #every possible page controller and template should be able to identify which host they are running on | ||
| 58 | #and if they are currently in debug mode - we shouldn't have to do this for each default controller | ||
| 59 | $helper->set_stash({ Debug => RWDE::Configuration->Debug }); | ||
| 60 | $helper->set_stash({ ServerName => $req->get_req->server_name }); | ||
| 61 | $helper->set_stash({ ServiceHost => RWDE::Configuration->ServiceHost }); | ||
| 62 | |||
| 63 | # The template needs to insert ServiceAddress for SSL protected forms | ||
| 64 | $helper->set_stash({ ServiceAddress => RWDE::Configuration->ServiceAddress }); | ||
| 65 | $helper->set_stash({ ServiceSSLAddress => RWDE::Configuration->ServiceSSLAddress }); | ||
| 66 | |||
| 67 | my $class = $req->get_class({ helper => $helper }); | ||
| 68 | |||
| 69 | try { | ||
| 70 | my $command = RWDE::AbstractFactory->instantiate({ class => $class, helper => $helper }); | ||
| 71 | |||
| 72 | if (not defined $req->{forward}) { | ||
| 73 | $command->execute($formdata); | ||
| 74 | } | ||
| 75 | |||
| 76 | if (not defined $req->{header}) { | ||
| 77 | $req->print_header({ pagetype => $helper->get_pagetype()}); | ||
| 78 | } | ||
| 79 | |||
| 80 | # we have to check again for forward, as $command->execute might have issued a forward | ||
| 81 | if (not defined $req->{forward}) { | ||
| 82 | RWDE::Web::TemplateAdapter->render({ helper => $helper }); | ||
| 83 | } | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | catch Error with { | ||
| 88 | my $ex = shift; | ||
| 89 | |||
| 90 | warn $ex; | ||
| 91 | |||
| 92 | $helper->set_stash({ info => $ex }); | ||
| 93 | |||
| 94 | # Set the appropriate error page (devel or customer) | ||
| 95 | $self->invoke({ class => 'Command::default', function => 'execute', helper => $helper }); | ||
| 96 | |||
| 97 | RWDE::PostMaster->send_report_message( | ||
| 98 | { | ||
| 99 | info => $ex, | ||
| 100 | record => $req->get_record() . "\nReferrer: " . $req->get_referer(), #both the request and the referrer | ||
| 101 | formdata => $formdata, | ||
| 102 | uri => $uri, | ||
| 103 | class => $class, | ||
| 104 | session => $helper->get_session, | ||
| 105 | } | ||
| 106 | ); | ||
| 107 | |||
| 108 | if (not defined $req->{header}) { | ||
| 109 | $req->print_header({ pagetype => $helper->get_pagetype()}); | ||
| 110 | } | ||
| 111 | |||
| 112 | RWDE::Web::TemplateAdapter->render({ helper => $helper }); | ||
| 113 | }; | ||
| 114 | |||
| 115 | return (); | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; |