Lucio Lelii 2016-09-07 12:11:46 +00:00
parent 8e7583391a
commit 39089fb2ae
4 changed files with 98 additions and 1 deletions

View File

@ -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<String, String> qualifierTokenMap= new HashMap<String, String>();
@SuppressWarnings("unused")
private QualifiersList(){}
public QualifiersList(Map<String, String> qualifierTokenMap) {
this.qualifierTokenMap = qualifierTokenMap;
}
public Map<String, String> getQualifiers() {
return qualifierTokenMap;
}
@Override
public String toString() {
return "QualifiersList [qualifierTokenMap=" + qualifierTokenMap + "]";
}
}

View File

@ -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<MapElements[], Map<String, String>> {
public MapElements[] marshal(Map<String, String> arg0) throws Exception {
MapElements[] mapElements = new MapElements[arg0.size()];
int i = 0;
for (Map.Entry<String, String> entry : arg0.entrySet())
mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());
return mapElements;
}
public Map<String, String> unmarshal(MapElements[] arg0) throws Exception {
Map<String, String> r = new HashMap<String, String>();
for (MapElements mapelement : arg0)
r.put(mapelement.key, mapelement.value);
return r;
}
}

View File

@ -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;
}
}

View File

@ -3,6 +3,7 @@ package org.gcube.common.authorization.library.binder;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -10,6 +11,7 @@ import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import org.gcube.common.authorization.library.AuthorizationEntry; 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.policies.Policy;
import org.gcube.common.authorization.library.provider.UserInfo; import org.gcube.common.authorization.library.provider.UserInfo;
import org.junit.Assert; import org.junit.Assert;
@ -18,7 +20,7 @@ import org.junit.Test;
public class AuthorizationEntryBinder { public class AuthorizationEntryBinder {
public static JAXBContext getContext() throws JAXBException{ public static JAXBContext getContext() throws JAXBException{
return JAXBContext.newInstance(AuthorizationEntry.class); return JAXBContext.newInstance(QualifiersList.class, AuthorizationEntry.class);
} }
@Test @Test
@ -34,5 +36,14 @@ public class AuthorizationEntryBinder {
AuthorizationEntry ae2 = (AuthorizationEntry)context.createUnmarshaller().unmarshal(new StringReader(sw.toString())); AuthorizationEntry ae2 = (AuthorizationEntry)context.createUnmarshaller().unmarshal(new StringReader(sw.toString()));
System.out.println(ae2.toString()); System.out.println(ae2.toString());
Assert.assertEquals(ae1, ae2); 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()));
} }
} }