gcube-cms-suite/geoportal-client/src/main/java/org/gcube/application/geoportal/client/DefaultDocumentsClient.java

303 lines
13 KiB
Java
Raw Normal View History

2022-02-02 18:34:32 +01:00
package org.gcube.application.geoportal.client;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.gcube.application.geoportal.client.utils.Serialization;
2022-05-17 11:02:49 +02:00
import org.gcube.application.geoportal.common.faults.InvalidRequestException;
2022-03-04 14:23:20 +01:00
import org.gcube.application.geoportal.common.model.document.Project;
2022-03-10 18:15:10 +01:00
import org.gcube.application.geoportal.common.model.configuration.Configuration;
2022-05-17 15:43:19 +02:00
import org.gcube.application.geoportal.common.model.document.access.Access;
2022-02-02 18:34:32 +01:00
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.StepExecutionRequest;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
2022-03-04 14:23:20 +01:00
import org.gcube.application.geoportal.common.rest.Projects;
2022-02-02 18:34:32 +01:00
import org.gcube.common.clients.Call;
import org.gcube.common.clients.delegates.ProxyDelegate;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Iterator;
@RequiredArgsConstructor
@Slf4j
2022-03-07 13:59:24 +01:00
public class DefaultDocumentsClient<T extends Project> implements Projects<T> {
2022-02-02 18:34:32 +01:00
@NonNull
protected final ProxyDelegate<WebTarget> delegate;
@NonNull
protected final String profileID;
2022-02-04 17:45:47 +01:00
@NonNull
protected final Class<T> managedClass;
2022-02-02 18:34:32 +01:00
2022-02-04 17:45:47 +01:00
public Class<T> getManagedClass() {
return managedClass;
}
2022-02-02 18:34:32 +01:00
@Override
public T createNew(Document toCreate) throws RemoteException {
try {
2022-03-04 14:23:20 +01:00
log.debug("Creating Profiled Document (class {}, useCaseDescriptor {}) with content {} ",
2022-02-02 18:34:32 +01:00
getManagedClass(),profileID, toCreate);
Call<WebTarget, T> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).request(MediaType.APPLICATION_JSON).
2022-02-02 18:34:32 +01:00
post(Entity.entity(toCreate, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn = delegate.make(call);
2022-02-23 17:13:22 +01:00
log.info("Registered {} profiled {} ",toReturn.getId(),profileID);
2022-02-02 18:34:32 +01:00
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public void deleteById(String id) throws RemoteException {
deleteById(id,false);
}
@Override
public void deleteById(String id, Boolean force) throws RemoteException {
try {
2022-03-04 14:23:20 +01:00
log.debug("Deleting ID {}  useCaseDescriptor {}  force {} ", id, profileID, force);
2022-02-02 18:34:32 +01:00
Call<WebTarget, T> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).path(id).
2022-02-02 18:34:32 +01:00
queryParam(InterfaceConstants.Parameters.FORCE,force).
2022-05-13 18:41:43 +02:00
request(MediaType.APPLICATION_JSON).delete(),null);
2022-02-02 18:34:32 +01:00
};
delegate.make(call);
2022-03-04 14:23:20 +01:00
log.info("Deleted ID {}  useCaseDescriptor {}  force {} ", id, profileID, force);
2022-02-02 18:34:32 +01:00
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T getById(String id) throws RemoteException {
try {
2022-03-04 14:23:20 +01:00
log.info("Loading Document ID {} (class {}, useCaseDescriptor {})",
2022-02-02 18:34:32 +01:00
id, getManagedClass(),profileID);
Call<WebTarget, T> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).path(id).
2022-02-02 18:34:32 +01:00
request(MediaType.APPLICATION_JSON).get(), getManagedClass());
};
return delegate.make(call);
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public Configuration getConfiguration() throws RemoteException {
try {
2022-03-04 14:23:20 +01:00
log.info("Loading Configuration for useCaseDescriptor {}", profileID);
2022-02-02 18:34:32 +01:00
Call<WebTarget, Configuration> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).path(InterfaceConstants.Methods.CONFIGURATION_PATH).
2022-02-02 18:34:32 +01:00
request(MediaType.APPLICATION_JSON).get(), Configuration.class);
};
return delegate.make(call);
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public Iterator<T> query(QueryRequest request) throws RemoteException {
return queryForClass(request,getManagedClass());
}
@Override
public <C> Iterator<C> queryForClass(QueryRequest request,Class<C> clazz) throws RemoteException {
String jsonString=queryForJSON(request);
log.debug("Deserializing query Result as {} ",clazz);
try {
return Serialization.readCollection(jsonString,clazz);
} catch (IOException e) {
log.error("Unable to deserialize result as "+clazz,e);
2022-04-22 11:04:25 +02:00
log.debug("Query request was {} ",request);
log.debug("Query result was {} ",jsonString);
2022-02-02 18:34:32 +01:00
throw new RemoteException("Invalid format for submitted query");
}
}
@Override
public String queryForJSON(QueryRequest request) throws RemoteException {
try {
2022-03-04 14:23:20 +01:00
log.debug("Querying useCaseDescriptor {}  for {}",profileID,request);
2022-02-02 18:34:32 +01:00
Call<WebTarget, String> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).path(InterfaceConstants.Methods.QUERY_PATH).
request(MediaType.APPLICATION_JSON).post(Entity.entity(request,MediaType.APPLICATION_JSON)), String.class);
2022-02-02 18:34:32 +01:00
};
return delegate.make(call);
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T performStep(String id, StepExecutionRequest request) throws RemoteException{
try {
2022-03-04 14:23:20 +01:00
log.debug("Executing step on {} (class {}, useCaseDescriptor {}) with request {} ",
2022-02-02 18:34:32 +01:00
id, getManagedClass(),profileID, request);
Call<WebTarget, T> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).path(id).request(MediaType.APPLICATION_JSON).
2022-02-02 18:34:32 +01:00
post(Entity.entity(request, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn = delegate.make(call);
2022-03-04 14:23:20 +01:00
log.info("Executed STEP {} on {} [useCaseDescriptor {}, class {}] ",request.getStepID(),
2022-02-02 18:34:32 +01:00
id,profileID,getManagedClass());
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
2022-05-17 11:02:49 +02:00
public T registerFileSet(String id, RegisterFileSetRequest req) throws RemoteException, InvalidRequestException {
2022-02-02 18:34:32 +01:00
try {
2022-03-04 14:23:20 +01:00
log.debug("Registering FileSet on {} (class {}, useCaseDescriptor {}) with request {} ",
2022-05-17 11:02:49 +02:00
id, getManagedClass(), profileID, req);
req.validate();
2022-02-02 18:34:32 +01:00
Call<WebTarget, T> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).path(InterfaceConstants.Methods.REGISTER_FILES_PATH)
2022-02-02 18:34:32 +01:00
.path(id).request(MediaType.APPLICATION_JSON).
2022-05-17 11:02:49 +02:00
post(Entity.entity(req, MediaType.APPLICATION_JSON)), getManagedClass());
2022-02-02 18:34:32 +01:00
};
T toReturn = delegate.make(call);
2022-03-04 14:23:20 +01:00
log.info("Registered FileSet on {} [useCaseDescriptor {}, class {}]  with {}",
2022-05-17 11:02:49 +02:00
id, profileID, getManagedClass(), req);
2022-02-02 18:34:32 +01:00
return toReturn;
2022-05-17 11:02:49 +02:00
}catch (InvalidRequestException e){
log.error("Invalid Request ",e);
throw e;
2022-02-02 18:34:32 +01:00
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T deleteFileSet(String id, String path, Boolean force) throws RemoteException {
try {
2022-03-04 14:23:20 +01:00
log.debug("Deleting Fileset for ID {}  [useCaseDescriptor {}  , class {}] at {} (force {} )",
2022-02-02 18:34:32 +01:00
id, profileID,getManagedClass(),path, force);
Call<WebTarget, T> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).path(InterfaceConstants.Methods.DELETE_FILES_PATH).
2022-02-02 18:34:32 +01:00
path(id).queryParam(InterfaceConstants.Parameters.FORCE,force).
request(MediaType.APPLICATION_JSON).
post(Entity.entity(path, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn=delegate.make(call);
2022-03-04 14:23:20 +01:00
log.info("Deleted ID {}  useCaseDescriptor {}  force {} ", id, profileID, force);
2022-02-02 18:34:32 +01:00
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
2022-05-17 15:43:19 +02:00
@Override
public T forceUnlock(String id) throws RemoteException {
try {
log.warn("Force Unlock of {} [useCaseDescriptor {} , class {}]",
id, profileID,getManagedClass());
Call<WebTarget, T> call = endpoint -> {
return ResponseCommons.check(endpoint.path(profileID).path(InterfaceConstants.Methods.FORCE_UNLOCK).path(id).
request(MediaType.APPLICATION_JSON).
put(Entity.json("")),getManagedClass());
};
T toReturn=delegate.make(call);
log.info("Unlocked ID {} useCaseDescriptor {}", id, profileID);
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T setAccessPolicy(String id, Access toSet) throws RemoteException {
try {
log.info("Setting Access of {} [useCaseDescriptor {} , class {}] as {}",
id, profileID,getManagedClass(),toSet);
Call<WebTarget, T> call = endpoint -> {
return ResponseCommons.check(endpoint.path(profileID).path(InterfaceConstants.Methods.SET_PROJECT_ACCESS_POLICY).path(id).
request(MediaType.APPLICATION_JSON).
put(Entity.json(toSet)),getManagedClass());
};
T toReturn=delegate.make(call);
log.debug("Updated Access of ID {} useCaseDescriptor {}", id, profileID);
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
2022-02-02 18:34:32 +01:00
@Override
public T updateDocument(String id, Document updatedDocument) throws RemoteException {
try {
2022-05-17 15:43:19 +02:00
log.debug("Updateing {} [useCaseDescriptor {} , class {}] with ",
2022-02-02 18:34:32 +01:00
id, profileID,getManagedClass(),updatedDocument);
Call<WebTarget, T> call = endpoint -> {
2022-03-07 13:59:24 +01:00
return ResponseCommons.check(endpoint.path(profileID).path(id).
2022-02-02 18:34:32 +01:00
request(MediaType.APPLICATION_JSON).
put(Entity.entity(updatedDocument, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn=delegate.make(call);
2022-05-17 15:43:19 +02:00
log.info("Updated ID {} useCaseDescriptor {}", id, profileID);
2022-02-02 18:34:32 +01:00
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
2022-03-07 13:59:24 +01:00
2022-02-02 18:34:32 +01:00
}