From 39089fb2ae38008b4091627c62555af767aa8f48 Mon Sep 17 00:00:00 2001 From: "lucio.lelii" Date: Wed, 7 Sep 2016 12:11:46 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/authorization-common-library@131172 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../authorization/library/QualifiersList.java | 42 +++++++++++++++++++ .../library/utils/MapAdapter.java | 26 ++++++++++++ .../library/utils/MapElement.java | 18 ++++++++ .../binder/AuthorizationEntryBinder.java | 13 +++++- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/gcube/common/authorization/library/QualifiersList.java create mode 100644 src/main/java/org/gcube/common/authorization/library/utils/MapAdapter.java create mode 100644 src/main/java/org/gcube/common/authorization/library/utils/MapElement.java diff --git a/src/main/java/org/gcube/common/authorization/library/QualifiersList.java b/src/main/java/org/gcube/common/authorization/library/QualifiersList.java new file mode 100644 index 0000000..3375e7c --- /dev/null +++ b/src/main/java/org/gcube/common/authorization/library/QualifiersList.java @@ -0,0 +1,42 @@ +package org.gcube.common.authorization.library; + +import java.util.HashMap; +import java.util.Map; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import org.gcube.common.authorization.library.utils.MapAdapter; + + + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class QualifiersList { + + + @XmlJavaTypeAdapter(MapAdapter.class) + Map qualifierTokenMap= new HashMap(); + + + @SuppressWarnings("unused") + private QualifiersList(){} + + public QualifiersList(Map qualifierTokenMap) { + this.qualifierTokenMap = qualifierTokenMap; + } + + public Map getQualifiers() { + return qualifierTokenMap; + } + + @Override + public String toString() { + return "QualifiersList [qualifierTokenMap=" + qualifierTokenMap + "]"; + } + + + +} diff --git a/src/main/java/org/gcube/common/authorization/library/utils/MapAdapter.java b/src/main/java/org/gcube/common/authorization/library/utils/MapAdapter.java new file mode 100644 index 0000000..6961374 --- /dev/null +++ b/src/main/java/org/gcube/common/authorization/library/utils/MapAdapter.java @@ -0,0 +1,26 @@ +package org.gcube.common.authorization.library.utils; + +import java.util.HashMap; +import java.util.Map; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + + +public class MapAdapter extends XmlAdapter> { + + public MapElements[] marshal(Map arg0) throws Exception { + MapElements[] mapElements = new MapElements[arg0.size()]; + int i = 0; + for (Map.Entry entry : arg0.entrySet()) + mapElements[i++] = new MapElements(entry.getKey(), entry.getValue()); + + return mapElements; + } + + public Map unmarshal(MapElements[] arg0) throws Exception { + Map r = new HashMap(); + for (MapElements mapelement : arg0) + r.put(mapelement.key, mapelement.value); + return r; + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/common/authorization/library/utils/MapElement.java b/src/main/java/org/gcube/common/authorization/library/utils/MapElement.java new file mode 100644 index 0000000..2b66bea --- /dev/null +++ b/src/main/java/org/gcube/common/authorization/library/utils/MapElement.java @@ -0,0 +1,18 @@ +package org.gcube.common.authorization.library.utils; + +import javax.xml.bind.annotation.XmlElement; + +class MapElements +{ + @XmlElement public String key; + @XmlElement public String value; + + @SuppressWarnings("unused") +private MapElements() {} //Required by JAXB + + public MapElements(String key, String value) + { + this.key = key; + this.value = value; + } +} diff --git a/src/test/java/org/gcube/common/authorization/library/binder/AuthorizationEntryBinder.java b/src/test/java/org/gcube/common/authorization/library/binder/AuthorizationEntryBinder.java index 8095ad7..411ee1f 100644 --- a/src/test/java/org/gcube/common/authorization/library/binder/AuthorizationEntryBinder.java +++ b/src/test/java/org/gcube/common/authorization/library/binder/AuthorizationEntryBinder.java @@ -3,6 +3,7 @@ package org.gcube.common.authorization.library.binder; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -10,6 +11,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.QualifiersList; import org.gcube.common.authorization.library.policies.Policy; import org.gcube.common.authorization.library.provider.UserInfo; import org.junit.Assert; @@ -18,7 +20,7 @@ import org.junit.Test; public class AuthorizationEntryBinder { public static JAXBContext getContext() throws JAXBException{ - return JAXBContext.newInstance(AuthorizationEntry.class); + return JAXBContext.newInstance(QualifiersList.class, AuthorizationEntry.class); } @Test @@ -34,5 +36,14 @@ public class AuthorizationEntryBinder { AuthorizationEntry ae2 = (AuthorizationEntry)context.createUnmarshaller().unmarshal(new StringReader(sw.toString())); System.out.println(ae2.toString()); Assert.assertEquals(ae1, ae2); + + QualifiersList entries = new QualifiersList(Collections.singletonMap("qualifier", "token")); + System.out.println(entries); + sw = new StringWriter(); + context.createMarshaller().marshal(entries, sw); + System.out.println(sw); + + QualifiersList entries2 = (QualifiersList)context.createUnmarshaller().unmarshal(new StringReader(sw.toString())); + } }