Search in sources :

Example 6 with UnmodifiableOntologyCollectorException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException in project stanbol by apache.

the class ScopeResource method manageOntology.

/**
     * Tells the scope that it should manage the ontology obtained by parsing the supplied content.<br>
     * <br>
     * Note that the PUT method cannot be used, as it is not possible to predict what ID the ontology will
     * have until it is parsed.
     * 
     * @param content
     *            the ontology content
     * @return {@link Status#OK} if the addition was successful, {@link Status#NOT_FOUND} if there is no such
     *         session at all, {@link Status#FORBIDDEN} if the session is locked or cannot modified for some
     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some other error occurs.
     */
@POST
@Consumes(value = { RDF_XML, OWL_XML, N_TRIPLE, N3, TURTLE, X_TURTLE, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON })
public Response manageOntology(InputStream content, @PathParam("scopeid") String scopeid, @Context HttpHeaders headers) {
    long before = System.currentTimeMillis();
    ResponseBuilder rb;
    scope = onm.getScope(scopeid);
    if (// Always check session first
    scope == null)
        // Always check session first
        rb = Response.status(NOT_FOUND);
    else
        try {
            MediaType mt = headers.getMediaType();
            log.debug("POST content claimed to be of type {}.", mt);
            OWLOntologyID key = scope.getCustomSpace().addOntology(/*
             * For the time being, REST services operate in-memory (i.e. no TcProvider is supplied to the
             * input source). This means that only the final processed graph is stored.
             * 
             * TODO : we might find a reason to change that in the future.
             */
            new GraphContentInputSource(content, mt.toString(), ontologyProvider.getStore()));
            if (key == null || key.isAnonymous()) {
                log.error("FAILED parse with media type {}.", mt);
                throw new WebApplicationException(INTERNAL_SERVER_ERROR);
            }
            // FIXME ugly but will have to do for the time being
            log.debug("SUCCESS parse with media type {}.", mt);
            // key.split("::")[1];
            String uri = OntologyUtils.encode(key);
            // uri = uri.substring((ontologyProvider.getGraphPrefix() + "::").length());
            URI created = null;
            if (uri != null && !uri.isEmpty()) {
                created = getCreatedResource(uri);
                rb = Response.created(created);
            } else
                rb = Response.ok();
            log.info("POST request for ontology addition completed in {} ms.", (System.currentTimeMillis() - before));
            log.info("New resource URL is {}", created);
        } catch (UnmodifiableOntologyCollectorException e) {
            throw new WebApplicationException(e, FORBIDDEN);
        }
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) GraphContentInputSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) MediaType(javax.ws.rs.core.MediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 7 with UnmodifiableOntologyCollectorException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException in project stanbol by apache.

the class SessionResource method manageOntology.

/**
     * Tells the session that it should manage the ontology obtained by parsing the supplied content.<br>
     * <br>
     * Note that the PUT method cannot be used, as it is not possible to predict what ID the ontology will
     * have until it is parsed.
     * 
     * @param content
     *            the ontology content
     * @return {@link Status#OK} if the addition was successful, {@link Status#NOT_FOUND} if there is no such
     *         session at all, {@link Status#FORBIDDEN} if the session is locked or cannot modified for some
     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some other error occurs.
     */
@POST
@Consumes(value = { RDF_XML, OWL_XML, N_TRIPLE, N3, TURTLE, X_TURTLE, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON })
public Response manageOntology(InputStream content, @PathParam("id") String sessionId, @Context HttpHeaders headers) {
    long before = System.currentTimeMillis();
    ResponseBuilder rb;
    session = sesMgr.getSession(sessionId);
    String mt = headers.getMediaType().toString();
    if (// Always check session first
    session == null)
        // Always check session first
        rb = Response.status(NOT_FOUND);
    else
        try {
            log.debug("POST content claimed to be of type {}.", mt);
            OntologyInputSource<?> src;
            if (OWL_XML.equals(mt) || FUNCTIONAL_OWL.equals(mt) || MANCHESTER_OWL.equals(mt))
                src = new OntologyContentInputSource(content);
            else
                // content = new BufferedInputStream(content);
                src = new GraphContentInputSource(content, mt, ontologyProvider.getStore());
            log.debug("SUCCESS parse with media type {}.", mt);
            OWLOntologyID key = session.addOntology(src);
            if (key == null || key.isAnonymous()) {
                log.error("FAILED parse with media type {}.", mt);
                throw new WebApplicationException(INTERNAL_SERVER_ERROR);
            }
            // FIXME ugly but will have to do for the time being
            log.debug("SUCCESS add ontology to session {}.", session.getID());
            log.debug("Storage key : {}", key);
            // key.split("::")[1];
            String uri = OntologyUtils.encode(key);
            // uri = uri.substring((ontologyProvider.getGraphPrefix() + "::").length());
            URI created = null;
            if (uri != null && !uri.isEmpty()) {
                created = getCreatedResource(uri);
                rb = Response.created(created);
            } else
                rb = Response.ok();
            log.info("POST request for ontology addition completed in {} ms.", (System.currentTimeMillis() - before));
            log.info("New resource URL is {}", created);
        } catch (UnmodifiableOntologyCollectorException e) {
            throw new WebApplicationException(e, FORBIDDEN);
        } catch (OWLOntologyCreationException e) {
            log.error("FAILED parse with media type {}.", mt);
            throw new WebApplicationException(e, BAD_REQUEST);
        }
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OntologyContentInputSource(org.apache.stanbol.ontologymanager.sources.owlapi.OntologyContentInputSource) GraphContentInputSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologyInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 8 with UnmodifiableOntologyCollectorException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException in project stanbol by apache.

the class SessionResource method manageOntology.

/**
     * Tells the session that it should manage the ontology obtained by dereferencing the supplied IRI.<br>
     * <br>
     * Note that the PUT method cannot be used, as it is not possible to predict what ID the ontology will
     * have until it is parsed.
     * 
     * @param content
     *            the ontology physical IRI
     * @return {@link Status#OK} if the addition was successful, {@link Status#NOT_FOUND} if there is no such
     *         session at all, {@link Status#FORBIDDEN} if the session is locked or cannot modified for some
     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some other error occurs.
     */
@POST
@Consumes(value = MediaType.TEXT_PLAIN)
public Response manageOntology(String iri, @PathParam("id") String sessionId, @Context HttpHeaders headers) {
    session = sesMgr.getSession(sessionId);
    if (session == null)
        return Response.status(NOT_FOUND).build();
    try {
        session.addOntology(new RootOntologySource(IRI.create(iri)));
    } catch (UnmodifiableOntologyCollectorException e) {
        throw new WebApplicationException(e, FORBIDDEN);
    } catch (OWLOntologyCreationException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    ResponseBuilder rb = Response.ok();
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 9 with UnmodifiableOntologyCollectorException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException in project stanbol by apache.

the class ScopeManagerImpl method bootstrapOntologyNetwork.

private void bootstrapOntologyNetwork(OWLOntology configOntology) {
    if (configOntology == null) {
        log.info("Ontology Network Manager starting with empty scope set.");
        return;
    }
    try {
        /**
             * We create and register the scopes before activating
             */
        for (String scopeId : OntologyNetworkConfigurationUtils.getScopes(configOntology)) {
            String[] cores = OntologyNetworkConfigurationUtils.getCoreOntologies(configOntology, scopeId);
            String[] customs = OntologyNetworkConfigurationUtils.getCustomOntologies(configOntology, scopeId);
            // "Be a man. Use printf"
            log.debug("Detected scope \"{}\"", scopeId);
            for (String s : cores) log.debug("\tDetected core ontology {}", s);
            for (String s : customs) log.debug("\tDetected custom ontology {}", s);
            // Create the scope
            log.debug("Rebuilding scope \"{}\"", scopeId);
            Scope sc = null;
            sc = /* factory. */
            createOntologyScope(scopeId, new BlankOntologySource());
            // Populate the core space
            if (cores.length > 0) {
                OntologySpace corespc = sc.getCoreSpace();
                corespc.tearDown();
                for (int i = 0; i < cores.length; i++) try {
                    corespc.addOntology(new RootOntologySource(IRI.create(cores[i])));
                } catch (Exception ex) {
                    log.warn("Failed to import ontology " + cores[i], ex);
                    continue;
                }
            }
            sc.setUp();
            registerScope(sc);
            sc.getCustomSpace().tearDown();
            for (String locationIri : customs) {
                try {
                    OntologyInputSource<?> src = new RootOntologySource(IRI.create(locationIri));
                    sc.getCustomSpace().addOntology(src);
                    log.debug("Added ontology from location {}", locationIri);
                } catch (UnmodifiableOntologyCollectorException e) {
                    log.error("An error occurred while trying to add the ontology from location: " + locationIri, e);
                    continue;
                }
            }
            sc.getCustomSpace().setUp();
        }
        /**
             * Try to get activation policies
             */
        toActivate = OntologyNetworkConfigurationUtils.getScopesToActivate(configOntology);
        for (String scopeID : toActivate) {
            try {
                scopeID = scopeID.trim();
                setScopeActive(scopeID, true);
                log.info("Ontology scope " + scopeID + " activated.");
            } catch (NoSuchScopeException ex) {
                log.warn("Tried to activate unavailable scope " + scopeID + ".");
            } catch (Exception ex) {
                log.error("Exception caught while activating scope " + scopeID + " . Skipping.", ex);
                continue;
            }
        }
    } catch (Throwable e) {
        log.warn("Invalid ONM configuration file found. " + "Starting with blank scope set.", e);
    }
}
Also used : Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) BlankOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.BlankOntologySource) NoSuchScopeException(org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) NoSuchScopeException(org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException) MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) IOException(java.io.IOException)

Example 10 with UnmodifiableOntologyCollectorException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException in project stanbol by apache.

the class TestOntologySpaces method testCustomLock.

@Test
public void testCustomLock() throws Exception {
    OntologySpace space = factory.createCustomOntologySpace("testCustomLock", inMemorySrc);
    space.setUp();
    try {
        space.addOntology(minorSrc);
        fail("Modification was permitted on locked ontology space.");
    } catch (UnmodifiableOntologyCollectorException e) {
        assertSame(space, e.getOntologyCollector());
    }
}
Also used : UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) Test(org.junit.Test)

Aggregations

UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)16 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)8 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)7 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)7 WebApplicationException (javax.ws.rs.WebApplicationException)6 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)6 RootOntologySource (org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource)5 Consumes (javax.ws.rs.Consumes)4 POST (javax.ws.rs.POST)4 IOException (java.io.IOException)3 URI (java.net.URI)3 IRI (org.apache.clerezza.commons.rdf.IRI)3 DuplicateIDException (org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException)3 MissingOntologyException (org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException)3 Test (org.junit.Test)3 FileNotFoundException (java.io.FileNotFoundException)2 DELETE (javax.ws.rs.DELETE)2 Path (javax.ws.rs.Path)2 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)2 IrremovableOntologyException (org.apache.stanbol.ontologymanager.servicesapi.collector.IrremovableOntologyException)2