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

154 lines
4.0 KiB
Java

package org.gcube.spatial.data.clients;
import java.io.IOException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.gcube.common.clients.Call;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.json.simple.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class AbstractGenericRESTClient {
private static ObjectMapper mapper = new ObjectMapper();
static {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
mapper.setSerializationInclusion(Include.NON_NULL);
}
@Getter
private ClientConfig config=new ClientConfig();
private ConnectionDescriptor conn;
private Client client;
@Setter
private String basePath=null;
protected AbstractGenericRESTClient(ConnectionDescriptor conn) {
this.conn=conn;
config.register(AuthorizationFilter.class);
}
protected void register(Class<?> providerClass) {
config.register(providerClass);
client=null;
}
protected void register(Object provider) {
config.register(provider);
client=null;
}
public Client getClient() {
if(client==null) {
client = ClientBuilder.newClient(config)
.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
}
return client;
}
protected WebTarget resolve() {
WebTarget toReturn =getClient().target(conn.getEndpoint());
if(basePath!=null) return toReturn.path(basePath);
else return toReturn;
}
protected <T> T makeCall(Call<WebTarget, T> call) throws Exception{
try{
return call.call(resolve());
}catch(RemoteException e) {
throw e;
}catch(Throwable t) {
throw new Exception(t);
}
}
protected JSONObject get(String path) throws Exception {
return get(path,JSONObject.class);
}
protected void delete(String path) throws Exception {
check(resolve().path(path).request(MediaType.APPLICATION_JSON).delete(),null);
}
protected void post(String path,Entity<?> entity)throws Exception{
post(path,entity,null);
}
protected <T> T post(String path,Entity<?> entity,Class<T> returnClazz) throws Exception {
return makeCall(new Call<WebTarget, T>() {
public T call(WebTarget endpoint) throws Exception {
return check(endpoint.path(path).request(MediaType.APPLICATION_JSON).
post(entity),returnClazz);
};
});
}
protected <T> T get(String path, Class<T> clazz) throws Exception{
return makeCall(new Call<WebTarget, T>() {
public T call(WebTarget endpoint) throws Exception {
return check(endpoint.path(path).request(MediaType.APPLICATION_JSON).get(),clazz);
};
});
}
protected static <T> T check(Response resp, Class<T> clazz) throws RemoteException{
if(resp.getStatus()<200||resp.getStatus()>=300) {
String remoteMessage=resp.readEntity(String.class);
Integer httpCode=resp.getStatus();
RemoteException e=new RemoteException("RESP STATUS IS "+httpCode+". Message : "+remoteMessage);
e.setRemoteMessage(remoteMessage);
e.setResponseHTTPCode(httpCode);
throw e;
}else {
if(clazz==null) return null;
String respString=resp.readEntity(String.class);
if(clazz==String.class) return (T) respString;
try {
return mapper.readValue(respString, clazz);
} catch (IOException e) {
throw new RemoteException("Unable to parse response from Zenodo. Content was : \n "+respString,e);
}
}
}
}