Added missing exist method

This commit is contained in:
Luca Frosini 2022-02-09 12:23:55 +01:00
parent 825c0ee20d
commit af76f9ff03
2 changed files with 75 additions and 31 deletions

View File

@ -13,11 +13,17 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.Cont
*/
public interface ResourceRegistryContextClient {
public List<Context> all() throws ResourceRegistryException;
public Context create(Context context) throws ContextAlreadyPresentException, ResourceRegistryException;
public String create(String context) throws ContextAlreadyPresentException, ResourceRegistryException;
public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException;
public boolean exist(UUID uuid) throws ResourceRegistryException;
public boolean exist(String uuid) throws ResourceRegistryException;
public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException;
@ -35,6 +41,4 @@ public interface ResourceRegistryContextClient {
public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException;
public List<Context> all() throws ResourceRegistryException;
}

View File

@ -13,6 +13,7 @@ import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
@ -57,6 +58,35 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
}
}
protected List<Context> getAllContextFromServer() throws ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", Context.NAME);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String all = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got contexts are {}", Context.NAME, all);
return ElementMapper.unmarshalList(Context.class, all);
} 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 List<Context> all() throws ResourceRegistryException {
ContextCache contextCache = ContextCache.getInstance();
return contextCache.getContexts();
}
protected String internalCreate(Context context) throws ContextAlreadyPresentException, ResourceRegistryException {
try {
Header header = context.getHeader();
@ -122,6 +152,45 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
}
}
public boolean existFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
HTTPUtility.getResponse(String.class, httpURLConnection);
return true;
} catch (NotFoundException e) {
return false;
} 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 boolean exist(String uuid) throws ResourceRegistryException {
return exist(UUID.fromString(uuid));
}
@Override
public boolean exist(UUID uuid) throws ResourceRegistryException {
try {
read(uuid);
return true;
}catch (ContextNotFoundException e) {
return false;
}
}
@Override
public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException {
return read(context.getHeader().getUUID());
@ -318,33 +387,4 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
}
}
protected List<Context> getAllContextFromServer() throws ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", Context.NAME);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String all = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got contexts are {}", Context.NAME, all);
return ElementMapper.unmarshalList(Context.class, all);
} 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 List<Context> all() throws ResourceRegistryException {
ContextCache contextCache = ContextCache.getInstance();
return contextCache.getContexts();
}
}