diff --git a/gcube-geoserver-client/pom.xml b/gcube-geoserver-client/pom.xml index 126058e..bdb7614 100644 --- a/gcube-geoserver-client/pom.xml +++ b/gcube-geoserver-client/pom.xml @@ -1,4 +1,5 @@ - 4.0.0 @@ -10,29 +11,38 @@ 0.0.1-SNAPSHOT Gcube GeoServer client - + scm:git:${gitBaseUrl}/gcube-sdi-suite scm:git:${gitBaseUrl}/gcube-sdi-suite ${gitBaseUrl}/gcube-sdi-suite - - + + sdi-generic-client org.gcube.spatial.data - + - + org.gcube.common authorization-client - - + + + - org.glassfish.jersey.media - jersey-media-json-jackson + com.googlecode.json-simple + json-simple + 1.1.1 + + + + + com.jayway.jsonpath + json-path + 2.5.0 @@ -42,9 +52,9 @@ sdi-test-commons org.gcube.spatial.data test - + - + Java library used to communicate with GeoServer instances in D4Science infrastructure. \ No newline at end of file diff --git a/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/DataStoreRegistrationRequest.java b/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/DataStoreRegistrationRequest.java new file mode 100644 index 0000000..d7c838e --- /dev/null +++ b/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/DataStoreRegistrationRequest.java @@ -0,0 +1,49 @@ +package org.gcube.spatial.data.clients.geoserver; + +import java.util.ArrayList; +import java.util.Map; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.json.simple.JSONObject; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@XmlRootElement(name = "datastore") +public class DataStoreRegistrationRequest { + + @Data + @AllArgsConstructor + private static class Parameter{ + private String key; + @XmlElement(name="$") + private String value; + } + + private JSONObject datastore=new JSONObject(); + + public DataStoreRegistrationRequest() { + } + + public DataStoreRegistrationRequest(String name) { + setName(name); + } + + public void setName(String name) { + datastore.put("name",name); + } + + public void setParameters(Map params) { + ArrayList paramList=new ArrayList(); + + params.forEach((String k,String v)->{paramList.add(new Parameter(k,v));}); + + datastore.put("connectionParameters", new JSONObject().put("entry", paramList)); + + } + + +} diff --git a/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/GSRESTClient.java b/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/GSRESTClient.java index 76af48c..9fbbcb2 100644 --- a/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/GSRESTClient.java +++ b/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/GSRESTClient.java @@ -2,6 +2,7 @@ package org.gcube.spatial.data.clients.geoserver; import java.util.List; +import org.gcube.spatial.data.clients.GenericLoginClient; //import org.apache.commons.httpclient.NameValuePair; //import org.gcube.application.geoportal.service.engine.RESTFeatureType; //import org.gcube.application.geoportal.service.engine.RESTLayer; @@ -11,7 +12,7 @@ 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 extends GenericLoginClient{ // Management @@ -46,14 +47,16 @@ public interface GSRESTClient { // FeatureTypes -// public JSONObject getFeatureType(JSONObject layer) - /** - * RESTLayer l=reader.getLayer(workspace, toSetLayerName); - RESTFeatureType f= reader.getFeatureType(l); - * @param ws - * @throws RemoteException - * @throws Exception - */ + public List getFeatureTypesInWorkspace(String workspace)throws RemoteException,Exception; + public List getFeatureTypesInDataStore(String workspace,String datastore)throws RemoteException,Exception; + public JSONObject getFeatureType(String workspace,String featureName)throws RemoteException,Exception; + + + //Layers + public List getLayers()throws RemoteException,Exception; + public List getLayers(String workspace)throws RemoteException, Exception; + public JSONObject getLayer(String layerName)throws RemoteException, Exception; + public JSONObject getLayerInWorkspace(String ws,String layerName)throws RemoteException, Exception; // *********** CREATE OPS public void createWorkspace(String ws)throws RemoteException,Exception; @@ -93,25 +96,24 @@ public interface GSRESTClient { * @throws Exception */ + /** + * Publish a DS described in @param parameters. Parameters vary depending on DS type. See https://docs.geoserver.org/latest/en/api/#1.0.0/datastores.yaml for more details. + * + * @param ws + * @param parameters + * @return + */ + public void publishDataStore(String ws, JSONObject parameters)throws RemoteException,Exception; + + // // +********** DELETE OPS - /** - * publisher.removeWorkspace(toDelete.getWorkspace(), true); - * @param ws - * @throws RemoteException - * @throws Exception - */ - public void deleteWorkspace(String ws)throws RemoteException,Exception; - public void deleteStyle(String style)throws RemoteException,Exception; + + public void deleteWorkspace(String ws, boolean recurse)throws RemoteException,Exception; + public void deleteStyle(String style, boolean purgeSLDFile,boolean updateReferences)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 deleteLayer(String layer)throws RemoteException,Exception; + public void deleteDataStore(String ds, boolean recurse)throws RemoteException,Exception; + public void deleteLayer(String layer,boolean recurse)throws RemoteException,Exception; } diff --git a/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/GSRESTClientImpl.java b/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/GSRESTClientImpl.java index 8f576c1..678fe41 100644 --- a/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/GSRESTClientImpl.java +++ b/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/GSRESTClientImpl.java @@ -1,17 +1,17 @@ package org.gcube.spatial.data.clients.geoserver; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; +import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest; import org.gcube.spatial.data.clients.AbstractGenericRESTClient; -import org.gcube.spatial.data.clients.GenericLoginClient; import org.gcube.spatial.data.clients.model.ConnectionDescriptor; -import org.gcube.spatial.data.sdi.model.credentials.Credentials; import org.gcube.spatial.data.sdi.model.faults.RemoteException; -import org.gcube.spatial.data.sdi.model.gn.LoginLevel; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.json.simple.JSONObject; import com.jayway.jsonpath.Configuration; @@ -22,7 +22,7 @@ import com.jayway.jsonpath.Option; import lombok.extern.slf4j.Slf4j; @Slf4j -public class GSRESTClientImpl extends AbstractGenericRESTClient implements GSRESTClient,GenericLoginClient{ +public class GSRESTClientImpl extends AbstractGenericRESTClient implements GSRESTClient{ private static final String API_BASE_PATH="rest"; @@ -30,6 +30,8 @@ public class GSRESTClientImpl extends AbstractGenericRESTClient implements GSRES private static final String DATASTORE_BASE_PATH="datastores"; private static final String STYLES_BASE_PATH="styles"; private static final String LAYERS_BASE_PATH="layers"; + private static final String FEATURES_BASE_PATH="featuretype"; + static Configuration JSON_PATH_ALWAYS_LIST_CONFIG= Configuration.builder().options(Option.ALWAYS_RETURN_LIST,Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build(); @@ -42,27 +44,6 @@ public class GSRESTClientImpl extends AbstractGenericRESTClient implements GSRES setBasePath(API_BASE_PATH); -// log.info("Connecting to GS @ "+conn.getEndpoint()); -// -// Credentials cr=conn.getCredentials().get(0); -// log.debug("Using "+cr); -// register(HttpAuthenticationFeature.universal(cr.getUsername(), cr.getPassword())); - - - } - - - - - @Override - public void login() { - - } - - @Override - public void login(LoginLevel lvl) { - // TODO Auto-generated method stub - } //************************** READ OPS @@ -70,33 +51,33 @@ public class GSRESTClientImpl extends AbstractGenericRESTClient implements GSRES @Override public JSONObject getWorkspaces() throws RemoteException,Exception { - return get(WS_BASE_PATH); + return get(WS_BASE_PATH,JSONObject.class); } @Override public JSONObject getWorkspace(String workspace) throws RemoteException, Exception { - return get(WS_BASE_PATH+"/"+workspace); + return get(WS_BASE_PATH+"/"+workspace,JSONObject.class); } @Override public JSONObject getDataStoresInWorkspace(String ws) throws RemoteException, Exception { - return get(WS_BASE_PATH+"/"+ws+"/datastores"); + return get(WS_BASE_PATH+"/"+ws+"/"+DATASTORE_BASE_PATH,JSONObject.class); } @Override public JSONObject getStyles() throws RemoteException, Exception { - return get(STYLES_BASE_PATH); + return get(STYLES_BASE_PATH,JSONObject.class); } @Override public JSONObject getStylesByLayer(String layer) throws RemoteException, Exception { - return get(LAYERS_BASE_PATH+"/"+layer+"/styles"); + return get(LAYERS_BASE_PATH+"/"+layer+"/"+STYLES_BASE_PATH,JSONObject.class); } @Override public JSONObject getStylesInWorkspace(String ws) throws RemoteException, Exception { - return get(WS_BASE_PATH+"/"+ws+"/styles"); + return get(WS_BASE_PATH+"/"+ws+"/"+STYLES_BASE_PATH,JSONObject.class); } @Override @@ -134,59 +115,124 @@ 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; + return get(WS_BASE_PATH+"/"+workspace+"/"+DATASTORE_BASE_PATH+"/"+dsName,JSONObject.class); } @Override public String getSLD(String styleName) throws RemoteException, Exception { - // TODO Auto-generated method stub - return null; + return get(STYLES_BASE_PATH+"/"+styleName,String.class,"application/vnd.ogc.sld+xml"); + } + + + + @Override + public JSONObject getFeatureType(String workspace, String featureName) throws RemoteException, Exception { + return get(WS_BASE_PATH+"/"+workspace+"/"+FEATURES_BASE_PATH+"/"+featureName,JSONObject.class); + } + + @Override + public List getFeatureTypesInDataStore(String workspace, String datastore) + throws RemoteException, Exception { + DocumentContext sourceCtx=JsonPath.using(JSON_PATH_ALWAYS_LIST_CONFIG).parse( + get(WS_BASE_PATH+"/"+workspace+"/"+DATASTORE_BASE_PATH+"/"+datastore+"/"+FEATURES_BASE_PATH)); + return sourceCtx.read("$..name"); + } + + + @Override + public List getFeatureTypesInWorkspace(String workspace) throws RemoteException, Exception { + DocumentContext sourceCtx=JsonPath.using(JSON_PATH_ALWAYS_LIST_CONFIG).parse( + get(WS_BASE_PATH+"/"+workspace+"/"+FEATURES_BASE_PATH)); + return sourceCtx.read("$..name"); + } + + + @Override + public JSONObject getLayer(String layerName) throws RemoteException, Exception { + return get(LAYERS_BASE_PATH+"/"+layerName,JSONObject.class); + } + + + @Override + public JSONObject getLayerInWorkspace(String ws, String layerName) throws RemoteException, Exception { + return get(WS_BASE_PATH+"/"+ws+"/"+LAYERS_BASE_PATH+"/"+layerName,JSONObject.class); + } + + + @Override + public List getLayers() throws RemoteException, Exception { + DocumentContext sourceCtx=JsonPath.using(JSON_PATH_ALWAYS_LIST_CONFIG).parse( + get(LAYERS_BASE_PATH).toString()); + return sourceCtx.read("$..name"); + } + + + @Override + public List getLayers(String workspace) throws RemoteException, Exception { + DocumentContext sourceCtx=JsonPath.using(JSON_PATH_ALWAYS_LIST_CONFIG).parse( + get(WS_BASE_PATH+"/"+workspace+"/"+LAYERS_BASE_PATH)); + return sourceCtx.read("$..name"); } //************* DELETE OPS + + @Override - public void deleteWorkspace(String ws) throws RemoteException, Exception { - delete(WS_BASE_PATH+"/"+ws); + public void deleteWorkspace(String ws,boolean recurse) throws RemoteException, Exception { + check(resolve().path(WS_BASE_PATH+"/"+ws).queryParams(Collections.singletonMap("recurse", new Object[] {recurse})).delete(),null); } + + @Override - public void deleteStyle(String style) throws RemoteException, Exception { - delete(STYLES_BASE_PATH+"/"+style); + public void deleteStyle(String style,boolean purgeSLD,boolean updateReferences) throws RemoteException, Exception { + Map params=new HashMap<>(); + params.put("recurse", new Object[] {updateReferences}); + params.put("purge", new Object[] {purgeSLD}); + check(resolve().path(STYLES_BASE_PATH+"/"+style).queryParams(params).delete(),null); } + @Override - public void deleteDataStore(String ds) throws RemoteException, Exception { - delete(DATASTORE_BASE_PATH+"/"+ds); + public void deleteDataStore(String ds,boolean recurse) throws RemoteException, Exception { + check(resolve().path(DATASTORE_BASE_PATH+"/"+ds).queryParams(Collections.singletonMap("recurse", new Object[] {recurse})).delete(),null); } + + @Override - public void deleteLayer(String layer) throws RemoteException, Exception { - delete(LAYERS_BASE_PATH+"/"+layer); + public void deleteLayer(String layer,boolean recurse) throws RemoteException, Exception { + check(resolve().path(DATASTORE_BASE_PATH+"/"+layer).queryParams(Collections.singletonMap("recurse", new Object[] {recurse})).delete(),null); } //************************* CREATE OPS -// -// @Override -// public void createStyle(String name, String content) throws RemoteException, Exception { -// // TODO Auto-generated method stub -// -// } -// + @Override public void createWorkspace(String ws) throws RemoteException, Exception { post(WS_BASE_PATH,Entity.entity(new JSONObject().put("name", ws),MediaType.APPLICATION_JSON)); } + + @Override + public void publishDataStore(String ws, JSONObject parameters) throws Exception { + post(WS_BASE_PATH+"/"+ws+"/"+DATASTORE_BASE_PATH, Entity.entity(parameters, MediaType.APPLICATION_JSON_TYPE)); + } + + + - - + //************************* BASIC @Override public boolean exist() { - // TODO Auto-generated method stub - return false; + try { + GXWebTargetAdapterRequest request =GXWebTargetAdapterRequest.newHTTPSRequest(getConn().getEndpoint()); + check(request.get(),null); + return true; + }catch(Exception e) { + return false; + } } diff --git a/gcube-geoserver-client/src/test/java/org/gcube/spatial/data/clients/geoserver/GSTests.java b/gcube-geoserver-client/src/test/java/org/gcube/spatial/data/clients/geoserver/GSTests.java index 7721753..021d500 100644 --- a/gcube-geoserver-client/src/test/java/org/gcube/spatial/data/clients/geoserver/GSTests.java +++ b/gcube-geoserver-client/src/test/java/org/gcube/spatial/data/clients/geoserver/GSTests.java @@ -3,12 +3,16 @@ package org.gcube.spatial.data.clients.geoserver; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; +import java.util.Collections; import java.util.UUID; +import javax.sound.midi.SysexMessage; + import org.gcube.sdi.test.GCubeSDITest; import org.gcube.spatial.data.clients.SDIGenericClient; import org.gcube.spatial.data.clients.model.engine.Engine; import org.gcube.spatial.data.sdi.model.faults.RemoteException; +import org.json.simple.JSONObject; import org.junit.Test; public class GSTests extends GCubeSDITest{ @@ -20,32 +24,87 @@ public class GSTests extends GCubeSDITest{ return (GSRESTClient) obj.getRESTClient(); } + @Test + public void basics() throws RemoteException, Exception { + assumeTrue(isTestInfrastructureEnabled()); + GSRESTClient client=getClient(); + assertTrue(client.exist()); + } + + @Test public void gets() throws RemoteException, Exception { assumeTrue(isTestInfrastructureEnabled()); - - + + GSRESTClient client=getClient(); - for(String ws : client.getWorkspaceNames()){ - try { + client.authenticate(); + client.getWorkspaceNames().forEach((String ws)->{ + try { + if(ws!=null) { + System.out.println("************************************ WS ***************************"); System.out.println(client.getWorkspace(ws)); - System.out.println(client.getStylesNamesinWorkspace(ws)); - System.out.println(client.getDataStoresInWorkspace(ws)); - }catch(RemoteException e) { - System.err.println("Unable to check "+ws+" : "+e.getRemoteMessage()); - } - } - } - + for(String sld:client.getStylesNamesinWorkspace(ws)) + if(sld!=null)try { + System.out.println(client.getSLD(sld)); + }catch(RemoteException e ) {System.err.println("Unable to read SLD "+sld+" from "+ws+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());} - @Test - public void CRUDS() throws RemoteException, Exception { - assumeTrue(isTestInfrastructureEnabled()); - GSRESTClient client=getClient(); - String ws=UUID.randomUUID().toString().replace("-", "_"); - client.createWorkspace(ws); - client.getWorkspace(ws); - client.deleteWorkspace(ws); + + System.out.println(client.getDataStoresInWorkspace(ws)); + + for(String ds:client.getDataStoresNamesInWorkspace(ws)) + if(ds!=null) { + try{ System.out.println(client.getDataStore(ws, ds)); + }catch(RemoteException e ) {System.err.println("Unable to read DS "+ds+" from "+ws+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());} + } + + + for(String ft : client.getFeatureTypesInWorkspace(ws)) + if(ft!=null)try { + System.out.println(client.getFeatureType(ws, ft)); + }catch(RemoteException e ) {System.err.println("Unable to read FT "+ft+" from "+ws+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());} + + + for(String l : client.getLayers(ws)) + if(l!=null)try { + System.out.println(client.getLayerInWorkspace(ws, l)); + System.out.println(client.getLayer(l)); + }catch(RemoteException e ) {System.err.println("Unable to read LA "+l+" from "+ws+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());} + + } + }catch(Exception e) { + System.err.println("Unable to check "+ws+" : "+e); + } + + + + }); + + System.out.println("Listing layers without ws"); +// client.getLayers().forEach((String l)->{ +// try{System.out.println(client.getLayer(l)); +// }catch(RemoteException e ) {System.err.println("Unable to read LA "+l+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());} +// catch(Exception e ) {System.err.println("Unable to read LA "+l+". Cause : "+e.getMessage());} +// }); } + + + @Test + public void CRUDS() throws RemoteException, Exception { + assumeTrue(isTestInfrastructureEnabled()); + GSRESTClient client=getClient(); + + client.authenticate(); + String ws=UUID.randomUUID().toString().replace("-", "_"); + client.createWorkspace(ws); + client.getWorkspace(ws); + + + String myDS=UUID.randomUUID().toString().replace("-", "_"); + + client.publishDataStore(ws, new DataStoreRegistrationRequest(myDS).getDatastore()); + + client.deleteWorkspace(ws,true); + } } diff --git a/sdi-generic-client/pom.xml b/sdi-generic-client/pom.xml index c49208f..c8b73ff 100644 --- a/sdi-generic-client/pom.xml +++ b/sdi-generic-client/pom.xml @@ -31,21 +31,31 @@ - org.gcube.core - common-fw-clients + org.gcube.core + common-fw-clients - org.gcube.core - common-generic-clients + org.gcube.core + common-generic-clients - org.glassfish.jersey.core - jersey-client + org.glassfish.jersey.core + jersey-client + + + + + + + + + + @@ -63,7 +73,7 @@ - + @@ -92,7 +102,13 @@ org.gcube.spatial.data test - + + + com.googlecode.json-simple + json-simple + 1.1.1 + test + diff --git a/sdi-generic-client/src/main/java/org/gcube/spatial/data/clients/AbstractGenericRESTClient.java b/sdi-generic-client/src/main/java/org/gcube/spatial/data/clients/AbstractGenericRESTClient.java index 715c9c8..b87c1c7 100644 --- a/sdi-generic-client/src/main/java/org/gcube/spatial/data/clients/AbstractGenericRESTClient.java +++ b/sdi-generic-client/src/main/java/org/gcube/spatial/data/clients/AbstractGenericRESTClient.java @@ -6,19 +6,25 @@ import java.util.HashSet; import java.util.Set; import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; 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.credentials.AccessType; +import org.gcube.spatial.data.sdi.model.credentials.Credentials; import org.gcube.spatial.data.sdi.model.faults.RemoteException; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; @Slf4j -public abstract class AbstractGenericRESTClient { +public abstract class AbstractGenericRESTClient implements GenericLoginClient{ + @Getter private ConnectionDescriptor conn; @@ -26,11 +32,43 @@ public abstract class AbstractGenericRESTClient { @Setter private String basePath=null; + @Setter + private MediaType defaultMediaType=MediaType.APPLICATION_JSON_TYPE; + + + private Set> toRegisterClasses=new HashSet>(); private Set toRegisterObjects=new HashSet<>(); protected AbstractGenericRESTClient(ConnectionDescriptor conn) { this.conn=conn; + + // defaults + toRegisterClasses.add(FollowRedirectFilter.class); + + } + + + 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) { @@ -45,6 +83,11 @@ public abstract class AbstractGenericRESTClient { protected GXWebTargetAdapterRequest resolve() throws UnsupportedEncodingException { GXWebTargetAdapterRequest toReturn =GXWebTargetAdapterRequest.newHTTPSRequest(conn.getEndpoint()); + + toRegisterClasses.forEach((Class clazz)->{toReturn.register(clazz);}); + toRegisterObjects.forEach((Object obj)->{toReturn.register(obj);}); + + if(basePath!=null) return toReturn.path(basePath); else return toReturn; @@ -86,27 +129,35 @@ public abstract class AbstractGenericRESTClient { } protected String get(String path) throws Exception { - return get(path,null); + return get(path,String.class); } - protected T get(String path, Class clazz) throws Exception{ + protected T get(String path, Class clazz) throws Exception { + return get(path,clazz,defaultMediaType); + } + + protected T get(String path, Class clazz, String mediaType) throws Exception{ return makeCall(new Call() { public T call(GXWebTargetAdapterRequest endpoint) throws Exception { - return check(endpoint.path(path).get(),clazz); + return check(endpoint.path(path).header("accept",mediaType).get(),clazz); }; }); } + protected T get(String path, Class clazz, MediaType type) throws Exception{ + return get(path,clazz,type+""); + } + protected static T check(GXInboundResponse resp, Class clazz) throws RemoteException{ try { - log.debug("Checking Response {}",resp); - if(resp.hasGXError()) { - RemoteException e=new RemoteException("Error received from server"); + log.debug("Checking Response [Status : {}, Msg : {}]",resp.getHTTPCode(),resp.getMessage()); + if(!resp.isSuccessResponse()) { + RemoteException e=new RemoteException("Error received from server "); e.setRemoteMessage(resp.getMessage()); e.setResponseHTTPCode(resp.getHTTPCode()); e.setContent(resp.getStreamedContentAsString()); diff --git a/sdi-generic-client/src/main/java/org/gcube/spatial/data/clients/FollowRedirectFilter.java b/sdi-generic-client/src/main/java/org/gcube/spatial/data/clients/FollowRedirectFilter.java new file mode 100644 index 0000000..28385fa --- /dev/null +++ b/sdi-generic-client/src/main/java/org/gcube/spatial/data/clients/FollowRedirectFilter.java @@ -0,0 +1,35 @@ +package org.gcube.spatial.data.clients; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.core.Response; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class FollowRedirectFilter implements ClientResponseFilter +{ + @Override + public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException + { + if (responseContext.getStatusInfo().getFamily() != Response.Status.Family.REDIRECTION) + return; + + + URI targetUri=responseContext.getLocation(); + URI originalURI=requestContext.getUri(); + log.debug("Following redirect from {} to {}",originalURI,targetUri); + + Response resp = requestContext.getClient(). + target(targetUri).request().method(requestContext.getMethod()); + + responseContext.setEntityStream((InputStream) resp.getEntity()); + responseContext.setStatusInfo(resp.getStatusInfo()); + responseContext.setStatus(resp.getStatus()); + } +} diff --git a/sdi-generic-client/src/test/java/org/gcube/spatial/data/clients/Tests.java b/sdi-generic-client/src/test/java/org/gcube/spatial/data/clients/Tests.java index c69ab92..78ce626 100644 --- a/sdi-generic-client/src/test/java/org/gcube/spatial/data/clients/Tests.java +++ b/sdi-generic-client/src/test/java/org/gcube/spatial/data/clients/Tests.java @@ -1,12 +1,22 @@ package org.gcube.spatial.data.clients; +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import static org.junit.Assume.assumeTrue; +import java.net.URI; +import java.util.List; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.sdi.test.GCubeSDITest; 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.junit.BeforeClass; +import org.gcube.spatial.data.sdi.model.ServiceConstants; +import org.gcube.spatial.data.sdi.model.credentials.AccessType; +import org.gcube.spatial.data.sdi.model.service.GeoServiceDescriptor; +import org.json.simple.JSONObject; import org.junit.Test; public class Tests extends GCubeSDITest{ @@ -19,43 +29,83 @@ public class Tests extends GCubeSDITest{ - String url=scopeConfiguration().getByEngine(Engine.GS_ENGINE).get(0).getBaseEndpoint(); - new TestClient(new ConnectionDescriptor(url)).get(""); +// String url=scopeConfiguration().getByEngine(Engine.GS_ENGINE).get(0).getBaseEndpoint(); + + + + URI uri=queryForGCoreEndpoint(ServiceConstants.SERVICE_CLASS, ServiceConstants.SERVICE_NAME).get(0).profile().endpointMap(). + get("org.gcube.spatial.data.sdi.SDIService").uri(); + + System.out.println(uri+"\t"+new TestClient(new ConnectionDescriptor(uri.toString())).get("SDI")); } -// static String sdiHostname="sdi-t.pre.d4science.org"; -// static String scope ="/pred4s/preprod/preVRE"; -// -// static String basePath=""; -// static String scope = "/gcube/devsec/devVRE"; -// -// -// static ConnectionDescriptor DEV_SDI=new ConnectionDescriptor("https://sdi.d4science.org/sdi-service/gcube/service/"); -// -// static ConnectionDescriptor DEV_GS=new ConnectionDescriptor("https://geoserver1.dev.d4science.org/geoserver/rest"); -// -// @BeforeClass -// public static void setScope(){ -// TokenSetter.set(scope); -// } -// -// @Test -// public void testContext() throws RemoteException, Exception { -// -// -// new TestClient(DEV_SDI).get("SDI"); -// -// } -// -// @Test -// public void testBasicAuth() throws RemoteException, Exception { -// -// -// TestClient c=new TestClient(DEV_GS); -// c.setHttpBasicAuth("admin", "GS-d3v-98751"); -// c.get("workspaces"); -// -// } -// + @Test + public void testRedirection() throws Exception { + assumeTrue(isTestInfrastructureEnabled()); + + assumeTrue(isTestInfrastructureEnabled()); + + + + GeoServiceDescriptor service=scopeConfiguration().getByEngine(Engine.GS_ENGINE).get(0); + + service.setBaseEndpoint(service.getBaseEndpoint().replaceAll("https", "http")); + + + TestClient client=new TestClient(service.getConnection()); + System.out.println(service.getBaseEndpoint()+"\t"+client.get("")); + } + + + + + @Test + public void testBasicAuth() throws Exception { + assumeTrue(isTestInfrastructureEnabled()); + + + + GeoServiceDescriptor service=scopeConfiguration().getByEngine(Engine.GS_ENGINE).get(0); + + + TestClient client=new TestClient(service.getConnection()); + client.setBasePath("rest"); + client.authenticate(AccessType.ADMIN); + System.out.println(service.getBaseEndpoint()+"\t"+client.get("workspaces")); + } + + + @Test + public void testSerialization() throws Exception { + assumeTrue(isTestInfrastructureEnabled()); + + + + GeoServiceDescriptor service=scopeConfiguration().getByEngine(Engine.GS_ENGINE).get(0); + + + TestClient client=new TestClient(service.getConnection()); + client.setBasePath("rest"); + client.authenticate(AccessType.ADMIN); + System.out.println(service.getBaseEndpoint()+"\t"+client.get("workspaces",JSONObject.class).toString()); + } + + + private static List queryForGCoreEndpoint(String serviceClass,String serviceName){ + + + SimpleQuery query =queryFor(GCoreEndpoint.class); + query.addCondition("$resource/Profile/ServiceClass/text() eq '"+serviceClass+"'") + .addCondition("$resource/Profile/ServiceName/text() eq '"+serviceName+"'"); + // .setResult("$resource/Profile/AccessPoint"); + + DiscoveryClient client = clientFor(GCoreEndpoint.class); + + return client.submit(query); + } + + + + } diff --git a/sdi-interface/src/main/java/org/gcube/spatial/data/clients/GenericLoginClient.java b/sdi-interface/src/main/java/org/gcube/spatial/data/clients/GenericLoginClient.java index e4f8d66..4f5b3f3 100644 --- a/sdi-interface/src/main/java/org/gcube/spatial/data/clients/GenericLoginClient.java +++ b/sdi-interface/src/main/java/org/gcube/spatial/data/clients/GenericLoginClient.java @@ -1,12 +1,12 @@ package org.gcube.spatial.data.clients; -import org.gcube.spatial.data.sdi.model.gn.LoginLevel; +import org.gcube.spatial.data.sdi.model.credentials.AccessType; public interface GenericLoginClient { - public void login(); + public void authenticate(); - public void login(LoginLevel lvl); + public void authenticate(AccessType type); } diff --git a/sdi-interface/src/main/java/org/gcube/spatial/data/sdi/interfaces/SDIManagement.java b/sdi-interface/src/main/java/org/gcube/spatial/data/sdi/interfaces/SDIManagement.java index a48bdca..be45873 100644 --- a/sdi-interface/src/main/java/org/gcube/spatial/data/sdi/interfaces/SDIManagement.java +++ b/sdi-interface/src/main/java/org/gcube/spatial/data/sdi/interfaces/SDIManagement.java @@ -2,6 +2,7 @@ package org.gcube.spatial.data.sdi.interfaces; import org.gcube.spatia.data.model.profiles.ApplicationProfile; import org.gcube.spatial.data.clients.SDIGenericClient; +import org.gcube.spatial.data.clients.model.ConnectionDescriptor; import org.gcube.spatial.data.sdi.model.ScopeConfiguration; import org.gcube.spatial.data.sdi.model.faults.RemoteException; import org.gcube.spatial.data.sdi.model.health.HealthReport; @@ -15,5 +16,5 @@ public interface SDIManagement { public SDIGenericClient getClientByEngineId(String engineId) throws Exception; - + } diff --git a/sdi-interface/src/main/java/org/gcube/spatial/data/sdi/model/service/GeoServerDescriptor.java b/sdi-interface/src/main/java/org/gcube/spatial/data/sdi/model/service/GeoServerDescriptor.java index 632070b..996d8aa 100644 --- a/sdi-interface/src/main/java/org/gcube/spatial/data/sdi/model/service/GeoServerDescriptor.java +++ b/sdi-interface/src/main/java/org/gcube/spatial/data/sdi/model/service/GeoServerDescriptor.java @@ -6,6 +6,7 @@ import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; +import org.gcube.spatial.data.clients.model.ConnectionDescriptor; import org.gcube.spatial.data.clients.model.engine.Engine; import org.gcube.spatial.data.sdi.model.credentials.Credentials; @@ -17,7 +18,7 @@ import lombok.ToString; @Getter @Setter -@ToString +@ToString(callSuper=true) @NoArgsConstructor @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @@ -46,4 +47,6 @@ public class GeoServerDescriptor extends GeoServiceDescriptor { private Long hostedLayersCount; private String engineId=Engine.GS_ENGINE; + + } diff --git a/sdi-test-commons/src/main/java/org/gcube/sdi/test/GCubeSDITest.java b/sdi-test-commons/src/main/java/org/gcube/sdi/test/GCubeSDITest.java index 2300e60..68837c7 100644 --- a/sdi-test-commons/src/main/java/org/gcube/sdi/test/GCubeSDITest.java +++ b/sdi-test-commons/src/main/java/org/gcube/sdi/test/GCubeSDITest.java @@ -3,6 +3,7 @@ package org.gcube.sdi.test; import static org.junit.Assume.assumeTrue; import org.gcube.sdi.test.client.SimpleSDIManagerClient; +import org.gcube.spatial.data.clients.model.ConnectionDescriptor; import org.gcube.spatial.data.sdi.interfaces.SDIManagement; import org.gcube.spatial.data.sdi.model.ScopeConfiguration; import org.gcube.spatial.data.sdi.model.faults.RemoteException; diff --git a/sdi-test-commons/src/main/java/org/gcube/sdi/test/client/SimpleSDIManagerClient.java b/sdi-test-commons/src/main/java/org/gcube/sdi/test/client/SimpleSDIManagerClient.java index 953d6ee..e829b4a 100644 --- a/sdi-test-commons/src/main/java/org/gcube/sdi/test/client/SimpleSDIManagerClient.java +++ b/sdi-test-commons/src/main/java/org/gcube/sdi/test/client/SimpleSDIManagerClient.java @@ -16,6 +16,7 @@ import org.gcube.common.gxrest.response.inbound.GXInboundResponse; import org.gcube.spatia.data.model.profiles.ApplicationProfile; import org.gcube.spatial.data.clients.SDIClientManager; import org.gcube.spatial.data.clients.SDIGenericClient; +import org.gcube.spatial.data.clients.model.ConnectionDescriptor; import org.gcube.spatial.data.sdi.interfaces.SDIManagement; import org.gcube.spatial.data.sdi.model.ScopeConfiguration; import org.gcube.spatial.data.sdi.model.ServiceConstants; @@ -63,7 +64,7 @@ public class SimpleSDIManagerClient implements SDIManagement{ } private final ProxyDelegate delegate; - private SDIClientManager clientManager=null; + private SDIClientManager clientManager=new SDIClientManager(); public SimpleSDIManagerClient(ProxyDelegate delegate) { this.delegate=delegate; @@ -119,4 +120,16 @@ public class SimpleSDIManagerClient implements SDIManagement{ throw new RuntimeException("No implementation available for "+engineId); } + +// @Override +// public ConnectionDescriptor getService() throws RemoteException { +// delegate.make(new Call() { +// @Override +// public ConnectionDescriptor call(GXWebTargetAdapterRequest endpoint) throws Exception { +// return new ConnectionDescriptor(endpoint.) +// } +// }); +// +// return new ConnectionDescriptor(endpoint) +// } }