2014-10-10 14:53:39 +02:00
|
|
|
/**
|
2016-04-05 11:57:55 +02:00
|
|
|
*
|
2014-10-10 14:53:39 +02:00
|
|
|
*/
|
|
|
|
package org.gcube.datatransfer.resolver.gis;
|
|
|
|
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.gcube.common.encryption.StringEncrypter;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
|
|
|
import org.gcube.common.resources.gcore.utils.Group;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.datatransfer.resolver.gis.entity.ServerParameters;
|
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2017-05-16 12:57:46 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
/**
|
2017-05-16 12:57:46 +02:00
|
|
|
* The Class GeoRuntimeReader.
|
2014-10-10 14:53:39 +02:00
|
|
|
*
|
2017-05-16 12:57:46 +02:00
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* May 16, 2017
|
2014-10-10 14:53:39 +02:00
|
|
|
*/
|
2014-10-14 12:05:38 +02:00
|
|
|
public class GeoRuntimeReader {
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
public static final String GEOSERVER_RESOURCE_NAME = "GeoServer";
|
|
|
|
public static final String GEONETWORK_RESOURCE_NAME = "GeoNetwork";
|
|
|
|
public static final String WORKSPACES_PROPERTY_NAME = "workspaces";
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2017-05-16 12:57:46 +02:00
|
|
|
/**
|
|
|
|
* The Enum GEO_SERVICE.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* May 16, 2017
|
|
|
|
*/
|
2014-10-10 14:53:39 +02:00
|
|
|
public static enum GEO_SERVICE{GEOSERVER, GEONETWORK};
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2016-09-07 15:54:30 +02:00
|
|
|
public static final Logger logger = LoggerFactory.getLogger(GeoRuntimeReader.class);
|
2014-10-10 14:53:39 +02:00
|
|
|
|
2017-05-16 12:57:46 +02:00
|
|
|
/**
|
|
|
|
* Gets the parameters.
|
|
|
|
*
|
|
|
|
* @param scope the scope
|
|
|
|
* @param geoservice the geoservice
|
|
|
|
* @return the parameters
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2017-03-24 17:38:36 +01:00
|
|
|
private ServerParameters getParameters(String scope, GEO_SERVICE geoservice) throws Exception{
|
|
|
|
String originalScope = ScopeProvider.instance.get();
|
2014-10-10 14:53:39 +02:00
|
|
|
ServerParameters parameters = new ServerParameters();
|
|
|
|
try{
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
boolean isGeoserver = geoservice.equals(GEO_SERVICE.GEOSERVER);
|
2016-04-05 11:57:55 +02:00
|
|
|
String resourceName = isGeoserver ? GEOSERVER_RESOURCE_NAME : GEONETWORK_RESOURCE_NAME;
|
2014-10-10 14:53:39 +02:00
|
|
|
ScopeProvider.instance.set(scope);
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
|
|
|
query.addCondition("$resource/Profile/Name/string() eq '"+resourceName+"'");
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2016-09-07 15:54:30 +02:00
|
|
|
logger.info("GeoRuntimeReader, using scope: "+scope + ", to get resource: "+resourceName);
|
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
|
|
|
|
|
|
|
List<ServiceEndpoint> r = client.submit(query);
|
|
|
|
if (r == null || r.isEmpty()) throw new Exception("Cannot retrieve the runtime resource: "+resourceName);
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
ServiceEndpoint se = r.get(0);
|
|
|
|
if(se.profile()==null)
|
|
|
|
throw new Exception("IS profile is null for resource: "+resourceName);
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
Group<AccessPoint> accessPoints = se.profile().accessPoints();
|
|
|
|
if(accessPoints.size()==0) throw new Exception("Accesspoint in resource "+resourceName+" not found");
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
AccessPoint ap = accessPoints.iterator().next();
|
|
|
|
parameters.setUrl(ap.address());
|
|
|
|
parameters.setUser(ap.username()); //username
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password());
|
|
|
|
parameters.setPassword(decryptedPassword); //password
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
if (isGeoserver){
|
|
|
|
Group<Property> properties = ap.properties();
|
|
|
|
if(properties.size()==0) throw new Exception("Properties in resource "+resourceName+" not found");
|
|
|
|
Iterator<Property> iter = properties.iterator();
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
while (iter.hasNext()) {
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
Property prop = iter.next();
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
if(prop.name().compareTo(WORKSPACES_PROPERTY_NAME)==0){
|
|
|
|
// logger.trace("Property "+WORKSPACES_PROPERTY_NAME+" found, setting value: "+prop.value());
|
|
|
|
// parameters.setWorkspaces(prop.value());
|
|
|
|
// break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
2016-05-12 18:29:33 +02:00
|
|
|
logger.error("Sorry, an error occurred on reading parameters in Runtime Resources",e);
|
2016-05-12 17:28:18 +02:00
|
|
|
}finally{
|
2017-03-24 17:38:36 +01:00
|
|
|
if(originalScope!=null){
|
2016-09-07 15:07:46 +02:00
|
|
|
ScopeProvider.instance.set(originalScope);
|
2017-03-24 17:38:36 +01:00
|
|
|
logger.info("scope provider set to orginal scope: "+originalScope);
|
2016-09-07 15:07:46 +02:00
|
|
|
}else{
|
|
|
|
ScopeProvider.instance.reset();
|
|
|
|
logger.info("scope provider reset");
|
|
|
|
}
|
2014-10-10 14:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:57:46 +02:00
|
|
|
/**
|
|
|
|
* Retrieve gis parameters.
|
|
|
|
*
|
|
|
|
* @param scope the scope
|
|
|
|
* @param geoservice the geoservice
|
|
|
|
* @return the server parameters
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2014-10-10 14:53:39 +02:00
|
|
|
public ServerParameters retrieveGisParameters(String scope, GEO_SERVICE geoservice) throws Exception
|
|
|
|
{
|
|
|
|
if(geoservice==null)
|
|
|
|
return null;
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
try {
|
|
|
|
return getParameters(scope, geoservice);
|
2016-04-05 11:57:55 +02:00
|
|
|
|
2014-10-10 14:53:39 +02:00
|
|
|
} catch (Exception e){
|
|
|
|
logger.error("Error retrieving the "+geoservice+" parameters", e);
|
|
|
|
throw new Exception("Error retrieving the "+geoservice+" parameters", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|