Search in sources :

Example 36 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class ReasoningServiceTaskResource method processBackgroundRequest.

/**
     * Process a background request. This service use the Stanbol Commons Jobs API to start a background job.
     * Returns 201 on success, with HTTP header Location pointing to the Job resource.
     * 
     * @return
     */
private Response processBackgroundRequest() {
    // If parameters is empty it's a bad request...
    if (this.parameters.isEmpty()) {
        log.error("Cannot start job without input parameters... sending BAD REQUEST");
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
    String target = getTarget();
    // Setup the input
    ReasoningServiceInputManager imngr = prepareInput();
    // The service executor
    ReasoningServiceExecutor executor = new ReasoningServiceExecutor(tcManager, imngr, getCurrentService(), getCurrentTask(), target, parameters);
    String jid = getJobManager().execute(executor);
    URI location = URI.create(getPublicBaseUri() + "jobs/" + jid);
    this.jobLocation = location.toString();
    /**
         * If everything went well, we return 201 Created We include the header Location: with the Job URL
         */
    Viewable view = new Viewable("created", this);
    return Response.created(location).entity(view).build();
}
Also used : ReasoningServiceExecutor(org.apache.stanbol.reasoners.web.utils.ReasoningServiceExecutor) ReasoningServiceInputManager(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) URI(java.net.URI)

Example 37 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class ReasoningServiceTaskResource method processRealTimeRequest.

/**
     * Process a real-time operation. Returns 200 when the process is ready, 500 if some error occurs
     * 
     * @return
     */
private Response processRealTimeRequest() {
    // page
    if (this.parameters.isEmpty() && file == null) {
        log.debug("no parameters no input file, show default index page");
        // return Response.ok(new Viewable("index", this)).build();
        ResponseBuilder rb = Response.ok(new Viewable("index", this));
        rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        return rb.build();
    }
    try {
        String target = getTarget();
        // Setup the input
        ReasoningServiceInputManager imngr = prepareInput();
        // The service executor
        ReasoningServiceExecutor executor = new ReasoningServiceExecutor(tcManager, imngr, getCurrentService(), getCurrentTask(), target, parameters);
        ReasoningServiceResult<?> result = executor.call();
        return new ResponseTaskBuilder(new ReasoningTaskResult(uriInfo, headers)).build(result);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : ReasoningServiceExecutor(org.apache.stanbol.reasoners.web.utils.ReasoningServiceExecutor) ReasoningServiceInputManager(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ResponseTaskBuilder(org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder) WebApplicationException(javax.ws.rs.WebApplicationException) UnboundReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.UnboundReasoningServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 38 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class ResponseTaskBuilder method buildCheckResponse.

/**
     * To build the Response for any CHECK task execution
     * 
     * @param isConsistent
     * @return
     */
private Response buildCheckResponse(boolean isConsistent) {
    if (isHTML()) {
        if (isConsistent) {
            log.debug("The input is consistent");
            result.setResult("The input is consistent :)");
            ResponseBuilder rb = Response.ok(new Viewable("result", result));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            //                addCORSOrigin(context, rb, headers);
            return rb.build();
        /*return Response.ok(
                        new Viewable("result",
                                new ReasoningPrettyResultResource(
                                        context, info,
                                        "The input is consistent :)")),
                        TEXT_HTML).build();*/
        } else {
            log.debug("The input is not consistent");
            ResponseBuilder rb = Response.status(Status.CONFLICT);
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            //                addCORSOrigin(context, rb, headers);
            result.setResult("The input is NOT consistent :(");
            rb.entity(new Viewable("result", result));
            return rb.build();
        /*return Response
                        .status(Status.CONFLICT)
                        .entity(new Viewable("result",
                                new ReasoningPrettyResultResource(
                                        context, info,
                                        "The input is NOT consistent :(")))
                        .type(TEXT_HTML).build();*/
        }
    } else {
        if (isConsistent) {
            log.debug("The input is consistent");
            //return Response.ok("The input is consistent :)").build();
            ResponseBuilder rb = Response.ok("The input is consistent :)");
            //                addCORSOrigin(context, rb, headers);
            return rb.build();
        } else {
            log.debug("The input is not consistent");
            //return Response.status(Status.CONFLICT).build();
            ResponseBuilder rb = Response.status(Status.CONFLICT);
            //                addCORSOrigin(context, rb, headers);
            return rb.build();
        }
    }
}
Also used : Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 39 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class RootResource method performShowOntology.

/**
     * Helper method to make sure a ResponseBuilder is created on every conditions, so that it is then
     * possible to enable CORS on it afterwards.
     * 
     * @param ontologyId
     * @return
     */
protected ResponseBuilder performShowOntology(String ontologyId) {
    if (ontologyId == null || ontologyId.isEmpty()) {
        return Response.status(BAD_REQUEST);
    }
    OWLOntologyID key = OntologyUtils.decode(ontologyId);
    if (ontologyProvider.listOrphans().contains(key)) {
        return Response.status(NO_CONTENT);
    }
    OWLOntology o = getOWLOntology(ontologyId, false, uriInfo.getRequestUri());
    if (o == null) {
        return Response.status(NOT_FOUND);
    }
    // Assemble dependency list
    Map<OWLOntologyID, OntologyProvider.Status> deps = new HashMap<OWLOntologyID, OntologyProvider.Status>();
    for (OWLOntologyID dep : getDescriptor().getDependencies(key)) {
        deps.put(dep, ontologyProvider.getStatus(dep));
    }
    Set<OntologyCollector> handles = new HashSet<OntologyCollector>();
    if (onManager != null) {
        for (Scope scope : onManager.getRegisteredScopes()) {
            if (scope.getCoreSpace().hasOntology(key)) {
                handles.add(scope.getCoreSpace());
            }
            if (scope.getCustomSpace().hasOntology(key)) {
                handles.add(scope.getCustomSpace());
            }
        }
    }
    if (sessionManager != null) {
        for (String sesId : sessionManager.getRegisteredSessionIDs()) {
            if (sessionManager.getSession(sesId).hasOntology(key)) {
                handles.add(sessionManager.getSession(sesId));
            }
        }
    }
    return Response.ok(new Viewable("ontology", new OntologyStats(uriInfo, key, o, ontologyProvider.listAliases(key), deps, handles)));
}
Also used : Status(javax.ws.rs.core.Response.Status) HashMap(java.util.HashMap) OntologyCollector(org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollector) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntologyProvider(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyProvider) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) HashSet(java.util.HashSet)

Example 40 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class ScopeResource method managedOntologyShow.

@GET
@Path("/{ontologyId:.+}")
@Produces(TEXT_HTML)
public Response managedOntologyShow(@PathParam("scopeid") String scopeid, @PathParam("ontologyId") String ontologyId, @Context HttpHeaders headers) {
    ResponseBuilder rb;
    scope = onm.getScope(scopeid);
    if (scope == null)
        rb = Response.status(NOT_FOUND);
    else if (ontologyId == null || ontologyId.isEmpty())
        rb = Response.status(BAD_REQUEST);
    else if (!ontologyProvider.hasOntology(OntologyUtils.decode(ontologyId)))
        rb = Response.status(NOT_FOUND);
    else {
        IRI prefix = IRI.create(getPublicBaseUri() + "ontonet/ontology/");
        OWLOntology o = scope.getCustomSpace().getOntology(OntologyUtils.decode(ontologyId), OWLOntology.class, false, prefix);
        if (o == null)
            o = scope.getCoreSpace().getOntology(OntologyUtils.decode(ontologyId), OWLOntology.class, false, prefix);
        if (o == null)
            rb = Response.status(NOT_FOUND);
        else
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                o.getOWLOntologyManager().saveOntology(o, new ManchesterOWLSyntaxOntologyFormat(), out);
                rb = Response.ok(new Viewable("ontology", new OntologyPrettyPrintResource(uriInfo, out, scope)));
            } catch (OWLOntologyStorageException e) {
                throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
            }
    }
    rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) WebApplicationException(javax.ws.rs.WebApplicationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) OntologyPrettyPrintResource(org.apache.stanbol.ontologymanager.web.util.OntologyPrettyPrintResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Viewable (org.apache.stanbol.commons.web.viewable.Viewable)43 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)40 GET (javax.ws.rs.GET)30 Produces (javax.ws.rs.Produces)26 Path (javax.ws.rs.Path)20 WebApplicationException (javax.ws.rs.WebApplicationException)15 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)14 MediaType (javax.ws.rs.core.MediaType)12 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)12 HashSet (java.util.HashSet)11 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)7 POST (javax.ws.rs.POST)6 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)6 IRI (org.semanticweb.owlapi.model.IRI)5 Consumes (javax.ws.rs.Consumes)4 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3