aboutsummaryrefslogtreecommitdiff
path: root/id/server/moa-id-frontend-resources/src/main/java/at/gv/egovernment/moa/id/auth/frontend/builder/GUIFormBuilderImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'id/server/moa-id-frontend-resources/src/main/java/at/gv/egovernment/moa/id/auth/frontend/builder/GUIFormBuilderImpl.java')
-rw-r--r--id/server/moa-id-frontend-resources/src/main/java/at/gv/egovernment/moa/id/auth/frontend/builder/GUIFormBuilderImpl.java221
1 files changed, 221 insertions, 0 deletions
diff --git a/id/server/moa-id-frontend-resources/src/main/java/at/gv/egovernment/moa/id/auth/frontend/builder/GUIFormBuilderImpl.java b/id/server/moa-id-frontend-resources/src/main/java/at/gv/egovernment/moa/id/auth/frontend/builder/GUIFormBuilderImpl.java
new file mode 100644
index 000000000..94fa4d977
--- /dev/null
+++ b/id/server/moa-id-frontend-resources/src/main/java/at/gv/egovernment/moa/id/auth/frontend/builder/GUIFormBuilderImpl.java
@@ -0,0 +1,221 @@
+/*
+ * 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.auth.frontend.builder;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.StringWriter;
+import java.net.URI;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import at.gv.egovernment.moa.id.auth.frontend.exception.GUIBuildException;
+import at.gv.egovernment.moa.id.auth.frontend.velocity.VelocityProvider;
+import at.gv.egovernment.moa.id.commons.api.AuthConfiguration;
+import at.gv.egovernment.moa.logging.Logger;
+import at.gv.egovernment.moa.util.MiscUtil;
+
+/**
+ * @author tlenz
+ *
+ */
+@Service("guiFormBuilder")
+public class GUIFormBuilderImpl implements IGUIFormBuilder {
+
+ private static final String DEFAULT_CONTENT_TYPE = "text/html;charset=UTF-8";
+ private static final String CONFIG_HTMLTEMPLATES_DIR = "htmlTemplates/";
+ private static final String CLASSPATH_HTMLTEMPLATES_DIR = "templates/";
+
+ @Autowired private AuthConfiguration authConfig;
+ private VelocityEngine engine;
+
+ public GUIFormBuilderImpl() throws GUIBuildException {
+ try {
+ engine = VelocityProvider.getClassPathVelocityEngine();
+
+ } catch (Exception e) {
+ Logger.fatal("Initialization of Velocity-Engine to render GUI components FAILED.", e);
+ throw new GUIBuildException("Initialization of Velocity-Engine to render GUI components FAILED.", e);
+
+ }
+
+ }
+
+ public void build(HttpServletResponse httpResp, IGUIBuilderConfiguration config, String loggerName) throws GUIBuildException {
+ build(httpResp, config, getInternalContentType(config), loggerName);
+
+ }
+
+
+ @Override
+ public void build(HttpServletResponse httpResp, IGUIBuilderConfiguration config,
+ String contentType, String loggerName) throws GUIBuildException {
+
+ InputStream is = null;
+ try {
+ String viewName = config.getViewName();
+
+ //load Tempate
+ is = getInternalTemplate(config);
+ if (is == null) {
+ Logger.warn("No GUI with viewName:" + viewName + " FOUND.");
+ throw new GUIBuildException("No GUI with viewName:" + viewName + " FOUND.");
+
+ }
+
+ //build Velocity Context from input paramters
+ VelocityContext context = buildContextFromViewParams(config.getViewParameters());
+
+ //evaluate template
+ StringWriter writer = new StringWriter();
+ engine.evaluate(context, writer, loggerName, new BufferedReader(new InputStreamReader(is)));
+
+ //write template to response
+ httpResp.setStatus(HttpServletResponse.SC_OK);
+ httpResp.setContentType(contentType);
+ httpResp.getOutputStream().write(writer.toString().getBytes("UTF-8"));
+
+ } catch (IOException e) {
+ Logger.error("GUI form-builder has an internal error.", e);
+ throw new GUIBuildException("GUI form-builder has an internal error.", e);
+
+ } finally {
+ if (is != null)
+ try {
+ is.close();
+
+ } catch (IOException e) {
+ Logger.error("Can NOT close GUI-Template InputStream.", e);
+
+ }
+ }
+
+ }
+
+ private String getInternalContentType(IGUIBuilderConfiguration config) {
+ if (MiscUtil.isEmpty(config.getDefaultContentType()))
+ return DEFAULT_CONTENT_TYPE;
+
+ else
+ return config.getDefaultContentType();
+
+ }
+
+ private InputStream getInternalTemplate(IGUIBuilderConfiguration config) throws GUIBuildException {
+ String viewName = config.getViewName();
+
+ //load specific template
+ InputStream is = config.getTemplate(viewName);
+
+ if (is == null) {
+ //load template from default resources
+ try {
+ Logger.trace("Loading GUI template:" + viewName + " from default resources ... ");
+ String pathLocation = null;
+ try {
+ //load template from config directory
+ String rootconfigdir = authConfig.getRootConfigFileDir();
+ pathLocation = rootconfigdir + CONFIG_HTMLTEMPLATES_DIR +
+ viewName;
+ File file = new File(new URI(pathLocation));
+ is = new FileInputStream(file);
+
+ } catch (Exception e) {
+ //load template from classpath as backup
+ Logger.info("GUI template:" + viewName + " is not found in configuration directory. "
+ + " Load template from project library ... ");
+ try {
+ pathLocation = getInternalClasspathTemplateDir(config) + viewName;
+ is = Thread.currentThread()
+ .getContextClassLoader()
+ .getResourceAsStream(pathLocation);
+
+ } catch (Exception e1) {
+ Logger.error("GUI template:" + pathLocation + " is NOT loadable!", e);
+ throw new GUIBuildException("GUI template:" + pathLocation + " is NOT loadable!", e);
+
+ }
+ }
+
+ } catch (GUIBuildException e) {
+ throw e;
+
+ } catch (Exception e) {
+ Logger.error("GUI builder has an internal error during template load operation", e);
+ throw new GUIBuildException("GUI builder has an internal error during template load operation", e);
+
+ }
+ }
+
+ return is;
+
+ }
+
+
+ /**
+ * @return
+ */
+ private String getInternalClasspathTemplateDir(IGUIBuilderConfiguration config) {
+ String dir = config.getClasspathTemplateDir();
+ if (dir != null) {
+ if (!dir.endsWith("/"))
+ dir += "/";
+
+ return dir;
+
+ } else
+ return CLASSPATH_HTMLTEMPLATES_DIR;
+ }
+
+ /**
+ * @param viewParams
+ * @return
+ */
+ private VelocityContext buildContextFromViewParams(Map<String, Object> viewParams) {
+ VelocityContext context = new VelocityContext();
+
+ if (viewParams != null) {
+ Iterator<Entry<String, Object>> interator = viewParams.entrySet().iterator();
+ while (interator.hasNext()) {
+ Entry<String, Object> el = interator.next();
+ context.put(el.getKey(), el.getValue());
+ }
+
+ }
+
+ return context;
+ }
+
+}