Search in sources :

Example 1 with LibrarySource

use of org.apache.stanbol.ontologymanager.registry.io.LibrarySource in project stanbol by apache.

the class SessionResource method postOntology.

@POST
@Consumes({ MULTIPART_FORM_DATA })
@Produces({ WILDCARD })
public Response postOntology(MultiPartBody data, @Context HttpHeaders headers) {
    log.debug(" post(FormDataMultiPart data)");
    long before = System.currentTimeMillis();
    ResponseBuilder rb;
    // TODO remove and make sure it is set across the method
    rb = Response.status(BAD_REQUEST);
    IRI location = null, library = null;
    // If found, it takes precedence over location.
    FormFile file = null;
    String format = null;
    Set<String> toAppend = null;
    Set<String> keys = new HashSet<String>();
    // }
    if (data.getFormFileParameterValues("file").length > 0) {
        file = data.getFormFileParameterValues("file")[0];
    }
    // else {
    if (data.getTextParameterValues("format").length > 0) {
        String value = data.getTextParameterValues("format")[0];
        if (!value.equals("auto")) {
            format = value;
        }
    }
    if (data.getTextParameterValues("url").length > 0) {
        String value = data.getTextParameterValues("url")[0];
        try {
            // To throw 400 if malformed.
            URI.create(value);
            location = IRI.create(value);
        } catch (Exception ex) {
            log.error("Malformed IRI for param url " + value, ex);
            throw new WebApplicationException(ex, BAD_REQUEST);
        }
    }
    if (data.getTextParameterValues("library").length > 0) {
        String value = data.getTextParameterValues("library")[0];
        try {
            // To throw 400 if malformed.
            URI.create(value);
            library = IRI.create(value);
        } catch (Exception ex) {
            log.error("Malformed IRI for param library " + value, ex);
            throw new WebApplicationException(ex, BAD_REQUEST);
        }
    }
    if (data.getTextParameterValues("stored").length > 0) {
        String value = data.getTextParameterValues("stored")[0];
        keys.add(value);
    }
    if (data.getTextParameterValues("scope").length > 0) {
        String value = data.getTextParameterValues("scope")[0];
        log.info("Request to append scope \"{}\".", value);
        if (toAppend == null) {
            toAppend = new HashSet<String>();
        }
        toAppend.add(value);
    }
    boolean fileOk = file != null;
    if (fileOk || location != null || library != null) {
        // File and location take precedence
        // Then add the file
        OntologyInputSource<?> src = null;
        if (fileOk) {
            // File first
            Collection<String> formats;
            if (format == null || "".equals(format.trim()))
                formats = OntologyUtils.getPreferredFormats();
            else
                formats = Collections.singleton(format);
            for (String f : formats) try {
                log.debug("Trying format {}.", f);
                long b4buf = System.currentTimeMillis();
                // Recreate the stream on each attempt
                InputStream content = new BufferedInputStream(new ByteArrayInputStream(file.getContent()));
                log.debug("Streams created in {} ms", System.currentTimeMillis() - b4buf);
                log.debug("Creating ontology input source...");
                b4buf = System.currentTimeMillis();
                OWLOntologyID guessed = OWLUtils.guessOntologyID(content, Parser.getInstance(), f);
                if (guessed != null && !guessed.isAnonymous() && ontologyProvider.hasOntology(guessed)) {
                    rb = Response.status(Status.CONFLICT);
                    this.submitted = guessed;
                    if (headers.getAcceptableMediaTypes().contains(MediaType.TEXT_HTML_TYPE)) {
                        rb.entity(new Viewable("/imports/409", this));
                        rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
                    }
                } else {
                    content = new BufferedInputStream(new ByteArrayInputStream(file.getContent()));
                    src = new GraphContentInputSource(content, f, ontologyProvider.getStore());
                }
                log.debug("Done in {} ms", System.currentTimeMillis() - b4buf);
                log.info("SUCCESS parse with format {}.", f);
                break;
            } catch (OntologyLoadingException e) {
                log.debug("FAILURE parse with format {}.", f);
                continue;
            } catch (IOException e) {
                log.debug("FAILURE parse with format {} (I/O error).", f);
                continue;
            }
            log.debug("No more formats to try.");
        } else if (location != null) {
            try {
                src = new RootOntologySource(location);
            } catch (Exception e) {
                log.error("Failed to load ontology from " + location, e);
                throw new WebApplicationException(e, BAD_REQUEST);
            }
        } else if (library != null) {
            // This comes last, since it will most likely have a value.
            try {
                long beforeLib = System.currentTimeMillis();
                log.debug("Creating library source for {}", library);
                src = new LibrarySource(library, regMgr);
                log.debug("Library source created in {} ms.", System.currentTimeMillis() - beforeLib);
            } catch (Exception e) {
                log.error("Failed to load ontology library " + library, e);
                throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
            }
        } else {
            log.error("Bad request");
            log.error(" file is: {}", file);
            throw new WebApplicationException(BAD_REQUEST);
        }
        if (src != null) {
            log.debug("Adding ontology from input source {}", src);
            long b4add = System.currentTimeMillis();
            OWLOntologyID key = session.addOntology(src);
            if (key == null || key.isAnonymous())
                throw new WebApplicationException(INTERNAL_SERVER_ERROR);
            // FIXME ugly but will have to do for the time being
            log.debug("Addition done in {} ms.", System.currentTimeMillis() - b4add);
            log.debug("Storage key : {}", key);
            // key.split("::")[1];
            String uri = OntologyUtils.encode(key);
            // uri = uri.substring((ontologyProvider.getGraphPrefix() + "::").length());
            if (uri != null && !uri.isEmpty())
                rb = Response.seeOther(URI.create("/ontonet/session/" + session.getID() + "/" + uri));
            else
                rb = Response.seeOther(URI.create("/ontonet/session/" + session.getID()));
        } else if (rb == null)
            rb = Response.status(INTERNAL_SERVER_ERROR);
    }
    if (!keys.isEmpty()) {
        for (String key : keys) session.addOntology(new StoredOntologySource(OntologyUtils.decode(key)));
        rb = Response.seeOther(URI.create("/ontonet/session/" + session.getID()));
    }
    // Now check scopes
    if (toAppend != null && (!toAppend.isEmpty() || (toAppend.isEmpty() && !getAppendedScopes().isEmpty()))) {
        for (Scope sc : getAllScopes()) {
            // First remove appended scopes not in the list
            String scid = sc.getID();
            if (!toAppend.contains(scid) && getAppendedScopes().contains(scid)) {
                session.detachScope(scid);
                log.info("Removed scope \"{}\".", scid);
            }
        }
        for (String scid : toAppend) {
            // Then add all the scopes in the list
            if (!getAppendedScopes().contains(scid)) {
                log.info("Appending scope \"{}\" to session \"{}\".", scid, session.getID());
                session.attachScope(scid);
                log.info("Appended scope \"{}\".", scid);
            }
        }
        rb = Response.seeOther(URI.create("/ontonet/session/" + session.getID()));
    }
    // else {
    // log.error("Nothing to do with session {}.", session.getID());
    // throw new WebApplicationException(BAD_REQUEST);
    // }
    // rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
    // addCORSOrigin(servletContext, rb, headers);
    log.info("POST ontology completed in {} ms.", System.currentTimeMillis() - before);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) WebApplicationException(javax.ws.rs.WebApplicationException) FormFile(org.apache.clerezza.jaxrs.utils.form.FormFile) BufferedInputStream(java.io.BufferedInputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) LibrarySource(org.apache.stanbol.ontologymanager.registry.io.LibrarySource) IOException(java.io.IOException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) IrremovableOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.IrremovableOntologyException) WebApplicationException(javax.ws.rs.WebApplicationException) SessionLimitException(org.apache.stanbol.ontologymanager.servicesapi.session.SessionLimitException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException) IOException(java.io.IOException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) OntologyCollectorModificationException(org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollectorModificationException) DuplicateSessionIDException(org.apache.stanbol.ontologymanager.servicesapi.session.DuplicateSessionIDException) StoredOntologySource(org.apache.stanbol.ontologymanager.servicesapi.io.StoredOntologySource) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) ByteArrayInputStream(java.io.ByteArrayInputStream) GraphContentInputSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 2 with LibrarySource

use of org.apache.stanbol.ontologymanager.registry.io.LibrarySource in project stanbol by apache.

the class ScopeResource method registerScope.

/**
 * At least one between corereg and coreont must be present. Registry iris supersede ontology iris.
 *
 * @param scopeid
 * @param coreRegistry
 *            a. If it is a well-formed IRI it supersedes <code>coreOntology</code>.
 * @param coreOntologies
 * @param customRegistry
 *            a. If it is a well-formed IRI it supersedes <code>customOntology</code>.
 * @param customOntologies
 * @param activate
 *            if true, the new scope will be activated upon creation.
 * @param uriInfo
 * @param headers
 * @return
 */
@PUT
@Consumes(MediaType.WILDCARD)
public Response registerScope(@PathParam("scopeid") String scopeid, @QueryParam("corereg") final List<String> coreRegistries, @QueryParam("coreont") final List<String> coreOntologies, @DefaultValue("false") @QueryParam("activate") boolean activate, @Context HttpHeaders headers) {
    log.debug("Request URI {}", uriInfo.getRequestUri());
    scope = onm.getScope(scopeid);
    List<OntologyInputSource<?>> srcs = new ArrayList<OntologyInputSource<?>>(coreOntologies.size() + coreRegistries.size());
    // First thing, check registry sources.
    if (coreRegistries != null)
        for (String reg : coreRegistries) if (reg != null && !reg.isEmpty())
            try {
                // Library IDs are sanitized differently
                srcs.add(new LibrarySource(URIUtils.desanitize(IRI.create(reg)), regMgr));
            } catch (Exception e1) {
                throw new WebApplicationException(e1, BAD_REQUEST);
            // Bad or not supplied core registry, try the ontology.
            }
    // Then ontology sources
    if (coreOntologies != null)
        for (String ont : coreOntologies) if (ont != null && !ont.isEmpty())
            try {
                srcs.add(new RootOntologySource(IRI.create(ont)));
            } catch (OWLOntologyCreationException e2) {
                // If this fails too, throw a bad request.
                throw new WebApplicationException(e2, BAD_REQUEST);
            }
    // Now the creation.
    try {
        // Expand core sources
        List<OntologyInputSource<?>> expanded = new ArrayList<OntologyInputSource<?>>();
        for (OntologyInputSource<?> coreSrc : srcs) if (coreSrc != null) {
            if (coreSrc instanceof SetInputSource) {
                for (Object o : ((SetInputSource<?>) coreSrc).getOntologies()) {
                    OntologyInputSource<?> src = null;
                    if (o instanceof OWLOntology)
                        src = new RootOntologySource((OWLOntology) o);
                    else if (o instanceof Graph)
                        src = new GraphSource((Graph) o);
                    if (src != null)
                        expanded.add(src);
                }
            } else
                // Must be denoting a single ontology
                expanded.add(coreSrc);
        }
        scope = onm.createOntologyScope(scopeid, expanded.toArray(new OntologyInputSource[0]));
        // Setup and register the scope. If no custom space was set, it will
        // still be open for modification.
        scope.setUp();
        onm.setScopeActive(scopeid, activate);
    } catch (DuplicateIDException e) {
        throw new WebApplicationException(e, CONFLICT);
    } catch (Exception ex) {
        throw new WebApplicationException(ex, INTERNAL_SERVER_ERROR);
    }
    ResponseBuilder rb = Response.created(uriInfo.getAbsolutePath());
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : SetInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.SetInputSource) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) LibrarySource(org.apache.stanbol.ontologymanager.registry.io.LibrarySource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) IrremovableOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.IrremovableOntologyException) WebApplicationException(javax.ws.rs.WebApplicationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException) IOException(java.io.IOException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) OntologyCollectorModificationException(org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollectorModificationException) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Graph(org.apache.clerezza.commons.rdf.Graph) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) GraphSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource) OntologyInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 3 with LibrarySource

use of org.apache.stanbol.ontologymanager.registry.io.LibrarySource in project stanbol by apache.

the class TestOntologyLibrary method testLibrarySourceCreation.

/**
 * Tests the creation of an ontology input source from a single library. Because the test is run offline,
 * import statements might be file URIs, so tests should not fail on this.
 *
 * @throws Exception
 */
@Test
public void testLibrarySourceCreation() throws Exception {
    IRI localTestRegistry = IRI.create(getClass().getResource(registryResourcePath));
    Dictionary<String, Object> regmanConf = new Hashtable<String, Object>();
    regmanConf.put(RegistryManager.REGISTRY_LOCATIONS, new String[] { localTestRegistry.toString() });
    // Instantiating the registry manager will also load the registry data.
    regMgr = new RegistryManagerImpl(offline, new ClerezzaOntologyProvider(tcManager, offline, parser), regmanConf);
    assertNotNull(regMgr);
    // Now use this registry manager to instantiate an input source.
    OntologyInputSource<OWLOntology> src = new LibrarySource(Locations.LIBRARY_TEST1, regMgr, virginOntologyManager);
    OWLOntology o = src.getRootOntology();
    boolean hasImporting = false, hasImported = false;
    for (OWLImportsDeclaration ax : o.getImportsDeclarations()) {
        // Since we added a local IRI mapping, import statements might be using file: IRIs instead of
        // HTTP, in which case IRI equality would fail. So it is enough here to just check the filename.
        String tmpstr = ax.getIRI().toString();
        if (!hasImporting && tmpstr.endsWith("characters_all.owl"))
            hasImporting = true;
        else if (!hasImported && tmpstr.endsWith("maincharacters.owl"))
            hasImported = true;
        if (hasImporting && hasImported)
            break;
    }
    assertTrue(hasImporting);
    assertTrue(hasImported);
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) Hashtable(java.util.Hashtable) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) LibrarySource(org.apache.stanbol.ontologymanager.registry.io.LibrarySource) RegistryManagerImpl(org.apache.stanbol.ontologymanager.registry.impl.RegistryManagerImpl) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) ClerezzaOntologyProvider(org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider) Test(org.junit.Test)

Aggregations

LibrarySource (org.apache.stanbol.ontologymanager.registry.io.LibrarySource)3 IOException (java.io.IOException)2 Consumes (javax.ws.rs.Consumes)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 IrremovableOntologyException (org.apache.stanbol.ontologymanager.servicesapi.collector.IrremovableOntologyException)2 OntologyCollectorModificationException (org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollectorModificationException)2 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)2 OntologyLoadingException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException)2 RootOntologySource (org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource)2 IRI (org.semanticweb.owlapi.model.IRI)2 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)2 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1