Search in sources :

Example 41 with Viewable

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

the class ReferencedSiteRootResource method getHtmlInfo.

@GET
@Produces(value = MediaType.TEXT_HTML)
public Response getHtmlInfo(@PathParam(value = "site") String siteId, @Context HttpHeaders headers) {
    ResponseBuilder rb = Response.ok(new Viewable("index", new SiteResultData(getSite(siteId))));
    rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
    //addCORSOrigin(servletContext, rb, headers);
    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 42 with Viewable

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

the class EntityhubRootResource method getSymbol.

@GET
@Path("entity")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getSymbol(@QueryParam("id") String symbolId, @Context HttpHeaders headers) throws WebApplicationException {
    log.info("GET /entity Request");
    log.info("  > id: " + symbolId);
    log.info("  > accept: " + headers.getAcceptableMediaTypes());
    MediaType acceptedMediaType = getAcceptableMediaType(headers, ENTITY_SUPPORTED_MEDIA_TYPE_INCL_HTML, APPLICATION_JSON_TYPE);
    if (acceptedMediaType.isCompatible(TEXT_HTML_TYPE) && symbolId == null) {
        //return HTML docu
        ResponseBuilder rb = Response.ok(new Viewable("entity", this));
        rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    }
    if (symbolId == null || symbolId.isEmpty()) {
        // TODO: how to parse an error message
        throw new WebApplicationException(BAD_REQUEST);
    }
    //Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
    Entity entity;
    try {
        entity = entityhub.getEntity(symbolId);
    } catch (EntityhubException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    if (entity == null) {
        throw new WebApplicationException(NOT_FOUND);
    } else {
        ResponseBuilder rb = Response.ok(entity);
        rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 43 with Viewable

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

the class EntityhubRootResource method lookupSymbol.

@GET
@Path("lookup")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response lookupSymbol(@QueryParam("id") String reference, @QueryParam("create") boolean create, @Context HttpHeaders headers) throws WebApplicationException {
    log.info("GET /lookup Request");
    log.info("  > id: " + reference);
    log.info("  > create   : " + create);
    log.info("  > accept: " + headers.getAcceptableMediaTypes());
    MediaType acceptedMediaType = getAcceptableMediaType(headers, ENTITY_SUPPORTED_MEDIA_TYPE_INCL_HTML, APPLICATION_JSON_TYPE);
    if (acceptedMediaType.isCompatible(TEXT_HTML_TYPE) && reference == null) {
        //return docu
        ResponseBuilder rb = Response.ok(new Viewable("lookup", this));
        rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    } else {
        if (reference == null || reference.isEmpty()) {
            // TODO: how to parse an error message
            throw new WebApplicationException(BAD_REQUEST);
        }
        Entity entity;
        try {
            entity = entityhub.lookupLocalEntity(reference, create);
        } catch (EntityhubException e) {
            throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
        }
        if (entity == null) {
            return Response.status(Status.NOT_FOUND).entity("No symbol found for '" + reference + "'.").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        } else {
            ResponseBuilder rb = Response.ok(entity);
            rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
            //addCORSOrigin(servletContext, rb, headers);
            return rb.build();
        }
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) 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