Search in sources :

Example 6 with UnsupportedFormatException

use of org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException 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

UnsupportedFormatException (org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException)6 IOException (java.io.IOException)5 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)5 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)4 InputStream (java.io.InputStream)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 IRI (org.apache.clerezza.commons.rdf.IRI)2 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)2 EntityAlreadyExistsException (org.apache.clerezza.rdf.core.access.EntityAlreadyExistsException)2 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)2 OntologyHandleException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException)2 OntologyLoadingException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException)2 OrphanOntologyKeyException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException)2 IRI (org.semanticweb.owlapi.model.IRI)2 BufferedInputStream (java.io.BufferedInputStream)1