GS client basic

This commit is contained in:
Fabio Sinibaldi 2021-02-18 18:12:58 +01:00
parent e626e9ebbe
commit 54b6f156da
14 changed files with 232 additions and 84 deletions

View File

@ -1,9 +1,6 @@
package org.gcube.spatial.data.clients.geoserver;
import java.util.List;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public interface GSRESTClient {
@ -13,16 +10,16 @@ public interface GSRESTClient {
// WS
public JSONArray getWorkspaces()throws RemoteException,Exception;
public JSONObject getWorkspaces()throws RemoteException,Exception;
public JSONObject getWorkspace(String workspace)throws RemoteException, Exception;
// DS
public List<String> getDataStoresInWorkspace(String ws)throws RemoteException,Exception;
public JSONObject getDataStoresInWorkspace(String ws)throws RemoteException,Exception;
// SLD
public List<String> getStyles()throws RemoteException,Exception;
public List<String> getStylesInWorkspace(String ws)throws RemoteException,Exception;
public List<String> getStylesByLayer(String layer)throws RemoteException,Exception;
public JSONObject getStyles()throws RemoteException,Exception;
public JSONObject getStylesInWorkspace(String ws)throws RemoteException,Exception;
public JSONObject getStylesByLayer(String layer)throws RemoteException,Exception;
// *********** CREATE OPS
public void createWorkspace(String ws)throws RemoteException,Exception;
@ -30,4 +27,10 @@ public interface GSRESTClient {
public void createStyle(String name,String content)throws RemoteException,Exception;
// // +********** DELETE OPS
// public void deleteWorkspace(String ws)throws RemoteException,Exception;
// public void deleteStyle(String style)throws RemoteException,Exception;
// public void deleteDataStore(String ds)throws RemoteException,Exception;
// public void deleteLayer(String layer)throws RemoteException,Exception;
}

View File

@ -2,6 +2,7 @@ package org.gcube.spatial.data.clients.geoserver;
import java.util.List;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
@ -9,21 +10,44 @@ import org.gcube.common.clients.Call;
import org.gcube.spatial.data.clients.AbstractGenericClient;
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.JSONArray;
import org.json.simple.JSONObject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class GSRESTClientImpl extends AbstractGenericClient implements GSRESTClient,GenericLoginClient{
private static final String API_BASE_PATH="rest";
private static final String WS_BASE_PATH="workspaces";
private static final String DATASTORE_BASE_PATH="datastores";
private static final String STYLES_BASE_PATH="styles";
private static final String LAYERS_BASE_PATH="layers";
protected GSRESTClientImpl(ConnectionDescriptor conn) {
super(conn);
// localhost:8080/geoserver/rest
// register(providerClass);
// getClient().
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() {
// TODO Auto-generated method stub
}
@ -34,60 +58,45 @@ public class GSRESTClientImpl extends AbstractGenericClient implements GSRESTCli
}
@Override
public JSONArray getWorkspaces() throws RemoteException,Exception {
return makeCall(new Call<WebTarget, JSONArray>() {
public JSONArray call(WebTarget endpoint) throws Exception {
return check(endpoint.path("workspace").request(MediaType.APPLICATION_JSON).get(),JSONArray.class);
};
});
public JSONObject getWorkspaces() throws RemoteException,Exception {
return get(WS_BASE_PATH);
}
@Override
public JSONObject getWorkspace(String workspace) throws RemoteException, Exception {
// TODO Auto-generated method stub
return null;
return get(WS_BASE_PATH+"/"+workspace);
}
@Override
public List<String> getDataStoresInWorkspace(String ws) {
// TODO Auto-generated method stub
return null;
public JSONObject getDataStoresInWorkspace(String ws) throws RemoteException, Exception {
return get(WS_BASE_PATH+"/"+ws+"/datastores");
}
@Override
public List<String> getStyles() {
// TODO Auto-generated method stub
return null;
public JSONObject getStyles() throws RemoteException, Exception {
return get(STYLES_BASE_PATH);
}
@Override
public JSONObject getStylesByLayer(String layer) throws RemoteException, Exception {
return get(LAYERS_BASE_PATH+"/"+layer+"/styles");
}
@Override
public JSONObject getStylesInWorkspace(String ws) throws RemoteException, Exception {
return get(WS_BASE_PATH+"/"+ws+"/styles");
}
@Override
public List<String> getStylesInWorkspace(String ws) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<String> getStylesByLayer(String layer) {
// TODO Auto-generated method stub
return null;
}
@Override
public void createWorkspace(String ws) {
public void createStyle(String name, String content) throws RemoteException, Exception {
// TODO Auto-generated method stub
}
@Override
public void createStyle(String name, String content) {
// TODO Auto-generated method stub
public void createWorkspace(String ws) throws RemoteException, Exception {
post(WS_BASE_PATH,Entity.entity(new JSONObject().put("name", ws),MediaType.APPLICATION_JSON));
}
}

View File

@ -3,18 +3,20 @@ package org.gcube.spatial.data.clients.geoserver;
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.clients.model.engine.Range;
public class GeoServerClient implements SDIGenericClient{
@Override
public ClientInfo getInfo() {
// TODO Auto-generated method stub
return null;
return new ClientInfo("gs-rest", "GeoServer REST", "GeoServer REST client", "come class",
new Engine(Engine.GS_ENGINE, "Default GeoServerVersion", new Range("2.0.0", null)));
}
@Override
public Object getRESTClient(ConnectionDescriptor conn) {
public GSRESTClient getRESTClient(ConnectionDescriptor conn) {
return new GSRESTClientImpl(conn);
}

View File

@ -0,0 +1 @@
org.gcube.spatial.data.clients.geoserver.GeoServerClient

View File

@ -16,19 +16,20 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class GeoServerFilter implements Filter {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.warn("init() method");
log.warn("init() method");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
logger.warn("doFilter() method");
log.warn("doFilter() method");
ServletRequestWrapper request = new ServletRequestWrapper((HttpServletRequest) servletRequest);
HttpServletResponse response = (HttpServletResponse) servletResponse;
@ -44,11 +45,11 @@ public class GeoServerFilter implements Filter {
+ Base64.getEncoder().encodeToString(token.getBytes());
request.addHeader(AuthenticationUtils.AUTHORIZATION, basic_authentication);
logger.warn("Added authorization header : " + request.getHeader(AuthenticationUtils.AUTHORIZATION));
log.warn("Added authorization header : " + request.getHeader(AuthenticationUtils.AUTHORIZATION));
request.addParameter(AuthenticationUtils.USERNAME, username);
request.addParameter(AuthenticationUtils.PASSWORD, password);
logger.warn("Added parameters in the request : " + username +"/" + password);
log.warn("Added parameters in the request : " + username +"/" + password);
}
filterChain.doFilter(request, response);
@ -56,7 +57,7 @@ public class GeoServerFilter implements Filter {
@Override
public void destroy() {
logger.warn("destroy() method");
log.warn("destroy() method");
}
}

View File

@ -6,7 +6,9 @@ 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.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.gcube.common.clients.Call;
@ -15,6 +17,7 @@ import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.gcube.spatial.data.sdi.model.metadata.TemplateDescriptor;
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;
@ -56,6 +59,11 @@ public abstract class AbstractGenericClient {
client=null;
}
protected void register(Object provider) {
config.register(provider);
client=null;
}
public Client getClient() {
if(client==null) {
client = ClientBuilder.newClient(config)
@ -80,6 +88,41 @@ public abstract class AbstractGenericClient {
}
}
protected JSONObject get(String path) throws Exception {
return get(path,JSONObject.class);
}
protected void delete(String path) throws Exception {
check(resolve().path(path).request(MediaType.APPLICATION_JSON).delete(),null);
}
protected void post(String path,Entity<?> entity)throws Exception{
post(path,entity,null);
}
protected <T> T post(String path,Entity<?> entity,Class<T> returnClazz) throws Exception {
return makeCall(new Call<WebTarget, T>() {
public T call(WebTarget endpoint) throws Exception {
return check(endpoint.path(path).request(MediaType.APPLICATION_JSON).
post(entity),returnClazz);
};
});
}
protected <T> T get(String path, Class<T> clazz) throws Exception{
return makeCall(new Call<WebTarget, T>() {
public T call(WebTarget endpoint) throws Exception {
return check(endpoint.path(path).request(MediaType.APPLICATION_JSON).get(),clazz);
};
});
}
protected static <T> T check(Response resp, Class<T> clazz) throws RemoteException{
@ -93,6 +136,7 @@ public abstract class AbstractGenericClient {
}else {
if(clazz==null) return null;
String respString=resp.readEntity(String.class);
if(clazz==String.class) return (T) respString;
try {
return mapper.readValue(respString, clazz);
} catch (IOException e) {

View File

@ -0,0 +1,23 @@
package org.gcube.spatial.data.clients;
import org.gcube.spatial.data.clients.model.ConnectionDescriptor;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
public class TestClient extends AbstractGenericClient{
public TestClient(ConnectionDescriptor conn) {
super(conn);
}
public void setHttpBasicAuth(String user,String pwd) {
super.register(HttpAuthenticationFeature.universal(user, pwd));
}
}

View File

@ -0,0 +1,33 @@
package org.gcube.spatial.data.clients;
import java.util.Properties;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TokenSetter {
private static Properties props=new Properties();
static{
try {
props.load(TokenSetter.class.getResourceAsStream("/tokens.properties"));
} catch (Exception e) {
throw new RuntimeException("YOU NEED TO SET TOKEN FILE IN CONFIGURATION");
}
}
public static void set(String scope){
try{
if(!props.containsKey(scope)) throw new RuntimeException("No token found for scope : "+scope);
SecurityTokenProvider.instance.set(props.getProperty(scope));
}catch(Throwable e){
log.warn("Unable to set token for scope "+scope,e);
}
ScopeProvider.instance.set(scope);
}
}

View File

@ -1,6 +1,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.sdi.model.ScopeConfiguration;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.gcube.spatial.data.sdi.model.health.HealthReport;
@ -12,4 +13,7 @@ public interface SDIManagement {
public ApplicationProfile getProfile(String serviceClass,String serviceName) throws RemoteException;
public SDIGenericClient getClientByEngineId(String engineId) throws Exception;
}

View File

@ -74,7 +74,11 @@
<scope>test</scope>
</dependency>
<dependency>
<artifactId>gcube-geoserver-client</artifactId>
<groupId>org.gcube.spatial.data</groupId>
<scope>test</scope>
</dependency>
<!-- Test log binding -->

View File

@ -1,14 +0,0 @@
package org.gcube.spatial.data.sdi;
import java.io.File;
import javax.xml.bind.JAXBException;
import org.opengis.metadata.Metadata;
public interface SDICatalog {
}

View File

@ -1,7 +0,0 @@
package org.gcube.spatial.data.sdi;
public interface SDIClient {
//public Scope getCurrentConfiguration();
}

View File

@ -6,18 +6,27 @@ import javax.ws.rs.core.MediaType;
import org.gcube.common.clients.Call;
import org.gcube.common.clients.delegates.ProxyDelegate;
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.engine.Engine;
import org.gcube.spatial.data.sdi.interfaces.SDIManagement;
import org.gcube.spatial.data.sdi.model.ScopeConfiguration;
import org.gcube.spatial.data.sdi.model.ServiceConstants;
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.service.GeoServiceDescriptor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DefaultSDIManagement implements SDIManagement{
private final ProxyDelegate<WebTarget> delegate;
private SDIClientManager clientManager=new SDIClientManager();
public DefaultSDIManagement(ProxyDelegate<WebTarget> delegate) {
this.delegate=delegate;
}
@ -67,4 +76,34 @@ public class DefaultSDIManagement implements SDIManagement{
throw new RemoteException(e);
}
}
@Override
public SDIGenericClient getClientByEngineId(String engineId) throws Exception {
ScopeConfiguration conf=getConfiguration();
GeoServiceDescriptor desc=null;
log.info("Looking for {} under {} ",engineId,conf.getContextName());
switch (engineId) {
case Engine.GN_ENGINE:
if(conf.getGeonetworkConfiguration().size()>0)
desc=conf.getGeonetworkConfiguration().get(0);
break;
case Engine.GS_ENGINE :
if(conf.getGeoserverClusterConfiguration().size()>0)
desc=conf.getGeoserverClusterConfiguration().get(0);
break;
case Engine.TH_ENGINE :
if(conf.getThreddsConfiguration().size()>0)
desc=conf.getThreddsConfiguration().get(0);
break;
default:
break;
}
if(desc==null) throw new Exception("No "+engineId+" available in current context. Check ScopeConfiguration object.");
else log.info("Getting client for "+desc);
return clientManager.get(engineId, desc.getVersion().toString());
}
}

View File

@ -1,8 +1,8 @@
package org.gcube.spatial.data.sdi;
import java.net.MalformedURLException;
import java.net.URL;
import org.gcube.spatial.data.clients.model.engine.Engine;
import org.gcube.spatial.data.sdi.interfaces.SDIManagement;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.gcube.spatial.data.sdi.plugins.SDIAbstractPlugin;
@ -12,15 +12,21 @@ public class SDITests extends BasicScopedTests{
@Test
public void getScopeConfiguration() throws RemoteException, IllegalArgumentException, MalformedURLException {
SDIManagement sdi=SDIAbstractPlugin.management().at(new URL("https://sdi-d-d4s.d4science.org/")).build();
SDIManagement sdi=SDIAbstractPlugin.management().build();
System.out.println(sdi.getConfiguration());
}
// @Test
// public void getScopeHealth() throws RemoteException {
// SDIManagement sdi=SDIAbstractPlugin.management().build();
// System.out.println(sdi.getReport());
// }
@Test
public void getScopeHealth() throws RemoteException {
SDIManagement sdi=SDIAbstractPlugin.management().build();
System.out.println(sdi.getReport());
}
@Test
public void getGS() throws Exception {
System.out.println(
SDIAbstractPlugin.management().build().getClientByEngineId(Engine.GS_ENGINE).getInfo());
}
}