gcube-sdi-suite/sdi-generic-client/src/main/java/org/gcube/spatial/data/clients/AbstractGenericRESTClient.java

265 lines
7.2 KiB
Java
Raw Normal View History

package org.gcube.spatial.data.clients;
2021-04-21 12:48:12 +02:00
import java.io.IOException;
2021-04-09 18:15:10 +02:00
import java.io.UnsupportedEncodingException;
2021-04-29 15:54:02 +02:00
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
2021-04-09 18:15:10 +02:00
import java.util.HashSet;
2021-04-29 15:54:02 +02:00
import java.util.Map;
2021-04-09 18:15:10 +02:00
import java.util.Set;
import javax.ws.rs.client.Entity;
2021-04-13 18:10:02 +02:00
import javax.ws.rs.core.MediaType;
import org.gcube.common.clients.Call;
2021-04-09 18:15:10 +02:00
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
2021-04-13 18:10:02 +02:00
import org.gcube.spatial.data.sdi.model.credentials.AccessType;
import org.gcube.spatial.data.sdi.model.credentials.Credentials;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
2021-04-13 18:10:02 +02:00
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
2021-04-13 18:10:02 +02:00
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
2021-04-13 18:10:02 +02:00
public abstract class AbstractGenericRESTClient implements GenericLoginClient{
2021-04-13 18:10:02 +02:00
@Getter
private ConnectionDescriptor conn;
@Setter
private String basePath=null;
2021-04-13 18:10:02 +02:00
@Setter
private MediaType defaultMediaType=MediaType.APPLICATION_JSON_TYPE;
2021-04-09 18:15:10 +02:00
private Set<Class<?>> toRegisterClasses=new HashSet<Class<?>>();
private Set<Object> toRegisterObjects=new HashSet<>();
protected AbstractGenericRESTClient(ConnectionDescriptor conn) {
this.conn=conn;
2021-04-13 18:10:02 +02:00
// defaults
toRegisterClasses.add(FollowRedirectFilter.class);
}
2021-04-21 12:48:12 +02:00
protected String getExistsPath() {
return conn.getEndpoint();
}
@Override
public boolean exist() {
try {
get(getExistsPath());
return true;
}catch(IOException e) {
log.warn("Unable to check instance "+conn,e);
return false;
} catch (Exception e) {
2021-04-21 17:47:48 +02:00
log.info("Server replied with error. Ignore this if it requires authentication. "+conn,e);
return true;
2021-04-21 12:48:12 +02:00
}
}
@Override
public ConnectionDescriptor getInstance() {
return conn;
}
2021-04-13 18:10:02 +02:00
public void authenticate(AccessType type) {
log.info("Setting access type {} ",type);
conn.getCredentials().forEach((Credentials c)->{
if(c.getAccessType().equals(type))
setHTTPBasicAuth(c.getUsername(), c.getPassword());
});
}
public void authenticate() {
log.info("Setting default access type between credentials {}",conn.getCredentials());
if(conn.getCredentials().size()>0) {
Credentials c =conn.getCredentials().get(0);
setHTTPBasicAuth(c.getUsername(), c.getPassword());
}else log.info("No credentials found in connection descriptor {} ",conn);
};
public void setHTTPBasicAuth(String user,String pwd) {
log.info("Setting basic authentication, user : {} ",user);
toRegisterObjects.add(HttpAuthenticationFeature.universal(user, pwd));
}
protected void register(Class<?> providerClass) {
2021-04-09 18:15:10 +02:00
toRegisterClasses.add(providerClass);
}
protected void register(Object provider) {
2021-04-09 18:15:10 +02:00
toRegisterObjects.add(provider);
}
2021-04-09 18:15:10 +02:00
protected GXWebTargetAdapterRequest resolve() throws UnsupportedEncodingException {
GXWebTargetAdapterRequest toReturn =GXWebTargetAdapterRequest.newHTTPSRequest(conn.getEndpoint());
2021-04-13 18:10:02 +02:00
toRegisterClasses.forEach((Class<?> clazz)->{toReturn.register(clazz);});
toRegisterObjects.forEach((Object obj)->{toReturn.register(obj);});
if(basePath!=null) return toReturn.path(basePath);
2021-04-09 18:15:10 +02:00
else return toReturn;
}
2021-04-29 15:54:02 +02:00
protected <T> T makeCall(Call<GXWebTargetAdapterRequest, T> call) throws RemoteException, Exception{
try{
return call.call(resolve());
}catch(RemoteException e) {
throw e;
}catch(Throwable t) {
throw new Exception(t);
}
}
2021-04-29 15:54:02 +02:00
//****** METHODS
protected void delete(String path) throws RemoteException,Exception {
makeCall(new Call<GXWebTargetAdapterRequest, Object>() {
public Object call(GXWebTargetAdapterRequest endpoint) throws Exception {
return check(resolve().path(path).delete(),null);
};
});
}
2021-04-09 18:15:10 +02:00
2021-04-29 15:54:02 +02:00
protected void post(String path,Object obj) throws RemoteException,Exception {
2021-04-21 12:48:12 +02:00
post(path,Entity.entity(obj, MediaType.APPLICATION_JSON_TYPE));
2021-04-09 18:15:10 +02:00
}
2021-04-29 15:54:02 +02:00
protected <T> T post (String path,Entity<?> entity,Class<T> returnClazz,Map<String,?> queryParams) throws RemoteException,Exception{
2021-04-09 18:15:10 +02:00
return makeCall(new Call<GXWebTargetAdapterRequest, T>() {
2021-04-09 18:15:10 +02:00
public T call(GXWebTargetAdapterRequest endpoint) throws Exception {
2021-04-29 15:54:02 +02:00
Map<String,Object[]> actualMap=new HashMap<String, Object[]>();
queryParams.forEach((String k, Object v)->{
Object[] theArray=null;
if(v instanceof Collection) {}
else theArray=new Object[] {v};
actualMap.put(k, theArray);
});
// for(java.util.Map.Entry<String,?> e:queryParams.entrySet()) {
// String k=e.getKey();
// if(!actualMap.containsKey(k))actualMap.
// }
return check(endpoint.path(path).queryParams(actualMap).post(entity),returnClazz);
};
});
}
2021-04-29 15:54:02 +02:00
protected void post(String path,Entity<?> entity)throws RemoteException,Exception{
post(path,entity,null,Collections.emptyMap());
2021-04-21 12:48:12 +02:00
}
2021-04-29 19:39:52 +02:00
protected <T> T post(String path,Entity<?> entity,Class<T> returnClazz)throws RemoteException,Exception{
return post(path,entity,returnClazz,Collections.emptyMap());
}
2021-04-21 12:48:12 +02:00
protected void put(String path,Object obj) throws Exception {
post(path,Entity.entity(obj, MediaType.APPLICATION_JSON_TYPE));
}
protected <T> T put(String path,Entity<?> entity,Class<T> returnClazz) throws Exception {
return makeCall(new Call<GXWebTargetAdapterRequest, T>() {
public T call(GXWebTargetAdapterRequest endpoint) throws Exception {
return check(endpoint.path(path).post(entity),returnClazz);
};
});
}
2021-04-09 18:15:10 +02:00
protected String get(String path) throws Exception {
2021-04-13 18:10:02 +02:00
return get(path,String.class);
}
protected <T> T get(String path, Class<T> clazz) throws Exception {
return get(path,clazz,defaultMediaType);
2021-04-09 18:15:10 +02:00
}
2021-04-13 18:10:02 +02:00
protected <T> T get(String path, Class<T> clazz, String mediaType) throws Exception{
2021-04-09 18:15:10 +02:00
return makeCall(new Call<GXWebTargetAdapterRequest, T>() {
2021-04-09 18:15:10 +02:00
public T call(GXWebTargetAdapterRequest endpoint) throws Exception {
2021-04-13 18:10:02 +02:00
return check(endpoint.path(path).header("accept",mediaType).get(),clazz);
};
});
}
2021-04-13 18:10:02 +02:00
protected <T> T get(String path, Class<T> clazz, MediaType type) throws Exception{
return get(path,clazz,type+"");
}
2021-04-09 18:15:10 +02:00
protected static <T> T check(GXInboundResponse resp, Class<T> clazz) throws RemoteException{
try {
2021-04-13 18:10:02 +02:00
log.debug("Checking Response [Status : {}, Msg : {}]",resp.getHTTPCode(),resp.getMessage());
if(!resp.isSuccessResponse()) {
RemoteException e=new RemoteException("Error received from server ");
2021-04-09 18:15:10 +02:00
e.setRemoteMessage(resp.getMessage());
e.setResponseHTTPCode(resp.getHTTPCode());
e.setContent(resp.getStreamedContentAsString());
throw e;
}else {
2021-04-09 18:15:10 +02:00
if(clazz==null) return null;
if(clazz==String.class) return (T) resp.getStreamedContentAsString();
return resp.tryConvertStreamedContentFromJson(clazz);
}
}catch(RemoteException e) {
throw e;
2021-04-29 19:39:52 +02:00
}catch(Exception e1) {
log.error("Reporting Remote Exception for ",e1);
RemoteException e = new RemoteException("Unable to read response from server.", e1);
e.setRemoteMessage(resp.getMessage());
e.setResponseHTTPCode(resp.getHTTPCode());
try{
e.setContent(resp.getStreamedContentAsString());
}catch(IOException e2) {
log.error("Error while reading resposne content",e2);
}
throw e;
}
}
}