aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/Constants.java2
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/Empty.java31
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/MiscUtil.java316
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/URLEncoder.java2
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/XPathUtils.java5
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/ex/EgovUtilException.java41
6 files changed, 395 insertions, 2 deletions
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/Constants.java b/common/src/main/java/at/gv/egovernment/moa/util/Constants.java
index 120c222cf..3b6e8c57d 100644
--- a/common/src/main/java/at/gv/egovernment/moa/util/Constants.java
+++ b/common/src/main/java/at/gv/egovernment/moa/util/Constants.java
@@ -265,6 +265,8 @@ public interface Constants {
/** URI of the XAdES v1.1.1 namespace */
public static final String XADES_1_1_1_NS_URI = "http://uri.etsi.org/01903/v1.1.1#";
+ public static final String XADES_1_1_1_NS_PREFIX = "xades";
+
/** Local location of the XAdES v1.2.2 schema definition */
public static final String XADES_1_2_2_SCHEMA_LOCATION =
SCHEMA_ROOT + "XAdES-1.2.2.xsd";
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/Empty.java b/common/src/main/java/at/gv/egovernment/moa/util/Empty.java
new file mode 100644
index 000000000..533b39b6b
--- /dev/null
+++ b/common/src/main/java/at/gv/egovernment/moa/util/Empty.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2011 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.egovernment.moa.util;
+
+/**
+ * @author <a href="mailto:thomas.knall@iaik.tugraz.at">Thomas Knall</a>
+ */
+public interface Empty {
+
+ /**
+ * Returns {@code true} if underlying object is empty.
+ *
+ * @return {@code true} if empty, {@code false} if not empty.
+ */
+ boolean isEmpty();
+
+}
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/MiscUtil.java b/common/src/main/java/at/gv/egovernment/moa/util/MiscUtil.java
new file mode 100644
index 000000000..043baea0e
--- /dev/null
+++ b/common/src/main/java/at/gv/egovernment/moa/util/MiscUtil.java
@@ -0,0 +1,316 @@
+/*
+ * Copyright 2011 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.egovernment.moa.util;
+
+import iaik.logging.Log;
+import iaik.logging.LogFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Iterator;
+import java.util.Properties;
+
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+import at.gv.egovernment.moa.util.ex.EgovUtilException;
+
+
+
+/**
+ * Class providing several utility methods.
+ *
+ * @author <a href="mailto:Arne.Tauber@egiz.gv.at">Arne Tauber</a>
+ *
+ */
+public class MiscUtil {
+
+ public static final String DEFAULT_SLASH = "/";
+
+ public static Log log = LogFactory.getLog(MiscUtil.class);
+
+ private static final int IO_BUFFER_SIZE = 4 * 1024;
+
+ public static void copyStream(InputStream is, OutputStream os) throws IOException {
+ byte[] b = new byte[IO_BUFFER_SIZE];
+ int read;
+ while ((read = is.read(b)) != -1) {
+ os.write(b, 0, read);
+ }
+ }
+
+ public static void assertNotNull(Object param, String name) {
+ if (param == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ }
+
+ public static boolean areAllNull(Object... objects) {
+ for (Object o : objects) {
+ if (o != null) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static String extractContentType(String contentTypeString) {
+ if (contentTypeString == null) {
+ return "";
+ }
+ if (contentTypeString.indexOf(";") != -1) {
+ return contentTypeString.substring(0, contentTypeString.indexOf(";"));
+ }
+ return contentTypeString;
+ }
+
+ public static XMLGregorianCalendar getXMLGregorianCalendar(Date date)
+ throws DatatypeConfigurationException {
+ GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
+ cal.setTime(date);
+ return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
+ }
+
+ public static XMLGregorianCalendar getXMLGregorianCalendar(String str)
+ throws DatatypeConfigurationException {
+ return DatatypeFactory.newInstance().newXMLGregorianCalendar(str);
+ }
+
+ public static X509Certificate readCertificate(InputStream certStream)
+ throws CertificateException {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ return (X509Certificate) cf.generateCertificate(certStream);
+ }
+
+ public static boolean isEmpty(String str) {
+ return str == null || "".equals(str);
+ }
+
+ public static boolean isNotEmpty(String str) {
+ return str != null && !"".equals(str);
+ }
+
+ public static byte[] sourceToByteArray(Source result)
+ throws TransformerException {
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer = factory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ StreamResult streamResult = new StreamResult();
+ streamResult.setOutputStream(out);
+ transformer.transform(result, streamResult);
+ return out.toByteArray();
+ }
+
+ public static Document parseDocument(InputStream inputStream)
+ throws IOException {
+ try {
+ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
+ .newInstance();
+ docBuilderFactory.setNamespaceAware(true);
+ DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
+ return docBuilder.parse(inputStream);
+ } catch (ParserConfigurationException e) {
+ throw new IOException(e);
+ } catch (SAXException e) {
+ throw new IOException(e);
+ }
+ }
+
+ public static String removePrecedingSlash(String path, String slash) {
+ assertNotEmpty(slash, "Shash");
+ if (!isEmpty(path)) {
+ while (path.startsWith(slash)) {
+ path = path.substring(slash.length(), path.length());
+ }
+ }
+ return path;
+ }
+
+ public static String removePrecedingSlash(String path) {
+ return removePrecedingSlash(path, DEFAULT_SLASH);
+ }
+
+ public static void assertNotEmpty(String param, String name) {
+ if (param == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ if (param.length() == 0) {
+ throw new IllegalArgumentException(name + " must not be empty.");
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ public static boolean isEmpty(Properties props) {
+ if (props == null || props.isEmpty()) {
+ return true;
+ }
+ Iterator it = props.values().iterator();
+ while (it.hasNext()) {
+ if (MiscUtil.isNotEmpty((String) it.next())) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static boolean isEmpty(Empty empty) {
+ return empty == null || empty.isEmpty();
+ }
+
+ public static boolean isNotEmpty(Empty empty) {
+ return !isEmpty(empty);
+ }
+
+ public static boolean isEmpty(byte[] data) {
+ return data == null || data.length == 0;
+ }
+
+ public static boolean isNotEmpty(byte[] data) {
+ return !isEmpty(data);
+ }
+
+ public static <T> boolean isEmpty(Collection<T> c) {
+ return c == null || c.isEmpty();
+ }
+
+ public static <T> boolean isNotEmpty(Collection<T> c) {
+ return !isEmpty(c);
+ }
+
+ public static boolean areAllEmpty(String... strings) {
+ for (String s : strings) {
+ if (s != null && s.trim().length() != 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static boolean areAllEmpty(Empty... empties) {
+ if (empties != null) {
+ for (Empty e : empties) {
+ if (e != null && !e.isEmpty()) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ public static <T> void assertNotEmpty(T[] param, String name) {
+ if (param == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ if (param.length == 0) {
+ throw new IllegalArgumentException(name + " must not be empty.");
+ }
+ }
+
+ public static void assertNotEmpty(Empty empty, String name) {
+ if (empty == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ if (empty.isEmpty()) {
+ throw new IllegalArgumentException(name + " must not be empty.");
+ }
+ }
+
+ public static void assertNotEmpty(byte[] param, String name) {
+ if (param == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ if (param.length == 0) {
+ throw new IllegalArgumentException(name + " must not be empty.");
+ }
+ }
+
+ public static Date parseXMLDate(String xmlDate) throws EgovUtilException {
+ if (xmlDate == null) {
+ return null;
+ }
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+ try {
+ return sdf.parse(xmlDate);
+ } catch (ParseException e) {
+ throw new EgovUtilException(e);
+ }
+ }
+
+ public static <T> boolean isEmpty(T[] array) {
+ return array == null || array.length == 0;
+ }
+
+ public static <T> boolean isNotEmpty(T[] array) {
+ return !isEmpty(array);
+ }
+
+ public static String convertDateFromStandardToXML(String dateString) {
+ MiscUtil.assertNotNull(dateString, "dateString");
+ Date date = parseDate(dateString);
+ return formatDate(date, "yyyy-MM-dd");
+ }
+
+ public static Date parseDate(String dateString) {
+ return parseDate(dateString, "dd.MM.yyyy");
+ }
+
+ public static Date parseDate(String dateString, String pattern) {
+ MiscUtil.assertNotNull(dateString, "dateString");
+ MiscUtil.assertNotNull(pattern, "pattern");
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
+ try {
+ return sdf.parse(dateString);
+ } catch (ParseException e) {
+ log.warn(null, "Error parsing date.", e);
+ return null;
+ }
+ }
+
+ public static String formatDate(Date date, String format) {
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
+ return sdf.format(date);
+ }
+
+}
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/URLEncoder.java b/common/src/main/java/at/gv/egovernment/moa/util/URLEncoder.java
index 3255b3157..e6df1fd33 100644
--- a/common/src/main/java/at/gv/egovernment/moa/util/URLEncoder.java
+++ b/common/src/main/java/at/gv/egovernment/moa/util/URLEncoder.java
@@ -44,6 +44,8 @@ public class URLEncoder {
* @throws UnsupportedEncodingException when the desired encoding is not supported
*/
public static String encode(String s, String encoding) throws UnsupportedEncodingException {
+// if (MiscUtil.isEmpty(s))
+// return null;
byte[] barr = s.getBytes(encoding);
ByteArrayInputStream bin = new ByteArrayInputStream(barr);
StringWriter out = new StringWriter();
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/XPathUtils.java b/common/src/main/java/at/gv/egovernment/moa/util/XPathUtils.java
index faa009b0e..21c41257a 100644
--- a/common/src/main/java/at/gv/egovernment/moa/util/XPathUtils.java
+++ b/common/src/main/java/at/gv/egovernment/moa/util/XPathUtils.java
@@ -87,6 +87,7 @@ public class XPathUtils {
ctx.addNamespace(Constants.SAML2_PREFIX, Constants.SAML2_NS_URI);
ctx.addNamespace(Constants.SAML2P_PREFIX, Constants.SAML2P_NS_URI);
ctx.addNamespace(Constants.XENC_PREFIX, Constants.XENC_NS_URI);
+ ctx.addNamespace(Constants.XADES_1_1_1_NS_PREFIX, Constants.XADES_1_1_1_NS_URI);
NS_CONTEXT = ctx;
}
@@ -480,7 +481,7 @@ public class XPathUtils {
else
{
MessageProvider msg = MessageProvider.getInstance();
- String message = msg.getMessage("xpath.00", new Object[] { "Ungültiger Security Layer Namespace: \"" + sLNamespace + "\"."});
+ String message = msg.getMessage("xpath.00", new Object[] { "Ung�ltiger Security Layer Namespace: \"" + sLNamespace + "\"."});
throw new XPathException(message, null);
}
@@ -546,7 +547,7 @@ public class XPathUtils {
return slPrefix;
} else {
MessageProvider msg = MessageProvider.getInstance();
- String message = msg.getMessage("xpath.00", new Object[] { "Ungültiger SecurityLayer Namespace: \"" + nameSpace + "\"."});
+ String message = msg.getMessage("xpath.00", new Object[] { "Ung�ltiger SecurityLayer Namespace: \"" + nameSpace + "\"."});
throw new XPathException(message, null);
}
}
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/ex/EgovUtilException.java b/common/src/main/java/at/gv/egovernment/moa/util/ex/EgovUtilException.java
new file mode 100644
index 000000000..733a2a845
--- /dev/null
+++ b/common/src/main/java/at/gv/egovernment/moa/util/ex/EgovUtilException.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2011 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.egovernment.moa.util.ex;
+
+public class EgovUtilException extends Exception {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ public EgovUtilException() {
+ }
+
+ public EgovUtilException(String message) {
+ super(message);
+ }
+
+ public EgovUtilException(Throwable cause) {
+ super(cause);
+ }
+
+ public EgovUtilException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}