2012-12-19 13:00:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* This software is part of the gCube Project.
|
|
|
|
* Site: http://www.gcube-system.org/
|
|
|
|
****************************************************************************
|
|
|
|
* The gCube/gCore software is licensed as Free Open Source software
|
|
|
|
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
|
|
|
|
* The software and documentation is provided by its authors/distributors
|
|
|
|
* "as is" and no expressed or
|
|
|
|
* implied warranty is given for its use, quality or fitness for a
|
|
|
|
* particular case.
|
|
|
|
****************************************************************************
|
|
|
|
* Filename: GenericResourceManager.java
|
|
|
|
****************************************************************************
|
|
|
|
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
|
|
|
|
***************************************************************************/
|
|
|
|
|
2012-12-28 17:28:46 +01:00
|
|
|
package org.gcube.resourcemanagement.support.server.managers.resources;
|
2012-12-19 13:00:57 +01:00
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2012-12-19 13:00:57 +01:00
|
|
|
import java.io.StringReader;
|
2013-09-11 18:40:58 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
import javax.xml.bind.Unmarshaller;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
|
|
|
import org.gcube.common.resources.gcore.GenericResource;
|
2013-09-13 15:38:01 +02:00
|
|
|
import org.gcube.common.resources.gcore.GenericResource.Profile;
|
2013-09-11 18:40:58 +02:00
|
|
|
import org.gcube.common.resources.gcore.Resource;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
|
|
|
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
2012-12-28 17:28:46 +01:00
|
|
|
import org.gcube.resourcemanagement.support.server.exceptions.AbstractResourceException;
|
|
|
|
import org.gcube.resourcemanagement.support.server.exceptions.ResourceAccessException;
|
|
|
|
import org.gcube.resourcemanagement.support.server.exceptions.ResourceParameterException;
|
|
|
|
import org.gcube.resourcemanagement.support.server.types.AllowedResourceTypes;
|
|
|
|
import org.gcube.resourcemanagement.support.server.utils.Assertion;
|
|
|
|
import org.gcube.resourcemanagement.support.server.utils.ServerConsole;
|
2013-09-11 18:40:58 +02:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
2012-12-19 13:00:57 +01:00
|
|
|
import org.w3c.dom.Document;
|
2013-09-13 15:38:01 +02:00
|
|
|
import org.w3c.dom.Element;
|
2013-09-11 18:40:58 +02:00
|
|
|
import org.w3c.dom.Node;
|
2013-09-12 18:47:59 +02:00
|
|
|
import org.w3c.dom.Text;
|
2013-09-11 18:40:58 +02:00
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
import org.xml.sax.SAXException;
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Daniele Strollo (ISTI-CNR)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class GenericResourceManager extends AbstractResourceManager {
|
|
|
|
// Used internally to require static functionalities (e.g. deploy).
|
|
|
|
private static GenericResourceManager singleton = null;
|
|
|
|
private static final String LOG_PREFIX = "[GenericResource-MGR]";
|
|
|
|
|
|
|
|
static {
|
|
|
|
if (GenericResourceManager.singleton == null) {
|
|
|
|
try {
|
|
|
|
GenericResourceManager.singleton = new GenericResourceManager("dummyservice", "dummyservice");
|
|
|
|
} catch (Exception e) {
|
|
|
|
ServerConsole.error(LOG_PREFIX, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated discouraged use. With no ID some operations cannot be accessed.
|
|
|
|
*/
|
|
|
|
public GenericResourceManager()
|
|
|
|
throws ResourceParameterException,
|
|
|
|
ResourceAccessException {
|
|
|
|
super(AllowedResourceTypes.GenericResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
public GenericResourceManager(final String id)
|
|
|
|
throws ResourceParameterException,
|
|
|
|
ResourceAccessException {
|
|
|
|
super(id, AllowedResourceTypes.GenericResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param id
|
|
|
|
* @param name
|
|
|
|
* @param type
|
|
|
|
* @throws ResourceParameterException
|
|
|
|
* @throws ResourceAccessException
|
|
|
|
*/
|
|
|
|
public GenericResourceManager(final String id, final String name)
|
|
|
|
throws ResourceParameterException,
|
|
|
|
ResourceAccessException {
|
|
|
|
super(id, name, AllowedResourceTypes.GenericResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param id
|
|
|
|
* @param name
|
|
|
|
* @param type
|
|
|
|
* @param subtype
|
|
|
|
* @throws ResourceParameterException
|
|
|
|
* @throws ResourceAccessException
|
|
|
|
*/
|
|
|
|
public GenericResourceManager(final String id, final String name, final String subtype)
|
|
|
|
throws ResourceParameterException, ResourceAccessException {
|
|
|
|
super(id, name, AllowedResourceTypes.GenericResource, subtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the resource.
|
|
|
|
* @param name (Mandatory) the name to assign to the resource
|
|
|
|
* @param description (optional) if null it will not be changed
|
|
|
|
* @param body (optional) if null it will not be changed
|
|
|
|
* @param subType (optional) if null it will not be changed
|
|
|
|
* @param scope (optional) if null it will not be changed
|
|
|
|
* @throws AbstractResourceException
|
2013-09-11 18:40:58 +02:00
|
|
|
* @throws ParserConfigurationException
|
|
|
|
* @throws SAXException
|
|
|
|
* @throws IOException
|
2012-12-19 13:00:57 +01:00
|
|
|
*/
|
2013-09-11 18:40:58 +02:00
|
|
|
public final void update(final String name, final String description, final String body, final String subType, final ScopeBean scope) throws Exception {
|
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
|
|
|
|
checker.validate(name != null && name.trim().length() != 0, new ResourceParameterException("Invalid field name. Null or empty value not allowed"));
|
|
|
|
|
|
|
|
ServerConsole.trace(LOG_PREFIX, "[RES-UPDATE] updating resource " + this.getType() + " " + this.getID());
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
GenericResource resource = getResourceToEditById(this.getID(), scope);
|
|
|
|
resource.profile().name(name.trim());
|
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
if (description != null) {
|
2013-09-11 18:40:58 +02:00
|
|
|
resource.profile().description(description.trim());
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
|
|
|
if (body != null) {
|
2013-09-13 15:38:01 +02:00
|
|
|
resource.profile().newBody(body);
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
2013-09-11 18:40:58 +02:00
|
|
|
if (subType != null)
|
|
|
|
resource.profile().type(subType.trim());
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope.toString());
|
|
|
|
RegistryPublisher publisher = getRegistryPublisher();
|
|
|
|
String id = publisher.update(resource).id();
|
|
|
|
|
|
|
|
if (id == null || id.length() == 0) {
|
|
|
|
throw new Exception("The GenericResource has not been updated");
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
2013-09-11 18:40:58 +02:00
|
|
|
ServerConsole.info(LOG_PREFIX, "Resource Updated with ID: " + id);
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
private GenericResource getResourceToEditById(String id, ScopeBean scope) throws Exception {
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope.toString());
|
|
|
|
|
|
|
|
SimpleQuery query = queryFor(GenericResource.class);
|
|
|
|
query.addCondition("$resource/ID/text() eq '"+ id +"'");
|
|
|
|
|
|
|
|
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
|
|
|
|
|
|
|
|
List<GenericResource> r = client.submit(query);
|
|
|
|
|
|
|
|
if (r == null || r.isEmpty())
|
|
|
|
throw new Exception("Could not retrieve GenericResource profile with id " + id + " in scope + " +scope);
|
|
|
|
else
|
|
|
|
return r.get(0);
|
|
|
|
}
|
2012-12-19 13:00:57 +01:00
|
|
|
/**
|
|
|
|
* Creates a Generic Resource and returns the ID given by the
|
|
|
|
* resource manager at creation phase.
|
|
|
|
* @return the id assigned to the newly created resource
|
|
|
|
*/
|
2013-09-11 18:40:58 +02:00
|
|
|
public static final synchronized String create(final String ID, final ScopeBean scope, final String name, final String description, final String body, final String subType)
|
2012-12-19 13:00:57 +01:00
|
|
|
throws Exception {
|
|
|
|
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
|
|
|
|
checker.validate(name != null && name.trim().length() != 0, new ResourceParameterException("Invalid field name. Null or empty value not allowed"));
|
|
|
|
checker.validate(subType != null && subType.trim().length() != 0, new ResourceParameterException("Invalid field subType. Null or empty value not allowed"));
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
GenericResource resource = new GenericResource();
|
|
|
|
resource.newProfile().name(name.trim());
|
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
if (description != null) {
|
2013-09-11 18:40:58 +02:00
|
|
|
resource.profile().description(description.trim());
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
|
|
|
if (body != null) {
|
2013-09-13 15:38:01 +02:00
|
|
|
resource.profile().newBody(body);
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
2013-09-11 18:40:58 +02:00
|
|
|
resource.profile().type(subType.trim());
|
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
GenericResourceManager gm = new GenericResourceManager();
|
2013-09-11 18:40:58 +02:00
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
ScopeProvider.instance.set(scope.toString());
|
|
|
|
RegistryPublisher publisher = gm.getRegistryPublisher();
|
|
|
|
String id = publisher.create(resource).id();
|
|
|
|
|
|
|
|
if (id == null || id.length() == 0) {
|
2012-12-19 13:00:57 +01:00
|
|
|
throw new Exception("The GenericResource has not been created");
|
|
|
|
}
|
|
|
|
ServerConsole.info(LOG_PREFIX, "Resource Created with ID: " + id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-11 18:40:58 +02:00
|
|
|
protected final Resource buildResource(final String xmlRepresentation) throws AbstractResourceException {
|
2012-12-19 13:00:57 +01:00
|
|
|
try {
|
2013-09-11 18:40:58 +02:00
|
|
|
JAXBContext ctx = JAXBContext.newInstance(GenericResource.class);
|
|
|
|
Unmarshaller unmarshaller = ctx.createUnmarshaller();
|
|
|
|
StringReader reader = new StringReader(xmlRepresentation);
|
|
|
|
GenericResource deserialised = (GenericResource) unmarshaller.unmarshal(reader);
|
|
|
|
return deserialised;
|
2012-12-19 13:00:57 +01:00
|
|
|
} catch (Exception e) {
|
2013-09-11 18:40:58 +02:00
|
|
|
throw new ResourceAccessException("Cannot load the resource " + this.getType(), e);
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|