2017-12-18 15:37:22 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.schema;
|
2017-12-18 15:34:25 +01:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
import java.net.HttpURLConnection;
|
2017-12-18 15:34:25 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2019-02-06 12:03:35 +01:00
|
|
|
import org.gcube.common.gxhttp.reference.GXConnection;
|
2019-01-17 11:18:02 +01:00
|
|
|
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
|
2023-02-22 15:16:07 +01:00
|
|
|
import org.gcube.common.http.GXHTTPUtility;
|
2021-09-20 12:15:14 +02:00
|
|
|
import org.gcube.informationsystem.base.reference.Element;
|
2022-02-09 12:14:19 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
2017-12-18 15:34:25 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2022-02-01 16:44:53 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
|
2021-10-25 11:01:48 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
|
2018-06-06 19:26:19 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath;
|
2019-01-17 11:18:02 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
|
2020-02-04 09:31:29 +01:00
|
|
|
import org.gcube.informationsystem.types.TypeMapper;
|
2020-02-03 10:52:50 +01:00
|
|
|
import org.gcube.informationsystem.types.reference.Type;
|
2023-04-21 15:58:07 +02:00
|
|
|
import org.gcube.informationsystem.utils.TypeUtility;
|
2017-12-18 15:34:25 +01:00
|
|
|
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);
|
|
|
|
|
2023-02-27 18:51:38 +01:00
|
|
|
private static final String ACCEPT_HTTP_HEADER_KEY = "Accept";
|
|
|
|
private static final String CONTENT_TYPE_HTTP_HEADER_KEY = "Content-Type";
|
|
|
|
|
2017-12-18 15:34:25 +01:00
|
|
|
protected final String address;
|
|
|
|
|
2023-02-27 18:51:38 +01:00
|
|
|
protected Map<String, String> headers;
|
|
|
|
|
2023-03-01 14:26:08 +01:00
|
|
|
@Override
|
2023-02-27 18:51:38 +01:00
|
|
|
public void addHeader(String name, String value) {
|
|
|
|
headers.put(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected GXHTTPStringRequest getGXHTTPStringRequest() {
|
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
|
|
|
gxHTTPStringRequest.from(this.getClass().getSimpleName());
|
|
|
|
for(String name : headers.keySet()) {
|
|
|
|
gxHTTPStringRequest.header(name, headers.get(name));
|
|
|
|
}
|
|
|
|
return gxHTTPStringRequest;
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:34:25 +01:00
|
|
|
public ResourceRegistrySchemaClientImpl(String address) {
|
|
|
|
this.address = address;
|
2023-02-27 18:51:38 +01:00
|
|
|
this.headers = new HashMap<>();
|
2017-12-18 15:34:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-09-20 12:15:14 +02:00
|
|
|
public <E extends Element> Type create(Class<E> clz)
|
2017-12-18 15:34:25 +01:00
|
|
|
throws SchemaException, ResourceRegistryException {
|
|
|
|
try {
|
2020-02-04 09:31:29 +01:00
|
|
|
String typeDefinition = TypeMapper.serializeType(clz);
|
2020-02-03 10:52:50 +01:00
|
|
|
// String type = AccessType.getAccessType(clz).getName();
|
|
|
|
String res = create(typeDefinition);
|
2020-02-04 09:31:29 +01:00
|
|
|
return TypeMapper.deserializeTypeDefinition(res);
|
2017-12-18 15:34:25 +01:00
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-03 10:52:50 +01:00
|
|
|
public String create(String typeDefinitition) throws ContextAlreadyPresentException, ResourceRegistryException {
|
2017-12-18 15:34:25 +01:00
|
|
|
try {
|
|
|
|
logger.trace("Going to create: {}", typeDefinitition);
|
2020-02-04 09:31:29 +01:00
|
|
|
Type typeDefinitionObj = TypeMapper.deserializeTypeDefinition(typeDefinitition);
|
2018-06-04 12:31:09 +02:00
|
|
|
|
2023-02-27 18:51:38 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
|
|
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
|
|
gxHTTPStringRequest.header(CONTENT_TYPE_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(typeDefinitionObj.getName());
|
2018-06-04 12:31:09 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(typeDefinitition);
|
|
|
|
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 12:14:19 +01:00
|
|
|
@Override
|
|
|
|
public <E extends Element> boolean exist(Class<E> clz) throws ResourceRegistryException {
|
|
|
|
try {
|
2023-04-21 15:58:07 +02:00
|
|
|
String typeName = TypeUtility.getTypeName(clz);
|
2022-02-09 12:14:19 +01:00
|
|
|
return exist(typeName);
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean exist(String typeName) throws ResourceRegistryException {
|
|
|
|
try {
|
|
|
|
logger.info("Going to get {} schema", typeName);
|
2023-02-27 18:51:38 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
|
|
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2022-02-09 12:14:19 +01:00
|
|
|
gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(typeName);
|
|
|
|
|
|
|
|
Map<String,String> parameters = new HashMap<>();
|
2022-07-20 12:09:51 +02:00
|
|
|
parameters.put(TypePath.POLYMORPHIC_QUERY_PARAMETER, Boolean.FALSE.toString());
|
2022-02-09 12:14:19 +01:00
|
|
|
gxHTTPStringRequest.queryParams(parameters);
|
|
|
|
|
2022-02-09 12:15:00 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
|
2022-02-09 12:14:19 +01:00
|
|
|
HTTPUtility.getResponse(String.class, httpURLConnection);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (NotFoundException e) {
|
|
|
|
return false;
|
|
|
|
} 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
|
2021-09-20 12:15:14 +02:00
|
|
|
public <E extends Element> List<Type> read(Class<E> clz, Boolean polymorphic)
|
2017-12-18 15:34:25 +01:00
|
|
|
throws SchemaNotFoundException, ResourceRegistryException {
|
|
|
|
try {
|
2023-04-21 15:58:07 +02:00
|
|
|
String typeName = TypeUtility.getTypeName(clz);
|
2020-02-03 10:52:50 +01:00
|
|
|
String res = read(typeName, polymorphic);
|
2020-02-04 09:31:29 +01:00
|
|
|
return TypeMapper.deserializeTypeDefinitions(res);
|
2017-12-18 15:34:25 +01:00
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-03 10:52:50 +01:00
|
|
|
public String read(String typeName, Boolean polymorphic) throws ContextNotFoundException, ResourceRegistryException {
|
2017-12-18 15:34:25 +01:00
|
|
|
try {
|
2020-02-03 10:52:50 +01:00
|
|
|
logger.info("Going to get {} schema", typeName);
|
2023-02-27 18:51:38 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
|
|
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART);
|
2020-02-03 10:52:50 +01:00
|
|
|
gxHTTPStringRequest.path(typeName);
|
2017-12-18 15:34:25 +01:00
|
|
|
|
|
|
|
Map<String,String> parameters = new HashMap<>();
|
2017-12-19 11:27:29 +01:00
|
|
|
if(polymorphic != null) {
|
2022-07-20 12:09:51 +02:00
|
|
|
parameters.put(TypePath.POLYMORPHIC_QUERY_PARAMETER, polymorphic.toString());
|
2017-12-18 15:34:25 +01:00
|
|
|
}
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.queryParams(parameters);
|
2017-12-18 15:34:25 +01:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
|
|
|
String json = HTTPUtility.getResponse(String.class, httpURLConnection);
|
2017-12-18 15:34:25 +01:00
|
|
|
|
2020-02-03 10:52:50 +01:00
|
|
|
logger.debug("Got schema for {} is {}", typeName, json);
|
2017-12-18 15:34:25 +01:00
|
|
|
return json;
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|