Search in sources :

Example 1 with Viewable

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

the class SparqlEndpointResource method sparql.

//TODO re-enable
/*@OPTIONS
    public Response handleCorsPreflight(@Context HttpHeaders headers) {
        ResponseBuilder res = Response.ok();
        enableCORS(servletContext, res, headers);
        return res.build();
    }*/
/**
     * HTTP GET service to execute SPARQL queries on {@link Graph}s registered to OSGi environment.
     * If a <code>null</code>, it is assumed that the request is coming from the HTML interface of SPARQL
     * endpoint. Otherwise the query is executed on the triple collection specified by <code>graphUri</code>.
     * But, if no graph uri is passed, then the triple collection having highest service.ranking value is
     * chosen.
     * 
     * Type of the result is determined according to type of the query such that if the specified query is
     * either a <b>describe query</b> or <b>construct query</b>, results are returned in
     * <b>application/rdf+xml</b> format, otherwise in <b>pplication/sparql-results+xml</b> format.
     * 
     * @param graphUri
     *            the URI of the graph on which the SPARQL query will be executed.
     * @param sparqlQuery
     *            SPARQL query to be executed
     * @param headers
     *            HTTP request Headers
     * @throws InvalidSyntaxException Invalid SPARQL Syntax Exception
     * @return Http Response 
     */
@GET
@Consumes(APPLICATION_FORM_URLENCODED)
@Produces({ TEXT_HTML + ";qs=2", "application/sparql-results+xml", "application/rdf+xml" })
public Response sparql(@QueryParam(value = "graphuri") String graphUri, @QueryParam(value = "query") String sparqlQuery, @Context HttpHeaders headers) throws InvalidSyntaxException {
    if (sparqlQuery == null) {
        populateGraphList(getServices(null));
        return Response.ok(new Viewable("index", this), TEXT_HTML).build();
    }
    String mediaType = "application/sparql-results+xml";
    Graph tripleCollection = getGraph(graphUri);
    ResponseBuilder rb;
    if (tripleCollection != null) {
        Object result;
        try {
            result = tcManager.executeSparqlQuery(sparqlQuery, tripleCollection);
            if (result instanceof Graph) {
                mediaType = "application/rdf+xml";
            }
            rb = Response.ok(result, mediaType);
        } catch (ParseException e) {
            rb = Response.status(Status.BAD_REQUEST).entity(e.getMessage());
        }
    } else {
        rb = Response.status(Status.NOT_FOUND).entity(String.format("There is no registered graph with given uri: %s", graphUri));
    }
    //addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : Graph(org.apache.clerezza.commons.rdf.Graph) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with Viewable

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

the class JobsResource method get.

/**
     * To read a /reasoners job output.
     * 
     * @param id
     * @return
     */
@GET
@Path("/{jid}")
public Response get(@PathParam("jid") String id, @Context HttpHeaders headers) {
    log.info("Pinging job {}", id);
    // No id
    if (id == null || id.equals("")) {
        ResponseBuilder rb = Response.status(Status.BAD_REQUEST);
        return rb.build();
    }
    JobManager m = getJobManager();
    // If the job exists
    if (m.hasJob(id)) {
        log.info("Found job with id {}", id);
        Future<?> f = m.ping(id);
        if (f.isDone() && (!f.isCancelled())) {
            /**
                 * We return OK with the result
                 */
            Object o;
            try {
                o = f.get();
                if (o instanceof ReasoningServiceResult) {
                    log.debug("Is a ReasoningServiceResult");
                    ReasoningServiceResult<?> result = (ReasoningServiceResult<?>) o;
                    return new ResponseTaskBuilder(new JobResultResource(uriInfo, headers)).build(result);
                } else {
                    log.error("Job {} does not belong to reasoners", id);
                    throw new WebApplicationException(Response.Status.NOT_FOUND);
                }
            } catch (InterruptedException e) {
                log.error("Error: ", e);
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            } catch (ExecutionException e) {
                log.error("Error: ", e);
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            /**
                 * We return 404 with additional info
                 */
            String jobService = new StringBuilder().append(getPublicBaseUri()).append("jobs/").append(id).toString();
            this.jobLocation = jobService;
            Viewable viewable = new Viewable("404.ftl", this);
            ResponseBuilder rb = Response.status(Status.NOT_FOUND);
            rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
            rb.entity(viewable);
            return rb.build();
        }
    } else {
        log.info("No job found with id {}", id);
        ResponseBuilder rb = Response.status(Status.NOT_FOUND);
        rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
        return rb.build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ReasoningServiceResult(org.apache.stanbol.reasoners.web.utils.ReasoningServiceResult) JobManager(org.apache.stanbol.commons.jobs.api.JobManager) ResponseTaskBuilder(org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ExecutionException(java.util.concurrent.ExecutionException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with Viewable

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

the class ReasoningServicesResource method getServiceDocumentation.

@GET
@Produces(TEXT_HTML)
@Path("{service}")
public Response getServiceDocumentation(@PathParam(value = "service") String serviceID, @Context HttpHeaders headers) {
    try {
        this.service = this.getServicesManager().get(serviceID);
    } catch (UnboundReasoningServiceException e) {
        log.info("Service {} is not bound", serviceID);
        ResponseBuilder rb = Response.status(Status.NOT_FOUND);
        rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        return rb.build();
    }
    ResponseBuilder rb = Response.ok(new Viewable("service", this));
    rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
    return rb.build();
}
Also used : UnboundReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.UnboundReasoningServiceException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with Viewable

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

the class ReasoningServicesResource method getDocumentation.

@GET
@Produces(TEXT_HTML)
public Response getDocumentation(@Context HttpHeaders headers) {
    ResponseBuilder rb = Response.ok(new Viewable("index", this));
    rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
    return rb.build();
}
Also used : Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with Viewable

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

the class ResponseTaskBuilder method build.

/**
     * Process the given object (result content output),
     * returning an HTML representation or delegating the rendering to jersey writers.
     * 
     * @param object
     * @return
     */
private Response build(Object object) {
    if (isHTML()) {
        OutputStream out = stream(object);
        this.result.setResult(out);
        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, out)),
                    TEXT_HTML).build();*/
    } else {
        //return Response.ok(object).build();
        ResponseBuilder rb = Response.ok(object);
        //            addCORSOrigin(context, rb, headers);
        return rb.build();
    }
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

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