package org.gcube.informationsystem.resourceregistry.queries.templates; import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.List; import org.gcube.com.fasterxml.jackson.databind.JavaType; import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.common.gxhttp.reference.GXConnection; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.informationsystem.model.reference.ERElement; import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.QueryTemplatePath; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility; import org.gcube.informationsystem.utils.ElementMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ public class ResourceRegistryQueryTemplateClientImpl implements ResourceRegistryQueryTemplateClient { private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryQueryTemplateClientImpl.class); protected final String address; public ResourceRegistryQueryTemplateClientImpl(String address) { this.address = address; } @Override public List all() throws ResourceRegistryException { try { logger.trace("Going to list {}s", QueryTemplate.NAME); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryQueryTemplateClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String all = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got {}s are {}", QueryTemplate.NAME, all); JavaType type = ElementMapper.getObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, QueryTemplate.class); return ElementMapper.getObjectMapper().readValue(all, type); } catch(ResourceRegistryException e) { // logger.trace("Error Creating {}", facet, e); throw e; } catch(Exception e) { // logger.trace("Error Creating {}", facet, e); throw new RuntimeException(e); } } @Override public QueryTemplate create(QueryTemplate queryTemplate) throws QueryTemplateAlreadyPresentException, ResourceRegistryException { try { String queryTemplateString = ElementMapper.marshal(queryTemplate); String res = create(queryTemplateString); return ElementMapper.unmarshal(QueryTemplate.class, res); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public String create(String queryTemplate) throws QueryTemplateAlreadyPresentException, ResourceRegistryException { try { logger.trace("Going to create: {}", queryTemplate); QueryTemplate qt = ElementMapper.unmarshal(QueryTemplate.class, queryTemplate); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryQueryTemplateClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); gxHTTPStringRequest.path(qt.getName()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(queryTemplate); String c = HTTPUtility.getResponse(String.class, httpURLConnection); logger.trace("{} successfully created", c); return c; } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public boolean exist(String queryTemplateName) throws ResourceRegistryException { try { logger.trace("Going to read {} with name {}", QueryTemplate.NAME, queryTemplateName); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryQueryTemplateClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); gxHTTPStringRequest.path(queryTemplateName); HttpURLConnection httpURLConnection = gxHTTPStringRequest.head(); HTTPUtility.getResponse(String.class, httpURLConnection); return true; } catch (NotFoundException e) { return false; } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public boolean exist(QueryTemplate queryTemplate) throws ResourceRegistryException { return exist(queryTemplate.getName()); } @Override public QueryTemplate read(QueryTemplate queryTemplate) throws QueryTemplateNotFoundException, ResourceRegistryException { return read(queryTemplate.getName()); } @Override public QueryTemplate read(String queryTemplateName) throws QueryTemplateNotFoundException, ResourceRegistryException { try { String queryTemplate = readAsString(queryTemplateName); return ElementMapper.unmarshal(QueryTemplate.class, queryTemplate); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public String readAsString(String queryTemplateName) throws QueryTemplateNotFoundException, ResourceRegistryException { try { logger.trace("Going to read {} with name {}", QueryTemplate.NAME, queryTemplateName); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryQueryTemplateClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); gxHTTPStringRequest.path(queryTemplateName); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String c = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got {} is {}", QueryTemplate.NAME, c); return c; } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public QueryTemplate update(QueryTemplate queryTemplate) throws QueryTemplateNotFoundException, ResourceRegistryException { try { String queryTemplateString = ElementMapper.marshal(queryTemplate); String res = update(queryTemplateString); return ElementMapper.unmarshal(QueryTemplate.class, res); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public String update(String queryTemplate) throws QueryTemplateNotFoundException, ResourceRegistryException { try { logger.trace("Going to update: {}", queryTemplate); QueryTemplate qt = ElementMapper.unmarshal(QueryTemplate.class, queryTemplate); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryQueryTemplateClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); gxHTTPStringRequest.path(qt.getName()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(queryTemplate); String c = HTTPUtility.getResponse(String.class, httpURLConnection); logger.trace("{} successfully updated", c); return c; } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public String runGetString(String name) throws QueryTemplateNotFoundException, ResourceRegistryException { return run(name, ""); } @Override public List run(String name) throws QueryTemplateNotFoundException, ResourceRegistryException { try { String ret = runGetString(name); JavaType type = ElementMapper.getObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, ERElement.class); return ElementMapper.getObjectMapper().readValue(ret, type); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public List run(QueryTemplate queryTemplate) throws QueryTemplateNotFoundException, ResourceRegistryException { return run(queryTemplate.getName()); } @Override public String run(String name, String params) throws QueryTemplateNotFoundException, ResourceRegistryException { try { if(params==null || params.compareTo("")==0) { logger.trace("Going to run {} using default parameters", QueryTemplate.NAME); params = null; }else { logger.trace("Going to run {} with the following parameters {}", QueryTemplate.NAME, params); } GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryQueryTemplateClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); gxHTTPStringRequest.path(name); HttpURLConnection httpURLConnection = gxHTTPStringRequest.post(params); String c = HTTPUtility.getResponse(String.class, httpURLConnection); logger.trace("The result of the query is {}", c); return c; } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public List run(String name, JsonNode jsonNode) throws QueryTemplateNotFoundException, ResourceRegistryException { try { ObjectMapper objectMapper = new ObjectMapper(); String ret = run(name, objectMapper.writeValueAsString(jsonNode)); JavaType type = ElementMapper.getObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, ERElement.class); return ElementMapper.getObjectMapper().readValue(ret, type); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } @Override public List run(QueryTemplate queryTemplate, JsonNode jsonNode) throws QueryTemplateNotFoundException, ResourceRegistryException { return run(queryTemplate.getName(), jsonNode); } @Override public boolean delete(QueryTemplate queryTemplate) throws QueryTemplateNotFoundException, ResourceRegistryException { return delete(queryTemplate.getName()); } @Override public boolean delete(String queryTemplateName) throws QueryTemplateNotFoundException, ResourceRegistryException { try { logger.trace("Going to delete {} with name {}", QueryTemplate.NAME, queryTemplateName); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryQueryTemplateClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); gxHTTPStringRequest.path(queryTemplateName); HttpURLConnection httpURLConnection = gxHTTPStringRequest.delete(); HTTPUtility.getResponse(String.class, httpURLConnection); boolean deleted = true; logger.info("{} with name {} {}", QueryTemplate.NAME, queryTemplateName, deleted ? "successfully deleted" : "was NOT deleted"); return deleted; } catch(ResourceRegistryException e) { // logger.trace("Error Creating {}", facet, e); throw e; } catch(Exception e) { // logger.trace("Error Creating {}", facet, e); throw new RuntimeException(e); } } }