aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/pdfas/web
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/web')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/web/filter/EncodingFilter.java123
-rw-r--r--src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java64
-rw-r--r--src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java96
-rw-r--r--src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java59
4 files changed, 258 insertions, 84 deletions
diff --git a/src/main/java/at/gv/egiz/pdfas/web/filter/EncodingFilter.java b/src/main/java/at/gv/egiz/pdfas/web/filter/EncodingFilter.java
new file mode 100644
index 0000000..5267918
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/web/filter/EncodingFilter.java
@@ -0,0 +1,123 @@
+package at.gv.egiz.pdfas.web.filter;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.log4j.Logger;
+
+/**
+ * @author <a href="mailto:thomas.knall@iaik.tugraz.at">Thomas Knall</a>
+ */
+public class EncodingFilter implements javax.servlet.Filter {
+
+ private static final String SERVLET_INIT_PARAM_ENCODING = "encoding";
+
+ private static final String SERVLET_INIT_PARAM_SET_REQUEST_ENCODING = "setRequestEncoding";
+ private static final String SERVLET_INIT_PARAM_FORCE_REQUEST_ENCODING = "forceRequestEncoding";
+
+ private static final String SERVLET_INIT_PARAM_SET_RESPONSE_ENCODING = "setResponseEncoding";
+ private static final String SERVLET_INIT_PARAM_FORCE_RESPONSE_ENCODING = "forceResponseEncoding";
+
+ private static final boolean DEFAULT_SET_REQUEST_ENCODING_VALUE = true;
+ private static final boolean DEFAULT_FORCE_REQUEST_ENCODING_VALUE = true;
+ private static final boolean DEFAULT_SET_RESPONSE_ENCODING_VALUE = false;
+ private static final boolean DEFAULT_FORCE_RESPONSE_ENCODING_VALUE = false;
+
+ private Logger log = Logger.getLogger(getClass().getName());
+
+ private String encoding = null;
+
+ private boolean setRequestEncoding;
+ private boolean forceRequestEncoding;
+
+ private boolean setResponseEncoding;
+ private boolean forceResponseEncoding;
+
+ private boolean enabled = false;
+
+ private boolean parseBooleanInitParameter(final FilterConfig filterConfig, String parameterName, boolean defaultValue) {
+ String paramValue = filterConfig.getInitParameter(parameterName);
+ if (paramValue == null) {
+ return defaultValue;
+ }
+ paramValue = paramValue.trim();
+ if (paramValue.equalsIgnoreCase("true")) {
+ return true;
+ } else if (paramValue.equalsIgnoreCase("false")){
+ return false;
+ } else {
+ log.warn("Unknown value \"" + paramValue + "\" for init parameter \"" + parameterName + "\" detected. Should be \"true\" or \"false\". Using default value \"" + defaultValue + "\".");
+ return defaultValue;
+ }
+ }
+
+ public void init(final FilterConfig filterConfig) throws ServletException {
+ log.debug("Initializing encoding filter (" + getClass().getName() + ").");
+
+ // mandatory parameter encoding
+ String desiredEncoding = filterConfig.getInitParameter(SERVLET_INIT_PARAM_ENCODING);
+ if (StringUtils.isEmpty(desiredEncoding)) {
+ log.warn("Unable to initialize encoding filter (" + getClass().getName() + "). Init parameter \"" + SERVLET_INIT_PARAM_ENCODING + "\" empty or not supplied.");
+ } else if (!Charset.isSupported(desiredEncoding)) {
+ log.warn("Unable to initialize encoding filter (" + getClass().getName() + "). Encoding \"" + desiredEncoding + "\" is not supported.");
+ } else {
+ this.encoding = desiredEncoding;
+ this.enabled = true;
+ this.setRequestEncoding = this.parseBooleanInitParameter(filterConfig, SERVLET_INIT_PARAM_SET_REQUEST_ENCODING, DEFAULT_SET_REQUEST_ENCODING_VALUE);
+ this.forceRequestEncoding = this.parseBooleanInitParameter(filterConfig, SERVLET_INIT_PARAM_FORCE_REQUEST_ENCODING, DEFAULT_FORCE_REQUEST_ENCODING_VALUE);
+ this.setResponseEncoding = this.parseBooleanInitParameter(filterConfig, SERVLET_INIT_PARAM_SET_RESPONSE_ENCODING, DEFAULT_SET_RESPONSE_ENCODING_VALUE);
+ this.forceResponseEncoding = this.parseBooleanInitParameter(filterConfig, SERVLET_INIT_PARAM_FORCE_RESPONSE_ENCODING, DEFAULT_FORCE_RESPONSE_ENCODING_VALUE);
+ log.debug("Encoding filter \"" + getClass().getName() + "\" configured: " + this.toString(true));
+
+ }
+ }
+
+ public String toString(boolean verbose) {
+ if (verbose) {
+ return new ToStringBuilder(this)
+ .append(SERVLET_INIT_PARAM_ENCODING, this.encoding)
+ .append(SERVLET_INIT_PARAM_SET_REQUEST_ENCODING, this.setRequestEncoding)
+ .append(SERVLET_INIT_PARAM_FORCE_REQUEST_ENCODING, this.forceRequestEncoding)
+ .append(SERVLET_INIT_PARAM_SET_RESPONSE_ENCODING, this.setResponseEncoding)
+ .append(SERVLET_INIT_PARAM_FORCE_RESPONSE_ENCODING, this.forceResponseEncoding)
+ .toString();
+ } else {
+ return super.toString();
+ }
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response, final FilterChain filterChain) throws IOException, ServletException {
+ if (this.enabled) {
+ if (this.setRequestEncoding) {
+ if (this.forceRequestEncoding) {
+ log.trace("Forcing request encoding \"" + this.encoding + "\".");
+ request.setCharacterEncoding(this.encoding);
+ } else if (request.getCharacterEncoding() == null) {
+ log.trace("Request character encoding not set. Setting to \"" + this.encoding + "\".");
+ request.setCharacterEncoding("UTF8");
+ }
+ }
+ if (this.setResponseEncoding) {
+ if (this.forceResponseEncoding) {
+ log.trace("Forcing response encoding \"" + this.encoding + "\".");
+ response.setCharacterEncoding(this.encoding);
+ } else if (response.getCharacterEncoding() == null) {
+ log.trace("Response character encoding not set. Setting to \"" + this.encoding + "\".");
+ response.setCharacterEncoding("UTF8");
+ }
+ }
+ }
+ filterChain.doFilter(request, response);
+ }
+
+ public void destroy() {
+ }
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java b/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java
index 1db7e3b..c610e42 100644
--- a/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java
+++ b/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java
@@ -18,9 +18,10 @@ import com.opensymphony.module.sitemesh.mapper.DefaultDecorator;
public class LanguageDecoratorMapper extends AbstractDecoratorMapper {
- public static final String LOCALE_SESSION_KEY = "at.gv.egiz.pdfas.web.i18n:currentLocale";
- private Logger log = Logger.getLogger(LanguageDecoratorMapper.class);
+ private String decoratorParameter = null;
+
+ private static final Logger LOG = Logger.getLogger(LanguageDecoratorMapper.class);
public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
super.init(config, properties, parent);
@@ -37,29 +38,42 @@ public class LanguageDecoratorMapper extends AbstractDecoratorMapper {
}
return null;
}
+
+ public static Locale getLocale(HttpServletRequest request, String decoratorParameter) {
+ Locale locale = null;
+ HttpSession session = request.getSession();
+
+ LOG.trace("Looking for locale in session (ID=" + session.getId() + ").");
+ locale = (Locale) session.getAttribute(LocaleParamFilter.LOCALE_SESSION_KEY);
+ if (locale == null) {
+ LOG.debug("Unable to find locale in session. Trying to create new locale based on Accept-Language header entry.");
+ String acceptLanguage = request.getHeader("Accept-Language");
+ if (acceptLanguage != null) {
+ LOG.trace("Accept-Language header entry: " + acceptLanguage);
+ } else {
+ LOG.trace("No header entry found.");
+ }
+ if (acceptLanguage != null && acceptLanguage.length() >= 2) {
+ locale = new Locale(acceptLanguage.substring(0, 2));
+ LOG.debug("New Locale created: " + locale);
+ LOG.trace("Setting language to " + locale.getDisplayLanguage() + ".");
+ session.setAttribute(LocaleParamFilter.LOCALE_SESSION_KEY, locale);
+ }
+ } else {
+ LOG.debug("Locale found: " + locale);
+ }
+
+ return locale;
+ }
public Decorator getDecorator(HttpServletRequest request, Page page) {
+ LOG.trace("SiteMesh language resource decorator mapper invoked.");
+
try {
Decorator result = null;
final Decorator d = super.getDecorator(request, page);
- HttpSession session = request.getSession();
- log.trace("Looking for Locale in session.");
- Locale locale = (Locale) session.getAttribute(LOCALE_SESSION_KEY);
- if (locale == null) {
- log.debug("Unable to find Locale in session. Creating new Locale based on Accept-Language header entry.");
- String acceptLanguage = request.getHeader("Accept-Language");
- if (acceptLanguage != null) {
- log.trace("Accept-Language header entry: " + acceptLanguage);
- } else {
- log.trace("No header entry found.");
- }
- if (acceptLanguage != null && acceptLanguage.length() >= 2) {
- locale = new Locale(acceptLanguage.substring(0, 2));
- log.debug("New Locale created: " + locale);
- }
- } else {
- log.debug("Locale found: " + locale);
- }
+ Locale locale = getLocale(request, this.decoratorParameter);
+
String path;
if (locale != null) {
path = modifyPath(d.getPage(), locale.getLanguage());
@@ -69,7 +83,7 @@ public class LanguageDecoratorMapper extends AbstractDecoratorMapper {
File decFile = new File(config.getServletContext().getRealPath(path));
- log.debug("Looking for decorator \"" + path + "\".");
+ LOG.debug("Looking for decorator \"" + path + "\".");
if (decFile.isFile()) {
result = new DefaultDecorator(d.getName(), path, null) {
public String getInitParameter(String paramName) {
@@ -79,15 +93,15 @@ public class LanguageDecoratorMapper extends AbstractDecoratorMapper {
}
if (result != null) {
- log.debug("Decorator found (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), "<n/a>"}) + "\").");
+ LOG.debug("Decorator found (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), "<n/a>"}) + "\").");
return result;
} else {
- log.trace("No decorator found. Delegating to super class.");
+ LOG.trace("No decorator found. Delegating to super class.");
result = super.getDecorator(request, page);
if (result != null) {
- log.debug("Super class returned decorator (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), "<n/a>"}) + "\").");
+ LOG.debug("Super class returned decorator (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), "<n/a>"}) + "\").");
} else {
- log.trace("Super class did not return a decorator.");
+ LOG.trace("Super class did not return a decorator.");
}
return result;
}
diff --git a/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java b/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java
new file mode 100644
index 0000000..598b23b
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java
@@ -0,0 +1,96 @@
+package at.gv.egiz.pdfas.web.i18n;
+
+import java.io.IOException;
+import java.util.Locale;
+import java.util.StringTokenizer;
+
+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.HttpSession;
+
+import org.apache.commons.lang.LocaleUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+
+public class LocaleParamFilter implements Filter {
+
+ private static final Logger LOG = Logger.getLogger(LocaleParamFilter.class);
+ public static final String LOCALE_SESSION_KEY = "at.gv.egiz.pdfas.web.i18n:currentLocale";
+ public static final String LOCALE_PARAM_KEY = "locale";
+
+ public void destroy() {
+ }
+
+ public static String normalizeLocaleString(String localeString) {
+ if (localeString == null) {
+ return null;
+ }
+ int jsessionIdIndex = localeString.toLowerCase().indexOf(";jsessionid");
+ if (jsessionIdIndex != -1) {
+ localeString = localeString.substring(0, jsessionIdIndex);
+ }
+ StringTokenizer tokenizer = new StringTokenizer(localeString, "_", false);
+ StringBuffer buffer = new StringBuffer();
+ int index = 0;
+ while (tokenizer.hasMoreTokens()) {
+ String token = tokenizer.nextToken();
+ switch (++index) {
+ case 1:
+ buffer.append(token.toLowerCase());
+ break;
+ case 2:
+ buffer.append(token.toUpperCase());
+ break;
+ default:
+ buffer.append(token);
+ break;
+ }
+ if (tokenizer.hasMoreTokens()) {
+ buffer.append("_");
+ }
+ }
+ return buffer.toString();
+ }
+
+ public static Locale getLocale(HttpServletRequest request) {
+ Locale locale = null;
+ HttpSession session = request.getSession();
+
+ LOG.trace("Looking for locale parameter \"" + LOCALE_PARAM_KEY + "\".");
+ String language = request.getParameter(LOCALE_PARAM_KEY);
+ if (!StringUtils.isEmpty(language)) {
+ LOG.debug("Locale parameter \"" + language + "\" found.");
+ String code = normalizeLocaleString(language.trim());
+ LOG.debug("Normalizing locale -> " + code);
+ try {
+ locale = LocaleUtils.toLocale(code);
+ LOG.info("Setting locale flag in session (ID=" + session.getId() + ") to \"" + locale.toString() + "\".");
+ session.setAttribute(LOCALE_SESSION_KEY, locale);
+ } catch (IllegalArgumentException e) {
+ LOG.error("Locale \"" + code + "\" is not valid. Flag will not be set.");
+ }
+ } else {
+ LOG.trace("No locale parameter found.");
+ }
+
+ return locale;
+ }
+
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
+ HttpServletRequest request = (HttpServletRequest) servletRequest;
+
+ LOG.trace("LocaleParamFilter invoked.");
+ getLocale(request);
+
+ filterChain.doFilter(servletRequest, servletResponse);
+ }
+
+ public void init(FilterConfig filterConfig) throws ServletException {
+ }
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java b/src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java
deleted file mode 100644
index 582f765..0000000
--- a/src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java
+++ /dev/null
@@ -1,59 +0,0 @@
-package at.gv.egiz.pdfas.web.i18n;
-
-import java.io.IOException;
-import java.util.Locale;
-
-import javax.servlet.RequestDispatcher;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-
-import org.apache.commons.lang.ArrayUtils;
-import org.apache.commons.lang.StringUtils;
-import org.apache.log4j.Logger;
-
-public class SetLanguageServlet extends HttpServlet {
-
- private static final long serialVersionUID = 1L;
-
- public static final String LANGUAGE_PARAMETER = "code";
- private Logger log = Logger.getLogger(LanguageDecoratorMapper.class);
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doPost(request, response);
- }
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- HttpSession session = request.getSession();
-
- // check manual language override (should be two-letter ISO-639 code)
- Locale currentManualSetLanguage = (Locale) session.getAttribute(LanguageDecoratorMapper.LOCALE_SESSION_KEY);
- String language = request.getParameter(LANGUAGE_PARAMETER);
- if (!StringUtils.isEmpty(language)) {
- String code = language.trim().toLowerCase();
- if (!ArrayUtils.contains(Locale.getISOLanguages(), code)) {
- log.error("Manually set language code \"" + code + "\" is not a valid ISO 639 language. Do not switch language.");
- } else {
- currentManualSetLanguage = new Locale(code);
- log.info("Switching language to " + currentManualSetLanguage.getDisplayLanguage() + ".");
- session.setAttribute(LanguageDecoratorMapper.LOCALE_SESSION_KEY, currentManualSetLanguage);
- }
- }
-
-// if (currentManualSetLanguage != null) {
-// log.info("Forcing language " + currentManualSetLanguage.getDisplayLanguage() + ".");
-// super.getServletContext().setAttribute(LanguageDecoratorMapper.LOCALE_SESSION_KEY, currentManualSetLanguage);
-// }
-
-// response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/"));
- RequestDispatcher rd = request.getRequestDispatcher("/");
- rd.forward(request, response);
-
- }
-
-
-
-}