From 5e10b6b575993c78ac22191849c464b618c4fa8a Mon Sep 17 00:00:00 2001 From: mcentner Date: Wed, 14 Jul 2010 15:22:56 +0000 Subject: MOCCA Online HTTP binding refactored. git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@762 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../egiz/bku/online/webapp/MoccaParameterBean.java | 210 +++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 BKUOnline/src/main/java/at/gv/egiz/bku/online/webapp/MoccaParameterBean.java (limited to 'BKUOnline/src/main/java/at/gv/egiz/bku/online/webapp/MoccaParameterBean.java') diff --git a/BKUOnline/src/main/java/at/gv/egiz/bku/online/webapp/MoccaParameterBean.java b/BKUOnline/src/main/java/at/gv/egiz/bku/online/webapp/MoccaParameterBean.java new file mode 100644 index 00000000..0d2f847e --- /dev/null +++ b/BKUOnline/src/main/java/at/gv/egiz/bku/online/webapp/MoccaParameterBean.java @@ -0,0 +1,210 @@ +/* + * Copyright 2009 Federal Chancellery Austria and + * Graz University of Technology + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package at.gv.egiz.bku.online.webapp; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.Locale; +import java.util.regex.Pattern; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.bku.binding.HTTPBindingProcessor; +import at.gv.egiz.bku.utils.StreamUtil; + +public class MoccaParameterBean { + + private final Logger log = LoggerFactory.getLogger(MoccaParameterBean.class); + + public static final String PARAM_UI_PAGE_P = "appletPage"; + + public static final String PARAM_APPLET_WIDTH = "appletWidth"; + + public static final String PARAM_APPLET_HEIGHT = "appletHeight"; + + public static final String PARAM_APPLET_BACKGROUND = "appletBackground"; + + public static final String PARAM_APPLET_BACKGROUND_COLOR = "appletBackgroundColor"; + public static final Pattern PATTERM_APPLET_BACKGROUND_COLOR = Pattern.compile("\\#[0-9a-fA-F]{6}"); + + public static final String PARAM_APPLET_GUI_STYLE = "appletGuiStyle"; + public static final String[] VALUES_APPLET_GUI_STYLE = new String[] {"tiny", "simple", "advanced"}; + + public static final String PARAM_APPLET_EXTENSION = "appletExtension"; + public static final String[] VALUES_APPLET_EXTENSION = new String[] {"pin", "activation"}; + + public static final String PARAM_LOCALE = "locale"; + public static final Pattern PATTERN_LOCALE = Pattern.compile("[a-zA-Z][a-zA-Z](_[a-zA-Z][a-zA-Z]){0,2}"); + + private Charset charset = Charset.forName("ISO-8859-1"); + + private HTTPBindingProcessor bindingProcessor; + + public MoccaParameterBean(HTTPBindingProcessor bindingProcessor) { + this.bindingProcessor = bindingProcessor; + } + + public Charset getCharset() { + return charset; + } + + public void setCharset(Charset charset) { + this.charset = (charset != null) ? Charset.forName("ISO-8859-1") : charset; + } + + public String getUIPage(String defaultValue) { + String uiPage = getString(PARAM_UI_PAGE_P); + return (uiPage != null) ? uiPage : defaultValue; + } + + public Integer getAppletWidth() { + return getInteger(PARAM_APPLET_WIDTH); + } + + public Integer getAppletHeight() { + return getInteger(PARAM_APPLET_HEIGHT); + } + + public String getAppletBackground() { + String background = getString(PARAM_APPLET_BACKGROUND); + if (background != null && !background.isEmpty()) { + try { + // must be a valid http or https URL + URI backgroundURL = new URI(background); + if ("http".equals(backgroundURL.getScheme()) + || "https".equals(backgroundURL.getScheme())) { + return background.toString(); + } else { + log.warn("Parameter " + PARAM_APPLET_BACKGROUND + + "='{}' is not a valid http/https URL.", background); + } + } catch (URISyntaxException e) { + log.warn("Parameter " + PARAM_APPLET_BACKGROUND + + "='{}' is not a valid http/https URL.", background, e); + } + } + return null; + } + + public String getAppletBackgroundColor() { + String backgroundColor = getString(PARAM_APPLET_BACKGROUND_COLOR); + if (backgroundColor != null && !backgroundColor.isEmpty()) { + // must be a valid color definition + if (PATTERM_APPLET_BACKGROUND_COLOR.matcher(backgroundColor).matches()) { + return backgroundColor; + } else { + log.warn("Parameter " + PARAM_APPLET_BACKGROUND_COLOR + + "='{}' is not a valid color definition " + + "(must be of form '#hhhhhh').", backgroundColor); + } + } + return null; + } + + public String getGuiStyle() { + String guiStyle = getString(PARAM_APPLET_GUI_STYLE); + if (guiStyle != null && !guiStyle.isEmpty()) { + // must be one of VALUES_APPLET_GUI_STYLE + String style = guiStyle.toLowerCase(); + if (Arrays.asList(VALUES_APPLET_GUI_STYLE).contains(style)) { + return style; + } else { + StringBuilder sb = new StringBuilder(); + sb.append("Parameter ").append(PARAM_APPLET_GUI_STYLE).append( + "='").append(guiStyle).append("' is not valid (must be one of ") + .append(Arrays.toString(VALUES_APPLET_GUI_STYLE)).append(")."); + log.warn(sb.toString()); + } + } + return null; + } + + public String getExtension() { + String extension = getString(PARAM_APPLET_EXTENSION); + if (extension != null && !extension.isEmpty()) { + // must be one of VALUES_APPLET_EXTENSION + String ext = extension.toLowerCase(); + if (Arrays.asList(VALUES_APPLET_EXTENSION).contains(ext)) { + log.debug("Found parameter " + PARAM_APPLET_EXTENSION + "='{}'.", ext); + return ext; + } else { + StringBuilder sb = new StringBuilder(); + sb.append("Parameter ").append(PARAM_APPLET_EXTENSION).append( + "='").append(extension).append("' is not valid (must be one of ") + .append(Arrays.toString(VALUES_APPLET_EXTENSION)).append(")."); + log.warn(sb.toString()); + } + } + return null; + } + + public Locale getLocale() { + String locale = getString(PARAM_LOCALE); + if (locale != null && !locale.isEmpty()) { + // must be a valid locale + if (PATTERN_LOCALE.matcher(locale).matches()) { + log.debug("Found parameter " + PARAM_LOCALE + "='{}'.", locale); + return new Locale(locale); + } else { + log.warn("Parameter " + PARAM_LOCALE + + "='{}' is not a valid locale definition.", locale); + } + } + return null; + } + + public Integer getInteger(String parameterName) { + String string = getString(parameterName); + if (string == null || string.isEmpty()) { + return null; + } else { + try { + return Integer.parseInt(string); + } catch (NumberFormatException e) { + log.warn("Parameter {} does not contain a valid value.", parameterName, + e); + return null; + } + } + } + + public String getString(String parameterName) { + if (bindingProcessor != null) { + InputStream formData = bindingProcessor.getFormData(parameterName); + if (formData != null) { + InputStreamReader reader = new InputStreamReader(formData, charset); + StringWriter writer = new StringWriter(); + try { + StreamUtil.copyStream(reader, writer); + } catch (IOException e) { + log.warn("Failed to get parameter {}.", parameterName, e); + } + return writer.toString(); + } + } + return null; + } + +} -- cgit v1.2.3