Search in sources :

Example 31 with Triple

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();
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Literal(org.apache.clerezza.commons.rdf.Literal) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Lock(java.util.concurrent.locks.Lock) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 32 with Triple

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);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) ArrayList(java.util.ArrayList) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) Lock(java.util.concurrent.locks.Lock)

Example 33 with Triple

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()]);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) IRI(org.apache.clerezza.commons.rdf.IRI) PermissionInfo(org.osgi.service.permissionadmin.PermissionInfo) ArrayList(java.util.ArrayList) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI)

Example 34 with Triple

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);
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) PermissionInfo(org.osgi.service.permissionadmin.PermissionInfo) Literal(org.apache.clerezza.commons.rdf.Literal) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI)

Example 35 with Triple

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;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) Literal(org.apache.clerezza.commons.rdf.Literal) ArrayList(java.util.ArrayList)

Aggregations

Triple (org.apache.clerezza.commons.rdf.Triple)151 IRI (org.apache.clerezza.commons.rdf.IRI)88 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)84 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)70 Graph (org.apache.clerezza.commons.rdf.Graph)45 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)41 HashSet (java.util.HashSet)34 Literal (org.apache.clerezza.commons.rdf.Literal)30 ArrayList (java.util.ArrayList)27 Lock (java.util.concurrent.locks.Lock)21 HashMap (java.util.HashMap)20 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)19 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)19 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)15 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)12 Test (org.junit.Test)12 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)10 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)9 GraphNode (org.apache.clerezza.rdf.utils.GraphNode)8 IOException (java.io.IOException)7