model serialization

git-svn-id: http://svn.research-infrastructures.eu/d4science/gcube/trunk/spatial-data/geonetwork@146246 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Fabio Sinibaldi 2017-03-21 15:12:10 +00:00
parent 0397c0e1f8
commit bbeb76d338
9 changed files with 219 additions and 9 deletions

View File

@ -20,4 +20,7 @@
<Changeset component="geonetwork.3-2-0" date="2016-11-01">
<Change>GeoNetwork 3.x compatibility</Change>
</Changeset>
<Changeset component="geonetwork.3-2-2" date="2017-03-20">
<Change>Model can be transferred via HTPP</Change>
</Changeset>
</ReleaseNotes>

View File

@ -8,7 +8,7 @@
</parent>
<groupId>org.gcube.spatial.data</groupId>
<artifactId>geonetwork</artifactId>
<version>3.2.1-SNAPSHOT</version>
<version>3.2.2-SNAPSHOT</version>
<name>geonetwork</name>
<properties>

View File

@ -1,14 +1,22 @@
package org.gcube.spatial.data.geonetwork.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Account {
public static enum Type{

View File

@ -1,14 +1,22 @@
package org.gcube.spatial.data.geonetwork.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Group {
private String name;

View File

@ -1,21 +1,26 @@
package org.gcube.spatial.data.geonetwork.model;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@ToString
@NoArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ScopeConfiguration {

View File

@ -1,15 +1,20 @@
package org.gcube.spatial.data.geonetwork.model;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
public static enum Profile{

View File

@ -0,0 +1,178 @@
package org.gcube.spatial.data.geonetwork.test;
import static org.junit.Assert.assertTrue;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.gcube.spatial.data.geonetwork.GeoNetworkAdministration;
import org.gcube.spatial.data.geonetwork.LoginLevel;
import org.gcube.spatial.data.geonetwork.model.Account;
import org.gcube.spatial.data.geonetwork.model.Group;
import org.gcube.spatial.data.geonetwork.model.ScopeConfiguration;
import org.gcube.spatial.data.geonetwork.model.User;
import org.gcube.spatial.data.geonetwork.model.faults.AuthorizationException;
import org.gcube.spatial.data.geonetwork.model.faults.EncryptionException;
import org.gcube.spatial.data.geonetwork.model.faults.MissingConfigurationException;
import org.gcube.spatial.data.geonetwork.model.faults.MissingServiceEndpointException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import it.geosolutions.geonetwork.exception.GNLibException;
import it.geosolutions.geonetwork.exception.GNServerException;
public class ModelMarshalling {
private static JAXBContext ctx =null;
@BeforeClass
public static void init() throws JAXBException{
ctx = JAXBContext.newInstance(
ScopeConfiguration.class,
Account.class,
Group.class,
User.class);
}
@Before
public void setScope(){
TokenSetter.set("/gcube/devsec");
}
@Test
public void Marshall() throws MissingConfigurationException, GNLibException, GNServerException, MissingServiceEndpointException, EncryptionException, AuthorizationException{
ScopeConfiguration config=createFakeConfig();
print(config);
print(config.getAccounts().values().iterator().next());
print(getGroup());
print(getUser());
}
@Test
public void unMarshall() throws MissingConfigurationException, MissingServiceEndpointException, EncryptionException, GNLibException, GNServerException, AuthorizationException{
ScopeConfiguration config=createFakeConfig();
assertTrue(roundTrip(config));
assertTrue(roundTrip(config.getAccounts().values().iterator().next()));
assertTrue(roundTrip(getGroup()));
assertTrue(roundTrip(getUser()));
}
private ScopeConfiguration createFakeConfig() throws MissingConfigurationException, MissingServiceEndpointException, EncryptionException, GNLibException, GNServerException, AuthorizationException{
return TestConfiguration.getClient().getConfiguration().getScopeConfiguration();
}
private Group getGroup() throws MissingConfigurationException, GNLibException, GNServerException, MissingServiceEndpointException, EncryptionException, AuthorizationException{
return TestConfiguration.getClient().getGroups().iterator().next();
}
private User getUser() throws MissingConfigurationException, GNLibException, GNServerException, MissingServiceEndpointException, EncryptionException, AuthorizationException{
GeoNetworkAdministration admin=TestConfiguration.getClient();
admin.login(LoginLevel.ADMIN);
return admin.getUsers().iterator().next();
}
public static boolean roundTrip(Object obj){
Object roundTripResult=unmarshal(obj.getClass(), new StringReader(marshal(obj,new StringWriter()).toString()));
return obj.equals(roundTripResult);
}
//********************** MARSHALL / UNMARSHALL
/**
* Write the serialisation of a given resource to a {@link Result}.
* @param resource the resource
* @param stream the result
* @return the result in input
*/
public static <T extends Result> T marshal(Object resource,T result) {
try {
JAXBContext context = ctx;
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(resource,result);
return result;
}
catch(Exception e) {
throw new RuntimeException("serialisation error",e);
}
}
public static void print(Object resource) {
marshal(resource,new OutputStreamWriter(System.out));
}
/**
* Write the serialisation of a given resource to a given character stream.
* @param resource the resource
* @param stream the stream in input
*/
public static <T extends Writer> T marshal(Object resource,T stream) {
marshal(resource,new StreamResult(stream));
return stream;
}
/**
* Creates a resource of given class from its serialisation in a given {@link Reader}.
* @param resourceClass the class of the resource
* @param reader the reader
* @return the resource
*/
public static <T> T unmarshal(Class<T> resourceClass, Reader reader) {
return unmarshal(resourceClass,new StreamSource(reader));
}
/**
* Creates a resource of given class from its serialisation in a given {@link InputStream}.
* @param resourceClass the class of the resource
* @param stream the stream
* @return the resource
*/
public static <T> T unmarshal(Class<T> resourceClass, InputStream stream) {
return unmarshal(resourceClass,new StreamSource(stream));
}
/**
* Creates a resource of given class from its serialisation in a given {@link Source}.
* @param resourceClass the class of the resource
* @param source the source
* @return the resource
*/
public static <T> T unmarshal(Class<T> resourceClass,Source source) {
try {
Unmarshaller um = ctx.createUnmarshaller();
return resourceClass.cast(um.unmarshal(source));
}
catch(Exception e) {
throw new RuntimeException("deserialisation error",e);
}
}
}

View File

@ -32,9 +32,9 @@ public class ScopeTests {
static{
// DEV
scopes.add("/gcube");
// scopes.add("/gcube");
// scopes.add("/gcube/devNext");
// scopes.add("/gcube/devsec");
scopes.add("/gcube/devsec");
//
// scopes.add("/gcube/devNext/NextNext");
//
@ -102,6 +102,9 @@ public class ScopeTests {
// scopes.add("/d4science.research-infrastructures.eu/gCubeApps/SoBigData.eu");
// scopes.add("/d4science.research-infrastructures.eu/gCubeApps/PerformanceEvaluationInAquaculture");
// scopes.add("/d4science.research-infrastructures.eu/gCubeApps/StrategicInvestmentAnalysis");
// scopes.add("/d4science.research-infrastructures.eu/D4Research/FisheriesandEcosystemAtMII");
}

View File

@ -1,4 +1,4 @@
log4j.rootLogger=DEBUG, A1,stdout
#log4j.rootLogger=DEBUG, A1,stdout
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=log.txt
log4j.appender.A1.layout=org.apache.log4j.PatternLayout