merged from trunk

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/information-system/ic-client/1.0@67999 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
fabio.simeoni 2013-01-17 18:03:05 +00:00
parent 7262578661
commit 8cc1491361
2 changed files with 51 additions and 21 deletions

View File

@ -3,7 +3,9 @@ package org.gcube.resources.discovery.icclient;
import static org.gcube.common.scope.impl.ScopeBean.Type.*; import static org.gcube.common.scope.impl.ScopeBean.Type.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -17,6 +19,8 @@ public class Helper {
private static final Logger log = LoggerFactory.getLogger(Helper.class); private static final Logger log = LoggerFactory.getLogger(Helper.class);
public static String queryAddAuthenticationControl(String expression) throws MalformedQueryException public static String queryAddAuthenticationControl(String expression) throws MalformedQueryException
{ {
@ -25,6 +29,7 @@ public class Helper {
if (scope==null || !new ScopeBean(scope).is(VRE)) if (scope==null || !new ScopeBean(scope).is(VRE))
return expression; return expression;
int wherePathIndex=0; int wherePathIndex=0;
int returnPathIndex=0; int returnPathIndex=0;
int collIndexEnd=0; int collIndexEnd=0;
@ -34,13 +39,15 @@ public class Helper {
String whereInsertFinal=""; String whereInsertFinal="";
String temp=""; String temp="";
HashMap<String, String> varReplacementMap = new HashMap<String, String>();
String forPropertiesString="\n *VAR* in *COLLECTION*/Data/child::*[local-name()='Scopes'] \n"; String forPropertiesString="\n *VAR* in *COLLECTION*/Data/child::*[local-name()='Scopes'] \n";
String forString=" *VAR* in *COLLECTION*/Scopes \n"; String forString=" *VAR* in *COLLECTION*/Scopes \n";
String authString=" (contains(*VAR*//Scope,'"+scope+"') or contains(*VAR*//Scope,'"+scope.substring(0,scope.lastIndexOf("/"))+"')) "; String authString=" (contains(*VAR*//Scope,'"+scope+"') or contains(*VAR*//Scope,'"+scope.substring(0,scope.lastIndexOf("/"))+"')) ";
String authStringNormal=" contains(*VAR*//Scope,'"+scope+"') "; String authStringNormal=" contains(*VAR*//Scope,'"+scope+"') ";
String queryFiltered; String queryFiltered;
List<Pair> collInsert= new ArrayList<Pair>(); List<Boolean> collInsert= new ArrayList<Boolean>();
Pattern wherePattern= Pattern.compile("where"); Pattern wherePattern= Pattern.compile("where");
//mathcing , $result in $outer/Data //mathcing , $result in $outer/Data
Pattern inSubResult= Pattern.compile("(,\\s*([^\\s]*)\\s*in\\s*([^\\s]*)\\s*)(where|order\\sby|return)"); Pattern inSubResult= Pattern.compile("(,\\s*([^\\s]*)\\s*in\\s*([^\\s]*)\\s*)(where|order\\sby|return)");
@ -79,19 +86,26 @@ public class Helper {
String tempPath=""; String tempPath="";
try{ try{
tempPath= temp.substring(resourceMat.end()); tempPath= temp.substring(resourceMat.end());
if (propBool) if (propBool)
{ {
String resourceMatString= resourceMat.group(); String resourceMatString= resourceMat.group();
forStringTemp=forPropertiesString.replace("*VAR*","$entry"+collInsert.size()+"ValueAuth" ).replace("*COLLECTION*", resourceMatString ); forStringTemp=forPropertiesString.replace("*VAR*","$entry"+collInsert.size()+"ValueAuth" ).replace("*COLLECTION*", resourceMatString );
Pair c= new Pair("let "+varMat.group()+" := "+" $entry"+collInsert.size()+"ValueAuth/.."+tempPath, true); String oldVar = varMat.group();
collInsert.add(c);
String newVar = "$entry"+collInsert.size()+"ValueAuth";
collInsert.add(true);
varReplacementMap.put(oldVar, newVar+"/.."+tempPath);
} }
else else
{ {
String resourceMatString= resourceMat.group(); String resourceMatString= resourceMat.group();
forStringTemp=forString.replace("*VAR*","$entry"+collInsert.size()+"ValueAuth" ).replace("*COLLECTION*",resourceMatString ); String oldVar = varMat.group();
Pair c= new Pair("let "+varMat.group()+" := "+" $entry"+collInsert.size()+"ValueAuth/.."+tempPath, (resourceMatString.contains("/Profiles/RunningInstance") || resourceMatString.contains("/Profiles/GHN") || resourceMatString.contains("/Profiles/Service"))); String newVar = "$entry"+collInsert.size()+"ValueAuth";
collInsert.add(c); forStringTemp=forString.replace("*VAR*",newVar ).replace("*COLLECTION*",resourceMatString );
collInsert.add(resourceMatString.contains("/Profiles/RunningInstance") || resourceMatString.contains("/Profiles/GHN") || resourceMatString.contains("/Profiles/Service"));
varReplacementMap.put(oldVar, newVar+"/.."+tempPath);
} }
}catch(IllegalStateException e){ log.debug("error parsing statement");} }catch(IllegalStateException e){ log.debug("error parsing statement");}
expression=expression.substring(0, collIndexStart)+ forStringTemp +expression.substring(collIndexEnd); expression=expression.substring(0, collIndexStart)+ forStringTemp +expression.substring(collIndexEnd);
@ -105,18 +119,19 @@ public class Helper {
for (int i=0; i<collInsert.size(); i++) for (int i=0; i<collInsert.size(); i++)
{
if (i==collInsert.size()-1){ if (i==collInsert.size()-1){
if(collInsert.get(i).isStrangeBehaviour()) if(collInsert.get(i))
whereInsertFinal+=authString.replace("*VAR*", "$entry"+i+"ValueAuth"); whereInsertFinal+=authString.replace("*VAR*", "$entry"+i+"ValueAuth");
else whereInsertFinal+=authStringNormal.replace("*VAR*", "$entry"+i+"ValueAuth"); else whereInsertFinal+=authStringNormal.replace("*VAR*", "$entry"+i+"ValueAuth");
}else{ }else{
if(collInsert.get(i).isStrangeBehaviour()) if(collInsert.get(i))
whereInsertFinal+=authString.replace("*VAR*", "$entry"+i+"ValueAuth")+" and " ; whereInsertFinal+=authString.replace("*VAR*", "$entry"+i+"ValueAuth")+" and " ;
else whereInsertFinal+=authStringNormal.replace("*VAR*", "$entry"+i+"ValueAuth")+" and " ; else whereInsertFinal+=authStringNormal.replace("*VAR*", "$entry"+i+"ValueAuth")+" and " ;
} }
forInsertFinal+="\n"+collInsert.get(i).getToInsert()+" ";
}
Matcher whereMat=wherePattern.matcher(expression); Matcher whereMat=wherePattern.matcher(expression);
Matcher inSubResultMat= inSubResult.matcher(expression); Matcher inSubResultMat= inSubResult.matcher(expression);
@ -125,10 +140,9 @@ public class Helper {
returnMat.reset(); returnMat.reset();
whereFinded=whereMat.find(); whereFinded=whereMat.find();
returnMat.find(); returnMat.find();
String inSubResultString="";
try{ try{
inSubResultMat.find(); inSubResultMat.find();
inSubResultString= inSubResultMat.group(1);
}catch(Exception e){} }catch(Exception e){}
try{ try{
@ -137,12 +151,14 @@ public class Helper {
try{ try{
returnPathIndex=returnMat.start(); returnPathIndex=returnMat.start();
}catch(IllegalStateException e){ log.error("error parsing return statement"); throw new MalformedQueryException("error parsing return statement");} }catch(IllegalStateException e){ log.error("error parsing return statement"); throw new MalformedQueryException("error parsing return statement");}
if (whereFinded) if (whereFinded)
queryFiltered=expression.substring(0,wherePathIndex)+"\nwhere "+whereInsertFinal queryFiltered=expression.substring(0,wherePathIndex)+"\nwhere "+whereInsertFinal
+" and ("+expression.substring(wherePathIndex+5, returnPathIndex)+" ) \n"+expression.substring(returnPathIndex); +" and ("+expression.substring(wherePathIndex+5, returnPathIndex)+" ) \n"+expression.substring(returnPathIndex);
else else
queryFiltered=expression.substring(0,returnPathIndex)+ "\nwhere "+whereInsertFinal+" \n "+expression.substring(returnPathIndex); queryFiltered=expression.substring(0,returnPathIndex)+ "\nwhere "+whereInsertFinal+" \n "+expression.substring(returnPathIndex);
//logger.trace("queryFiltered to match: " + queryFiltered); //logger.trace("queryFiltered to match: " + queryFiltered);
Pattern letPattern=Pattern.compile("let.*:=", Pattern.DOTALL); Pattern letPattern=Pattern.compile("let.*:=", Pattern.DOTALL);
Matcher letMat=letPattern.matcher(queryFiltered); Matcher letMat=letPattern.matcher(queryFiltered);
@ -165,22 +181,36 @@ public class Helper {
else { else {
queryFiltered=queryFiltered.substring(0,letPathIndex)+forInsertFinal+queryFiltered.substring(letPathIndex); queryFiltered=queryFiltered.substring(0,letPathIndex)+forInsertFinal+queryFiltered.substring(letPathIndex);
} }
queryFiltered=queryFiltered.replace(inSubResultString, "");
for (Entry<String, String> entry: varReplacementMap.entrySet())
queryFiltered = queryFiltered.replace(entry.getKey(), entry.getValue());
/*
if (inSubResultString.compareTo("")!=0){ if (inSubResultString.compareTo("")!=0){
String newInSubResultString = inSubResultString;
for (Entry<String, String> entry: varReplacementMap.entrySet())
newInSubResultString = newInSubResultString.replace(entry.getKey(), entry.getValue());
queryFiltered =queryFiltered.replace(inSubResultString, newInSubResultString);
/*
String firstVar=inSubResultMat.group(2); String firstVar=inSubResultMat.group(2);
String replaceVar=inSubResultMat.group(3); String replaceVar=inSubResultMat.group(3);
queryFiltered= queryFiltered.replace(firstVar, replaceVar); queryFiltered= queryFiltered.replace(firstVar, replaceVar);
//System.out.println(firstVar); System.out.println(firstVar);
//System.out.println(replaceVar); System.out.println(replaceVar);
} System.out.println("query filtered final: "+queryFiltered);
} else
queryFiltered=queryFiltered.replace(inSubResultString, "");
*/
log.trace("submitting filtered query: {}",queryFiltered); log.trace("submitting filtered query: {}",queryFiltered);
return queryFiltered; return queryFiltered;
} }
/*
private static class Pair { private static class Pair {
private String toInsert; private String toInsert;
//is strange Behaviour is true if we are searching for RI , GHN or Service //is strange Behaviour is true if we are searching for RI , GHN or Service
@ -199,7 +229,7 @@ public class Helper {
return this.isStrangeBehaviour; return this.isStrangeBehaviour;
} }
} }
*/
} }

View File

@ -26,7 +26,7 @@ public class APIClient {
@BeforeClass @BeforeClass
public static void setup() { public static void setup() {
ScopeProvider.instance.set("/gcube/devsec"); ScopeProvider.instance.set("/gcube/devsec/devVre");
} }