2021-02-19 12:25:00 +01:00
|
|
|
|
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;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Set;
|
2021-02-19 12:25:00 +01:00
|
|
|
|
|
|
|
|
|
import javax.ws.rs.client.Entity;
|
2021-04-13 18:10:02 +02:00
|
|
|
|
import javax.ws.rs.core.MediaType;
|
2021-02-19 12:25:00 +01:00
|
|
|
|
|
|
|
|
|
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;
|
2021-02-19 12:25:00 +01:00
|
|
|
|
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;
|
2021-02-19 12:25:00 +01:00
|
|
|
|
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-02-19 12:25:00 +01:00
|
|
|
|
|
2021-04-13 18:10:02 +02:00
|
|
|
|
import lombok.Getter;
|
2021-02-19 12:25:00 +01:00
|
|
|
|
import lombok.Setter;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
2021-04-13 18:10:02 +02:00
|
|
|
|
public abstract class AbstractGenericRESTClient implements GenericLoginClient{
|
2021-02-19 12:25:00 +01:00
|
|
|
|
|
2021-04-13 18:10:02 +02:00
|
|
|
|
@Getter
|
2021-02-19 12:25:00 +01:00
|
|
|
|
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<>();
|
2021-02-19 12:25:00 +01:00
|
|
|
|
|
|
|
|
|
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));
|
2021-02-19 12:25:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void register(Class<?> providerClass) {
|
2021-04-09 18:15:10 +02:00
|
|
|
|
toRegisterClasses.add(providerClass);
|
2021-02-19 12:25:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void register(Object provider) {
|
2021-04-09 18:15:10 +02:00
|
|
|
|
toRegisterObjects.add(provider);
|
2021-02-19 12:25:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);});
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 12:25:00 +01:00
|
|
|
|
if(basePath!=null) return toReturn.path(basePath);
|
2021-04-09 18:15:10 +02:00
|
|
|
|
|
2021-02-19 12:25:00 +01:00
|
|
|
|
else return toReturn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-09 18:15:10 +02:00
|
|
|
|
protected <T> T makeCall(Call<GXWebTargetAdapterRequest, T> call) throws Exception{
|
2021-02-19 12:25:00 +01:00
|
|
|
|
try{
|
|
|
|
|
return call.call(resolve());
|
|
|
|
|
}catch(RemoteException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}catch(Throwable t) {
|
|
|
|
|
throw new Exception(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void delete(String path) throws Exception {
|
2021-04-09 18:15:10 +02:00
|
|
|
|
check(resolve().path(path).delete(),null);
|
2021-02-19 12:25:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void post(String path,Entity<?> entity)throws Exception{
|
|
|
|
|
post(path,entity,null);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 18:15:10 +02:00
|
|
|
|
|
2021-04-21 12:48:12 +02:00
|
|
|
|
protected void post(String path,Object obj) throws Exception {
|
|
|
|
|
post(path,Entity.entity(obj, MediaType.APPLICATION_JSON_TYPE));
|
2021-04-09 18:15:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 12:25:00 +01:00
|
|
|
|
protected <T> T post(String path,Entity<?> entity,Class<T> returnClazz) throws Exception {
|
2021-04-09 18:15:10 +02:00
|
|
|
|
return makeCall(new Call<GXWebTargetAdapterRequest, T>() {
|
2021-02-19 12:25:00 +01:00
|
|
|
|
|
2021-04-09 18:15:10 +02:00
|
|
|
|
public T call(GXWebTargetAdapterRequest endpoint) throws Exception {
|
|
|
|
|
return check(endpoint.path(path).post(entity),returnClazz);
|
2021-02-19 12:25:00 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 12:48:12 +02:00
|
|
|
|
|
|
|
|
|
protected void put(String path,Entity<?> entity)throws Exception{
|
|
|
|
|
post(path,entity,null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-02-19 12:25:00 +01: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-02-19 12:25:00 +01:00
|
|
|
|
|
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-02-19 12:25:00 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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());
|
2021-02-19 12:25:00 +01:00
|
|
|
|
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;
|
|
|
|
|
}catch(Exception e) {
|
|
|
|
|
throw new RemoteException("Unable to read response from server.",e);
|
2021-02-19 12:25:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|