Search in sources :

Example 41 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class SessionManagerResource method createSessionWithAutomaticId.

@POST
public Response createSessionWithAutomaticId(@Context UriInfo uriInfo, @Context HttpHeaders headers) {
    Session s;
    try {
        s = sessionManager.createSession();
    } catch (SessionLimitException e) {
        throw new WebApplicationException(e, FORBIDDEN);
    }
    String uri = uriInfo.getRequestUri().toString();
    while (uri.endsWith("/")) uri = uri.substring(0, uri.length() - 1);
    uri += "/" + s.getID();
    ResponseBuilder rb = Response.created(URI.create(uri));
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SessionLimitException(org.apache.stanbol.ontologymanager.servicesapi.session.SessionLimitException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session) POST(javax.ws.rs.POST)

Example 42 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class SessionManagerResource method listSessions.

@GET
@Produces(value = { RDF_XML, OWL_XML, TURTLE, X_TURTLE, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON, N3, N_TRIPLE, TEXT_PLAIN })
public Response listSessions(@Context UriInfo uriInfo, @Context HttpHeaders headers) {
    OWLOntologyManager ontMgr = OWLManager.createOWLOntologyManager();
    OWLDataFactory df = ontMgr.getOWLDataFactory();
    OWLClass cSession = df.getOWLClass(IRI.create("http://stanbol.apache.org/ontologies/meta/Session"));
    OWLOntology o;
    try {
        o = ontMgr.createOntology(IRI.create(uriInfo.getRequestUri()));
        List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
        for (String id : sessionManager.getRegisteredSessionIDs()) {
            IRI sessionid = IRI.create(sessionManager.getDefaultNamespace() + sessionManager.getID() + "/" + id);
            OWLNamedIndividual ind = df.getOWLNamedIndividual(sessionid);
            changes.add(new AddAxiom(o, df.getOWLClassAssertionAxiom(cSession, ind)));
        }
        ontMgr.applyChanges(changes);
    } catch (OWLOntologyCreationException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    ResponseBuilder rb = Response.ok(o);
    MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
    if (mediaType != null)
        rb.header(HttpHeaders.CONTENT_TYPE, mediaType);
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) AddAxiom(org.semanticweb.owlapi.model.AddAxiom) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) MediaType(javax.ws.rs.core.MediaType) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 43 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder 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 44 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class ScopeResource method deregisterScope.

@DELETE
public Response deregisterScope(@PathParam("scopeid") String scopeid, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context ServletContext servletContext) {
    scope = onm.getScope(scopeid);
    onm.deregisterScope(scope);
    scope = null;
    ResponseBuilder rb = Response.ok();
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) DELETE(javax.ws.rs.DELETE)

Example 45 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class ScopeResource method asOntologyGraph.

@GET
@Produces(value = { APPLICATION_JSON, N3, N_TRIPLE, RDF_JSON })
public Response asOntologyGraph(@PathParam("scopeid") String scopeid, @DefaultValue("false") @QueryParam("merge") boolean merge, @Context HttpHeaders headers) {
    scope = onm.getScope(scopeid);
    if (scope == null)
        return Response.status(NOT_FOUND).build();
    IRI prefix = IRI.create(getPublicBaseUri() + "ontonet/ontology/");
    // Export to Clerezza ImmutableGraph, which can be rendered as JSON-LD.
    ResponseBuilder rb = Response.ok(scope.export(ImmutableGraph.class, merge, prefix));
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)175 GET (javax.ws.rs.GET)84 Produces (javax.ws.rs.Produces)81 Path (javax.ws.rs.Path)69 WebApplicationException (javax.ws.rs.WebApplicationException)47 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)40 IOException (java.io.IOException)30 MediaType (javax.ws.rs.core.MediaType)29 POST (javax.ws.rs.POST)23 IRI (org.semanticweb.owlapi.model.IRI)22 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)21 Response (javax.ws.rs.core.Response)20 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)19 URI (java.net.URI)18 Consumes (javax.ws.rs.Consumes)18 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)16 HashSet (java.util.HashSet)14 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)12