This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
UrlRewriteConfig.java
177 lines (147 loc) · 9.22 KB
/
UrlRewriteConfig.java
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package org.zanata.servlet;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.ocpsoft.rewrite.annotation.RewriteConfiguration;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.config.Direction;
import org.ocpsoft.rewrite.context.EvaluationContext;
import org.ocpsoft.rewrite.servlet.config.Forward;
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
import org.ocpsoft.rewrite.servlet.config.HttpOperation;
import org.ocpsoft.rewrite.servlet.config.Path;
import org.ocpsoft.rewrite.servlet.config.Query;
import org.ocpsoft.rewrite.servlet.config.Redirect;
import org.ocpsoft.rewrite.servlet.config.rule.Join;
import org.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
import org.ocpsoft.urlbuilder.Address;
import org.ocpsoft.urlbuilder.AddressBuilder;
import javax.servlet.ServletContext;
import java.util.List;
import static java.nio.charset.StandardCharsets.UTF_8;
/*
* This class replaces urlrewrite.xml, with simpler bidirectional mappings for external/internal URLs.
*/
@RewriteConfiguration
public class UrlRewriteConfig extends HttpConfigurationProvider {
@Override
public Configuration getConfiguration(final ServletContext context) {
String contextPath = context.getContextPath();
// NB: inbound rules are processed in order, outbound rules in reverse order (as of Rewrite 3.0.0.Alpha1)
return ConfigurationBuilder.begin()
// strip cid params to avoid NonexistentConversationException
.addRule()
.when(Query.parameterExists("cid"))
.perform(new RedirectWithoutParam("cid"))
.addRule()
.when(Direction.isInbound().and(Path.matches("/seam/resource/restv1/{path}")))
.perform(Forward.to("/rest/{path}"))
.where("path").matches(".*")
// a redirect would be nicer (preferably to a pretty url, not
// to .xhtml), but make sure you handle parameters, eg for
// /search.seam?query=foo
.addRule(Join.pathNonBinding("/{path}.seam").to("{path}.xhtml"))
.when(Direction.isInbound())
.where("path").matches(".*")
.addRule()
.when(Direction.isInbound().and(Path.matches("/iteration/files/{projectSlug}/{iterationSlug}")))
.perform(Redirect.permanent(contextPath + "/iteration/view/{projectSlug}/{iterationSlug}/documents"))
.addRule()
.when(Direction.isInbound().and(Path.matches("/iteration/source_files/{projectSlug}/{iterationSlug}")))
.perform(Redirect.permanent(contextPath + "/iteration/view/{projectSlug}/{iterationSlug}/documents"))
.addRule(Join.path("/").to("/home.xhtml"))
.addRule(Join.path("/account/activate/{key}").to("/account/activate.xhtml"))
.addRule(Join.path("/account/google_password_reset_request").to("/account/google_password_reset_request.xhtml"))
.addRule(Join.path("/account/password_reset/{key}").to("/account/password_reset.xhtml"))
.addRule(Join.path("/account/password_reset_request").to("/account/password_reset_request.xhtml"))
.addRule(Join.path("/account/inactive").to("/account/inactive_account.xhtml"))
.addRule(Join.path("/account/sign_in").to("/account/login.xhtml"))
.addRule(Join.path("/account/register").to("/account/register.xhtml"))
.addRule(Join.path("/account/sign_out").to("/account/logout.xhtml"))
.addRule(Join.path("/account/validate_email/{key}").to("/account/email_validation.xhtml"))
.addRule(Join.path("/admin/").to("/admin/home.xhtml"))
.addRule(Join.pathNonBinding("/admin/{page}").to("/admin/{page}.xhtml")).where("page").matches(".+")
.addRule(Join.path("/dashboard/").to("/dashboard/home.xhtml"))
.addRule(Join.path("/dashboard/{section}").to("/dashboard/home.xhtml"))
.where("section").matches(".*")
.addRule(Join.path("/error/").to("/error.xhtml"))
.addRule(Join.pathNonBinding("/error/{path}").to("/error/{path}.xhtml"))
.addRule(Join.path("/glossary/").to("/glossary/view.xhtml"))
.addRule(Join.path("/iteration/view/{projectSlug}/{iterationSlug}").to("/iteration/view.xhtml"))
.addRule(Join.path("/iteration/view/{projectSlug}/{iterationSlug}/{section}").to("/iteration/view.xhtml"))
.where("section").matches(".*")
/* JSF serves zanata-assets with suffix of .xhtml only.
This is to make sure any reference to zanata-assets
without .xhtml can access the resource.
e.g. jars/assets/style.css forwards to
jars/assets/style.css.xhtml
*/
.addRule(Join.pathNonBinding("/javax.faces.resource/jars/assets/{path}")
.to("/javax.faces.resource/jars/assets/{path}.xhtml"))
.when(Direction.isInbound())
.where("path").matches(".*(?<!.xhtml)")
.addRule(Join.path("/language/list").to("/language/home.xhtml"))
.addRule(Join.path("/language/view/{id}").to("/language/language.xhtml"))
.addRule(Join.path("/language/view/{id}/{section}").to("/language/language.xhtml"))
.where("section").matches(".*")
.addRule(Join.path("/profile/").to("/profile/home.xhtml"))
.addRule(Join.path("/profile/add_identity").to("/profile/add_identity.xhtml"))
.addRule(Join.path("/profile/create").to("/profile/create_user.xhtml"))
.addRule(Join.path("/profile/edit").to("/profile/edit.xhtml"))
.addRule(Join.path("/profile/merge_account").to("/profile/merge_account.xhtml"))
.addRule(Join.path("/profile/view/{username}").to("/profile/home.xhtml"))
.addRule(Join.path("/project/add_iteration/{projectSlug}").to("/project/add_iteration.xhtml"))
.addRule(Join.path("/project/add_iteration/{projectSlug}/{copyFromVersionSlug}").to("/project/add_iteration.xhtml"))
.addRule(Join.path("/project/create").to("/project/create_project.xhtml"))
.addRule(Join.path("/project/list").to("/project/home.xhtml"))
.addRule(Join.path("/project/view/{slug}").to("/project/project.xhtml"))
.addRule(Join.path("/project/view/{slug}/{section}").to("/project/project.xhtml"))
.where("section").matches(".*")
// generate zanata.xml config
.addRule(Join.path("/project/view/{projectSlug}/iter/{iterationSlug}/config").
to("/project/project.xhtml?actionMethod=project%2Fproject.xhtml%3AconfigurationAction.getData"))
// TODO fix this
.addRule(Join.path("/rest").to("/rest/index.xrd"))
.addRule(Join.path("/search/{query}").to("/search.xhtml"))
// Translation Memory
.addRule(Join.path("/tm/").to("/tm/home.xhtml"))
.addRule(Join.path("/tm/create").to("/tm/create.xhtml"))
.addRule(Join.path("/version-group/create").to("/version-group/create_version_group.xhtml"))
.addRule(Join.path("/version-group/list").to("/version-group/home.xhtml"))
.addRule(Join.path("/version-group/view/{versionGroupSlug}").to("/version-group/version_group.xhtml"))
.addRule(Join.path("/version-group/view/{versionGroupSlug}/{section}").to("/version-group/version_group.xhtml"))
.where("section").matches(".*")
.addRule(Join.path("/webtrans/Application.html").to("/webtrans/Application.xhtml")).when(Direction.isInbound())
.addRule(Join.path("/webtrans/translate").to("/webtrans/Application.xhtml"))
.addRule(Join.path("/404").to("/404.xhtml"))
;
}
@Override
public int priority() {
return 0;
}
static class RedirectWithoutParam extends HttpOperation {
private final String paramName;
RedirectWithoutParam(String paramName) {
this.paramName = paramName;
}
@Override
public void performHttp(HttpServletRewrite event,
EvaluationContext context) {
// Remove param from address query
Address address = event.getAddress();
String query = address.getQuery();
List<NameValuePair> nameValuePairs =
URLEncodedUtils.parse(query, UTF_8);
nameValuePairs.removeIf(nvp -> nvp.getName().equals(paramName));
String newAddress;
if (nameValuePairs.isEmpty()) {
newAddress = address.getPath();
} else {
newAddress = address.getPath() + "?" + URLEncodedUtils.format(nameValuePairs, UTF_8);
}
((HttpInboundServletRewrite) event).redirectTemporary(AddressBuilder.create(newAddress).toString());
}
}
}