aboutsummaryrefslogtreecommitdiff
path: root/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java')
-rw-r--r--spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java181
1 files changed, 181 insertions, 0 deletions
diff --git a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java
new file mode 100644
index 000000000..743a17c22
--- /dev/null
+++ b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2003 Federal Chancellery Austria
+ * MOA-SPSS 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.spss.api.xmlbind;
+
+import java.text.ParseException;
+import java.util.Date;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import at.gv.egovernment.moa.util.Base64Utils;
+import at.gv.egovernment.moa.util.Constants;
+import at.gv.egovernment.moa.util.DOMUtils;
+import at.gv.egovernment.moa.util.DateTimeUtils;
+import at.gv.egovernment.moa.util.XPathUtils;
+
+import at.gv.egovernment.moa.spss.MOAApplicationException;
+import at.gv.egovernment.moa.spss.api.SPSSFactory;
+import at.gv.egovernment.moa.spss.api.common.Content;
+import at.gv.egovernment.moa.spss.api.common.MetaInfo;
+import at.gv.egovernment.moa.spss.api.common.XMLDataObjectAssociation;
+
+/**
+ * Utility methods for parsing XML requests definied in the MOA XML schema.
+ *
+ * @author Patrick Peck
+ * @version $Id$
+ */
+public class RequestParserUtils {
+ //
+ // XPath expressions for parsing parts of a request
+ //
+ private static final String MOA = Constants.MOA_PREFIX + ":";
+ private static final String REFERENCE_ATTR_NAME = "Reference";
+ private static final String MIME_TYPE_XPATH = MOA + "MimeType";
+ private static final String DESCRIPTION_XPATH = MOA + "Description";
+ private static final String TYPE_XPATH = MOA + "Type";
+ private static final String XML_ASSOC_CONTENT_XPATH = MOA + "Content";
+ private static final String CONTENT_XPATH =
+ MOA + "Base64Content | " + MOA + "XMLContent |" + MOA + "LocRefContent";
+ private static final String ANY_OTHER_XPATH =
+ "*[namespace-uri() != \"" + Constants.MOA_NS_URI + "\"]";
+
+ /**
+ * Parse a <code>XMLDataObjectAssociationType</code> kind of DOM element.
+ *
+ * @param assocElem The <code>XMLDataObjectAssociationType</code> kind of
+ * DOM elmeent to parse.
+ * @return The <code>XMLDataObjectAssociation</code> API object containing
+ * the data from the <code>XMLDataObjectAssociationType</code> DOM element.
+ */
+ public static XMLDataObjectAssociation parseXMLDataObjectAssociation(Element assocElem) {
+ SPSSFactory factory = SPSSFactory.getInstance();
+ MetaInfo metaInfo = parseMetaInfo(assocElem);
+ Element contentElem =
+ (Element) XPathUtils.selectSingleNode(assocElem, XML_ASSOC_CONTENT_XPATH);
+ Content content = parseContent(contentElem);
+
+ return factory.createXMLDataObjectAssociation(metaInfo, content);
+ }
+
+ /**
+ * Parse a <code>MetaInfoType</code> kind of DOM element.
+ *
+ * @param metaInfoElem The <code>MetaInfoType</code> kind of DOM element.
+ * @return The <code>MetaInfo</code> API object containing the data from
+ * the <code>metaInfoElem</code>.
+ */
+ public static MetaInfo parseMetaInfo(Element metaInfoElem) {
+ SPSSFactory factory = SPSSFactory.getInstance();
+ String mimeType =
+ XPathUtils.getElementValue(metaInfoElem, MIME_TYPE_XPATH, null);
+ String description =
+ XPathUtils.getElementValue(metaInfoElem, DESCRIPTION_XPATH, null);
+ NodeList anyOther =
+ XPathUtils.selectNodeList(metaInfoElem, ANY_OTHER_XPATH);
+ String type =
+ XPathUtils.getElementValue(metaInfoElem, TYPE_XPATH, null);
+
+ return factory.createMetaInfo(mimeType, description, anyOther, type);
+ }
+
+ /**
+ * Parse a <code>ContentOptionalRefType</code> or
+ * <code>ContentRequiredRefType</code> kind of DOM element.
+ * @param contentParentElem The DOM element being the parent of the
+ * content element.
+ * @return The <code>Content</code> API object containing the data from
+ * the given DOM element.
+ */
+ public static Content parseContent(Element contentParentElem) {
+ SPSSFactory factory = SPSSFactory.getInstance();
+ String referenceURI =
+ contentParentElem.hasAttribute(REFERENCE_ATTR_NAME)
+ ? contentParentElem.getAttribute(REFERENCE_ATTR_NAME)
+ : null;
+ Element contentElem =
+ (Element) XPathUtils.selectSingleNode(contentParentElem, CONTENT_XPATH);
+
+ if (contentElem == null) {
+ return factory.createContent(referenceURI);
+ }
+
+ if ("Base64Content".equals(contentElem.getLocalName())) {
+ String base64String = DOMUtils.getText(contentElem);
+ return factory.createContent(
+ Base64Utils.decodeToStream(base64String, true),
+ referenceURI);
+ } else if ("LocRefContent".equals(contentElem.getLocalName())) {
+ String locationReferenceURI = DOMUtils.getText(contentElem);
+ return factory.createContent(
+ locationReferenceURI,
+ referenceURI);
+ } else { // "XMLContent".equals(contentElem.getLocalName())
+ return factory.createContent(
+ contentElem.getChildNodes(),
+ referenceURI);
+ }
+ }
+
+ /**
+ * Get the signing time from a Verfiy(CMS|XML)SignatureRequest.
+ *
+ * @param requestElem A <code>Verify(CMS|XML)SignatureRequest</code> DOM
+ * element.
+ * @param dateTimeXPath The XPath to lookup the <code>DateTime</code> element
+ * within the request.
+ * @return Date The date and time corresponding to the <code>DateTime</code>
+ * element in the request. If no <code>DateTime</code> element exists in the
+ * request, <code>null</code> is returned.
+ * @throws MOAApplicationException An error occurred during a parsing the
+ * <code>DateTime</code> element or creating the return value.
+ */
+ public static Date parseDateTime(Element requestElem, String dateTimeXPath)
+ throws MOAApplicationException {
+
+ Element dateTimeElem;
+ String dateTimeText;
+
+ // select the DateTime element
+ dateTimeElem =
+ (Element) XPathUtils.selectSingleNode(requestElem, dateTimeXPath);
+
+ // parse a date from the element value
+ if (dateTimeElem != null) {
+ dateTimeText = DOMUtils.getText(dateTimeElem);
+ try {
+ return DateTimeUtils.parseDateTime(dateTimeText);
+ } catch (ParseException e) {
+ throw new MOAApplicationException(
+ "1104",
+ new Object[] { dateTimeText });
+ }
+ } else {
+ return null;
+ }
+ }
+
+}