From fa22d2f7f43d1faa8c3ce12ada3802e9c55c4d33 Mon Sep 17 00:00:00 2001
From: Thomas Lenz <tlenz@iaik.tugraz.at>
Date: Wed, 22 Jul 2015 07:12:50 +0200
Subject: stor correct BASE64 content into configuration

---
 .../moa/id/config/webgui/helper/GUIDataParser.java | 67 ++++++++++++++++++++++
 .../task/impl/ServicesBKUSelectionTask.java        | 32 ++++++-----
 2 files changed, 84 insertions(+), 15 deletions(-)
 create mode 100644 id/moa-id-webgui/src/main/java/at/gv/egovernment/moa/id/config/webgui/helper/GUIDataParser.java

diff --git a/id/moa-id-webgui/src/main/java/at/gv/egovernment/moa/id/config/webgui/helper/GUIDataParser.java b/id/moa-id-webgui/src/main/java/at/gv/egovernment/moa/id/config/webgui/helper/GUIDataParser.java
new file mode 100644
index 000000000..be47a404f
--- /dev/null
+++ b/id/moa-id-webgui/src/main/java/at/gv/egovernment/moa/id/config/webgui/helper/GUIDataParser.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2014 Federal Chancellery Austria
+ * MOA-ID has been developed in a cooperation between BRZ, the Federal
+ * Chancellery Austria - ICT staff unit, and Graz University of Technology.
+ *
+ * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
+ * the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ * You may obtain a copy of the Licence at:
+ * http://www.osor.eu/eupl/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and
+ * limitations under the Licence.
+ *
+ * This product combines work with different licenses. See the "NOTICE" text
+ * file for details on the various modules and licenses.
+ * The "NOTICE" text file is part of the distribution. Any derivative works
+ * that you distribute must include a readable copy of the "NOTICE" text file.
+ */
+package at.gv.egovernment.moa.id.config.webgui.helper;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import at.gv.egovernment.moa.id.commons.config.MOAIDConfigurationConstants;
+import at.gv.egovernment.moa.util.Base64Utils;
+import at.gv.egovernment.moa.util.MiscUtil;
+
+/**
+ * @author tlenz
+ *
+ */
+public class GUIDataParser {
+	private static final Logger log = LoggerFactory.getLogger(GUIDataParser.class);
+	/**
+	 * Extract the BASE64 content from GUI upload
+	 * 
+	 * @param guiUpload {String}, which is committed by GUI
+	 * @return Base64 encoded data or null if data is not parseable or empty
+	 */
+	public static String getBase64ContentFromGUIUpload(String guiUpload) {
+		if (MiscUtil.isNotEmpty(guiUpload) && 
+				!guiUpload.equals(MOAIDConfigurationConstants.WEBGUI_EMPTY_ELEMENT) ) {
+			String[] bkuSelectTemplateSplitted = guiUpload.split(",");
+			String base64 = null;
+			if (bkuSelectTemplateSplitted.length > 1)
+				base64 = bkuSelectTemplateSplitted[1];
+			else
+				base64 = guiUpload;
+		
+			try {
+				if (Base64Utils.decode(base64, false) == null)
+					return base64;
+				
+			} catch (IOException e) {
+				log.debug("GUI upoad is not decodeable.", e);
+				
+			}			
+		}		
+		return null;
+	}
+}
diff --git a/id/moa-id-webgui/src/main/java/at/gv/egovernment/moa/id/config/webgui/validation/task/impl/ServicesBKUSelectionTask.java b/id/moa-id-webgui/src/main/java/at/gv/egovernment/moa/id/config/webgui/validation/task/impl/ServicesBKUSelectionTask.java
index 7ed9751cb..b6561ccf4 100644
--- a/id/moa-id-webgui/src/main/java/at/gv/egovernment/moa/id/config/webgui/validation/task/impl/ServicesBKUSelectionTask.java
+++ b/id/moa-id-webgui/src/main/java/at/gv/egovernment/moa/id/config/webgui/validation/task/impl/ServicesBKUSelectionTask.java
@@ -29,7 +29,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.regex.Pattern;
 
-import org.apache.commons.codec.binary.Base64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -38,6 +37,7 @@ import at.gv.egovernment.moa.id.commons.config.MOAIDConfigurationConstants;
 import at.gv.egovernment.moa.id.commons.validation.ValidationHelper;
 import at.gv.egovernment.moa.id.config.webgui.exception.ConfigurationTaskValidationException;
 import at.gv.egovernment.moa.id.config.webgui.exception.ValidationObjectIdentifier;
+import at.gv.egovernment.moa.id.config.webgui.helper.GUIDataParser;
 import at.gv.egovernment.moa.id.config.webgui.helper.LanguageHelper;
 import at.gv.egovernment.moa.id.config.webgui.validation.task.AbstractTaskValidator;
 import at.gv.egovernment.moa.id.config.webgui.validation.task.ITaskValidator;
@@ -94,6 +94,18 @@ public class ServicesBKUSelectionTask extends AbstractTaskValidator implements I
 			
 		}
 		
+		String bkuSelectionFileUpload = input.get(MOAIDConfigurationConstants.SERVICE_AUTH_TEMPLATES_BKUSELECTION_DATA);
+		String bkuSelectionFile = GUIDataParser.getBase64ContentFromGUIUpload(bkuSelectionFileUpload);
+		if (bkuSelectionFile != null)
+			newConfigValues.put(MOAIDConfigurationConstants.SERVICE_AUTH_TEMPLATES_BKUSELECTION_DATA, 
+					bkuSelectionFile);
+		
+		String sendAssertionTemplateUpload = input.get(MOAIDConfigurationConstants.SERVICE_AUTH_TEMPLATES_BKUSELECTION_DATA);
+		String sendAssertionTemplate = GUIDataParser.getBase64ContentFromGUIUpload(sendAssertionTemplateUpload);
+		if (sendAssertionTemplate != null)
+			newConfigValues.put(MOAIDConfigurationConstants.SERVICE_AUTH_TEMPLATES_BKUSELECTION_DATA, 
+					sendAssertionTemplate);
+		
 		if (newConfigValues.isEmpty())
 			return null;
 		else
@@ -148,13 +160,8 @@ public class ServicesBKUSelectionTask extends AbstractTaskValidator implements I
 					
 				}
 				
-				String[] bkuSelectTemplateSplitted = bkuSelectionFileUpload.split(",");
-				if (bkuSelectTemplateSplitted.length > 1)
-					bkuSelectTemplate = bkuSelectTemplateSplitted[1];
-				else
-					bkuSelectTemplate = bkuSelectionFileUpload;
-			
-				if (!Base64.isBase64(bkuSelectTemplate)) {
+				bkuSelectTemplate = GUIDataParser.getBase64ContentFromGUIUpload(bkuSelectionFileUpload);			
+				if (bkuSelectTemplate == null) {
 					log.info("BKU Selection Template is not decodeable.");
 					errors.add(new ValidationObjectIdentifier(
 							MOAIDConfigurationConstants.SERVICE_AUTH_TEMPLATES_BKUSELECTION_DATA, 
@@ -202,13 +209,8 @@ public class ServicesBKUSelectionTask extends AbstractTaskValidator implements I
 					
 				}
 				
-				String[] sendAssertionTemplateSplitted = sendAssertionFileUpload.split(",");
-				if (sendAssertionTemplateSplitted.length > 1)
-					sendAssertionTemplate = sendAssertionTemplateSplitted[1];
-				else
-					sendAssertionTemplate = sendAssertionFileUpload;
-			
-				if (!Base64.isBase64(sendAssertionTemplate)) {
+				sendAssertionTemplate = GUIDataParser.getBase64ContentFromGUIUpload(sendAssertionFileUpload);
+				if (sendAssertionTemplate == null) {
 					log.info("Send Assertion Template is not decodeable.");
 					errors.add(new ValidationObjectIdentifier(
 							MOAIDConfigurationConstants.SERVICE_AUTH_TEMPLATES_SENDASSERTION_DATA, 
-- 
cgit v1.2.3