diff options
author | pdanner <pdanner@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c> | 2010-12-06 16:59:57 +0000 |
---|---|---|
committer | pdanner <pdanner@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c> | 2010-12-06 16:59:57 +0000 |
commit | 8662870179eed5ca6e2b7b10fd1912f922fce67b (patch) | |
tree | f6184f2006fd2ccc1995e54d08770eab0c5885c8 /src/main/java/at/gv/egiz/pdfas/web/i18n | |
parent | 12e08e6bc47592045f54e3b46274f0231e88d369 (diff) | |
download | pdf-as-3-8662870179eed5ca6e2b7b10fd1912f922fce67b.tar.gz pdf-as-3-8662870179eed5ca6e2b7b10fd1912f922fce67b.tar.bz2 pdf-as-3-8662870179eed5ca6e2b7b10fd1912f922fce67b.zip |
Moved to pdf-as-web project
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@678 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/web/i18n')
-rw-r--r-- | src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java | 125 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java | 96 |
2 files changed, 0 insertions, 221 deletions
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 deleted file mode 100644 index 42e6b03..0000000 --- a/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java +++ /dev/null @@ -1,125 +0,0 @@ -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/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java b/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java deleted file mode 100644 index 598b23b..0000000 --- a/src/main/java/at/gv/egiz/pdfas/web/i18n/LocaleParamFilter.java +++ /dev/null @@ -1,96 +0,0 @@ -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 {
- }
-
-}
|