redirecting /qtl/search to /qtl/search, to make use of the tab formatted SGN search...
[sgn.git] / lib / SGN / Controller / Root.pm
blob7cc7b854663d154320cd465d4c9f34b35fe6bed1
1 package SGN::Controller::Root;
2 use Moose;
3 use namespace::autoclean;
5 use Scalar::Util 'weaken';
6 use CatalystX::GlobalContext ();
8 use CXGN::Login;
9 use CXGN::People::Person;
11 BEGIN { extends 'Catalyst::Controller' }
14 # Sets the actions in this controller to be registered with no prefix
15 # so they function identically to actions created in MyApp.pm
17 __PACKAGE__->config(namespace => '');
19 =head1 NAME
21 SGN::Controller::Root - Root Controller for SGN
23 =head1 DESCRIPTION
25 Web application to run the SGN web site.
27 =head1 PUBLIC ACTIONS
29 =head2 index
31 The root page (/)
33 =cut
35 sub index :Path :Args(0) {
36 my ( $self, $c ) = @_;
38 # Hello World
39 $c->stash->{template} = '/index.mas';
40 $c->stash->{schema} = $c->dbic_schema('SGN::Schema');
43 =head2 default
45 Attempt to find index.pl pages, and prints standard 404 error page if
46 nothing could be found.
48 =cut
50 sub default :Path {
51 my ( $self, $c ) = @_;
53 return 1 if $self->_do_redirects($c);
55 $c->throw_404;
58 =head2 bare_mason
60 Render a bare mason component, with no autohandler wrapping.
61 Currently used for GBrowse integration (GBrowse makes a subrequest for
62 the mason header and footer).
64 =cut
66 sub bare_mason :Path('bare_mason') {
67 my ( $self, $c, @args ) = @_;
69 # figure out our template path
70 my $t = File::Spec->catfile( @args );
71 $t .= '.mas' unless $t =~ m|\.[^/\\\.]+$|;
72 $c->stash->{template} = $t;
74 # TODO: check that it exists
76 $c->forward('View::BareMason');
79 =head1 PRIVATE ACTIONS
81 =head2 end
83 Attempt to render a view, if needed.
85 =cut
87 sub render : ActionClass('RenderView') { }
88 sub end : Private {
89 my ( $self, $c ) = @_;
91 return if @{$c->error};
93 # don't try to render a default view if this was handled by a CGI
94 $c->forward('render') unless $c->req->path =~ /\.pl$/;
96 # enforce a default text/html content type regardless of whether
97 # we tried to render a default view
98 $c->res->content_type('text/html') unless $c->res->content_type;
100 # insert our javascript packages into the rendered view
101 if( $c->res->content_type eq 'text/html' ) {
102 $c->forward('/js/insert_js_pack_html');
103 $c->res->headers->push_header('Vary', 'Cookie');
104 } else {
105 $c->log->debug("skipping JS pack insertion for page with content type ".$c->res->content_type)
106 if $c->debug;
111 =head2 auto
113 Run for every request to the site.
115 =cut
117 sub auto : Private {
118 my ($self, $c) = @_;
119 CatalystX::GlobalContext->set_context( $c );
120 $c->stash->{c} = $c;
121 weaken $c->stash->{c};
123 # gluecode for logins
125 unless( $c->config->{'disable_login'} ) {
126 my $dbh = $c->dbc->dbh;
127 if ( my $sp_person_id = CXGN::Login->new( $dbh )->has_session ) {
129 my $sp_person = CXGN::People::Person->new( $dbh, $sp_person_id);
131 $c->authenticate({
132 username => $sp_person->get_username(),
133 password => $sp_person->get_password(),
138 return 1;
143 ########### helper methods ##########3
145 sub _do_redirects {
146 my ($self, $c) = @_;
147 my $path = $c->req->path;
148 my $query = $c->req->uri->query || '';
149 $query = "?$query" if $query;
151 $c->log->debug("searching for redirects ($path) ($query)") if $c->debug;
153 # if the path has multiple // in it, collapse them and redirect to
154 # the result
155 if( $path =~ s!/{2,}!/!g ) {
156 $c->log->debug("redirecting multi-/ request to /$path$query") if $c->debug;
157 $c->res->redirect( "/$path$query", 301 );
158 return 1;
161 # try an internal redirect for index.pl files if the url has not
162 # already been found and does not have an extension
163 if( $path !~ m|\.\w{2,4}$| ) {
164 if( my $index_action = $self->_find_cgi_action( $c, "$path/index.pl" ) ) {
165 $c->log->debug("redirecting to action $index_action") if $c->debug;
166 my $uri = $c->uri_for_action($index_action, $c->req->query_parameters)
167 ->rel( $c->uri_for('/') );
168 $c->res->redirect( "/$uri", 302 );
169 return 1;
173 # redirect away from cgi-bin URLs
174 elsif( $path =~ s!cgi-bin/!! ) {
175 $c->log->debug("redirecting cgi-bin url to /$path$query") if $c->debug;
176 $c->res->redirect( "/$path$query", 301 );
177 return 1;
183 ############# helper subs ##########
185 sub _find_cgi_action {
186 my ($self,$c,$path) = @_;
188 $path =~ s!/+!/!g;
189 my $cgi = $c->controller('CGI')
190 or return;
192 my $index_action = $cgi->cgi_action_for( $path )
193 or return;
195 $c->log->debug("found CGI index action '$index_action'") if $c->debug;
197 return $index_action;
200 =head1 AUTHOR
202 Robert Buels, Jonathan "Duke" Leto
204 =head1 LICENSE
206 This library is free software. You can redistribute it and/or modify
207 it under the same terms as Perl itself.
209 =cut
211 __PACKAGE__->meta->make_immutable;