Commit b6a046ab authored by Matija Obreza's avatar Matija Obreza
Browse files

Allowed locales, reduced log levels

parent f886a42d
......@@ -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);
......
......@@ -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;
}
......
......@@ -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;
}
}
......@@ -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;
}
}
......
......@@ -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>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment