Search in sources :

Example 16 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID 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 17 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID 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 18 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class RootResource method performLoadOntology.

protected ResponseBuilder performLoadOntology(MultiPartBody data, HttpHeaders headers, Origin<?>... keys) {
    log.debug(" post(MultiPartBody data)");
    ResponseBuilder rb = null;
    IRI location = null;
    // If found, it takes precedence over location.
    byte[] file = null;
    String format = null;
    List<OWLOntologyID> aliases = new ArrayList<OWLOntologyID>();
    if (data.getFormFileParameterValues("file").length > 0) {
        file = data.getFormFileParameterValues("file")[0].getContent();
    }
    // 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 " + value, ex);
            throw new WebApplicationException(ex, BAD_REQUEST);
        }
    }
    if (data.getTextParameterValues("alias").length > 0) {
        for (String value : data.getTextParameterValues("alias")) {
            if (!"null".equals(value)) {
                try {
                    aliases.add(OntologyUtils.decode(value));
                } catch (Exception ex) {
                    log.error("Malformed public key for " + value, ex);
                    throw new WebApplicationException(ex, BAD_REQUEST);
                }
            }
        }
    }
    log.debug("Parameters:");
    log.debug("file: {}", file != null && file.length > 0 ? "NOT-NULL" : "null");
    log.trace("file data: {}", file);
    log.debug("url: {}", location);
    log.debug("format: {}", format);
    log.debug("alias: {}", aliases);
    // Then add the file
    OWLOntologyID key = null;
    if (file != null && file.length > 0) {
        /*
             * Because the ontology provider's load method could fail after only one attempt without resetting
             * the stream, we might have to do that ourselves.
             */
        List<String> formats;
        if (format != null && !format.trim().isEmpty()) {
            formats = Collections.singletonList(format);
        } else // The RESTful API has its own list of preferred formats
        {
            formats = Arrays.asList(RDF_XML, TURTLE, X_TURTLE, N3, N_TRIPLE, OWL_XML, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON);
        }
        log.debug("Will try {} supported formats", formats.size());
        int unsupported = 0, failed = 0;
        Iterator<String> itf = formats.iterator();
        if (!itf.hasNext()) {
            throw new OntologyLoadingException("No suitable format found or defined.");
        }
        do {
            String f = itf.next();
            try {
                // Re-instantiate the stream on every attempt
                InputStream content = new BufferedInputStream(new ByteArrayInputStream(file));
                // ClerezzaOWLUtils.guessOntologyID(new FileInputStream(file), Parser.getInstance(), f);
                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.ftl", this));
                        rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
                    }
                    break;
                } else {
                    content = new BufferedInputStream(new ByteArrayInputStream(file));
                    key = ontologyProvider.loadInStore(content, f, true, keys);
                }
            } catch (UnsupportedFormatException e) {
                log.warn("POST method failed for media type {}. This should not happen (should fail earlier)", headers.getMediaType());
                // rb = Response.status(UNSUPPORTED_MEDIA_TYPE);
                unsupported++;
            } catch (IOException e) {
                log.debug(">>> FAILURE format {} (I/O error)", f);
                failed++;
            } catch (ConcurrentModificationException e) {
                log.error("Exception logged", e);
                failed++;
            } catch (Exception e) {
                // SAXParseException and others
                log.debug(">>> FAILURE format {} (parse error)", f);
                log.debug("Caught exception {} : {}", e.getClass(), e.getLocalizedMessage());
                log.trace("Exception logged", e);
                failed++;
            }
        } while ((key == null) && itf.hasNext());
        if ((key == null || key.isAnonymous()) && rb == null) {
            if (failed > 0) {
                throw new WebApplicationException(BAD_REQUEST);
            } else if (unsupported > 0) {
                throw new WebApplicationException(UNSUPPORTED_MEDIA_TYPE);
            }
        }
    } else if (location != null) {
        try {
            // Here we try every format supported by the Java API
            key = ontologyProvider.loadInStore(location, null, true, keys);
        } catch (Exception e) {
            log.error("Failed to load ontology from " + location, e);
            Throwable cause = e.getCause();
            String html = "<h1>400 Bad Request</h1>" + "<p>Failed to load ontology from <a href=\"" + location + "\" target=\"_blank\">" + location + "</a></p>";
            if (cause != null)
                html += "<p>logged cause was: " + cause.getLocalizedMessage().replace("<", "&lt;").replace(">", "&gt;") + "</p>";
            return Response.status(BAD_REQUEST).type(TEXT_HTML).entity(html);
        }
    } else if (// No content but there are aliases.
    !aliases.isEmpty()) {
        for (Origin<?> origin : keys) {
            if (origin.getReference() instanceof OWLOntologyID) {
                OWLOntologyID primary = ((OWLOntologyID) origin.getReference());
                if (ontologyProvider.getStatus(primary) != org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyProvider.Status.NO_MATCH) {
                    for (OWLOntologyID alias : aliases) {
                        try {
                            if (ontologyProvider.addAlias(primary, alias) && key == null) {
                                key = alias;
                            }
                        } catch (IllegalArgumentException ex) {
                            log.warn("Cannot add alias");
                            log.warn(" ... ontology key: {}", primary);
                            log.warn(" ... alias: {}", alias);
                            log.warn(" ... reason: ", ex);
                            continue;
                        }
                    }
                }
            }
        }
    } else {
        log.error("Bad request");
        log.error(" file is: {}", file);
        throw new WebApplicationException(BAD_REQUEST);
    }
    if (key != null && !key.isAnonymous()) {
        String uri = OntologyUtils.encode(key);
        if (uri != null && !uri.isEmpty()) {
            rb = Response.ok();
            if (headers.getAcceptableMediaTypes().contains(MediaType.TEXT_HTML_TYPE)) {
                rb.entity(new Viewable("index", this));
                rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
            }
        } else {
            rb = Response.ok();
        }
    } else if (rb == null) {
        rb = Response.status(Status.INTERNAL_SERVER_ERROR);
    }
    return rb;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ConcurrentModificationException(java.util.ConcurrentModificationException) WebApplicationException(javax.ws.rs.WebApplicationException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) WebApplicationException(javax.ws.rs.WebApplicationException) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OntologyHandleException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OrphanOntologyKeyException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 19 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class RootResource method deleteOntology.

@DELETE
@Path("/{ontologyId:.+}")
public Response deleteOntology(@PathParam("ontologyId") String ontologyId, @Context HttpHeaders headers) {
    OWLOntologyID key = OntologyUtils.decode(ontologyId);
    ResponseBuilder rb;
    try {
        if (!ontologyProvider.hasOntology(key)) {
            rb = Response.status(NOT_FOUND);
        } else {
            try {
                // TODO check aliases!
                ontologyProvider.removeOntology(key);
                rb = Response.ok();
            } catch (OntologyHandleException e) {
                rb = Response.status(CONFLICT);
            }
        }
    } catch (OrphanOntologyKeyException e) {
        log.warn("Orphan ontology key {}. No associated graph found in store.", e.getOntologyKey());
        rb = Response.status(NOT_FOUND);
    }
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologyHandleException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) OrphanOntologyKeyException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 20 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class ScopeResource method postOntology.

@POST
@Consumes({ MULTIPART_FORM_DATA })
@Produces({ TEXT_HTML, TEXT_PLAIN, RDF_XML, TURTLE, X_TURTLE, N3 })
public Response postOntology(MultiPartBody data, @PathParam("scopeid") String scopeid, @Context HttpHeaders headers) {
    log.info(" post(MultiPartBody data) scope: {}", scopeid);
    ResponseBuilder rb;
    scope = onm.getScope(scopeid);
    // 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> 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);
    }
    log.debug("Parameters:");
    log.debug("file: {}", file);
    log.debug("url: {}", location);
    log.debug("format: {}", format);
    log.debug("keys: {}", keys);
    boolean fileOk = file != null;
    // }
    if (fileOk || location != null || library != null) {
        // File and location take precedence
        // src = new GraphContentInputSource(content, format, ontologyProvider.getStore());
        // Then add the file
        OntologyInputSource<?> src = null;
        if (fileOk) {
            /*
                 * Because the ontology provider's load method could fail after only one attempt without
                 * resetting the stream, we might have to do that ourselves.
                 */
            List<String> formats;
            if (format != null && !format.trim().isEmpty())
                formats = Collections.singletonList(format);
            else
                // The RESTful API has its own list of preferred formats
                formats = Arrays.asList(RDF_XML, TURTLE, X_TURTLE, N3, N_TRIPLE, OWL_XML, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON);
            int unsupported = 0, failed = 0;
            Iterator<String> itf = formats.iterator();
            if (!itf.hasNext())
                throw new OntologyLoadingException("No suitable format found or defined.");
            do {
                String f = itf.next();
                try {
                    // Re-instantiate the stream on every attempt
                    InputStream content = new ByteArrayInputStream(file.getContent());
                    // ClerezzaOWLUtils.guessOntologyID(new FileInputStream(file), Parser.getInstance(),
                    // f);
                    OWLOntologyID guessed = OWLUtils.guessOntologyID(content, Parser.getInstance(), f);
                    log.debug("guessed ontology id: {}", guessed);
                    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("conflict.ftl", new ScopeResultData()));
                            rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
                        }
                        break;
                    } else {
                        content = new ByteArrayInputStream(file.getContent());
                        log.debug("Recreated input stream for format {}", f);
                        src = new GraphContentInputSource(content, f, ontologyProvider.getStore());
                    }
                } catch (UnsupportedFormatException e) {
                    log.warn("POST method failed for media type {}. This should not happen (should fail earlier)", headers.getMediaType());
                    // rb = Response.status(UNSUPPORTED_MEDIA_TYPE);
                    unsupported++;
                } catch (IOException e) {
                    log.debug(">>> FAILURE format {} (I/O error)", f);
                    failed++;
                } catch (Exception e) {
                    // SAXParseException and others
                    log.debug(">>> FAILURE format {} (parse error)", f);
                    failed++;
                }
            } while (src == null && itf.hasNext());
        }
        if (src != null) {
            OWLOntologyID key = scope.getCustomSpace().addOntology(src);
            if (key == null || key.isAnonymous())
                throw new WebApplicationException(INTERNAL_SERVER_ERROR);
            // FIXME ugly but will have to do for the time being
            // 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/ontology/" + scope.getID() + "/" + uri));
            } else
                rb = Response.ok();
        } else if (rb == null)
            rb = Response.status(INTERNAL_SERVER_ERROR);
    }
    if (!keys.isEmpty()) {
        for (String key : keys) scope.getCustomSpace().addOntology(new StoredOntologySource(OntologyUtils.decode(key)));
        rb = Response.seeOther(URI.create("/ontonet/ontology/" + scope.getID()));
    }
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) WebApplicationException(javax.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) 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) StoredOntologySource(org.apache.stanbol.ontologymanager.servicesapi.io.StoredOntologySource) FormFile(org.apache.clerezza.jaxrs.utils.form.FormFile) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) GraphContentInputSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)101 Test (org.junit.Test)32 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)32 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)23 HashSet (java.util.HashSet)22 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)21 InputStream (java.io.InputStream)20 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)19 Triple (org.apache.clerezza.commons.rdf.Triple)19 IRI (org.semanticweb.owlapi.model.IRI)18 IRI (org.apache.clerezza.commons.rdf.IRI)17 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)16 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)13 IOException (java.io.IOException)12 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)12 WebApplicationException (javax.ws.rs.WebApplicationException)11 Graph (org.apache.clerezza.commons.rdf.Graph)10 AddImport (org.semanticweb.owlapi.model.AddImport)10 Path (javax.ws.rs.Path)9 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)8