Search in sources :

Example 41 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity 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

Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)41 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)16 HashSet (java.util.HashSet)12 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)12 MediaType (javax.ws.rs.core.MediaType)11 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)11 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)10 Path (javax.ws.rs.Path)9 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)9 Site (org.apache.stanbol.entityhub.servicesapi.site.Site)9 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)9 GET (javax.ws.rs.GET)7 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)7 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)7 ArrayList (java.util.ArrayList)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 IRI (org.apache.clerezza.commons.rdf.IRI)6 EntityImpl (org.apache.stanbol.entityhub.core.model.EntityImpl)6 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)6 Produces (javax.ws.rs.Produces)5