use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class UserResource method changeUser.
/**
* Modify user given a graph describing the change.
*
* @param inputGraph change graph
* @return HTTP response
*/
@POST
@Consumes(SupportedFormat.TURTLE)
@Path("change-user")
public Response changeUser(ImmutableGraph inputGraph) {
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
Iterator<Triple> changes = inputGraph.filter(null, null, Ontology.Change);
Triple oldTriple = null;
Triple newTriple = null;
if (changes.hasNext()) {
Triple changeTriple = changes.next();
BlankNodeOrIRI changeNode = changeTriple.getSubject();
Literal userName = (Literal) inputGraph.filter(changeNode, PLATFORM.userName, null).next().getObject();
Iterator<Triple> userTriples = systemGraph.filter(null, PLATFORM.userName, userName);
// if (userTriples.hasNext()) {
BlankNodeOrIRI userNode = userTriples.next().getSubject();
IRI predicateIRI = (IRI) inputGraph.filter(changeNode, Ontology.predicate, null).next().getObject();
// handle old value (if it exists)
Iterator<Triple> iterator = inputGraph.filter(changeNode, Ontology.oldValue, null);
RDFTerm oldValue = null;
if (iterator.hasNext()) {
oldValue = iterator.next().getObject();
// Triple oldTriple = systemGraph.filter(null, predicateIRI,
// oldValue).next();
Iterator<Triple> oldTriples = systemGraph.filter(userNode, predicateIRI, oldValue);
if (oldTriples.hasNext()) {
oldTriple = oldTriples.next();
}
}
RDFTerm newValue = inputGraph.filter(changeNode, Ontology.newValue, null).next().getObject();
newTriple = new TripleImpl(userNode, predicateIRI, newValue);
// }
}
readLock.unlock();
Lock writeLock = systemGraph.getLock().writeLock();
writeLock.lock();
if (oldTriple != null) {
systemGraph.remove(oldTriple);
}
systemGraph.add(newTriple);
writeLock.unlock();
// seems the most appropriate response
return Response.noContent().build();
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class UserResource method changeResource.
/**
* Replaces/inserts resource value for predicate assumes there is only one
* triple for the given predicate
*
* @param userNode node in systemGraph corresponding to the user to change
* @param predicate property of the triple to change
* @param newValue new value for given predicate
*/
private void changeResource(GraphNode userNode, IRI predicate, IRI newValue) {
Iterator<Triple> oldTriples = systemGraph.filter((BlankNodeOrIRI) userNode.getNode(), predicate, null);
ArrayList<Triple> oldBuffer = new ArrayList<Triple>();
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
try {
while (oldTriples.hasNext()) {
Triple triple = oldTriples.next();
RDFTerm oldValue = triple.getObject();
if (newValue.equals(oldValue)) {
return;
}
oldBuffer.add(triple);
}
} finally {
readLock.unlock();
}
// filter appears to see plain literals and xsd:strings as differerent
// so not
// userNode.addPropertyValue(predicate, newValue);
userNode.addProperty(predicate, newValue);
systemGraph.removeAll(oldBuffer);
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class PermissionDefinitions method retrievePermissions.
/**
* Returns the permissions of a specified location.
* I.e. the permissions of all permission assignments matching <code>location</code>.
*
* @param location the location of a bundle
* @return an array with <code>PermissionInfo</code> elements
*/
PermissionInfo[] retrievePermissions(String location) {
List<PermissionInfo> permInfoList = new ArrayList<PermissionInfo>();
Iterator<Triple> ownerTriples = systemGraph.filter(new IRI(location), OSGI.owner, null);
if (ownerTriples.hasNext()) {
BlankNodeOrIRI user = (BlankNodeOrIRI) ownerTriples.next().getObject();
lookForPermissions(user, permInfoList);
}
if (permInfoList.isEmpty()) {
return null;
}
return permInfoList.toArray(new PermissionInfo[permInfoList.size()]);
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class PermissionDefinitions method lookForPermissions.
/**
* Look for all permissions of a role and add them to a list.
* And if the role has another role, then execute this function recursively,
* until all permissions are found.
*
* @param role a <code>BlankNodeOrIRI</code> which is either a user or a role
* @param permInfoList a list with all the added permissions of this bundle
*/
private void lookForPermissions(BlankNodeOrIRI role, List<PermissionInfo> permInfoList) {
Iterator<Triple> permissionTriples = systemGraph.filter(role, PERMISSION.hasPermission, null);
while (permissionTriples.hasNext()) {
BlankNodeOrIRI permission = (BlankNodeOrIRI) permissionTriples.next().getObject();
Iterator<Triple> javaPermissionTriples = systemGraph.filter(permission, PERMISSION.javaPermissionEntry, null);
while (javaPermissionTriples.hasNext()) {
Triple t = javaPermissionTriples.next();
Literal permEntry = (Literal) t.getObject();
permInfoList.add(new PermissionInfo(permEntry.getLexicalForm()));
}
}
Iterator<Triple> roleTriples = systemGraph.filter(role, SIOC.has_function, null);
while (roleTriples.hasNext()) {
BlankNodeOrIRI anotherRole = (BlankNodeOrIRI) roleTriples.next().getObject();
this.lookForPermissions(anotherRole, permInfoList);
}
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class UserAwarePolicy method getPermissionEntriesOfARole.
//note that users are roles too
private List<String> getPermissionEntriesOfARole(BlankNodeOrIRI role, String userName, BlankNodeOrIRI user) {
List<String> result = new ArrayList<String>();
Iterator<Triple> permsForRole = systemGraph.filter(role, PERMISSION.hasPermission, null);
while (permsForRole.hasNext()) {
Iterator<Triple> javaPermForRole = systemGraph.filter((BlankNode) permsForRole.next().getObject(), PERMISSION.javaPermissionEntry, null);
if (javaPermForRole.hasNext()) {
Literal permissionEntry = (Literal) javaPermForRole.next().getObject();
String permission = permissionEntry.getLexicalForm();
if (permission.contains("{username}")) {
permission = permission.replace("{username}", userName);
}
result.add(permission);
}
}
return result;
}
Aggregations