From ab7b4927a5790764afa0dc2076d3fad2ac293c61 Mon Sep 17 00:00:00 2001
From: Thomas Lenz <thomas.lenz@egiz.gv.at>
Date: Thu, 6 Dec 2018 15:22:06 +0100
Subject: add 'direct' mode to provide signed PDF documents

---
 .../at/gv/egiz/pdfas/web/helper/PdfAsHelper.java   | 44 ++++++++++++++++++++++
 .../pdfas/web/helper/PdfAsParameterExtractor.java  |  6 +++
 .../egiz/pdfas/web/servlets/ExternSignServlet.java |  4 ++
 .../egiz/pdfas/web/servlets/ProvidePDFServlet.java | 22 ++++++++---
 4 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java
index d327e27d..8f8850ed 100644
--- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java
+++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java
@@ -56,6 +56,7 @@ import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.http.entity.ContentType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -136,6 +137,7 @@ public class PdfAsHelper {
 	private static final String PDF_VER_RESP = "PDF_VER_RESP";
 	private static final String PDF_INVOKE_URL = "PDF_INVOKE_URL";
 	private static final String PDF_INVOKE_TARGET = "PDF_INVOKE_TARGET";
+	private static final String PDF_RESPONSE_MODE = "PDF_RESPONSE_MODE";
 	private static final String REQUEST_FROM_DU = "REQ_DATA_URL";
 	private static final String SIGNATURE_DATA_HASH = "SIGNATURE_DATA_HASH";
 	private static final String SIGNATURE_ACTIVE = "SIGNATURE_ACTIVE";
@@ -143,6 +145,9 @@ public class PdfAsHelper {
 	private static final String QRCODE_CONTENT = "QR_CONT";
 	public static final String PDF_SESSION_PREFIX = "PDF_SESSION_";
 
+	
+	public enum PDF_RESPONSE_MODES {htmlform, direct};
+	
 	private static final Logger logger = LoggerFactory
 			.getLogger(PdfAsHelper.class);
 
@@ -1543,6 +1548,43 @@ public class PdfAsHelper {
 		return obj == null ? null : obj.toString();
 	}
 
+	public static void setResponseMode(HttpServletRequest request, 
+			HttpServletResponse response, String responseMode) {
+		
+		PDF_RESPONSE_MODES mode = PDF_RESPONSE_MODES.htmlform;
+		if (StringUtils.isNotEmpty(responseMode)) {
+			try {
+				mode = PDF_RESPONSE_MODES.valueOf(responseMode);
+				
+			} catch (Exception e) {
+				logger.warn("HTTP parameter 'responsemode' has an unsupported value: " + responseMode 
+						+ ". Use default value: " + mode.toString());
+				
+			}
+		}
+				
+		HttpSession session = request.getSession();				
+		session.setAttribute(PDF_RESPONSE_MODE , mode);
+		logger.debug("External ResponseMode: " + mode.toString());
+		
+	}
+	
+	public static PDF_RESPONSE_MODES getResponseMode(HttpServletRequest request,
+			HttpServletResponse response) {
+		HttpSession session = request.getSession();
+		Object obj = session.getAttribute(PDF_RESPONSE_MODE);
+		
+		if (obj == null) {
+			logger.debug("'responseMode' parameter is 'null'. Use defaultvalue: " + PDF_RESPONSE_MODES.htmlform.toString());
+			return PDF_RESPONSE_MODES.htmlform;
+			
+		} else {
+			logger.debug("'responseMode' parameter is " + ((PDF_RESPONSE_MODES) obj).toString());
+			return (PDF_RESPONSE_MODES) obj;
+		}
+				
+	}
+	
 	private static String generateURL(HttpServletRequest request,
 			HttpServletResponse response, String Servlet) {
 		HttpSession session = request.getSession();
@@ -1852,4 +1894,6 @@ public class PdfAsHelper {
 			
 		}
 	}
+
+
 }
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java
index ee41a8cf..b50b7dbd 100644
--- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java
+++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java
@@ -50,6 +50,8 @@ public class PdfAsParameterExtractor {
 	public static final String PARAM_INVOKE_URL = "invoke-app-url";
 	public static final String PARAM_INVOKE_URL_TARGET = "invoke-app-url-target";
 	public static final String PARAM_INVOKE_URL_ERROR = "invoke-app-error-url";
+	public static final String PARAM_RESPONSE_MODE = "responsemode";
+	
 	
 	public static final String PARAM_VERIFY_LEVEL = "verify-level";
 	public static final String PARAM_VERIFY_LEVEL_OPTION_FULL = "full";
@@ -239,4 +241,8 @@ public class PdfAsParameterExtractor {
 	public static String getSigIdx(HttpServletRequest request) {
 		return (String)request.getAttribute(PARAM_SIG_IDX);
 	}
+
+	public static String getResonseMode(HttpServletRequest request) {
+		return (String)request.getAttribute(PARAM_RESPONSE_MODE);
+	}
 }
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java
index 1d2ab14e..d1345eef 100644
--- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java
+++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java
@@ -336,6 +336,10 @@ public class ExternSignServlet extends HttpServlet {
 		String locale = PdfAsParameterExtractor.getLocale(request);
 		PdfAsHelper.setLocale(request, response, locale);
 
+		String responseMode = PdfAsParameterExtractor.getResonseMode(request);
+		PdfAsHelper.setResponseMode(request, response, responseMode);
+		
+		
 		//read and set placholder web id
 		String placeholder_id = PdfAsParameterExtractor.getPlaceholderId(request);
 		PlaceholderWebConfiguration.setValue(IConfigurationConstants.PLACEHOLDER_WEB_ID, placeholder_id);
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ProvidePDFServlet.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ProvidePDFServlet.java
index 815d532e..7262586d 100644
--- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ProvidePDFServlet.java
+++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ProvidePDFServlet.java
@@ -27,6 +27,7 @@ import java.io.IOException;
 import java.net.URL;
 import java.net.URLEncoder;
 
+import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -90,12 +91,21 @@ public class ProvidePDFServlet extends HttpServlet {
 					logger.warn(invokeURL + " is not allowed by whitelist");
 				}
 				
-				String template = PdfAsHelper.getProvideTemplate();
-				template = template.replace(PDF_DATA_URL, PdfAsHelper.generatePdfURL(request, response));
-				// Deliver to Browser directly!
-				response.setContentType("text/html");
-				response.getWriter().write(template);
-				response.getWriter().close();
+				if (PdfAsHelper.getResponseMode(request, response).equals(PdfAsHelper.PDF_RESPONSE_MODES.htmlform)) {								
+					String template = PdfAsHelper.getProvideTemplate();
+					template = template.replace(PDF_DATA_URL, PdfAsHelper.generatePdfURL(request, response));
+					// Deliver to Browser directly!
+					response.setContentType("text/html");
+					response.getWriter().write(template);
+					response.getWriter().close();
+					
+				} else {
+					logger.debug("PDFResult directMode: Forward to PDFData Servlet directly");
+					RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/PDFData");
+					dispatcher.forward(request, response);
+								
+				}
+					
 			} else {
 				// Redirect Browser
 				String template = PdfAsHelper.getInvokeRedirectTemplateSL();
-- 
cgit v1.2.3