Search in sources :

Example 26 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI 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 27 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI 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 28 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI 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 29 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class UserResource method store.

// **********************************
// ****** ADD PERMISSION TO USER **** 
// **********************************
// **************************************
// ****** REMOVE PERMISSION FROM USER *** 
// **************************************
// ************************************
// ****** ADD PERMISSION TO ROLE ****** 
// ************************************
// **************************************
// ****** REMOVE PERMISSION FROM ROLE *** 
// **************************************
////////////////////////////////////////////////////////////////
/**
     * Pushes user data into system graph
     *
     * @param userNode
     * @param uriInfo
     * @param currentUserName
     * @param newUserName
     * @param fullName
     * @param email
     * @param password
     * @param roles
     * @param permissions
     * @return
     */
private Response store(GraphNode userNode, UriInfo uriInfo, String currentUserName, String newUserName, String fullName, String email, String password, List<String> roles, List<String> permissions) {
    if (newUserName != null && !newUserName.equals("")) {
        changeLiteral(userNode, PLATFORM.userName, newUserName);
    }
    if (fullName != null && !fullName.equals("")) {
        changeLiteral(userNode, FOAF.name, fullName);
    }
    if (password != null && !password.equals("")) {
        String passwordSha1 = PasswordUtil.convertPassword(password);
        changeLiteral(userNode, PERMISSION.passwordSha1, passwordSha1);
    }
    if (email != null && !email.equals("")) {
        changeResource(userNode, FOAF.mbox, new IRI("mailto:" + email));
    }
    BlankNodeOrIRI userResource = (BlankNodeOrIRI) userNode.getNode();
    if (roles != null) {
        clearRoles(userResource);
        Lock writeLock = systemGraph.getLock().writeLock();
        writeLock.lock();
        try {
            for (int i = 0; i < roles.size(); i++) {
                roles.set(i, roles.get(i).trim());
                if (!roles.get(i).equals("")) {
                    addRole(userNode, roles.get(i));
                }
            }
        } finally {
            writeLock.unlock();
        }
    }
    if (permissions != null) {
        clearPermissions(userResource);
        Lock writeLock = systemGraph.getLock().writeLock();
        writeLock.lock();
        try {
            for (int i = 0; i < permissions.size(); i++) {
                permissions.set(i, permissions.get(i).trim());
                if (!permissions.get(i).equals("")) {
                    addPermission(userNode, permissions.get(i));
                }
            }
        } finally {
            writeLock.unlock();
        }
    }
    URI pageUri = uriInfo.getBaseUriBuilder().path("system/console/usermanagement").build();
    // header Cache-control: no-cache, just in case intermediaries are
    // holding onto old stuff
    CacheControl cc = new CacheControl();
    cc.setNoCache(true);
    // the jax-rs things available
    return Response.seeOther(pageUri).cacheControl(cc).build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) CacheControl(javax.ws.rs.core.CacheControl) URI(java.net.URI) Lock(java.util.concurrent.locks.Lock)

Example 30 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class ExecutionMetadataHelper method initExecutionMetadata.

/**
     * Initialises execution metadata based on the parsed parameter. If the parsed
     * graph with the execution metadata is empty it will initialise the metadata
     * based on the execution plan. If there are already metadata in the graph
     * it will initialise the returned map based on the existing data.<p>
     * This method can be therefore used to both:<ul>
     * <li> create a new set of execution metadata: as needed before a
     * {@link EnhancementJobManager} implementation can start processing a 
     * {@link ContentItem} by using an {@link Chain} 
     * <li> read existing executionMetadata allowing to let an 
     * {@link EnhancementJobManager} to continue from an uncompleted enhancement.
     * </ul><p>
     * If both the execution metadata and the execution plan are stored within the
     * same graph users need to base this graph as both the first and second
     * parameter
     * @param em The graph containing the execution metadata. MUST NOT be NULL
     * @param ep The graph containing the execution plan. MUST NOT be NULL
     * @param ciUri the URI of the content item. MUST NOT be NULL
     * @param chainName the name of the chain to execute. May be NULL if
     * initialising from existing metadata. MUST NOT be NULL if initialising from
     * empty execution metadata
     * @param isDefaultChain if the chain to execute is the default chain. Will be
     * ignored if initialising from existing execution metadata. MUST NOT be NULL
     * if initialising from empty execution metadata
     * @return A map containing all em:Execution nodes as key and the according
     * ep:ExecutionNode of the execution plan as values.
     * @throws IllegalArgumentException if any of the requirements stated in the
     * documentation for the parameters is not fulfilled.
     */
public static final Map<BlankNodeOrIRI, BlankNodeOrIRI> initExecutionMetadata(Graph em, Graph ep, IRI ciUri, String chainName, Boolean isDefaultChain) {
    if (em == null) {
        throw new IllegalArgumentException("The parsed ExecutionMetadata graph MUST NOT be NULL!");
    }
    if (ciUri == null) {
        throw new IllegalArgumentException("The parsed URI of the contentItem MUST NOT be NULL!");
    }
    //1. check for the ChainExecution node for the parsed content item
    final BlankNodeOrIRI executionPlanNode;
    BlankNodeOrIRI chainExecutionNode = getChainExecutionForExecutionPlan(em, ciUri);
    if (chainExecutionNode != null) {
        //init from existing executin metadata
        // -> chainName and isDefaultChain may be null
        //init from existing
        executionPlanNode = getExecutionPlanNode(em, chainExecutionNode);
        if (executionPlanNode == null) {
            throw new IllegalArgumentException("The em:ChainExecution '" + chainExecutionNode + "'that enhances ContentItem '" + ciUri + "' does not define a link to an valid ExecutionPlan");
        }
        isDefaultChain = get(em, chainExecutionNode, IS_DEFAULT_CHAIN, Boolean.class, lf);
        String extractedChainName = EnhancementEngineHelper.getString(ep, executionPlanNode, CHAIN);
        if (extractedChainName == null) {
            throw new IllegalArgumentException("The em:ChainExecution '" + chainExecutionNode + "'that enhances ContentItem '" + ciUri + "' links to the ep:ExecutionPlan '" + executionPlanNode + "' that does not define a ChainName (property: " + CHAIN + ")!");
        }
        if (chainName == null) {
            chainName = extractedChainName;
        } else if (!chainName.equals(extractedChainName)) {
            throw new IllegalArgumentException("The em:ChainExecution '" + chainExecutionNode + "'that enhances ContentItem '" + ciUri + "' links to the ep:ExecutionPlan '" + executionPlanNode + "' with the chain name '" + extractedChainName + "' but '" + chainName + "' was parsed " + "as expected chain name!");
        }
    } else {
        //create a new one 
        // -> in that case chainName and isDefaultChain are required
        executionPlanNode = ExecutionPlanHelper.getExecutionPlan(ep, chainName);
        if (executionPlanNode == null) {
            throw new IllegalArgumentException("The parsed ExectuonPlan graph does not contain an" + "ExecutionPlan for a Chain with the name '" + chainName + "'!");
        }
        if (isDefaultChain == null) {
            throw new IllegalArgumentException("The isDefaultChain parameter MUST NOT" + "be NULL if initialising from empty ExecutionMetadata!");
        }
        chainExecutionNode = createChainExecutionNode(em, executionPlanNode, ciUri, isDefaultChain);
    }
    //2. check/init the EngineExecution nodes for for the ExecutionNodes of the ExecutionPlan
    Map<BlankNodeOrIRI, BlankNodeOrIRI> executionsMap = new HashMap<BlankNodeOrIRI, BlankNodeOrIRI>();
    Set<BlankNodeOrIRI> executionNodes = getExecutionNodes(ep, executionPlanNode);
    Set<BlankNodeOrIRI> executions = getExecutions(em, chainExecutionNode);
    for (BlankNodeOrIRI en : executionNodes) {
        Iterator<Triple> it = em.filter(null, EXECUTION_NODE, en);
        BlankNodeOrIRI execution;
        if (it.hasNext()) {
            execution = it.next().getSubject();
            if (!executions.contains(execution)) {
                throw new IllegalStateException("Execution '" + execution + "' for ExecutionNode '" + en + "' (engine: '" + getEngine(ep, en) + "') is not part of ChainExecution '" + chainExecutionNode + "' (chain: '" + chainName + ")!");
            }
        } else {
            execution = createEngineExecution(em, chainExecutionNode, en);
            executions.add(execution);
        }
        executionsMap.put(execution, en);
    }
    //   parsed ExecutionPlan
    for (BlankNodeOrIRI e : executions) {
        if (!executionsMap.containsKey(e)) {
            BlankNodeOrIRI en = getExecutionNode(em, e);
            throw new IllegalStateException("ChainExecution '" + chainExecutionNode + "' (chain: '" + chainName + ") contains" + "Execution '" + e + "' for ExecutionNode '" + en + "' (engine: '" + ExecutionPlanHelper.getEngine(ep, en) + "') that is not part of the pased ExecutionPlan '" + executionPlanNode + "'(chain; '" + chainName + "')!");
        }
    }
    return executionsMap;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) HashMap(java.util.HashMap) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI)

Aggregations

BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)89 Triple (org.apache.clerezza.commons.rdf.Triple)52 IRI (org.apache.clerezza.commons.rdf.IRI)41 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)30 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)27 HashSet (java.util.HashSet)24 Graph (org.apache.clerezza.commons.rdf.Graph)22 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)14 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)14 Literal (org.apache.clerezza.commons.rdf.Literal)13 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)12 Lock (java.util.concurrent.locks.Lock)10 BlankNode (org.apache.clerezza.commons.rdf.BlankNode)10 EnhancementEngineHelper.getString (org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString)8 Test (org.junit.Test)8 Collection (java.util.Collection)7 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)7 Language (org.apache.clerezza.commons.rdf.Language)6 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)6