Prepare a test method for throwing Template exceptions. Add code to handle HTTP 304 for visitors.

This commit is contained in:
Colin Kuskie 2010-08-03 16:49:24 -07:00
parent 08f453475e
commit 25fb019a18
3 changed files with 58 additions and 11 deletions

View file

@ -40,6 +40,7 @@ use WebGUI::ProgressBar;
use WebGUI::Search::Index; use WebGUI::Search::Index;
use WebGUI::TabForm; use WebGUI::TabForm;
use WebGUI::Utility; use WebGUI::Utility;
use WebGUI::PassiveAnalytics::Logging;
=head1 NAME =head1 NAME
@ -564,27 +565,44 @@ sub definition {
Based on the URL and query parameters in the current request, call internal methods Based on the URL and query parameters in the current request, call internal methods
like www_view, www_edit, etc. If no query parameter is present, then it returns the output like www_view, www_edit, etc. If no query parameter is present, then it returns the output
from the view method. from the www_view method. If the requested method does not exist in the object, it returns
the output from the www_view method.
When this method returns undef, it means that the requested URL does not match this Asset's
URL.
=head3 $fragment =head3 $fragment
Any leftover part of the requested URL. Any leftover part of the requested URL.
=head3 _view
This option should only be used internally, when trying to call the view method when
the requested method has failed.
=cut =cut
sub dispatch { sub dispatch {
my ($self, $fragment) = @_; my ($self, $fragment, $_view) = @_;
return undef if defined $fragment; return undef if defined $fragment;
my $session = $self->session;
my $state = $self->get('state'); my $state = $self->get('state');
##Only allow interaction with assets in certain states ##Only allow interaction with assets in certain states
return if $state ne 'published' && $state ne 'archived' && !$self->session->var->isAdminOn; return if $state ne 'published' && $state ne 'archived' && !$session->var->isAdminOn;
my $func = $self->session->form->param('func') || 'view'; my $func = $session->form->param('func') || 'view';
my $sub = $self->can('www_'.$func) || $self->can('www_view'); $func = 'view' if $_view;
my $viewing = $func eq 'view' ? 1 : 0;
my $sub = $self->can('www_'.$func);
if (!$sub && $func ne 'view') {
$sub = $self->can('www_view');
$viewing = 1;
}
return undef unless $sub; return undef unless $sub;
my $output = $sub->(); my $output = eval { $sub->(); };
if (my $e = Exception::Class->caught('WebGUI::Error::ObjectNotFound::Template')) {
$session->log->error(sprintf "%s templateId: %s assetId: %s", $e->error, $e->templateId, $e->assetId);
}
elsif ($@) {
$session->errorHandler->warn("Couldn't call method www_".$func." on asset for url: ".$session->url->getRequestedUrl." Root cause: ".$@);
die $@;
}
return $output; return $output;
} }

View file

@ -66,9 +66,27 @@ sub dispatch {
my $permutations = getUrlPermutations($assetUrl); my $permutations = getUrlPermutations($assetUrl);
foreach my $url (@{ $permutations }) { foreach my $url (@{ $permutations }) {
if (my $asset = getAsset($session, $url)) { if (my $asset = getAsset($session, $url)) {
# display from cache if page hasn't been modified.
if ($session->user->isVisitor
&& !$session->http->ifModifiedSince($asset->getContentLastModified, $session->setting->get('maxCacheTimeout'))) {
$session->http->setStatus("304","Content Not Modified");
$session->http->sendHeader;
$session->close;
return "chunked";
}
my $fragment = $assetUrl; my $fragment = $assetUrl;
$fragment =~ s/$url//; $fragment =~ s/$url//;
my $output = $asset->dispatch($fragment); my $output = eval { $asset->dispatch($fragment); };
if ($@) {
$session->errorHandler->warn("Couldn't call method ".$method." on asset for url: ".$session->url->getRequestedUrl." Root cause: ".$@);
if ($method ne "view") {
$output = tryAssetMethod($session,$asset,'view');
} else {
# fatals return chunked
$output = 'chunked';
}
}
return $output if defined $output; return $output if defined $output;
} }
} }

View file

@ -30,6 +30,8 @@ BEGIN {
package WebGUI::Asset::TestDispatch; package WebGUI::Asset::TestDispatch;
use WebGUI::Asset;
use WebGUI::Exception;
our @ISA = ('WebGUI::Asset'); our @ISA = ('WebGUI::Asset');
# Override dispatch to handle special /foo URL # Override dispatch to handle special /foo URL
@ -55,6 +57,15 @@ sub www_alsoView {
return; return;
} }
sub www_brokenTemplate {
my $self = shift;
WebGUI::Error::ObjectNotFound::Template->throw(
error => qq{Template not found},
templateId => "This is a GUID",
assetId => $self->getId,
);
}
package main; package main;
my $tag = WebGUI::VersionTag->getWorking( $session ); my $tag = WebGUI::VersionTag->getWorking( $session );
@ -100,7 +111,7 @@ is( $td->dispatch, "www_view", "requests for non-existant methods return www_vie
$session->request->setup_body( { $session->request->setup_body( {
func => 'alsoView', func => 'alsoView',
} ); } );
is( $td->dispatch, "www_view", "if a query method return undef, view is still returned" ); is( $td->dispatch, "www_view", "if a query method returns undef, view is still returned" );
$session->request->setup_body( { } ); $session->request->setup_body( { } );
$output = $td->dispatch( '/not-foo' ); $output = $td->dispatch( '/not-foo' );