77 lines
2.1 KiB
Java
77 lines
2.1 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.core.Response;
|
|
import org.glassfish.jersey.client.ClientProperties;
|
|
|
|
import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
|
|
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
|
|
|
|
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.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
public abstract class AbstractGenericClient {
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
protected ConnectionDescriptor conn;
|
|
protected Client client;
|
|
|
|
|
|
|
|
|
|
public AbstractGenericClient(ConnectionDescriptor conn) {
|
|
|
|
}
|
|
|
|
|
|
private synchronized Client getWebClient() {
|
|
if(client==null) {
|
|
client = ClientBuilder.newClient()
|
|
.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
|
|
// client.register(MultiPartFeature.class);
|
|
}
|
|
return client;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
try {
|
|
return mapper.readValue(respString, clazz);
|
|
} catch (IOException e) {
|
|
throw new RemoteException("Unable to parse response from Zenodo. Content was : \n "+respString,e);
|
|
}
|
|
}
|
|
}
|
|
}
|