summaryrefslogtreecommitdiff
path: root/bkucommon
diff options
context:
space:
mode:
Diffstat (limited to 'bkucommon')
-rw-r--r--bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessor.java1
-rw-r--r--bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/DataObjectHashDataInput.java6
-rw-r--r--bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/xsect/DataObject.java88
-rw-r--r--bkucommon/src/test/java/at/gv/egiz/bku/slcommands/impl/xsect/SignatureTest.java50
-rw-r--r--bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_Detached_LocRefContent.xml13
-rw-r--r--bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_LocRefContent_2.xml2
6 files changed, 150 insertions, 10 deletions
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessor.java b/bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessor.java
index b1906666..e39addb5 100644
--- a/bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessor.java
+++ b/bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessor.java
@@ -339,6 +339,7 @@ public class HTTPBindingProcessor extends AbstractBindingProcessor implements
// process headers and request
setHTTPHeaders(dataUrlResponse.getResponseHeaders());
consumeRequestStream(dataUrlResponse.getStream());
+ //TODO check for bindingProcessorError
closeDataUrlConnection();
srcContex.setSourceCertificate(conn.getServerCertificate());
srcContex.setSourceIsDataURL(true);
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/DataObjectHashDataInput.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/DataObjectHashDataInput.java
index 1a9b56fb..57358ba0 100644
--- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/DataObjectHashDataInput.java
+++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/DataObjectHashDataInput.java
@@ -50,4 +50,10 @@ public class DataObjectHashDataInput implements HashDataInput {
return HttpUtil.getCharset(dataObject.getMimeType(), false);
}
+ @Override
+ public String getFilename() {
+ //TODO obtain filename from dataObject, if not set return null or get filename (extension!) from mimetype
+ return dataObject.getFilename();
+ }
+
}
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/xsect/DataObject.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/xsect/DataObject.java
index 89124d16..6e84081e 100644
--- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/xsect/DataObject.java
+++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/xsect/DataObject.java
@@ -49,8 +49,6 @@ import javax.xml.crypto.dsig.spec.XPathType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.w3._2000._09.xmldsig_.TransformType;
-import org.w3._2000._09.xmldsig_.TransformsType;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
@@ -71,6 +69,7 @@ import at.buergerkarte.namespaces.securitylayer._1.DataObjectInfoType;
import at.buergerkarte.namespaces.securitylayer._1.MetaInfoType;
import at.buergerkarte.namespaces.securitylayer._1.TransformsInfoType;
import at.gv.egiz.bku.binding.HttpUtil;
+import at.gv.egiz.bku.gui.viewer.MimeTypes;
import at.gv.egiz.bku.slexceptions.SLCommandException;
import at.gv.egiz.bku.slexceptions.SLRequestException;
import at.gv.egiz.bku.slexceptions.SLRuntimeException;
@@ -81,11 +80,11 @@ import at.gv.egiz.bku.viewer.ValidationException;
import at.gv.egiz.bku.viewer.Validator;
import at.gv.egiz.bku.viewer.ValidatorFactory;
import at.gv.egiz.dom.DOMUtils;
-import at.gv.egiz.marshal.NamespacePrefixMapperImpl;
import at.gv.egiz.slbinding.impl.XMLContentType;
-import javax.xml.namespace.NamespaceContext;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
/**
* This class represents a <code>DataObject</code> of an XML-Signature
@@ -184,7 +183,9 @@ public class DataObject {
* An optional description of the digest input.
*/
private String description;
-
+
+ private String filename;
+
/**
* Creates a new instance.
*
@@ -230,6 +231,10 @@ public class DataObject {
return mimeType;
}
+ public String getFilename() {
+ return filename;
+ }
+
/**
* @return the description
*/
@@ -336,7 +341,74 @@ public class DataObject {
}
// other values are not allowed by the schema and are therefore ignored
-
+
+ this.filename = deriveFilename();
+ }
+
+ /**
+ * Extract filename from reference URI
+ * or propose reference Id with an apropriate (mime-type) file extension
+ *
+ * @return if neither reference nor id can be extracted return null (or data.extension?)
+ */
+ private String deriveFilename() {
+
+ String filename = null;
+
+ if (reference != null) {
+ if (reference.getURI() != null && !"".equals(reference.getURI())) {
+ try {
+ log.info("deriving filename from reference URI " + reference.getURI());
+ URI refURI = new URI(reference.getURI());
+
+ if (refURI.isOpaque()) {
+ // could check scheme component, but also allow other schemes (e.g. testlocal)
+ log.trace("opaque reference URI, use scheme-specific part as filename");
+ filename = refURI.getSchemeSpecificPart();
+ if (!hasExtension(filename)) {
+ filename += MimeTypes.getExtension(mimeType);
+ }
+ // else hierarchical URI:
+ // for shorthand xpointer use fragment as filename,
+ // for any other xpointer use reference Id and
+ // for any other hierarchical (absolute or relative) use filename (ignore fragment, see xmldsig section 4.3.3.2: fragments not recommendet)
+ } else if ("".equals(refURI.getPath()) &&
+ refURI.getFragment() != null &&
+ refURI.getFragment().indexOf('(') < 0) { // exclude (schemebased) xpointer expressions
+ log.trace("fragment (shorthand xpointer) URI, use fragment as filename");
+ filename = refURI.getFragment();
+ if(!hasExtension(filename)) {
+ filename += MimeTypes.getExtension(mimeType);
+ }
+ } else if (!"".equals(refURI.getPath())) {
+ log.trace("hierarchical URI with path component, use path as filename");
+ File refFile = new File(refURI.getPath());
+ filename = refFile.getName();
+ if(!hasExtension(filename)) {
+ filename += MimeTypes.getExtension(mimeType);
+ }
+ } else {
+ log.info("failed to derive filename from URI '" + refURI + "', derive filename from reference ID");
+ filename = reference.getId() + MimeTypes.getExtension(mimeType);
+ }
+ } catch (URISyntaxException ex) {
+ log.error("failed to derive filename from invalid URI " + ex.getMessage());
+ filename = reference.getId() + MimeTypes.getExtension(mimeType);
+ }
+ } else {
+ log.info("same-document URI, derive filename from reference ID");
+ filename = reference.getId() + MimeTypes.getExtension(mimeType);
+ }
+ } else {
+ log.error("failed to derive filename, no reference created");
+ }
+ log.debug("derived filename for reference " + reference.getId() + ": " + filename);
+ return filename;
+ }
+
+ private static boolean hasExtension(String filename) {
+ int extDelimiterInd = filename.lastIndexOf('.');
+ return extDelimiterInd >= 0 && extDelimiterInd >= filename.length() - 4;
}
private byte[] getTransformsBytes(at.gv.egiz.slbinding.impl.TransformsInfoType ti) {
diff --git a/bkucommon/src/test/java/at/gv/egiz/bku/slcommands/impl/xsect/SignatureTest.java b/bkucommon/src/test/java/at/gv/egiz/bku/slcommands/impl/xsect/SignatureTest.java
index 7ce7b42d..ccd29e85 100644
--- a/bkucommon/src/test/java/at/gv/egiz/bku/slcommands/impl/xsect/SignatureTest.java
+++ b/bkucommon/src/test/java/at/gv/egiz/bku/slcommands/impl/xsect/SignatureTest.java
@@ -443,8 +443,11 @@ public class SignatureTest {
@SuppressWarnings("unchecked")
@Test
+
public void testDataObject_XMLContent_1() throws JAXBException, SLCommandException, XMLStreamException, SLRequestException, MarshalException, XMLSignatureException, SLViewerException {
+ System.out.println("\n ****************** testDataObject_XMLContent_1 \n");
+
List<DataObjectInfoType> dataObjectInfos = unmarshalDataObjectInfo("DataObjectInfo_XMLContent_1.xml");
Signature signature = new Signature(null, new IdValueFactoryImpl(), new AlgorithmMethodFactoryImpl());
@@ -485,6 +488,8 @@ public class SignatureTest {
@Test
public void testDataObject_XMLContent_2() throws JAXBException, SLCommandException, XMLStreamException, SLRequestException, MarshalException, XMLSignatureException, SLViewerException {
+ System.out.println("\n ****************** testDataObject_XMLContent_2 \n");
+
List<DataObjectInfoType> dataObjectInfos = unmarshalDataObjectInfo("DataObjectInfo_XMLContent_2.xml");
Signature signature = new Signature(null, new IdValueFactoryImpl(), new AlgorithmMethodFactoryImpl());
@@ -526,6 +531,8 @@ public class SignatureTest {
@Test
public void testDataObject_LocRefContent_1() throws JAXBException, SLCommandException, XMLStreamException, SLRequestException, MarshalException, XMLSignatureException, SLViewerException {
+ System.out.println("\n ****************** testDataObject_LocRefContent_1 \n");
+
List<DataObjectInfoType> dataObjectInfos = unmarshalDataObjectInfo("DataObjectInfo_LocRefContent_1.xml");
Signature signature = new Signature(null, new IdValueFactoryImpl(), new AlgorithmMethodFactoryImpl());
@@ -535,7 +542,7 @@ public class SignatureTest {
}
signature.buildXMLSignature();
-
+
signAndMarshalSignature(signature);
List<Reference> references = signature.getReferences();
@@ -564,6 +571,8 @@ public class SignatureTest {
@Test
public void testDataObject_LocRefContent_2() throws JAXBException, SLCommandException, XMLStreamException, SLRequestException, MarshalException, XMLSignatureException, SLViewerException {
+ System.out.println("\n ****************** testDataObject_LocRefContent_2 \n");
+
List<DataObjectInfoType> dataObjectInfos = unmarshalDataObjectInfo("DataObjectInfo_LocRefContent_2.xml");
Signature signature = new Signature(null, new IdValueFactoryImpl(), new AlgorithmMethodFactoryImpl());
@@ -602,6 +611,8 @@ public class SignatureTest {
@Test
public void testDataObject_Reference_1() throws JAXBException, SLCommandException, XMLStreamException, SLRequestException, MarshalException, XMLSignatureException, SLViewerException {
+ System.out.println("\n ****************** testDataObject_Reference_1 \n");
+
List<DataObjectInfoType> dataObjectInfos = unmarshalDataObjectInfo("DataObjectInfo_Reference_1.xml");
Signature signature = new Signature(null, new IdValueFactoryImpl(), new AlgorithmMethodFactoryImpl());
@@ -640,6 +651,8 @@ public class SignatureTest {
@Test
public void testDataObject_Detached_1() throws JAXBException, SLCommandException, XMLStreamException, SLRequestException, MarshalException, XMLSignatureException, SLViewerException {
+ System.out.println("\n ****************** testDataObject_Detached_1 \n");
+
List<DataObjectInfoType> dataObjectInfos = unmarshalDataObjectInfo("DataObjectInfo_Detached_1.xml");
Signature signature = new Signature(null, new IdValueFactoryImpl(), new AlgorithmMethodFactoryImpl());
@@ -671,6 +684,8 @@ public class SignatureTest {
@Test
public void testDataObject_Detached_Base64Content() throws JAXBException, SLCommandException, XMLStreamException, SLRequestException, MarshalException, XMLSignatureException, SLViewerException {
+ System.out.println("\n ****************** testDataObject_Detached_Base64Content \n");
+
List<DataObjectInfoType> dataObjectInfos = unmarshalDataObjectInfo("DataObjectInfo_Detached_Base64Content.xml");
Signature signature = new Signature(null, new IdValueFactoryImpl(), new AlgorithmMethodFactoryImpl());
@@ -698,6 +713,39 @@ public class SignatureTest {
}
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testDataObject_Detached_LocRefContent() throws JAXBException, SLCommandException, XMLStreamException, SLRequestException, MarshalException, XMLSignatureException, SLViewerException {
+
+ System.out.println("\n ****************** testDataObject_Detached_LocRefContent \n");
+
+ List<DataObjectInfoType> dataObjectInfos = unmarshalDataObjectInfo("DataObjectInfo_Detached_LocRefContent.xml");
+
+ Signature signature = new Signature(null, new IdValueFactoryImpl(), new AlgorithmMethodFactoryImpl());
+
+ for (DataObjectInfoType dataObjectInfo : dataObjectInfos) {
+ signature.addDataObject(dataObjectInfo);
+ }
+
+ signature.buildXMLSignature();
+
+ signAndMarshalSignature(signature);
+
+ List<Reference> references = signature.getReferences();
+ assertTrue(references.size() == 2);
+
+ Reference reference = references.get(0);
+ assertNotNull(reference.getId());
+
+ List<Transform> transforms = reference.getTransforms();
+ assertTrue(transforms.size() == 0);
+
+ List<XMLObject> objects = signature.getXMLObjects();
+ assertNotNull(objects);
+ assertTrue(objects.size() == 1);
+
+ }
+
//
//
// TransformsInfo
diff --git a/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_Detached_LocRefContent.xml b/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_Detached_LocRefContent.xml
new file mode 100644
index 00000000..75f45ff0
--- /dev/null
+++ b/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_Detached_LocRefContent.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<sl:CreateXMLSignatureRequest xmlns:sl="http://www.buergerkarte.at/namespaces/securitylayer/1.2#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
+ <sl:DataObjectInfo Structure="detached">
+ <sl:DataObject Reference="http://example.com/path/filenameNoExt#fragmentNoExt">
+ <sl:LocRefContent>testlocal:DataObject1.bin</sl:LocRefContent>
+ </sl:DataObject>
+ <sl:TransformsInfo>
+ <sl:FinalDataMetaInfo>
+ <sl:MimeType>application/octet-stream</sl:MimeType>
+ </sl:FinalDataMetaInfo>
+ </sl:TransformsInfo>
+ </sl:DataObjectInfo>
+</sl:CreateXMLSignatureRequest> \ No newline at end of file
diff --git a/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_LocRefContent_2.xml b/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_LocRefContent_2.xml
index 852c115f..a94f51b6 100644
--- a/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_LocRefContent_2.xml
+++ b/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/impl/DataObjectInfo_LocRefContent_2.xml
@@ -6,7 +6,7 @@
</sl:DataObject>
<sl:TransformsInfo>
<sl:FinalDataMetaInfo>
- <sl:MimeType>application/octetstream</sl:MimeType>
+ <sl:MimeType>application/octet-stream</sl:MimeType>
</sl:FinalDataMetaInfo>
</sl:TransformsInfo>
</sl:DataObjectInfo>