Search in sources :

Example 1 with OntologyContentInputSource

use of org.apache.stanbol.ontologymanager.sources.owlapi.OntologyContentInputSource 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 2 with OntologyContentInputSource

use of org.apache.stanbol.ontologymanager.sources.owlapi.OntologyContentInputSource in project stanbol by apache.

the class RootResource method storeOntology.

/**
     * POSTs an ontology content as application/x-www-form-urlencoded
     * 
     * @param content
     * @param headers
     * @return
     */
@POST
@Consumes(value = { RDF_XML, TURTLE, X_TURTLE, N3, N_TRIPLE, OWL_XML, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON })
public Response storeOntology(InputStream content, @Context HttpHeaders headers) {
    long before = System.currentTimeMillis();
    ResponseBuilder rb;
    MediaType mt = headers.getMediaType();
    if (RDF_XML_TYPE.equals(mt) || TURTLE_TYPE.equals(mt) || X_TURTLE_TYPE.equals(mt) || N3_TYPE.equals(mt) || N_TRIPLE_TYPE.equals(mt) || RDF_JSON_TYPE.equals(mt)) {
        OWLOntologyID key = null;
        try {
            key = ontologyProvider.loadInStore(content, headers.getMediaType().toString(), true);
            rb = Response.ok();
        } 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);
        } catch (IOException e) {
            throw new WebApplicationException(e, BAD_REQUEST);
        }
        // An exception should have been thrown earlier, but just in case.
        if (key == null || key.isAnonymous()) {
            rb = Response.status(Status.INTERNAL_SERVER_ERROR);
        }
    } else if (OWL_XML_TYPE.equals(mt) || FUNCTIONAL_OWL_TYPE.equals(mt) || MANCHESTER_OWL_TYPE.equals(mt)) {
        try {
            OntologyInputSource<OWLOntology> src = new OntologyContentInputSource(content);
            ontologyProvider.loadInStore(src.getRootOntology(), true);
            rb = Response.ok();
        } catch (OWLOntologyCreationException e) {
            throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
        }
    } else {
        rb = Response.status(UNSUPPORTED_MEDIA_TYPE);
    }
    // addCORSOrigin(servletContext, rb, headers);
    Response r = rb.build();
    log.debug("POST request for ontology addition completed in {} ms with status {}.", (System.currentTimeMillis() - before), r.getStatus());
    return r;
}
Also used : Response(javax.ws.rs.core.Response) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) WebApplicationException(javax.ws.rs.WebApplicationException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OntologyContentInputSource(org.apache.stanbol.ontologymanager.sources.owlapi.OntologyContentInputSource) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) MediaType(javax.ws.rs.core.MediaType) OntologyInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource) IOException(java.io.IOException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 OntologyInputSource (org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource)2 OntologyContentInputSource (org.apache.stanbol.ontologymanager.sources.owlapi.OntologyContentInputSource)2 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)2 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)2 IOException (java.io.IOException)1 URI (java.net.URI)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1 UnsupportedFormatException (org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException)1 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)1 GraphContentInputSource (org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource)1