From ea60aae139c666c610c1af5cfd0b616d3f4a535c Mon Sep 17 00:00:00 2001
From: tknall
Date: Tue, 25 Nov 2008 12:48:15 +0000
Subject: Preparation for multilingual support for web application.
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@312 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
---
.../at/gv/egiz/pdfas/api/commons/Constants.java | 32 +++---
.../at/gv/egiz/pdfas/exceptions/ErrorCode.java | 1 +
.../pdfas/web/i18n/LanguageDecoratorMapper.java | 111 +++++++++++++++++++++
.../gv/egiz/pdfas/web/i18n/SetLanguageServlet.java | 59 +++++++++++
4 files changed, 190 insertions(+), 13 deletions(-)
create mode 100644 src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java
create mode 100644 src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java
(limited to 'src/main/java/at/gv')
diff --git a/src/main/java/at/gv/egiz/pdfas/api/commons/Constants.java b/src/main/java/at/gv/egiz/pdfas/api/commons/Constants.java
index a7bc776..19f7613 100644
--- a/src/main/java/at/gv/egiz/pdfas/api/commons/Constants.java
+++ b/src/main/java/at/gv/egiz/pdfas/api/commons/Constants.java
@@ -21,13 +21,15 @@ public final class Constants
/**
* A binary signature.
+ * This value should not be modified due to external dependencies!
*/
- public static String SIGNATURE_TYPE_BINARY = "binary";
+ public static final String SIGNATURE_TYPE_BINARY = "binary";
/**
* A textual signature.
+ * This value should not be modified due to external dependencies!
*/
- public static String SIGNATURE_TYPE_TEXTUAL = "textual";
+ public static final String SIGNATURE_TYPE_TEXTUAL = "textual";
/**
* A "detached" textual signature.
@@ -37,32 +39,36 @@ public final class Constants
* the sign result XML of the connector is returned.
*
*/
- public static String SIGNATURE_TYPE_DETACHEDTEXTUAL = "detachedtextual";
+ public static final String SIGNATURE_TYPE_DETACHEDTEXTUAL = "detachedtextual";
/**
* The signature device moa.
+ * This value should not be modified due to external dependencies!
*/
- public static String SIGNATURE_DEVICE_MOA = "moa";
+ public static final String SIGNATURE_DEVICE_MOA = "moa";
/**
* The signature device bku.
+ * This value should not be modified due to external dependencies!
*/
- public static String SIGNATURE_DEVICE_BKU = "bku";
+ public static final String SIGNATURE_DEVICE_BKU = "bku";
/**
* The signature device a1.
+ * This value should not be modified due to external dependencies!
*/
- public static String SIGNATURE_DEVICE_A1 = "a1";
+ public static final String SIGNATURE_DEVICE_A1 = "a1";
/**
* The signature device MOCCA (online bku).
+ * This value should not be modified due to external dependencies!
*/
public static final String SIGNATURE_DEVICE_MOC = "moc";
/**
* Only binary signatures are verified.
*/
- public static String VERIFY_MODE_BINARY_ONLY = "binaryOnly";
+ public static final String VERIFY_MODE_BINARY_ONLY = "binaryOnly";
/**
* Binary and textual signatures are verified with time optimization.
@@ -73,27 +79,27 @@ public final class Constants
* signatures may not be found.
*
*/
- public static String VERIFY_MODE_SEMI_CONSERVATIVE = "semiConservative";
+ public static final String VERIFY_MODE_SEMI_CONSERVATIVE = "semiConservative";
/**
* Binary and textual signatures are verified.
*/
- public static String VERIFY_MODE_FULL_CONSERVATIVE = "fullConservative";
+ public static final String VERIFY_MODE_FULL_CONSERVATIVE = "fullConservative";
/**
* All signatures are verified.
*/
- public static int VERIFY_ALL = -1;
+ public static final int VERIFY_ALL = -1;
/**
* The system property that may be used to declare the pdf-as configuration folder.
*/
- public final static String CONFIG_DIR_SYSTEM_PROPERTY = "pdf-as.work-dir";
+ public static final String CONFIG_DIR_SYSTEM_PROPERTY = "pdf-as.work-dir";
/**
* The zip file containing the default configuration.
*/
- public final static String DEFAULT_CONFIGURATION_ZIP_RESOURCE = "DefaultConfiguration.zip";
+ public static final String DEFAULT_CONFIGURATION_ZIP_RESOURCE = "DefaultConfiguration.zip";
/**
* The configuration folder for pdf-as within the user's home folder.
@@ -103,7 +109,7 @@ public final class Constants
/**
* The name of the directory, where temporary files are stored.
*/
- public final static String TEMP_DIR_NAME = "pdfastmp";
+ public static final String TEMP_DIR_NAME = "pdfastmp";
}
diff --git a/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java b/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java
index 062ff6b..ab8914c 100644
--- a/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java
+++ b/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java
@@ -25,6 +25,7 @@ public final class ErrorCode
public static final int CANNOT_WRITE_PDF = 205;
public static final int DOCUMENT_NOT_SIGNED = 206;
public static final int SIGNATURE_TYPES_EXCEPTION = 223;
+ public static final int FONT_NOT_FOUND = 230;
public static final int SIGNATURE_COULDNT_BE_CREATED = 300;
public static final int SIGNED_TEXT_EMPTY = 301;
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
new file mode 100644
index 0000000..1db7e3b
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java
@@ -0,0 +1,111 @@
+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 {
+
+ public static final String LOCALE_SESSION_KEY = "at.gv.egiz.pdfas.web.i18n:currentLocale";
+
+ private 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 Decorator getDecorator(HttpServletRequest request, Page page) {
+ 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);
+ }
+ String path;
+ if (locale != null) {
+ path = modifyPath(d.getPage(), locale.getLanguage());
+ } else {
+ path = d.getPage();
+ }
+
+ File decFile = new File(config.getServletContext().getRealPath(path));
+
+ log.debug("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.debug("Decorator found (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), ""}) + "\").");
+ return result;
+ } else {
+ 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(), ""}) + "\").");
+ } 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/SetLanguageServlet.java b/src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java
new file mode 100644
index 0000000..582f765
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java
@@ -0,0 +1,59 @@
+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);
+
+ }
+
+
+
+}
--
cgit v1.2.3