aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n
diff options
context:
space:
mode:
Diffstat (limited to 'pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n')
-rw-r--r--pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java125
-rw-r--r--pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java96
2 files changed, 221 insertions, 0 deletions
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java
new file mode 100644
index 0000000..42e6b03
--- /dev/null
+++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java
@@ -0,0 +1,125 @@
+package at.gv.egiz.pdfas.web.i18n;
+
+import java.io.File;
+import java.util.Locale;
+import java.util.Properties;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.apache.log4j.Logger;
+
+import com.opensymphony.module.sitemesh.Config;
+import com.opensymphony.module.sitemesh.Decorator;
+import com.opensymphony.module.sitemesh.DecoratorMapper;
+import com.opensymphony.module.sitemesh.Page;
+import com.opensymphony.module.sitemesh.mapper.AbstractDecoratorMapper;
+import com.opensymphony.module.sitemesh.mapper.DefaultDecorator;
+
+public class LanguageDecoratorMapper extends AbstractDecoratorMapper {
+
+
+ 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);
+ }
+
+ public static Object getFirstNotNull(Object[] objects) {
+ if (objects == null) {
+ return null;
+ }
+ for (int i = 0; i < objects.length; i++) {
+ if (objects[i] != null) {
+ return objects[i];
+ }
+ }
+ 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.trace("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);
+ Locale locale = getLocale(request, this.decoratorParameter);
+
+ String path;
+ if (locale != null) {
+ path = modifyPath(d.getPage(), locale.getLanguage());
+ } else {
+ path = d.getPage();
+ }
+
+ File decFile = new File(config.getServletContext().getRealPath(path));
+
+ LOG.trace("Looking for decorator \"" + path + "\".");
+ if (decFile.isFile()) {
+ result = new DefaultDecorator(d.getName(), path, null) {
+ public String getInitParameter(String paramName) {
+ return d.getInitParameter(paramName);
+ }
+ };
+ }
+
+ if (result != null) {
+ LOG.trace("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.");
+ result = super.getDecorator(request, page);
+ if (result != null) {
+ LOG.trace("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.");
+ }
+ return result;
+ }
+ } catch (NullPointerException e) {
+ return super.getDecorator(request, page);
+ }
+ }
+
+ private static String modifyPath(String path, String ext) {
+ if (ext == null || ext.length() == 0) {
+ return path;
+ }
+ int dot = path.lastIndexOf('.');
+ if (dot > -1) {
+ return path.substring(0, dot) + '-' + ext + path.substring(dot);
+ } else {
+ return path + '-' + ext;
+ }
+ }
+
+}
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java
new file mode 100644
index 0000000..598b23b
--- /dev/null
+++ b/pdf-as-web/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 {
+ }
+
+}