Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / lib / SGN / Controller / Redirects.pm
blobc87c62ed5e9d19380ddbc0bfdfd39dea8d4cc4f2
1 package SGN::Controller::Redirects;
2 use Moose;
4 BEGIN { extends 'Catalyst::Controller' }
6 =head1 CONFIGURATION
8 =head2 paths
10 Hashref of specific paths to do redirects to. Merged with the
11 hardcoded paths in this controller.
13 =cut
15 # put specific redirect paths here
16 my %paths = (
18 # redirects from refactoring the search controller
19 qw(
20 /search/qtl /search/phenotypes/qtl
21 /trait/search /search/phenotypes/traits
22 /search/trait /search/phenotypes/traits
24 /search/phenotype /search/stocks
25 /search/phenotype_qtl_trait /search/stocks
26 /search/phenotypes/stock /search/stocks
28 /search/locus_search.pl /search/locus
29 /search/loci /search/locus
31 /search/unigene /search/transcripts/unigene
32 /search/unigenes /search/transcripts/unigene
33 /search/platform /search/expression/platform
34 /search/template_experiment_platform /search/expression/template
35 /search/experiment /search/expression/experiment
36 /search/est /search/transcripts/est
37 /search/ests /search/transcripts/est
38 /search/EST /search/transcripts/est
39 /search/ESTs /search/transcripts/est
40 /search/est_library /search/transcripts/est_library
41 /search/library /search/transcripts/est_library
42 /search/bacs /search/genomic/clones
43 /search/BACs /search/genomic/clones
44 /search/marker /search/markers
47 # qtl redirects
48 qw(
49 /qtl/population /search/phenotypes/qtl
50 /qtl/search /search/phenotypes/qtl
51 /search/qtl/help /qtl/search/help
52 /qtl/guide.pl /qtl/submission/guide
53 /phenome/qtl_form.pl /qtl/form
54 /qtl/submit /qtl/form
55 /qtl/index.pl /search/phenotypes/qtl
56 /qtl /search/phenotypes/qtl
57 /qtl/ /search/phenotypes/qtl
60 # genomes redirects
61 qw(
62 /genomes/index.pl /genomes
63 /genomes/Solanum_pimpinellifolium /organism/Solanum_pimpinellifolium/genome
64 /genomes/Solanum_pimpinellifolium/ /organism/Solanum_pimpinellifolium/genome
65 /genomes/Solanum_pimpinellifolium/index.pl /organism/Solanum_pimpinellifolium/genome
66 /genomes/Solanum_pimpinellifolium.pl /organism/Solanum_pimpinellifolium/genome
68 /genomes/Solanum_lycopersicum /organism/Solanum_lycopersicum/genome
69 /genomes/Solanum_lycopersicum/ /organism/Solanum_lycopersicum/genome
70 /genomes/Solanum_lycopersicum/index.pl /organism/Solanum_lycopersicum/genome
71 /genomes/Solanum_lycopersicum.pl /organism/Solanum_lycopersicum/genome
72 /genomes/Solanum_lycopersicum/genome_data.pl /organism/Solanum_lycopersicum/genome
73 /tomato/genome_data.pl /organism/Solanum_lycopersicum/genome
77 # also can get redirect paths from the configuration
78 has 'paths' => (
79 is => 'rw',
80 default => sub { {} },
83 # these are merged at construction time, with the configured paths
84 # overriding hardcoded paths
85 sub BUILD {
86 my $self = shift;
87 $self->paths({
88 %paths,
89 %{$self->paths},
90 });
93 sub find_redirect : Private {
94 my ($self, $c) = @_;
95 my $path = $c->req->path;
96 my $query = $c->req->uri->query || '';
97 $query = "?$query" if $query;
99 $c->log->debug("searching for redirects, path='$path' query='$query'") if $c->debug;
101 # if the path has multiple // in it, collapse them and redirect to
102 # the result
103 if( $path =~ s!/{2,}!/!g ) {
104 $c->log->debug("redirecting multi-/ request to /$path$query") if $c->debug;
105 $c->res->redirect( "/$path$query", 301 );
106 return 1;
109 # try an internal redirect for index.pl files if the url has not
110 # already been found and does not have an extension
111 if( $path !~ m|\.\w{2,4}$| ) {
112 if( my $index_action = $self->_find_cgi_action( $c, "$path/index.pl" ) ) {
113 $c->log->debug("redirecting to action $index_action") if $c->debug;
114 my $uri = $c->uri_for_action($index_action, $c->req->query_parameters)
115 ->rel( $c->uri_for('/') );
116 $c->res->redirect( "/$uri", 302 );
117 return 1;
121 # redirect away from cgi-bin URLs
122 if( $path =~ s!cgi-bin/!! ) {
123 $c->log->debug("redirecting cgi-bin url to /$path$query") if $c->debug;
124 $c->res->redirect( "/$path$query", 301 );
125 return 1;
128 # redirect any explicitly-added paths
129 if( my $re = $self->paths->{"/$path"} ) {
130 $c->res->redirect( $re, 301 );
131 return 1;
134 return 0;
137 ############# helper methods ##########
139 sub _find_cgi_action {
140 my ($self,$c,$path) = @_;
142 $path =~ s!/+!/!g;
143 my $cgi = $c->controller('CGI')
144 or return;
146 my $index_action = $cgi->cgi_action_for( $path )
147 or return;
149 $c->log->debug("found CGI index action '$index_action'") if $c->debug;
151 return $index_action;