2017-12-18 15:37:22 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.schema;
|
2017-12-18 15:34:25 +01:00
|
|
|
|
|
|
|
import java.io.StringWriter;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2017-12-19 11:27:29 +01:00
|
|
|
import org.gcube.informationsystem.model.AccessType;
|
2017-12-18 15:34:25 +01:00
|
|
|
import org.gcube.informationsystem.model.ISManageable;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
2018-06-06 19:26:19 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath;
|
2017-12-18 15:34:25 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
|
|
|
|
import org.gcube.informationsystem.types.TypeBinder;
|
|
|
|
import org.gcube.informationsystem.types.TypeBinder.TypeDefinition;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaClient {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientImpl.class);
|
|
|
|
|
|
|
|
public static final String PATH_SEPARATOR = "/";
|
|
|
|
|
|
|
|
protected final String address;
|
|
|
|
protected HTTPCall httpCall;
|
|
|
|
|
|
|
|
public ResourceRegistrySchemaClientImpl(String address) {
|
|
|
|
this.address = address;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private HTTPCall getHTTPCall() throws MalformedURLException {
|
|
|
|
if(httpCall == null) {
|
|
|
|
httpCall = new HTTPCall(address, ResourceRegistrySchemaClient.class.getSimpleName());
|
|
|
|
}
|
|
|
|
return httpCall;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-19 11:27:29 +01:00
|
|
|
public <ISM extends ISManageable> TypeDefinition create(Class<ISM> clz)
|
2017-12-18 15:34:25 +01:00
|
|
|
throws SchemaException, ResourceRegistryException {
|
|
|
|
try {
|
2017-12-19 11:27:29 +01:00
|
|
|
String typeDefinition = TypeBinder.serializeType(clz);
|
|
|
|
String type = AccessType.getAccessType(clz).getName();
|
|
|
|
String res = create(type, typeDefinition);
|
2017-12-18 15:34:25 +01:00
|
|
|
return TypeBinder.deserializeTypeDefinition(res);
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-19 11:27:29 +01:00
|
|
|
public String create(String baseType, String typeDefinitition) throws ContextAlreadyPresentException, ResourceRegistryException {
|
2017-12-18 15:34:25 +01:00
|
|
|
try {
|
|
|
|
logger.trace("Going to create: {}", typeDefinitition);
|
2018-06-04 12:31:09 +02:00
|
|
|
TypeDefinition typeDefinitionObj = TypeBinder.deserializeTypeDefinition(typeDefinitition);
|
|
|
|
|
2017-12-18 15:34:25 +01:00
|
|
|
StringWriter stringWriter = new StringWriter();
|
|
|
|
stringWriter.append(PATH_SEPARATOR);
|
2018-06-06 19:26:19 +02:00
|
|
|
stringWriter.append(TypePath.TYPES_PATH_PART);
|
2017-12-19 11:27:29 +01:00
|
|
|
stringWriter.append(PATH_SEPARATOR);
|
2018-06-04 12:31:09 +02:00
|
|
|
stringWriter.append(typeDefinitionObj.getName());
|
|
|
|
|
2017-12-18 15:34:25 +01:00
|
|
|
HTTPCall httpCall = getHTTPCall();
|
2018-06-06 19:26:19 +02:00
|
|
|
String c = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.PUT, typeDefinitition);
|
2017-12-18 15:34:25 +01:00
|
|
|
|
|
|
|
logger.trace("{} successfully created", c);
|
|
|
|
return c;
|
|
|
|
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 11:27:29 +01:00
|
|
|
|
2017-12-18 15:34:25 +01:00
|
|
|
@Override
|
2017-12-19 11:27:29 +01:00
|
|
|
public <ISM extends ISManageable> List<TypeDefinition> read(Class<ISM> clz, Boolean polymorphic)
|
2017-12-18 15:34:25 +01:00
|
|
|
throws SchemaNotFoundException, ResourceRegistryException {
|
|
|
|
try {
|
2017-12-19 11:27:29 +01:00
|
|
|
String type = Utility.getType(clz);
|
2017-12-18 15:34:25 +01:00
|
|
|
String res = read(type, polymorphic);
|
|
|
|
return TypeBinder.deserializeTypeDefinitions(res);
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String read(String type, Boolean polymorphic) throws ContextNotFoundException, ResourceRegistryException {
|
|
|
|
try {
|
|
|
|
logger.info("Going to get {} schema", type);
|
|
|
|
StringWriter stringWriter = new StringWriter();
|
|
|
|
stringWriter.append(PATH_SEPARATOR);
|
2018-06-06 19:26:19 +02:00
|
|
|
stringWriter.append(TypePath.TYPES_PATH_PART);
|
2017-12-18 15:34:25 +01:00
|
|
|
stringWriter.append(PATH_SEPARATOR);
|
|
|
|
stringWriter.append(type);
|
|
|
|
|
|
|
|
Map<String,String> parameters = new HashMap<>();
|
2017-12-19 11:27:29 +01:00
|
|
|
if(polymorphic != null) {
|
2018-06-06 19:26:19 +02:00
|
|
|
parameters.put(TypePath.POLYMORPHIC_PARAM, polymorphic.toString());
|
2017-12-18 15:34:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTPCall httpCall = getHTTPCall();
|
|
|
|
String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
|
|
|
|
|
|
|
|
logger.debug("Got schema for {} is {}", type, json);
|
|
|
|
return json;
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|