Commit 03596c12 authored by root's avatar root
Browse files

Include the functionality for read dataset info

parent bc4dfb57
......@@ -67,8 +67,11 @@ public class GetAccessApiController implements GetAccessApi {
*/
String organizationAttributesAsString=policyService.getDataSeekersOrganizationPolicy(dataSeekerID);
/*
organizationAttributesAsString="{\"name\":\"Test Organization\",\"sector\":\"Test Sector\",\"continent\":\"EU\",\"country\":\"GRE\",\"type\":\"Private Entity\",\"size\":\"medium\",\"reputation\":\"7\"}";
System.out.println("organizationAttributesAsString:"+organizationAttributesAsString);
/*"
2.- GetDatasetPolicies (DatasetID) -> de aqui obtendremos la politica con la siguiente información. -> llamada a API blockchain
struct access_policy {
string[] Sector;
......@@ -83,7 +86,9 @@ public class GetAccessApiController implements GetAccessApi {
List<Access> listAccess =new ArrayList<Access>();
for (String datasetID : datasetIDList) {
String accessPolicyasString=policyService.getDatasetPolicies(datasetID);
String accessPolicyasString=policyService.getDatasetPolicies(datasetID);
System.out.println("accessPolicyasString:"+accessPolicyasString);
// 3.-Comparar los atributos de la organización con los de la politica y construir el objeto Access y añadirlo a la lista
......
......@@ -11,8 +11,12 @@ import com.tecnalia.DVPolicyEngine.utils.HttpUtils;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
......@@ -82,6 +86,7 @@ public class PolicyService {
public String getDatasetPolicies(String datasetID) {
String accessPolicyAsString="";
try {
......@@ -95,9 +100,22 @@ public class PolicyService {
string[] Reputation_Score;
}*/
accessPolicyAsString = httpUtils.sendHttpGetRequest("URI_API_Blockain");
//{"DatasetId":"ActivityData-Riccardo"}
String jsondata="{\"DatasetId\":"+"\""+datasetID+"\""+"}";
String datasetInformationAsString="";
String address="https://datavaults-test-126.euprojects.net/readDatasetId";
datasetInformationAsString = httpUtils.sendHttpPostRequest(address, jsondata);
JSONObject accessPolicyAsJSON = new JSONObject();
try{
accessPolicyAsJSON = new JSONObject(datasetInformationAsString);
JSONObject policy=accessPolicyAsJSON.getJSONObject("Policy");
accessPolicyAsString=policy.toString();
}
catch (Exception e) {
}
} catch (Exception e) {
......@@ -109,24 +127,115 @@ public class PolicyService {
public Access checkAtributesInPolicy(String datasetID,String organizationAttributesAsString, String accessPolicyasString){
Access access=new Access();
Iterator<String> orgKeys = null;
Iterator<String> policyKeys = null;
JSONObject organizationAttributesJson = null;
JSONObject accessPolicyAsJSON = null;
//Example
//organizationAttributesAsString:{"name":"Test Organization","sector":"Test Sector","continent":"EU","country":"GRE","type":"Private Entity","size":"medium","reputation":"7"}
//accessPolicyasString:{"Continent":["testContinent","test2Continent"],"Type":["testType","test2Type"],"Sector":["testSector","test2Sector"],"Size":["testSize","test2Size"],"Country":["testCountry","test2Country"],"Reputation":11}
try{
JSONObject organizationAttributesJson = new JSONObject();
// boolean result= this.checkParameter("continent", "Continent", organizationAttributesAsString, accessPolicyasString);
try{
organizationAttributesJson = new JSONObject(organizationAttributesAsString);
orgKeys = organizationAttributesJson.keys();
/* String sContinent=organizationAttributesJson.getString("continent");
String sCountry=organizationAttributesJson.getString("country");
String sSector=organizationAttributesJson.getString("sector");
String sType=organizationAttributesJson.getString("type");
String sSize=organizationAttributesJson.getString("size");
String sReputation=organizationAttributesJson.getString("reputation");*/
System.out.println(organizationAttributesJson.toString());
//sector,continent,country,type,size,reputation
}
catch(Exception e){
System.out.println("Error");
}
JSONObject accessPolicyAsJSON = new JSONObject();
try{
accessPolicyAsJSON = new JSONObject(accessPolicyasString);
accessPolicyAsJSON = new JSONObject(accessPolicyasString);
//policyKeys=accessPolicyAsJSON.keys();
/* JSONArray jArrayContinent=accessPolicyAsJSON.getJSONArray("Continent");
JSONArray jArrayCountry=accessPolicyAsJSON.getJSONArray("Country
JSONArray jArraySector=accessPolicyAsJSON.getJSONArray("Sector");
JSONArray jArrayType=accessPolicyAsJSON.getJSONArray("Type");
JSONArray jArraySize=accessPolicyAsJSON.getJSONArray("Size");
JSONArray jArrayReputation=accessPolicyAsJSON.getJSONArray("Reputation");*/
System.out.println(accessPolicyAsJSON.toString());
}
catch(Exception e){
System.out.println("Error");
}
boolean result=false;
String orgKey="";
String policyKey="";
String sNonConformities="";
while (orgKeys.hasNext()) {
orgKey = orgKeys.next();
System.out.println("Key organization:"+orgKey);
String lowerOrgKey=orgKey.toLowerCase();
policyKeys=accessPolicyAsJSON.keys();
while (policyKeys.hasNext()) {
policyKey = policyKeys.next();
System.out.println("Key policy:"+policyKey);
String lowerPolicyKey=policyKey.toLowerCase();
if (lowerOrgKey.equals(lowerPolicyKey)){
result= this.checkParameter(orgKey, policyKey, organizationAttributesJson, accessPolicyAsJSON);
System.out.println(result);
break;
}
}
// policyKeys.next();
if (!result){
sNonConformities+="Organization key: "+ orgKey + " value not the same as policy key: "+ policyKey + " value.\n";
}
}
access.setDatasetID(datasetID);
if (sNonConformities.equals("")){
access.setGranted(true);
}
else access.setGranted(false);
access.setNonConformities(sNonConformities);
// boolean result= this.checkParameter("continent", "Continent", organizationAttributesJson, accessPolicyAsJSON);
//check if any value in policy is same of the organization, if everything is on policy return true in granted value and conformities string is empty
//if one of the value is not the same return false and construct non conformities string
......@@ -135,22 +244,46 @@ public class PolicyService {
access.setDatasetID(datasetID);
access.setGranted(Boolean.TRUE);
access.setNonConformities("non conf");
return access;
}
public boolean checkParameter (String parameterOrg, String parameterPolicy, JSONObject organizationAttributesJson, JSONObject accessPolicyAsJSON){
boolean result=false;
try{
String sOrgValue=organizationAttributesJson.getString(parameterOrg);
System.out.println("Value for organization:"+sOrgValue);
// sOrgValue="testContinent";
JSONArray jArray=accessPolicyAsJSON.getJSONArray(parameterPolicy);
if (jArray.toString().contains(sOrgValue))
result=true;
System.out.println("Is the same value?:"+result);
}
catch(Exception e){
}
return access;
return result;
}
......
......@@ -136,17 +136,13 @@ public class HttpUtils {
* @throws RuntimeException if an error occurred when connecting or processing the HTTP
* request.
*/
public String sendHttpPostRequest(String address, Map<String,String>params, String jsonData) throws
public String sendHttpPostRequest(String address, String jsonData) throws
RuntimeException, URISyntaxException {
OkHttpClient client = new OkHttpClient();
try {
HttpUrl.Builder httpBuilder = HttpUrl.parse(address).newBuilder();
if (params != null) {
for(Map.Entry<String, String> param : params.entrySet()) {
httpBuilder.addQueryParameter(param.getKey(),param.getValue());
}
}
RequestBody body = RequestBody.create(jsonData, MediaType.parse("application/json"));
Request request = new Request.Builder()
.url(httpBuilder.build())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment