2015-07-03 11:02:43 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.accounting.persistence;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2016-07-25 17:27:51 +02:00
|
|
|
import org.gcube.accounting.datamodel.BasicUsageRecord;
|
2019-11-13 11:33:20 +01:00
|
|
|
import org.gcube.common.encryption.encrypter.StringEncrypter;
|
2015-07-03 11:02:43 +02:00
|
|
|
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;
|
2015-12-18 17:09:05 +01:00
|
|
|
import org.gcube.documentstore.persistence.PersistenceBackendConfiguration;
|
2015-07-03 11:02:43 +02:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.resources.discovery.icclient.ICFactory;
|
2018-07-18 16:19:47 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-07-03 11:02:43 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-12 14:15:28 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2015-07-03 11:02:43 +02:00
|
|
|
*/
|
2015-12-18 17:09:05 +01:00
|
|
|
public class AccountingPersistenceConfiguration extends PersistenceBackendConfiguration {
|
2015-07-03 11:02:43 +02:00
|
|
|
|
2018-07-18 16:19:47 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceConfiguration.class);
|
|
|
|
|
2015-08-28 14:49:36 +02:00
|
|
|
protected static final String TARGET_SCOPE = "targetScope";
|
|
|
|
|
2015-11-18 16:36:27 +01:00
|
|
|
protected static final String SERVICE_ENDPOINT_CATEGORY = "Accounting";
|
|
|
|
protected static final String SERVICE_ENDPOINT_NAME = "Persistence";
|
2015-07-03 11:02:43 +02:00
|
|
|
|
2015-12-18 17:09:05 +01:00
|
|
|
public static final String URL_PROPERTY_KEY = "URL";
|
|
|
|
public static final String USERNAME_PROPERTY_KEY = "username";
|
|
|
|
public static final String PASSWORD_PROPERTY_KEY = "password";
|
2015-07-08 16:41:05 +02:00
|
|
|
|
2015-07-07 12:10:52 +02:00
|
|
|
public AccountingPersistenceConfiguration(){
|
2015-12-18 17:09:05 +01:00
|
|
|
super();
|
2015-07-03 11:02:43 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 18:24:14 +01:00
|
|
|
public AccountingPersistenceConfiguration(Class<?> clz) throws Exception {
|
|
|
|
super();
|
2018-07-18 16:19:47 +02:00
|
|
|
try {
|
|
|
|
ServiceEndpoint serviceEndpoint = getServiceEndpoint(SERVICE_ENDPOINT_CATEGORY, SERVICE_ENDPOINT_NAME, clz);
|
|
|
|
setValues(serviceEndpoint, clz);
|
|
|
|
}catch (Exception e) {
|
|
|
|
logger.error("Error while instancing {} in context {}", this.getClass().getSimpleName(),
|
|
|
|
BasicUsageRecord.getContextFromToken(), e);
|
|
|
|
}
|
2015-07-08 16:41:05 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 18:24:14 +01:00
|
|
|
protected ServiceEndpoint getServiceEndpoint(String serviceEndpointCategory, String serviceEndpointName, Class<?> clz){
|
2015-07-03 11:02:43 +02:00
|
|
|
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class);
|
2015-07-14 14:44:40 +02:00
|
|
|
query.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", serviceEndpointCategory));
|
|
|
|
query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", serviceEndpointName));
|
2015-12-18 17:09:05 +01:00
|
|
|
query.addCondition(String.format("$resource/Profile/AccessPoint/Interface/Endpoint/@EntryName eq '%s'", clz.getSimpleName()));
|
|
|
|
/*
|
|
|
|
* Used in old version
|
|
|
|
* query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", PERSISTENCE_CLASS_NAME));
|
|
|
|
* query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", clz.getSimpleName()));
|
|
|
|
*/
|
2015-07-03 11:02:43 +02:00
|
|
|
query.setResult("$resource");
|
|
|
|
|
|
|
|
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
|
|
|
|
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
|
2015-08-28 14:49:36 +02:00
|
|
|
if(serviceEndpoints.size()>1){
|
2017-09-06 11:47:13 +02:00
|
|
|
String scope = BasicUsageRecord.getContextFromToken();
|
2015-08-28 14:49:36 +02:00
|
|
|
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", TARGET_SCOPE));
|
2016-02-12 17:08:49 +01:00
|
|
|
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", scope));
|
2015-08-28 14:49:36 +02:00
|
|
|
serviceEndpoints = client.submit(query);
|
|
|
|
}
|
|
|
|
|
2015-07-03 11:02:43 +02:00
|
|
|
return serviceEndpoints.get(0);
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:33:20 +01:00
|
|
|
private static String decrypt(String encrypted) throws Exception {
|
2015-07-06 15:33:54 +02:00
|
|
|
return StringEncrypter.getEncrypter().decrypt(encrypted);
|
2015-07-03 11:02:43 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 18:24:14 +01:00
|
|
|
protected void setValues(ServiceEndpoint serviceEndpoint, Class<?> clz) throws Exception{
|
2015-07-03 11:02:43 +02:00
|
|
|
Group<AccessPoint> accessPoints = serviceEndpoint.profile().accessPoints();
|
|
|
|
for(AccessPoint accessPoint : accessPoints){
|
2015-12-18 17:09:05 +01:00
|
|
|
if(accessPoint.name().compareTo(clz.getSimpleName())==0){
|
|
|
|
|
|
|
|
addProperty(URL_PROPERTY_KEY, accessPoint.address());
|
|
|
|
addProperty(USERNAME_PROPERTY_KEY, accessPoint.username());
|
2015-07-14 15:09:23 +02:00
|
|
|
|
|
|
|
String encryptedPassword = accessPoint.password();
|
|
|
|
String password = decrypt(encryptedPassword);
|
2015-12-18 17:09:05 +01:00
|
|
|
addProperty(PASSWORD_PROPERTY_KEY, password);
|
|
|
|
|
|
|
|
Map<String, Property> propertyMap = accessPoint.propertyMap();
|
|
|
|
for(String key : propertyMap.keySet()){
|
|
|
|
Property property = propertyMap.get(key);
|
|
|
|
String value = property.value();
|
|
|
|
if(property.isEncrypted()){
|
|
|
|
value = decrypt(value);
|
|
|
|
}
|
|
|
|
addProperty(key, value);
|
|
|
|
}
|
2015-07-14 15:09:23 +02:00
|
|
|
|
|
|
|
}
|
2015-07-03 11:02:43 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-06 12:36:41 +02:00
|
|
|
|
2015-07-03 11:02:43 +02:00
|
|
|
}
|