gxRest integration

This commit is contained in:
Fabio Sinibaldi 2021-04-09 18:15:10 +02:00
parent 3ed3648739
commit 4822913bc7
24 changed files with 619 additions and 287 deletions

View File

@ -1,14 +1,25 @@
package org.gcube.spatial.data.clients.geoserver; package org.gcube.spatial.data.clients.geoserver;
import java.io.InputStream;
import java.util.List; import java.util.List;
//import org.apache.commons.httpclient.NameValuePair;
//import org.gcube.application.geoportal.service.engine.RESTFeatureType;
//import org.gcube.application.geoportal.service.engine.RESTLayer;
import org.gcube.spatial.data.sdi.model.faults.RemoteException; import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
//import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.UploadMethod;
//import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy;
public interface GSRESTClient { public interface GSRESTClient {
// Management
public boolean exist();
// ********** READ OPS // ********** READ OPS
// WS // WS
@ -20,6 +31,9 @@ public interface GSRESTClient {
// DS // DS
public JSONObject getDataStoresInWorkspace(String ws)throws RemoteException,Exception; public JSONObject getDataStoresInWorkspace(String ws)throws RemoteException,Exception;
public List<String> getDataStoresNamesInWorkspace(String ws)throws RemoteException,Exception; public List<String> getDataStoresNamesInWorkspace(String ws)throws RemoteException,Exception;
public JSONObject getDataStore(String workspace,String dsName)throws RemoteException,Exception;
// SLD // SLD
public JSONObject getStyles()throws RemoteException,Exception; public JSONObject getStyles()throws RemoteException,Exception;
@ -28,6 +42,18 @@ public interface GSRESTClient {
public List<String> getStylesNamesinWorkspace(String ws)throws RemoteException,Exception; public List<String> getStylesNamesinWorkspace(String ws)throws RemoteException,Exception;
public JSONObject getStylesByLayer(String layer)throws RemoteException,Exception; public JSONObject getStylesByLayer(String layer)throws RemoteException,Exception;
public List<String> getStylesNamesByLayer(String layer)throws RemoteException,Exception; public List<String> getStylesNamesByLayer(String layer)throws RemoteException,Exception;
public String getSLD(String styleName)throws RemoteException,Exception;
// FeatureTypes
// public JSONObject getFeatureType(JSONObject layer)
/**
* RESTLayer l=reader.getLayer(workspace, toSetLayerName);
RESTFeatureType f= reader.getFeatureType(l);
* @param ws
* @throws RemoteException
* @throws Exception
*/
// *********** CREATE OPS // *********** CREATE OPS
public void createWorkspace(String ws)throws RemoteException,Exception; public void createWorkspace(String ws)throws RemoteException,Exception;
@ -39,10 +65,53 @@ public interface GSRESTClient {
// publish local files // publish local files
// create store for postgis db // create store for postgis db
/**
* getDataStoreManager().create(workspace, encoder)
*/
/**
* if(!gis.getCurrentGeoServer().getPublisher().publishStyle(sldFile, name)
*
*/
/**
*
* gis.getCurrentGeoServer().getPublisher().publishDBLayer(workspace, storeName, fte, layerEncoder))
*/
/**
* public boolean publishShp(String workspace, String storeName, NameValuePair[] storeParams,
String datasetName, UploadMethod method, URI shapefile, String srs, String defaultStyle)
throws FileNotFoundException, IllegalArgumentException {
return publishShp(workspace, storeName, storeParams, datasetName, method, shapefile, srs,
null, ProjectionPolicy.FORCE_DECLARED, defaultStyle);
}
* @param ws
* @throws RemoteException
* @throws Exception
*/
// // +********** DELETE OPS // // +********** DELETE OPS
/**
* publisher.removeWorkspace(toDelete.getWorkspace(), true);
* @param ws
* @throws RemoteException
* @throws Exception
*/
public void deleteWorkspace(String ws)throws RemoteException,Exception; public void deleteWorkspace(String ws)throws RemoteException,Exception;
public void deleteStyle(String style)throws RemoteException,Exception; public void deleteStyle(String style)throws RemoteException,Exception;
/**
* publisher.removeDatastore(toDelete.getWorkspace(), toDelete.getStore(), true);
* @param ds
* @throws RemoteException
* @throws Exception
*/
public void deleteDataStore(String ds)throws RemoteException,Exception; public void deleteDataStore(String ds)throws RemoteException,Exception;
public void deleteLayer(String layer)throws RemoteException,Exception; public void deleteLayer(String layer)throws RemoteException,Exception;
} }

View File

@ -125,8 +125,6 @@ public class GSRESTClientImpl extends AbstractGenericRESTClient implements GSRES
} }
@Override @Override
public List<String> getStylesNamesByLayer(String layer) throws RemoteException, Exception { public List<String> getStylesNamesByLayer(String layer) throws RemoteException, Exception {
DocumentContext sourceCtx=JsonPath.using(JSON_PATH_ALWAYS_LIST_CONFIG).parse(getStylesByLayer(layer).toString()); DocumentContext sourceCtx=JsonPath.using(JSON_PATH_ALWAYS_LIST_CONFIG).parse(getStylesByLayer(layer).toString());
@ -134,6 +132,18 @@ public class GSRESTClientImpl extends AbstractGenericRESTClient implements GSRES
} }
@Override
public JSONObject getDataStore(String workspace, String dsName) throws RemoteException, Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public String getSLD(String styleName) throws RemoteException, Exception {
// TODO Auto-generated method stub
return null;
}
//************* DELETE OPS //************* DELETE OPS
@Override @Override
@ -169,4 +179,18 @@ public class GSRESTClientImpl extends AbstractGenericRESTClient implements GSRES
public void createWorkspace(String ws) throws RemoteException, Exception { public void createWorkspace(String ws) throws RemoteException, Exception {
post(WS_BASE_PATH,Entity.entity(new JSONObject().put("name", ws),MediaType.APPLICATION_JSON)); post(WS_BASE_PATH,Entity.entity(new JSONObject().put("name", ws),MediaType.APPLICATION_JSON));
} }
@Override
public boolean exist() {
// TODO Auto-generated method stub
return false;
}
} }

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
@ -10,57 +11,79 @@
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>Generic SDI Client</name> <name>Generic SDI Client</name>
<scm> <scm>
<connection>scm:git:${gitBaseUrl}/gcube-sdi-suite</connection> <connection>scm:git:${gitBaseUrl}/gcube-sdi-suite</connection>
<developerConnection>scm:git:${gitBaseUrl}/gcube-sdi-suite</developerConnection> <developerConnection>scm:git:${gitBaseUrl}/gcube-sdi-suite</developerConnection>
<url>${gitBaseUrl}/gcube-sdi-suite</url> <url>${gitBaseUrl}/gcube-sdi-suite</url>
</scm> </scm>
<dependencies> <dependencies>
<dependency> <dependency>
<artifactId>sdi-interface</artifactId> <artifactId>sdi-interface</artifactId>
<groupId>org.gcube.spatial.data</groupId> <groupId>org.gcube.spatial.data</groupId>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-client</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.common</groupId>
<artifactId>common-gcube-calls</artifactId> <artifactId>gxJRS</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-fw-clients</artifactId> <artifactId>common-fw-clients</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-generic-clients</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.gcube.common</groupId> -->
<!-- <artifactId>authorization-client</artifactId> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.gcube.core</groupId> -->
<!-- <artifactId>common-gcube-calls</artifactId> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.gcube.core</groupId> -->
<!-- <artifactId>common-fw-clients</artifactId> -->
<!-- </dependency> -->
<!-- read JSON --> <!-- read JSON -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</dependency>
<dependency> <!-- <dependency> -->
<groupId>org.glassfish.jersey.media</groupId> <!-- <groupId>org.glassfish.jersey.core</groupId> -->
<artifactId>jersey-media-json-jackson</artifactId> <!-- <artifactId>jersey-client</artifactId> -->
</dependency> <!-- </dependency> -->
<dependency> <!-- <dependency> -->
<groupId>org.glassfish.jersey.media</groupId> <!-- <groupId>org.glassfish.jersey.media</groupId> -->
<artifactId>jersey-media-multipart</artifactId> <!-- <artifactId>jersey-media-json-jackson</artifactId> -->
</dependency> <!-- </dependency> -->
<dependency> <!-- <dependency> -->
<groupId>com.jayway.jsonpath</groupId> <!-- <groupId>org.glassfish.jersey.media</groupId> -->
<artifactId>json-path</artifactId> <!-- <artifactId>jersey-media-multipart</artifactId> -->
<version>2.4.0</version> <!-- </dependency> -->
</dependency>
<!-- <dependency> -->
<!-- <groupId>com.jayway.jsonpath</groupId> -->
<!-- <artifactId>json-path</artifactId> -->
<!-- <version>2.4.0</version> -->
<!-- </dependency> -->
<!-- TEST --> <!-- TEST -->
@ -68,10 +91,10 @@
<artifactId>sdi-test-commons</artifactId> <artifactId>sdi-test-commons</artifactId>
<groupId>org.gcube.spatial.data</groupId> <groupId>org.gcube.spatial.data</groupId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<description>Java library used to implement SDI clients.</description> <description>Java library used to implement SDI clients.</description>
</project> </project>

View File

@ -1,87 +1,57 @@
package org.gcube.spatial.data.clients; package org.gcube.spatial.data.clients;
import java.io.IOException; import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity; 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.common.clients.Call;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor; import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
import org.gcube.spatial.data.sdi.model.faults.RemoteException; 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.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public abstract class AbstractGenericRESTClient { 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 ConnectionDescriptor conn;
private Client client;
@Setter @Setter
private String basePath=null; private String basePath=null;
private Set<Class<?>> toRegisterClasses=new HashSet<Class<?>>();
private Set<Object> toRegisterObjects=new HashSet<>();
protected AbstractGenericRESTClient(ConnectionDescriptor conn) { protected AbstractGenericRESTClient(ConnectionDescriptor conn) {
this.conn=conn; this.conn=conn;
config.register(AuthorizationFilter.class);
} }
protected void register(Class<?> providerClass) { protected void register(Class<?> providerClass) {
config.register(providerClass); toRegisterClasses.add(providerClass);
client=null;
} }
protected void register(Object provider) { protected void register(Object provider) {
config.register(provider); toRegisterObjects.add(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()); protected GXWebTargetAdapterRequest resolve() throws UnsupportedEncodingException {
GXWebTargetAdapterRequest toReturn =GXWebTargetAdapterRequest.newHTTPSRequest(conn.getEndpoint());
if(basePath!=null) return toReturn.path(basePath); if(basePath!=null) return toReturn.path(basePath);
else return toReturn; else return toReturn;
} }
protected <T> T makeCall(Call<WebTarget, T> call) throws Exception{ protected <T> T makeCall(Call<GXWebTargetAdapterRequest, T> call) throws Exception{
try{ try{
return call.call(resolve()); return call.call(resolve());
}catch(RemoteException e) { }catch(RemoteException e) {
@ -91,57 +61,67 @@ public abstract class AbstractGenericRESTClient {
} }
} }
protected JSONObject get(String path) throws Exception {
return get(path,JSONObject.class);
}
protected void delete(String path) throws Exception { protected void delete(String path) throws Exception {
check(resolve().path(path).request(MediaType.APPLICATION_JSON).delete(),null); check(resolve().path(path).delete(),null);
} }
protected void post(String path,Entity<?> entity)throws Exception{ protected void post(String path,Entity<?> entity)throws Exception{
post(path,entity,null); post(path,entity,null);
} }
protected void post(String path,Object obj) {
}
protected <T> T post(String path,Entity<?> entity,Class<T> returnClazz) throws Exception { protected <T> T post(String path,Entity<?> entity,Class<T> returnClazz) throws Exception {
return makeCall(new Call<WebTarget, T>() { return makeCall(new Call<GXWebTargetAdapterRequest, T>() {
public T call(WebTarget endpoint) throws Exception { public T call(GXWebTargetAdapterRequest endpoint) throws Exception {
return check(endpoint.path(path).request(MediaType.APPLICATION_JSON). return check(endpoint.path(path).post(entity),returnClazz);
post(entity),returnClazz);
}; };
}); });
} }
protected String get(String path) throws Exception {
return get(path,null);
}
protected <T> T get(String path, Class<T> clazz) throws Exception{ protected <T> T get(String path, Class<T> clazz) throws Exception{
return makeCall(new Call<WebTarget, T>() { return makeCall(new Call<GXWebTargetAdapterRequest, T>() {
public T call(WebTarget endpoint) throws Exception { public T call(GXWebTargetAdapterRequest endpoint) throws Exception {
return check(endpoint.path(path).request(MediaType.APPLICATION_JSON).get(),clazz); return check(endpoint.path(path).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(); protected static <T> T check(GXInboundResponse resp, Class<T> clazz) throws RemoteException{
RemoteException e=new RemoteException("RESP STATUS IS "+httpCode+". Message : "+remoteMessage); try {
e.setRemoteMessage(remoteMessage); log.debug("Checking Response {}",resp);
e.setResponseHTTPCode(httpCode); if(resp.hasGXError()) {
RemoteException e=new RemoteException("Error received from server");
e.setRemoteMessage(resp.getMessage());
e.setResponseHTTPCode(resp.getHTTPCode());
e.setContent(resp.getStreamedContentAsString());
throw e; throw e;
}else { }else {
if(clazz==null) return null; if(clazz==null) return null;
String respString=resp.readEntity(String.class);
if(clazz==String.class) return (T) respString; if(clazz==String.class) return (T) resp.getStreamedContentAsString();
try {
return mapper.readValue(respString, clazz); return resp.tryConvertStreamedContentFromJson(clazz);
} catch (IOException e) { }
throw new RemoteException("Unable to parse response from Zenodo. Content was : \n "+respString,e); }catch(RemoteException e) {
} throw e;
}catch(Exception e) {
throw new RemoteException("Unable to read response from server.",e);
} }
} }

View File

@ -1,25 +1,18 @@
package org.gcube.spatial.data.clients; package org.gcube.spatial.data.clients;
import java.io.IOException;
import java.util.Collections;
import java.util.Map.Entry;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter; import javax.ws.rs.client.ClientRequestFilter;
import org.gcube.common.calls.Call; public class AuthorizationFilter {
import org.gcube.common.calls.Interceptors; // implements ClientRequestFilter {
import org.gcube.common.calls.Request;
public class AuthorizationFilter implements ClientRequestFilter {
@Override // @Override
public void filter(final ClientRequestContext rc) throws IOException { // public void filter(final ClientRequestContext rc) throws IOException {
Request requestContext = Interceptors.executeRequestChain(new Call()); // Request requestContext = Interceptors.executeRequestChain(new Call());
//
for (Entry<String, String> entry: requestContext.getHeaders()){ // for (Entry<String, String> entry: requestContext.getHeaders()){
rc.getHeaders().put(entry.getKey(), Collections.singletonList((Object)entry.getValue())); // rc.getHeaders().put(entry.getKey(), Collections.singletonList((Object)entry.getValue()));
} // }
} // }
} }

View File

@ -1,7 +1,6 @@
package org.gcube.spatial.data.clients; package org.gcube.spatial.data.clients;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor; import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
public class TestClient extends AbstractGenericRESTClient{ public class TestClient extends AbstractGenericRESTClient{
@ -14,10 +13,10 @@ public class TestClient extends AbstractGenericRESTClient{
//
//
//
public void setHttpBasicAuth(String user,String pwd) { // public void setHttpBasicAuth(String user,String pwd) {
super.register(HttpAuthenticationFeature.universal(user, pwd)); // super.register(HttpAuthenticationFeature.universal(user, pwd));
} // }
} }

View File

@ -1,14 +1,28 @@
package org.gcube.spatial.data.clients; package org.gcube.spatial.data.clients;
import static org.junit.Assume.assumeTrue;
import org.gcube.sdi.test.GCubeSDITest;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor; import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
import org.gcube.spatial.data.clients.model.engine.Engine;
import org.gcube.spatial.data.sdi.model.faults.RemoteException; import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
public class Tests { public class Tests extends GCubeSDITest{
@Test
public void testContextAuth() throws Exception {
assumeTrue(isTestInfrastructureEnabled());
String url=scopeConfiguration().getByEngine(Engine.GS_ENGINE).get(0).getBaseEndpoint();
new TestClient(new ConnectionDescriptor(url)).get("");
}
// static String sdiHostname="sdi-t.pre.d4science.org"; // static String sdiHostname="sdi-t.pre.d4science.org";
// static String scope ="/pred4s/preprod/preVRE"; // static String scope ="/pred4s/preprod/preVRE";

View File

@ -3,7 +3,7 @@ package org.gcube.spatial.data.sdi.model;
public class ServiceConstants { public class ServiceConstants {
public static final String APPLICATION="SDI-Service"; public static final String APPLICATION="SDI-Service";
public static final String INTERFACE="SDI";
public static final String SERVICE_CLASS="SDI"; public static final String SERVICE_CLASS="SDI";
public static final String SERVICE_NAME="sdi-service"; public static final String SERVICE_NAME="sdi-service";
@ -42,6 +42,7 @@ public class ServiceConstants {
} }
public static final class SDI{ public static final class SDI{
public static final String INTERFACE="SDI";
public static final String STATUS_PATH="status"; public static final String STATUS_PATH="status";
public static final String PROFILES_PATH="profile"; public static final String PROFILES_PATH="profile";
public static final String SERVICE_CLASS_PARAM="service_class"; public static final String SERVICE_CLASS_PARAM="service_class";

View File

@ -1,8 +1,8 @@
package org.gcube.spatial.data.sdi.model.faults; package org.gcube.spatial.data.sdi.model.faults;
import lombok.ToString; import lombok.Data;
@ToString @Data
public class RemoteException extends Exception { public class RemoteException extends Exception {
/** /**
@ -34,24 +34,12 @@ public class RemoteException extends Exception {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
} }
private String remoteMessage=null; private String remoteMessage=null;
private Integer responseHTTPCode=0; private Integer responseHTTPCode=0;
public void setResponseHTTPCode(Integer responseHTTPCode) { private String content=null;
this.responseHTTPCode = responseHTTPCode;
}
public Integer getResponseHTTPCode() {
return responseHTTPCode;
}
public String getRemoteMessage() {
return remoteMessage;
}
public void setRemoteMessage(String remoteMessage) {
this.remoteMessage = remoteMessage;
}
} }

View File

@ -34,15 +34,15 @@ public class GeoServerDescriptor extends GeoServiceDescriptor {
this.sharedWorkspace = sharedWorkspace; this.sharedWorkspace = sharedWorkspace;
this.publicWorkspace = publicWorkspace; this.publicWorkspace = publicWorkspace;
} }
@NonNull
private String confidentialWorkspace; private String confidentialWorkspace;
@NonNull
private String contextVisibilityWorkspace; private String contextVisibilityWorkspace;
@NonNull
private String sharedWorkspace; private String sharedWorkspace;
@NonNull
private String publicWorkspace; private String publicWorkspace;
@NonNull
private Long hostedLayersCount; private Long hostedLayersCount;
private String engineId=Engine.GS_ENGINE; private String engineId=Engine.GS_ENGINE;

View File

@ -1,20 +1,17 @@
package org.gcube.spatial.data.sdi.plugins; package org.gcube.spatial.data.sdi.plugins;
import javax.ws.rs.client.WebTarget;
import javax.xml.namespace.QName;
import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.EndpointReference; import javax.xml.ws.EndpointReference;
import org.gcube.common.calls.jaxrs.GcubeService;
import org.gcube.common.calls.jaxrs.TargetFactory;
import org.gcube.common.clients.config.ProxyConfig; import org.gcube.common.clients.config.ProxyConfig;
import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.spatial.data.sdi.interfaces.Metadata; import org.gcube.spatial.data.sdi.interfaces.Metadata;
import org.gcube.spatial.data.sdi.model.ServiceConstants; import org.gcube.spatial.data.sdi.model.ServiceConstants;
import org.gcube.spatial.data.sdi.proxies.DefaultMetadata; import org.gcube.spatial.data.sdi.proxies.DefaultMetadata;
import org.w3c.dom.Node; import org.w3c.dom.Node;
public class MetadataPlugin extends SDIAbstractPlugin<WebTarget, Metadata>{ public class MetadataPlugin extends SDIAbstractPlugin<GXWebTargetAdapterRequest, Metadata>{
public MetadataPlugin() { public MetadataPlugin() {
super("sdi-service/gcube/service"); super("sdi-service/gcube/service");
@ -25,21 +22,21 @@ public class MetadataPlugin extends SDIAbstractPlugin<WebTarget, Metadata>{
return arg0; return arg0;
} }
@Override @Override
public Metadata newProxy(ProxyDelegate<WebTarget> arg0) { public Metadata newProxy(ProxyDelegate<GXWebTargetAdapterRequest> arg0) {
return new DefaultMetadata(arg0); return new DefaultMetadata(arg0);
} }
@Override @Override
public WebTarget resolve(EndpointReference epr, ProxyConfig<?, ?> arg1) throws Exception { public GXWebTargetAdapterRequest resolve(EndpointReference epr, ProxyConfig<?, ?> arg1) throws Exception {
DOMResult result = new DOMResult(); DOMResult result = new DOMResult();
epr.writeTo(result); epr.writeTo(result);
Node node =result.getNode(); Node node =result.getNode();
Node child=node.getFirstChild(); Node child=node.getFirstChild();
String address = child.getTextContent(); String address = child.getTextContent();
GcubeService service = GcubeService.service(). GXWebTargetAdapterRequest request =
withName(new QName(ServiceConstants.NAMESPACE,ServiceConstants.Metadata.INTERFACE)). GXWebTargetAdapterRequest.newRequest(address).path(ServiceConstants.Metadata.INTERFACE);
andPath(ServiceConstants.Metadata.INTERFACE); //set additional path parts or parameters here
return TargetFactory.stubFor(service).at(address); return request;
} }
} }

View File

@ -1,10 +1,9 @@
package org.gcube.spatial.data.sdi.plugins; package org.gcube.spatial.data.sdi.plugins;
import javax.ws.rs.client.WebTarget; import org.gcube.common.clients.Plugin;
import org.gcube.common.clients.ProxyBuilder; import org.gcube.common.clients.ProxyBuilder;
import org.gcube.common.clients.ProxyBuilderImpl; import org.gcube.common.clients.ProxyBuilderImpl;
import org.gcube.common.clients.Plugin; import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.spatial.data.sdi.interfaces.Metadata; import org.gcube.spatial.data.sdi.interfaces.Metadata;
import org.gcube.spatial.data.sdi.interfaces.SDIManagement; import org.gcube.spatial.data.sdi.interfaces.SDIManagement;
import org.gcube.spatial.data.sdi.model.ServiceConstants; import org.gcube.spatial.data.sdi.model.ServiceConstants;
@ -18,12 +17,12 @@ public abstract class SDIAbstractPlugin<S, P> implements Plugin<S, P>{
public static ProxyBuilder<Metadata> metadata() { public static ProxyBuilder<Metadata> metadata() {
return new ProxyBuilderImpl<WebTarget,Metadata>(metadata_plugin); return new ProxyBuilderImpl<GXWebTargetAdapterRequest,Metadata>(metadata_plugin);
} }
public static ProxyBuilder<SDIManagement> management() { public static ProxyBuilder<SDIManagement> management() {
return new ProxyBuilderImpl<WebTarget,SDIManagement>(sdi_plugin); return new ProxyBuilderImpl<GXWebTargetAdapterRequest,SDIManagement>(sdi_plugin);
} }

View File

@ -9,12 +9,13 @@ import org.gcube.common.calls.jaxrs.GcubeService;
import org.gcube.common.calls.jaxrs.TargetFactory; import org.gcube.common.calls.jaxrs.TargetFactory;
import org.gcube.common.clients.config.ProxyConfig; import org.gcube.common.clients.config.ProxyConfig;
import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.spatial.data.sdi.interfaces.SDIManagement; import org.gcube.spatial.data.sdi.interfaces.SDIManagement;
import org.gcube.spatial.data.sdi.model.ServiceConstants; import org.gcube.spatial.data.sdi.model.ServiceConstants;
import org.gcube.spatial.data.sdi.proxies.DefaultSDIManagement; import org.gcube.spatial.data.sdi.proxies.DefaultSDIManagement;
import org.w3c.dom.Node; import org.w3c.dom.Node;
public class SDIPlugin extends SDIAbstractPlugin<WebTarget, SDIManagement>{ public class SDIPlugin extends SDIAbstractPlugin<GXWebTargetAdapterRequest, SDIManagement>{
public SDIPlugin() { public SDIPlugin() {
super("sdi-service/gcube/service"); super("sdi-service/gcube/service");
@ -25,21 +26,21 @@ public class SDIPlugin extends SDIAbstractPlugin<WebTarget, SDIManagement>{
return arg0; return arg0;
} }
@Override @Override
public SDIManagement newProxy(ProxyDelegate<WebTarget> arg0) { public SDIManagement newProxy(ProxyDelegate<GXWebTargetAdapterRequest> arg0) {
return new DefaultSDIManagement(arg0); return new DefaultSDIManagement(arg0);
} }
@Override @Override
public WebTarget resolve(EndpointReference epr, ProxyConfig<?, ?> arg1) throws Exception { public GXWebTargetAdapterRequest resolve(EndpointReference epr, ProxyConfig<?, ?> arg1) throws Exception {
DOMResult result = new DOMResult(); DOMResult result = new DOMResult();
epr.writeTo(result); epr.writeTo(result);
Node node =result.getNode(); Node node =result.getNode();
Node child=node.getFirstChild(); Node child=node.getFirstChild();
String address = child.getTextContent(); String address = child.getTextContent();
GcubeService service = GcubeService.service(). GXWebTargetAdapterRequest request =
withName(new QName(ServiceConstants.NAMESPACE,ServiceConstants.INTERFACE)). GXWebTargetAdapterRequest.newRequest(address).path(ServiceConstants.SDI.INTERFACE);
andPath(ServiceConstants.INTERFACE); //set additional path parts or parameters here
return TargetFactory.stubFor(service).at(address); return request;
} }
} }

View File

@ -0,0 +1,126 @@
package org.gcube.spatial.data.sdi.proxies;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.client.Entity;
import org.gcube.common.clients.Call;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class AbstractProxy {
private ConnectionDescriptor conn;
@Setter
private String basePath=null;
private Set<Class<?>> toRegisterClasses=new HashSet<Class<?>>();
private Set<Object> toRegisterObjects=new HashSet<>();
protected AbstractProxy(ConnectionDescriptor conn) {
this.conn=conn;
}
protected void register(Class<?> providerClass) {
toRegisterClasses.add(providerClass);
}
protected void register(Object provider) {
toRegisterObjects.add(provider);
}
protected GXWebTargetAdapterRequest resolve() throws UnsupportedEncodingException {
GXWebTargetAdapterRequest toReturn =GXWebTargetAdapterRequest.newHTTPSRequest(conn.getEndpoint());
if(basePath!=null) return toReturn.path(basePath);
else return toReturn;
}
protected <T> T makeCall(Call<GXWebTargetAdapterRequest, T> call) throws Exception{
try{
return call.call(resolve());
}catch(RemoteException e) {
throw e;
}catch(Throwable t) {
throw new Exception(t);
}
}
protected void delete(String path) throws Exception {
check(resolve().path(path).delete(),null);
}
protected void post(String path,Entity<?> entity)throws Exception{
post(path,entity,null);
}
protected void post(String path,Object obj) {
}
protected <T> T post(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);
};
});
}
protected String get(String path) throws Exception {
return get(path,null);
}
protected <T> T get(String path, Class<T> clazz) throws Exception{
return makeCall(new Call<GXWebTargetAdapterRequest, T>() {
public T call(GXWebTargetAdapterRequest endpoint) throws Exception {
return check(endpoint.path(path).get(),clazz);
};
});
}
protected static <T> T check(GXInboundResponse resp, Class<T> clazz) throws RemoteException{
try {
log.debug("Checking Response {}",resp);
if(resp.hasGXError()) {
RemoteException e=new RemoteException("Error received from server");
e.setRemoteMessage(resp.getMessage());
e.setResponseHTTPCode(resp.getHTTPCode());
e.setContent(resp.getStreamedContentAsString());
throw e;
}else {
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);
}
}
}

View File

@ -2,16 +2,17 @@ package org.gcube.spatial.data.sdi.proxies;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.WeakHashMap;
import javax.ws.rs.client.Entity; import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType; import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.gcube.common.clients.Call; import org.gcube.common.clients.Call;
import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.spatial.data.sdi.interfaces.Metadata; import org.gcube.spatial.data.sdi.interfaces.Metadata;
import org.gcube.spatial.data.sdi.model.ServiceConstants; import org.gcube.spatial.data.sdi.model.ServiceConstants;
import org.gcube.spatial.data.sdi.model.metadata.MetadataPublishOptions; import org.gcube.spatial.data.sdi.model.metadata.MetadataPublishOptions;
@ -19,16 +20,17 @@ import org.gcube.spatial.data.sdi.model.metadata.MetadataReport;
import org.gcube.spatial.data.sdi.model.metadata.TemplateCollection; import org.gcube.spatial.data.sdi.model.metadata.TemplateCollection;
import org.gcube.spatial.data.sdi.model.metadata.TemplateDescriptor; import org.gcube.spatial.data.sdi.model.metadata.TemplateDescriptor;
import org.gcube.spatial.data.sdi.model.metadata.TemplateInvocation; import org.gcube.spatial.data.sdi.model.metadata.TemplateInvocation;
import org.gcube.spatial.data.sdi.utils.ResponseUtils;
import org.glassfish.jersey.media.multipart.FormDataMultiPart; import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart; import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
public class DefaultMetadata implements Metadata{ public class DefaultMetadata implements Metadata {
private final ProxyDelegate<WebTarget> delegate; private final ProxyDelegate<GXWebTargetAdapterRequest> delegate;
public DefaultMetadata(ProxyDelegate<WebTarget> config){ public DefaultMetadata(ProxyDelegate<GXWebTargetAdapterRequest> config){
this.delegate = config; this.delegate = config;
} }
@ -36,12 +38,13 @@ public class DefaultMetadata implements Metadata{
@Override @Override
public TemplateCollection getAvailableTemplates() { public TemplateCollection getAvailableTemplates() {
Call<WebTarget, Set<TemplateDescriptor>> call = new Call<WebTarget, Set<TemplateDescriptor>>() {
Call<GXWebTargetAdapterRequest, Set<TemplateDescriptor>> call = new Call<GXWebTargetAdapterRequest, Set<TemplateDescriptor>>() {
@Override @Override
public Set<TemplateDescriptor> call(WebTarget templates) throws Exception { public Set<TemplateDescriptor> call(GXWebTargetAdapterRequest templates) throws Exception {
GenericType<Set<TemplateDescriptor>> generic=new GenericType<Set<TemplateDescriptor>>() { GenericType<Set<TemplateDescriptor>> generic=new GenericType<Set<TemplateDescriptor>>() {
}; };
return templates.path(ServiceConstants.Metadata.LIST_METHOD).request(MediaType.APPLICATION_JSON).get(generic); return ResponseUtils.check(templates.path(ServiceConstants.Metadata.LIST_METHOD).get()).readEntity(generic);
} }
}; };
try { try {
@ -60,52 +63,48 @@ public class DefaultMetadata implements Metadata{
public MetadataReport pushMetadata(final File toPublish, final MetadataPublishOptions options) { public MetadataReport pushMetadata(final File toPublish, final MetadataPublishOptions options) {
// upload Meta // upload Meta
Call<WebTarget,MetadataReport> applyTemplatesCall=null; //needs uploaded id Call<GXWebTargetAdapterRequest,MetadataReport> applyTemplatesCall=null; //needs uploaded id
Call<WebTarget,MetadataReport> publishCall=null; //needs uploaded id Call<GXWebTargetAdapterRequest,MetadataReport> publishCall=null; //needs uploaded id
try{ try{
Call<WebTarget,String> uploadCall=new Call<WebTarget, String>() { Call<GXWebTargetAdapterRequest,String> uploadCall=new Call<GXWebTargetAdapterRequest, String>() {
@Override @Override
public String call(WebTarget endpoint) throws Exception { public String call(GXWebTargetAdapterRequest endpoint) throws Exception {
endpoint.register(MultiPartFeature.class); endpoint.register(MultiPartFeature.class);
FormDataMultiPart multi=new FormDataMultiPart(); FormDataMultiPart multi=new FormDataMultiPart();
// multi.field("file",toPublish,MediaType.APPLICATION_OCTET_STREAM_TYPE); // multi.field("file",toPublish,MediaType.APPLICATION_OCTET_STREAM_TYPE);
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(ServiceConstants.Metadata.UPLOADED_FILE_PARAMETER, FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(ServiceConstants.Metadata.UPLOADED_FILE_PARAMETER,
toPublish,MediaType.APPLICATION_OCTET_STREAM_TYPE); toPublish,MediaType.APPLICATION_OCTET_STREAM_TYPE);
multi.bodyPart(fileDataBodyPart); multi.bodyPart(fileDataBodyPart);
Response resp= endpoint.request().post(Entity.entity(multi, multi.getMediaType())); return ResponseUtils.check(endpoint.post(Entity.entity(multi, multi.getMediaType())),String.class);
checkResponse(resp);
return resp.readEntity(String.class);
} }
}; };
final String id=delegate.make(uploadCall); final String id=delegate.make(uploadCall);
applyTemplatesCall=new Call<WebTarget, MetadataReport>() { applyTemplatesCall=new Call<GXWebTargetAdapterRequest, MetadataReport>() {
@Override @Override
public MetadataReport call(WebTarget endpoint) throws Exception { public MetadataReport call(GXWebTargetAdapterRequest endpoint) throws Exception {
return ResponseUtils.check(endpoint.path(id).put(Entity.entity(
Response resp= endpoint.path(id). new HashSet<TemplateInvocation>(options.getTemplateInvocations()),MediaType.APPLICATION_JSON)), MetadataReport.class);
request(MediaType.APPLICATION_JSON).put(Entity.entity(
new HashSet<TemplateInvocation>(options.getTemplateInvocations()),MediaType.APPLICATION_JSON));
checkResponse(resp);
return resp.readEntity(MetadataReport.class);
} }
}; };
publishCall=new Call<WebTarget,MetadataReport>(){ publishCall=new Call<GXWebTargetAdapterRequest,MetadataReport>(){
@Override @Override
public MetadataReport call(WebTarget endpoint) throws Exception { public MetadataReport call(GXWebTargetAdapterRequest endpoint) throws Exception {
Response resp= endpoint.path(ServiceConstants.Metadata.PUBLISH_METHOD).path(id).path(options.getGeonetworkCategory()).
queryParam(ServiceConstants.Metadata.VALIDATE_PARAMETER, options.isValidate()). Map<String,String> queryParams = new WeakHashMap<>();
queryParam(ServiceConstants.Metadata.PUBLIC_PARAMETER, options.isMakePublic()). queryParams.put(ServiceConstants.Metadata.VALIDATE_PARAMETER, options.isValidate()+"");
queryParam(ServiceConstants.Metadata.STYLESHEET_PARAMETER, options.getGeonetworkStyleSheet()). queryParams.put(ServiceConstants.Metadata.PUBLIC_PARAMETER, options.isMakePublic()+"");
request(MediaType.APPLICATION_JSON).get(); queryParams.put(ServiceConstants.Metadata.STYLESHEET_PARAMETER, options.getGeonetworkStyleSheet());
checkResponse(resp);
return resp.readEntity(MetadataReport.class); return ResponseUtils.check(endpoint.path(ServiceConstants.Metadata.PUBLISH_METHOD).path(id).path(options.getGeonetworkCategory()).
get(), MetadataReport.class);
} }
}; };
}catch(Throwable t){ }catch(Throwable t){
@ -136,10 +135,4 @@ public class DefaultMetadata implements Metadata{
} }
protected void checkResponse(Response toCheck) throws Exception{
switch(toCheck.getStatusInfo().getFamily()){
case SUCCESSFUL : break;
default : throw new Exception("Unexpected Response code : "+toCheck.getStatus(),new Exception(toCheck.readEntity(String.class)));
}
}
} }

View File

@ -2,32 +2,32 @@ package org.gcube.spatial.data.sdi.proxies;
import java.util.List; import java.util.List;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import org.gcube.common.clients.Call; import org.gcube.common.clients.Call;
import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.spatia.data.model.profiles.ApplicationProfile; import org.gcube.spatia.data.model.profiles.ApplicationProfile;
import org.gcube.spatial.data.clients.SDIClientManager; import org.gcube.spatial.data.clients.SDIClientManager;
import org.gcube.spatial.data.clients.SDIGenericClient; import org.gcube.spatial.data.clients.SDIGenericClient;
import org.gcube.spatial.data.clients.model.ClientInfo; import org.gcube.spatial.data.clients.model.ClientInfo;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
import org.gcube.spatial.data.sdi.interfaces.SDIManagement; import org.gcube.spatial.data.sdi.interfaces.SDIManagement;
import org.gcube.spatial.data.sdi.model.ScopeConfiguration; import org.gcube.spatial.data.sdi.model.ScopeConfiguration;
import org.gcube.spatial.data.sdi.model.ServiceConstants; import org.gcube.spatial.data.sdi.model.ServiceConstants;
import org.gcube.spatial.data.sdi.model.faults.RemoteException; import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.gcube.spatial.data.sdi.model.health.HealthReport; import org.gcube.spatial.data.sdi.model.health.HealthReport;
import org.gcube.spatial.data.sdi.model.service.GeoServiceDescriptor; import org.gcube.spatial.data.sdi.model.service.GeoServiceDescriptor;
import org.gcube.spatial.data.sdi.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class DefaultSDIManagement implements SDIManagement{ public class DefaultSDIManagement implements SDIManagement{
private final ProxyDelegate<WebTarget> delegate; private final ProxyDelegate<GXWebTargetAdapterRequest> delegate;
private SDIClientManager clientManager=null; private SDIClientManager clientManager=null;
public DefaultSDIManagement(ProxyDelegate<WebTarget> delegate) { public DefaultSDIManagement(ProxyDelegate<GXWebTargetAdapterRequest> delegate) {
this.delegate=delegate; this.delegate=delegate;
clientManager=new SDIClientManager(); clientManager=new SDIClientManager();
log.debug("Available clients are "); log.debug("Available clients are ");
@ -39,10 +39,11 @@ public class DefaultSDIManagement implements SDIManagement{
@Override @Override
public ScopeConfiguration getConfiguration() throws RemoteException { public ScopeConfiguration getConfiguration() throws RemoteException {
Call<WebTarget, ScopeConfiguration> call=new Call<WebTarget,ScopeConfiguration>(){ Call<GXWebTargetAdapterRequest, ScopeConfiguration> call=
new Call<GXWebTargetAdapterRequest,ScopeConfiguration>(){
@Override @Override
public ScopeConfiguration call(WebTarget endpoint) throws Exception { public ScopeConfiguration call(GXWebTargetAdapterRequest endpoint) throws Exception {
return endpoint.request().get(ScopeConfiguration.class); return ResponseUtils.check(endpoint.get(),ScopeConfiguration.class);
} }
}; };
try { try {
@ -54,10 +55,11 @@ public class DefaultSDIManagement implements SDIManagement{
@Override @Override
public HealthReport getReport() throws RemoteException { public HealthReport getReport() throws RemoteException {
Call<WebTarget, HealthReport> call=new Call<WebTarget,HealthReport>(){ Call<GXWebTargetAdapterRequest, HealthReport> call=new Call<GXWebTargetAdapterRequest,HealthReport>(){
@Override @Override
public HealthReport call(WebTarget endpoint) throws Exception { public HealthReport call(GXWebTargetAdapterRequest endpoint) throws Exception {
return endpoint.path(ServiceConstants.SDI.STATUS_PATH).request(MediaType.APPLICATION_JSON).get(HealthReport.class); return ResponseUtils.check(endpoint.setAcceptedResponseType(MediaType.APPLICATION_JSON_TYPE).path(ServiceConstants.SDI.STATUS_PATH).
get(),HealthReport.class);
} }
}; };
try { try {
@ -70,11 +72,11 @@ public class DefaultSDIManagement implements SDIManagement{
@Override @Override
public ApplicationProfile getProfile(String serviceClass,String serviceName) throws RemoteException { public ApplicationProfile getProfile(String serviceClass,String serviceName) throws RemoteException {
Call<WebTarget, ApplicationProfile> call=new Call<WebTarget,ApplicationProfile>(){ Call<GXWebTargetAdapterRequest, ApplicationProfile> call=new Call<GXWebTargetAdapterRequest,ApplicationProfile>(){
@Override @Override
public ApplicationProfile call(WebTarget endpoint) throws Exception { public ApplicationProfile call(GXWebTargetAdapterRequest endpoint) throws Exception {
return endpoint.path(ServiceConstants.SDI.PROFILES_PATH).path(serviceClass). return ResponseUtils.check(endpoint.path(ServiceConstants.SDI.PROFILES_PATH).path(serviceClass).
path(serviceName).request(MediaType.APPLICATION_JSON).get(ApplicationProfile.class); path(serviceName).get(),ApplicationProfile.class);
} }
}; };
try { try {

View File

@ -0,0 +1,82 @@
package org.gcube.spatial.data.sdi.utils;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ResponseUtils {
public static <T> T check(GXInboundResponse resp, Class<T> clazz) throws RemoteException{
try {
log.debug("Checking Response {}",resp);
if(resp.hasGXError()||!resp.getSource().getStatusInfo().getFamily().equals(Family.SUCCESSFUL)) {
RemoteException e=new RemoteException("Error received from server");
e.setRemoteMessage(resp.getMessage());
e.setResponseHTTPCode(resp.getHTTPCode());
e.setContent(resp.getStreamedContentAsString());
throw e;
}else {
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);
}
}
public static Response check(GXInboundResponse resp) throws RemoteException{
try {
log.debug("Checking Response {}",resp);
if(resp.hasGXError()||!resp.getSource().getStatusInfo().getFamily().equals(Family.SUCCESSFUL)) {
RemoteException e=new RemoteException("Error received from server");
e.setRemoteMessage(resp.getMessage());
e.setResponseHTTPCode(resp.getHTTPCode());
e.setContent(resp.getStreamedContentAsString());
throw e;
}else {
return resp.getSource();
}
}catch(RemoteException e) {
throw e;
}catch(Exception e) {
throw new RemoteException("Unable to read response from server.",e);
}
}
// protected static <T> T check(GXInboundResponse resp, Class<T> clazz) throws RemoteException{
// try {
// log.debug("Checking Response {}",resp);
// if(resp.hasGXError()) {
// RemoteException e=new RemoteException("Error received from server");
// e.setRemoteMessage(resp.getMessage());
// e.setResponseHTTPCode(resp.getHTTPCode());
// e.setContent(resp.getStreamedContentAsString());
// throw e;
// }else {
// 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);
// }
// }
}

View File

@ -1,5 +1,6 @@
package org.gcube.spatial.data.sdi; package org.gcube.spatial.data.sdi;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
import java.io.File; import java.io.File;
@ -41,11 +42,16 @@ public class MetadataTests extends GCubeSDITest{
} }
})) { })) {
try {
// System.out.println(meta.pushMetadata(f)); // System.out.println(meta.pushMetadata(f));
MetadataPublishOptions opts=new MetadataPublishOptions(new TemplateInvocationBuilder().threddsOnlineResources("my_hostname", "some_dataset.nc", "myPersonalCatalog").get()); MetadataPublishOptions opts=new MetadataPublishOptions(new TemplateInvocationBuilder().threddsOnlineResources("my_hostname", "some_dataset.nc", "myPersonalCatalog").get());
opts.setGeonetworkCategory("service"); opts.setGeonetworkCategory("service");
opts.setValidate(false); opts.setValidate(false);
System.out.println(meta.pushMetadata(f, opts)); System.out.println(meta.pushMetadata(f, opts));
}catch(Exception e) {
throw new RuntimeException("Unable to push "+f.getName(),e);
}
} }
} }

View File

@ -36,8 +36,8 @@ public class SDITests extends GCubeSDITest{
System.out.println( System.out.println(
SDIAbstractPlugin.management().build().getClientByEngineId(Engine.GS_ENGINE).getInfo()); SDIAbstractPlugin.management().build().getClientByEngineId(Engine.GS_ENGINE).getInfo());
GSRESTClient gs=(GSRESTClient) SDIAbstractPlugin.management().build().getClientByEngineId(Engine.GS_ENGINE).getRESTClient(); // GSRESTClient gs=(GSRESTClient) SDIAbstractPlugin.management().build().getClientByEngineId(Engine.GS_ENGINE).getRESTClient();
gs.getWorkspaces(); // gs.getWorkspaces();
//
} }
} }

View File

@ -18,7 +18,7 @@ import org.gcube.spatial.data.sdi.model.health.HealthReport;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Path(ServiceConstants.INTERFACE) @Path(ServiceConstants.SDI.INTERFACE)
//@Api(value=ServiceConstants.INTERFACE) //@Api(value=ServiceConstants.INTERFACE)
@ManagedBy(SDIServiceManager.class) @ManagedBy(SDIServiceManager.class)
@Slf4j @Slf4j

View File

@ -25,8 +25,8 @@ public class MainTest extends ServiceTest{
@Test @Test
public void getConfiguration(){ public void getConfiguration(){
assumeTrue(isTestInfrastructureEnabled()); assumeTrue(isTestInfrastructureEnabled());
System.out.println(target(ServiceConstants.INTERFACE).request(MediaType.APPLICATION_JSON_TYPE).get(String.class)); System.out.println(target(ServiceConstants.SDI.INTERFACE).request(MediaType.APPLICATION_JSON_TYPE).get(String.class));
ScopeConfiguration conf=target(ServiceConstants.INTERFACE).request(MediaType.APPLICATION_JSON_TYPE).get(ScopeConfiguration.class); ScopeConfiguration conf=target(ServiceConstants.SDI.INTERFACE).request(MediaType.APPLICATION_JSON_TYPE).get(ScopeConfiguration.class);
} }
// //
@ -34,7 +34,7 @@ public class MainTest extends ServiceTest{
public void getGeoServer() throws MalformedURLException{ public void getGeoServer() throws MalformedURLException{
assumeTrue(isTestInfrastructureEnabled()); assumeTrue(isTestInfrastructureEnabled());
ScopeConfiguration conf=target(ServiceConstants.INTERFACE).request(MediaType.APPLICATION_JSON_TYPE).get(ScopeConfiguration.class); ScopeConfiguration conf=target(ServiceConstants.SDI.INTERFACE).request(MediaType.APPLICATION_JSON_TYPE).get(ScopeConfiguration.class);
assumeTrue(!conf.getByEngine(Engine.GS_ENGINE).isEmpty()); assumeTrue(!conf.getByEngine(Engine.GS_ENGINE).isEmpty());
@ -49,8 +49,8 @@ public class MainTest extends ServiceTest{
@Test @Test
public void testHealthReport() { public void testHealthReport() {
assumeTrue(isTestInfrastructureEnabled()); assumeTrue(isTestInfrastructureEnabled());
System.out.println(target(ServiceConstants.INTERFACE).path("status").request(MediaType.APPLICATION_JSON_TYPE).get(String.class)); System.out.println(target(ServiceConstants.SDI.INTERFACE).path("status").request(MediaType.APPLICATION_JSON_TYPE).get(String.class));
System.out.println(target(ServiceConstants.INTERFACE).path("status").request(MediaType.APPLICATION_XML_TYPE).get(String.class)); System.out.println(target(ServiceConstants.SDI.INTERFACE).path("status").request(MediaType.APPLICATION_XML_TYPE).get(String.class));
} }

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
@ -10,45 +11,49 @@
<version>0.0.2-SNAPSHOT</version> <version>0.0.2-SNAPSHOT</version>
<name>SDI test commons</name> <name>SDI test commons</name>
<scm> <scm>
<connection>scm:git:${gitBaseUrl}/gcube-sdi-suite</connection> <connection>scm:git:${gitBaseUrl}/gcube-sdi-suite</connection>
<developerConnection>scm:git:${gitBaseUrl}/gcube-sdi-suite</developerConnection> <developerConnection>scm:git:${gitBaseUrl}/gcube-sdi-suite</developerConnection>
<url>${gitBaseUrl}/gcube-sdi-suite</url> <url>${gitBaseUrl}/gcube-sdi-suite</url>
</scm> </scm>
<dependencies> <dependencies>
<dependency> <dependency>
<artifactId>sdi-interface</artifactId> <artifactId>sdi-interface</artifactId>
<groupId>org.gcube.spatial.data</groupId> <groupId>org.gcube.spatial.data</groupId>
</dependency> </dependency>
<dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-fw-clients</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-generic-clients</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId> <groupId>org.gcube.common</groupId>
<artifactId>common-jaxrs-client</artifactId> <artifactId>gxJRS</artifactId>
<version>[1.0.0,2.0.0)</version>
</dependency> </dependency>
<dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-fw-clients</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-generic-clients</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>4.12</version>
</dependency> </dependency>
</dependencies> </dependencies>
<description>Java library used to implement SDI clients.</description> <description>Java library used to implement SDI clients.</description>
</project> </project>

View File

@ -2,25 +2,20 @@ package org.gcube.sdi.test.client;
import java.util.List; import java.util.List;
import javax.ws.rs.client.WebTarget;
import javax.xml.namespace.QName;
import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.EndpointReference; import javax.xml.ws.EndpointReference;
import org.gcube.common.calls.jaxrs.GcubeService;
import org.gcube.common.calls.jaxrs.TargetFactory;
import org.gcube.common.clients.Call; import org.gcube.common.clients.Call;
import org.gcube.common.clients.Plugin; import org.gcube.common.clients.Plugin;
import org.gcube.common.clients.ProxyBuilder; import org.gcube.common.clients.ProxyBuilder;
import org.gcube.common.clients.ProxyBuilderImpl; import org.gcube.common.clients.ProxyBuilderImpl;
import org.gcube.common.clients.config.ProxyConfig; import org.gcube.common.clients.config.ProxyConfig;
import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.gcube.spatia.data.model.profiles.ApplicationProfile; import org.gcube.spatia.data.model.profiles.ApplicationProfile;
import org.gcube.spatial.data.clients.SDIClientManager; import org.gcube.spatial.data.clients.SDIClientManager;
import org.gcube.spatial.data.clients.SDIGenericClient; import org.gcube.spatial.data.clients.SDIGenericClient;
import org.gcube.spatial.data.clients.model.ClientInfo;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
import org.gcube.spatial.data.clients.model.engine.Engine;
import org.gcube.spatial.data.sdi.interfaces.SDIManagement; import org.gcube.spatial.data.sdi.interfaces.SDIManagement;
import org.gcube.spatial.data.sdi.model.ScopeConfiguration; import org.gcube.spatial.data.sdi.model.ScopeConfiguration;
import org.gcube.spatial.data.sdi.model.ServiceConstants; import org.gcube.spatial.data.sdi.model.ServiceConstants;
@ -32,8 +27,8 @@ import org.w3c.dom.Node;
public class SimpleSDIManagerClient implements SDIManagement{ public class SimpleSDIManagerClient implements SDIManagement{
public static ProxyBuilder<SDIManagement> contextService() { public static ProxyBuilder<SDIManagement> contextService() {
return new ProxyBuilderImpl<WebTarget,SDIManagement>( return new ProxyBuilderImpl<GXWebTargetAdapterRequest,SDIManagement>(
new Plugin<WebTarget, SDIManagement>(){ new Plugin<GXWebTargetAdapterRequest, SDIManagement>(){
@Override @Override
public String serviceClass() {return ServiceConstants.SERVICE_CLASS;} public String serviceClass() {return ServiceConstants.SERVICE_CLASS;}
@Override @Override
@ -44,36 +39,52 @@ public class SimpleSDIManagerClient implements SDIManagement{
public Exception convert(Exception fault, ProxyConfig<?, ?> config) {return fault;} public Exception convert(Exception fault, ProxyConfig<?, ?> config) {return fault;}
@Override @Override
public String namespace() {return ServiceConstants.NAMESPACE;} public String namespace() {return ServiceConstants.NAMESPACE;}
@Override @Override
public WebTarget resolve(EndpointReference epr, ProxyConfig<?, ?> arg1) throws Exception { public GXWebTargetAdapterRequest resolve(EndpointReference epr, ProxyConfig<?, ?> config)
throws Exception {
DOMResult result = new DOMResult(); DOMResult result = new DOMResult();
epr.writeTo(result); epr.writeTo(result);
Node node =result.getNode(); Node node =result.getNode();
Node child=node.getFirstChild(); Node child=node.getFirstChild();
String address = child.getTextContent(); String address = child.getTextContent();
GcubeService service = GcubeService.service(). GXWebTargetAdapterRequest request =
withName(new QName(ServiceConstants.NAMESPACE,ServiceConstants.INTERFACE)). GXWebTargetAdapterRequest.newRequest(address).path(ServiceConstants.SDI.INTERFACE);
andPath(ServiceConstants.INTERFACE); //set additional path parts or parameters here
return TargetFactory.stubFor(service).at(address); return request;
} }
@Override @Override
public SDIManagement newProxy(ProxyDelegate<WebTarget> delegate) {return new SimpleSDIManagerClient(delegate);} public SDIManagement newProxy(ProxyDelegate<GXWebTargetAdapterRequest> delegate) {
return new SimpleSDIManagerClient(delegate);}
}); });
} }
private final ProxyDelegate<WebTarget> delegate; private final ProxyDelegate<GXWebTargetAdapterRequest> delegate;
private SDIClientManager clientManager=null; private SDIClientManager clientManager=null;
public SimpleSDIManagerClient(ProxyDelegate<WebTarget> delegate) { public SimpleSDIManagerClient(ProxyDelegate<GXWebTargetAdapterRequest> delegate) {
this.delegate=delegate; this.delegate=delegate;
} }
@Override @Override
public ScopeConfiguration getConfiguration() throws RemoteException { public ScopeConfiguration getConfiguration() throws RemoteException {
Call<WebTarget, ScopeConfiguration> call=new Call<WebTarget,ScopeConfiguration>(){ Call<GXWebTargetAdapterRequest, ScopeConfiguration> call=new Call<GXWebTargetAdapterRequest,ScopeConfiguration>(){
@Override @Override
public ScopeConfiguration call(WebTarget endpoint) throws Exception { public ScopeConfiguration call(GXWebTargetAdapterRequest request) throws Exception {
return endpoint.request().get(ScopeConfiguration.class); GXInboundResponse response = request.get();
if (response.hasGXError()||
response.getHTTPCode()<200||response.getHTTPCode()>299) {
RemoteException e=new RemoteException("Received error response.");
e.setContent(response.getStreamedContentAsString());
e.setRemoteMessage(response.getMessage());
e.setResponseHTTPCode(response.getHTTPCode());
throw e;
}
//MultiLocatorResponse is the expected content of the response in the form of a serialized Json
return response.tryConvertStreamedContentFromJson(ScopeConfiguration.class);
} }
}; };
try { try {

View File

@ -0,0 +1,19 @@
package org.gcube.sdi.test;
import static org.junit.Assume.assumeTrue;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.junit.Test;
public class TestsCalls extends GCubeSDITest{
@Test
public void testUtils() throws RemoteException {
assumeTrue(isTestInfrastructureEnabled());
System.out.println(scopeConfiguration());
System.out.println(manager().getReport());
}
}