Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Genesys PGR
Genesys Backend
Commits
b6a046ab
Commit
b6a046ab
authored
Nov 06, 2014
by
Matija Obreza
Browse files
Allowed locales, reduced log levels
parent
f886a42d
Changes
5
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/genesys2/server/servlet/filter/LocaleURLFilter.java
View file @
b6a046ab
...
...
@@ -40,13 +40,13 @@ public class LocaleURLFilter implements Filter {
public
static
final
String
REQUEST_LOCALE_ATTR
=
LocaleURLFilter
.
class
.
getName
()
+
".LOCALE"
;
private
static
final
String
REQUEST_INTERNAL_URL
=
LocaleURLFilter
.
class
.
getName
()
+
".INTERNALURL"
;
private
String
[]
allowedLocales
=
null
;
private
Locale
defaultLocale
;
@Override
public
void
init
(
FilterConfig
filterConfig
)
throws
ServletException
{
String
excludePaths
=
filterConfig
.
getInitParameter
(
"exclude-paths"
);
if
(
StringUtils
.
isNotBlank
(
excludePaths
))
{
LOG
.
info
(
"Excluding paths: "
+
excludePaths
);
String
[]
ex
=
excludePaths
.
split
(
"\\s+"
);
for
(
String
e
:
ex
)
{
LOG
.
info
(
"Excluding path: "
+
e
);
...
...
@@ -61,6 +61,15 @@ public class LocaleURLFilter implements Filter {
this
.
defaultLocale
=
Locale
.
getDefault
();
}
LOG
.
info
(
"Using default locale: "
+
this
.
defaultLocale
);
String
allowedLocales
=
filterConfig
.
getInitParameter
(
"allowed-locales"
);
if
(
StringUtils
.
isNotBlank
(
allowedLocales
))
{
String
[]
ex
=
allowedLocales
.
split
(
"\\s+"
);
for
(
String
l
:
ex
)
{
LOG
.
info
(
"Allowed locale: "
+
l
);
}
this
.
allowedLocales
=
ex
;
}
}
@Override
...
...
@@ -90,6 +99,22 @@ public class LocaleURLFilter implements Filter {
String
urlLanguage
=
matcher
.
group
(
1
);
String
remainingUrl
=
matcher
.
group
(
2
);
if
(
this
.
allowedLocales
!=
null
)
{
boolean
localeAllowed
=
false
;
for
(
String
allowedLocale
:
this
.
allowedLocales
)
{
if
(
allowedLocale
.
equalsIgnoreCase
(
urlLanguage
))
{
localeAllowed
=
true
;
break
;
}
}
if
(!
localeAllowed
)
{
LOG
.
warn
(
"Locale not allowed. Temporary redirect to default locale."
);
httpResponse
.
sendRedirect
(
remainingUrl
);
return
;
}
}
Locale
urlLocale
=
Locale
.
forLanguageTag
(
urlLanguage
);
if
(
urlLocale
.
equals
(
this
.
defaultLocale
))
{
...
...
@@ -108,18 +133,20 @@ public class LocaleURLFilter implements Filter {
if
(
LOG
.
isDebugEnabled
())
{
LOG
.
debug
(
"URL matches! lang="
+
urlLanguage
+
" remaining="
+
remainingUrl
);
LOG
.
debug
(
"Country: "
+
urlLocale
.
getCountry
()
+
" Lang: "
+
urlLocale
.
getLanguage
()
+
" locale="
+
urlLocale
);
}
Enumeration
<
String
>
attrNames
=
httpRequest
.
getAttributeNames
();
while
(
attrNames
.
hasMoreElements
())
{
String
attrName
=
attrNames
.
nextElement
();
LOG
.
info
(
"Request attr "
+
attrName
+
" = "
+
httpRequest
.
getAttribute
(
attrName
));
Enumeration
<
String
>
attrNames
=
httpRequest
.
getAttributeNames
();
while
(
attrNames
.
hasMoreElements
())
{
String
attrName
=
attrNames
.
nextElement
();
LOG
.
debug
(
"Request attr "
+
attrName
+
" = "
+
httpRequest
.
getAttribute
(
attrName
));
}
LOG
.
debug
(
"Proxying request to remaining URL "
+
remainingUrl
);
}
LocaleWrappedServletResponse
localeResponse
=
new
LocaleWrappedServletResponse
(
httpResponse
,
localeUrlMatcher
,
urlLanguage
,
defaultLocale
.
toLanguageTag
());
LocaleWrappedServletResponse
localeResponse
=
new
LocaleWrappedServletResponse
(
httpResponse
,
localeUrlMatcher
,
urlLanguage
,
defaultLocale
.
toLanguageTag
());
LocaleWrappedServletRequest
localeRequest
=
new
LocaleWrappedServletRequest
(
httpRequest
,
url
,
remainingUrl
);
LOG
.
info
(
"Proxying request to remaining URL "
+
remainingUrl
);
// request.getRequestDispatcher(remainingUrl == null ? "/" :
// remainingUrl).forward(servletRequest, localeResponse);
filterChain
.
doFilter
(
localeRequest
,
localeResponse
);
...
...
src/main/java/org/genesys2/server/servlet/filter/LocaleURLMatcher.java
View file @
b6a046ab
...
...
@@ -53,10 +53,6 @@ public class LocaleURLMatcher {
}
}
// if (localePattern.matcher(url).matches()) {
// LOG.info("Excluding locale-URL " + url);
// return true;
// }
if
(
fastCheck
(
url
))
{
return
true
;
}
...
...
src/main/java/org/genesys2/server/servlet/filter/LocaleWrappedServletRequest.java
View file @
b6a046ab
...
...
@@ -56,10 +56,4 @@ public class LocaleWrappedServletRequest extends HttpServletRequestWrapper {
return
requestURI
;
}
@Override
public
StringBuffer
getRequestURL
()
{
StringBuffer
requestURL
=
super
.
getRequestURL
();
// LOG.info("requestURL: " + requestURL);
return
requestURL
;
}
}
src/main/java/org/genesys2/server/servlet/filter/LocaleWrappedServletResponse.java
View file @
b6a046ab
...
...
@@ -50,7 +50,9 @@ public class LocaleWrappedServletResponse extends HttpServletResponseWrapper {
return
super
.
encodeURL
(
url
);
}
else
{
String
encodedURL
=
prefix
+
super
.
encodeURL
(
url
);
LOG
.
info
(
"encodeURL "
+
url
+
" to "
+
encodedURL
);
if
(
LOG
.
isDebugEnabled
())
{
LOG
.
debug
(
"encodeURL "
+
url
+
" to "
+
encodedURL
);
}
return
encodedURL
;
}
}
...
...
@@ -62,7 +64,9 @@ public class LocaleWrappedServletResponse extends HttpServletResponseWrapper {
return
super
.
encodeUrl
(
url
);
}
else
{
String
encodedURL
=
prefix
+
super
.
encodeUrl
(
url
);
LOG
.
info
(
"encodeUrl "
+
url
+
" to "
+
encodedURL
);
if
(
LOG
.
isDebugEnabled
())
{
LOG
.
debug
(
"encodeUrl "
+
url
+
" to "
+
encodedURL
);
}
return
encodedURL
;
}
}
...
...
@@ -73,7 +77,9 @@ public class LocaleWrappedServletResponse extends HttpServletResponseWrapper {
return
super
.
encodeRedirectURL
(
url
);
}
else
{
String
encodedURL
=
prefix
+
super
.
encodeRedirectURL
(
url
);
LOG
.
info
(
"encodeRedirectURL "
+
url
+
" to "
+
encodedURL
);
if
(
LOG
.
isDebugEnabled
())
{
LOG
.
debug
(
"encodeRedirectURL "
+
url
+
" to "
+
encodedURL
);
}
return
encodedURL
;
}
}
...
...
@@ -85,7 +91,9 @@ public class LocaleWrappedServletResponse extends HttpServletResponseWrapper {
return
super
.
encodeRedirectUrl
(
url
);
}
else
{
String
encodedURL
=
prefix
+
super
.
encodeRedirectUrl
(
url
);
LOG
.
info
(
"encodeRedirectUrl "
+
url
+
" to "
+
encodedURL
);
if
(
LOG
.
isDebugEnabled
())
{
LOG
.
debug
(
"encodeRedirectUrl "
+
url
+
" to "
+
encodedURL
);
}
return
encodedURL
;
}
}
...
...
src/main/webapp/WEB-INF/web.xml
View file @
b6a046ab
...
...
@@ -75,6 +75,10 @@
<param-name>
default-locale
</param-name>
<param-value>
en
</param-value>
</init-param>
<init-param>
<param-name>
allowed-locales
</param-name>
<param-value>
en es de fr fa ar ru zh pt
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>
localeURLFilter
</filter-name>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment