aboutsummaryrefslogtreecommitdiff
path: root/id/server/modules/eID4UExtensions/src/main/java/at/gv/egiz/eid4u/impl/attributes/natural/PhotoTypeAttributeValueMarshaller.java
diff options
context:
space:
mode:
authorThomas Lenz <tlenz@iaik.tugraz.at>2018-09-28 14:19:50 +0200
committerThomas Lenz <tlenz@iaik.tugraz.at>2018-09-28 14:19:50 +0200
commitb76b6e6212784d622ca79bd258fa3e529b353346 (patch)
treefc1a83b6775e043aaabaa88648fc20063e938948 /id/server/modules/eID4UExtensions/src/main/java/at/gv/egiz/eid4u/impl/attributes/natural/PhotoTypeAttributeValueMarshaller.java
parent5a147ba2714436e5fb15ddcf3db8df6273ac4d57 (diff)
downloadmoa-id-spss-b76b6e6212784d622ca79bd258fa3e529b353346.tar.gz
moa-id-spss-b76b6e6212784d622ca79bd258fa3e529b353346.tar.bz2
moa-id-spss-b76b6e6212784d622ca79bd258fa3e529b353346.zip
add first code for eID4U
Diffstat (limited to 'id/server/modules/eID4UExtensions/src/main/java/at/gv/egiz/eid4u/impl/attributes/natural/PhotoTypeAttributeValueMarshaller.java')
-rw-r--r--id/server/modules/eID4UExtensions/src/main/java/at/gv/egiz/eid4u/impl/attributes/natural/PhotoTypeAttributeValueMarshaller.java67
1 files changed, 67 insertions, 0 deletions
diff --git a/id/server/modules/eID4UExtensions/src/main/java/at/gv/egiz/eid4u/impl/attributes/natural/PhotoTypeAttributeValueMarshaller.java b/id/server/modules/eID4UExtensions/src/main/java/at/gv/egiz/eid4u/impl/attributes/natural/PhotoTypeAttributeValueMarshaller.java
new file mode 100644
index 000000000..59f1817a4
--- /dev/null
+++ b/id/server/modules/eID4UExtensions/src/main/java/at/gv/egiz/eid4u/impl/attributes/natural/PhotoTypeAttributeValueMarshaller.java
@@ -0,0 +1,67 @@
+package at.gv.egiz.eid4u.impl.attributes.natural;
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import at.gv.egiz.eid4u.impl.attributes.xjc.Document;
+import eu.eidas.auth.commons.EidasStringUtil;
+import eu.eidas.auth.commons.attribute.AttributeValue;
+import eu.eidas.auth.commons.attribute.AttributeValueMarshaller;
+import eu.eidas.auth.commons.attribute.AttributeValueMarshallingException;
+
+public class PhotoTypeAttributeValueMarshaller implements AttributeValueMarshaller<Document>{
+
+ @Override
+ public String marshal(AttributeValue<Document> value) throws AttributeValueMarshallingException {
+ try {
+ JAXBContext context = JAXBContext.newInstance(Document.class);
+ Marshaller m = context.createMarshaller();
+ StringWriter sw = new StringWriter();
+ m.marshal(value, sw);
+ return EidasStringUtil.encodeToBase64(sw.toString());
+
+ } catch (JAXBException e) {
+ throw new AttributeValueMarshallingException("Can NOT create JAXB marshaller for type 'Document'", e);
+
+ }
+
+ }
+
+ @Override
+ public AttributeValue<Document> unmarshal(String value, boolean isNonLatinScriptAlternateVersion)
+ throws AttributeValueMarshallingException {
+ try {
+ Reader reader = new StringReader(EidasStringUtil.decodeStringFromBase64(value));
+
+ //initialize XML reader to prevent XXE
+ XMLInputFactory xif = XMLInputFactory.newInstance();
+ xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
+ XMLStreamReader xmlReader = xif.createXMLStreamReader(reader);
+
+ //unmarshal
+ JAXBContext context = JAXBContext.newInstance(Document.class);
+ Unmarshaller um = context.createUnmarshaller();
+ Object obj = um.unmarshal(xmlReader);
+
+ if (!(obj instanceof Document))
+ throw new AttributeValueMarshallingException("Unmarshalled result is NOT of type 'Document'");
+
+ return new DocumentAttributeValue((Document)obj, isNonLatinScriptAlternateVersion);
+
+ } catch (JAXBException | XMLStreamException e) {
+ throw new AttributeValueMarshallingException("Can NOT create JAXB unmarshaller for type 'Document'", e);
+
+ }
+
+ }
+}