My favorites
▼
|
Sign in
visural-common
visural.com - common Java utility toolkit
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
visural-common
/
src
/
com
/
visural
/
common
/
web
/
filter
/
PermanentRedirectFilter.java
‹r60
r113
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2010 Richard Nichols.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package com.visural.common.web.filter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet filter which assists with doing permanent host and url redirects.
* @author Richard Nichols
*/
public abstract class PermanentRedirectFilter implements Filter {
private final List<Route> routes = new ArrayList<Route>();
private final Map<String,String> rHost = new HashMap<String,String>();
private final Map<String,String> rURL = new HashMap<String,String>();
public void init(FilterConfig fc) throws ServletException {
configureRoutes();
buildRouteMaps();
routes.clear();
}
/**
* Implement this method and configure your redirects.
*/
public abstract void configureRoutes();
public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {
HttpServletRequest hsr = (HttpServletRequest) sr;
HttpServletResponse resp = (HttpServletResponse) sr1;
String url = hsr.getRequestURL().toString();
String host = url.replaceAll("https?://([^:/]+).*", "$1");
if (hsr.getQueryString() != null) {
url = url + "?" + hsr.getQueryString();
}
String redirect = null;
if (rHost.get(host) != null) {
redirect = url.replaceFirst(Matcher.quoteReplacement(host), rHost.get(host));
} else if (rURL.get(url) != null) {
redirect = rURL.get(url);
}
if (redirect == null) {
redirect = getAlgorithmicRedirect(url);
}
if (redirect != null) {
resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
resp.setHeader("Location", redirect);
} else {
fc.doFilter(sr, sr1);
}
}
/**
* override this method to do an algorithmic redirect, e.g. using regex
* @param url
* @return
*/
protected String getAlgorithmicRedirect(String url) {
return null;
}
public void destroy() {
}
protected Route fromURL(String from) {
return new Route(RouteType.URL, from);
}
protected Route fromHost(String from) {
return new Route(RouteType.HOST, from);
}
private void buildRouteMaps() {
for (Route r : routes) {
switch (r.type) {
case HOST:
rHost.put(r.from, r.to);
break;
case URL:
rURL.put(r.from, r.to);
break;
}
}
}
protected enum RouteType {
HOST,
URL;
}
public class Route {
private final RouteType type;
private String from;
private String to;
public Route(RouteType type, String from) {
this.type = type;
this.from = from;
}
public void to(String to) {
this.to = to;
routes.add(this);
}
}
}
Show details
Hide details
Change log
r97
by tibes80 on Nov 3, 2010
Diff
Add customisation point for redirect filter
Go to:
...ter/PermanentRedirectFilter.java
Project members,
sign in
to write a code review
Older revisions
r60
by tibes80 on Jun 8, 2010
Diff
release 0.3.2
r59
by tibes80 on Jun 7, 2010
Diff
WIP
All revisions of this file
File info
Size: 4022 bytes, 134 lines
View raw file
Powered by
Google Project Hosting