Search in sources :

Example 11 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class SiteManagerRootResource method getEntityById.

/**
     * Cool URI handler for Signs.
     * 
     * @param id
     *            The id of the entity (required)
     * @param headers
     *            the request headers used to get the requested {@link MediaType}
     * @return a redirection to either a browser view, the RDF meta data or the raw binary content
     */
@GET
@Path("/entity")
public Response getEntityById(@QueryParam(value = "id") String id, @Context HttpHeaders headers) {
    log.debug("getSignById() request\n\t> id       : {}\n\t> accept   : {}\n\t> mediaType: {}", new Object[] { id, headers.getAcceptableMediaTypes(), headers.getMediaType() });
    Collection<String> supported = new HashSet<String>(JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    final MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, MediaType.APPLICATION_JSON_TYPE);
    if (id == null || id.isEmpty()) {
        if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            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();
        } else {
            return Response.status(Status.BAD_REQUEST).entity("No or empty ID was parsed. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    Entity sign = referencedSiteManager.getEntity(id);
    if (sign != null) {
        ResponseBuilder rb = Response.ok(sign);
        rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    } else {
        // TODO: How to parse an ErrorMessage?
        // create an Response with the the Error?
        log.info("getSignById() entity {} not found on any referenced site");
        return Response.status(Status.NOT_FOUND).entity("Entity with ID '" + id + "' not found an any referenced site\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) 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) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) GET(javax.ws.rs.GET)

Example 12 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class ReferencedSiteRootResource method executeLDPathQuery.

/**
     * Execute a Query that uses LDPath to process results.
     * @param query the query
     * @param mediaType the mediaType for the response
     * @param headers the http headers of the request
     * @return the response
     */
private Response executeLDPathQuery(Site site, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
    QueryResultList<Representation> result;
    ValueFactory vf = new RdfValueFactory(new IndexedGraph());
    SiteBackend backend = new SiteBackend(site, vf);
    EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
    //copy the selected fields, because we might need to delete some during
    //the preparation phase
    Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
    //first prepare (only execute the query if the parameters are valid)
    Program<Object> program;
    try {
        program = prepareQueryLDPathProgram(ldpathProgramString, selectedFields, backend, ldPath);
    } catch (LDPathParseException e) {
        log.warn("Unable to parse LDPath program used as select for Query:");
        log.warn("FieldQuery: \n {}", query);
        log.warn("LDPath: \n {}", ((LDPathSelect) query).getLDPathSelect());
        log.warn("Exception:", e);
        return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, mediaType).build();
    } catch (IllegalStateException e) {
        log.warn("parsed LDPath program is not compatible with parsed Query!", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //2. execute the query
    Iterator<Representation> resultIt;
    try {
        // we need to adapt from Entity to Representation
        resultIt = new AdaptingIterator<Entity, Representation>(site.findEntities(query).iterator(), new AdaptingIterator.Adapter<Entity, Representation>() {

            @Override
            public Representation adapt(Entity value, Class<Representation> type) {
                return value.getRepresentation();
            }
        }, Representation.class);
    } catch (SiteException e) {
        String message = String.format("Unable to Query Site '%s' (message: %s)", site.getId(), e.getMessage());
        log.error(message, e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //process the results
    Collection<Representation> transformedResults = transformQueryResults(resultIt, program, selectedFields, ldPath, backend, vf);
    result = new QueryResultListImpl<Representation>(query, transformedResults, Representation.class);
    ResponseBuilder rb = Response.ok(result);
    rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
    //addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) SiteBackend(org.apache.stanbol.entityhub.ldpath.backend.SiteBackend) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Example 13 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class EntityhubRootResource method getMapping.

@GET
@Path("mapping")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getMapping(@QueryParam("id") String reference, @Context HttpHeaders headers) throws WebApplicationException {
    log.debug("get mapping for request > id : {} > accept: {}", reference, headers.getAcceptableMediaTypes());
    Set<String> supported = new HashSet<String>(JerseyUtils.REPRESENTATION_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, APPLICATION_JSON_TYPE);
    if (reference == null || reference.isEmpty()) {
        //if HTML -> print the docu of the restfull service
        if (TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("mapping", this));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        } else {
            return Response.status(Status.BAD_REQUEST).entity("The mapping id (URI) is missing.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    //Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
    Entity mapping;
    try {
        mapping = entityhub.getMappingById(reference);
    } catch (EntityhubException e) {
        log.error("error while getting the mapping for {}", reference, e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
    if (mapping == null) {
        return Response.status(Status.NOT_FOUND).entity("No mapping found for '" + reference + "'.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    } else {
        ResponseBuilder rb = Response.ok(mapping);
        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) 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) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 14 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class EntityhubRootResource method getEntityMapping.

@GET
@Path("mapping/entity")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getEntityMapping(@QueryParam("id") String entity, @Context HttpHeaders headers) throws WebApplicationException {
    log.debug("getEntityMapping() POST Request > entity: {} > accept: {}", entity, headers.getAcceptableMediaTypes());
    Set<String> supported = new HashSet<String>(JerseyUtils.REPRESENTATION_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, APPLICATION_JSON_TYPE);
    if (entity == null || entity.isEmpty()) {
        //if HTML -> print the docu of the restfull service
        if (TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("mapping_entity", this));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        } else {
            return Response.status(Status.BAD_REQUEST).entity("No entity given. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    //Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
    Entity mapping;
    try {
        mapping = entityhub.getMappingBySource(entity);
    } catch (EntityhubException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    if (mapping == null) {
        return Response.status(Status.NOT_FOUND).entity("No mapping found for entity '" + entity + "'.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    } else {
        ResponseBuilder rb = Response.ok(mapping);
        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) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 15 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class EntityhubRootResource method getSymbolMappings.

@GET
@Path("mapping/symbol")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getSymbolMappings(@QueryParam("id") String symbol, @Context HttpHeaders headers) throws WebApplicationException {
    log.debug("getSymbolMappings() POST Request > symbol: {} > accept: {}", symbol, headers.getAcceptableMediaTypes());
    Set<String> supported = new HashSet<String>(JerseyUtils.REPRESENTATION_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, APPLICATION_JSON_TYPE);
    if (symbol == null || symbol.isEmpty()) {
        //if HTML -> print the docu of the restfull service
        if (TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("mapping_symbol", this));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            //addCORSOrigin(servletContext, rb, headers);
            return rb.build();
        } else {
            return Response.status(Status.BAD_REQUEST).entity("No symbol given. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    //Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
    Collection<Entity> mappings;
    try {
        mappings = entityhub.getMappingsByTarget(symbol);
    } catch (EntityhubException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    if (mappings == null || mappings.isEmpty()) {
        return Response.status(Status.NOT_FOUND).entity("No mapping found for symbol '" + symbol + "'.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    } else {
        // TODO: Implement Support for list of Signs, Representations and Strings
        // For now use a pseudo QueryResultList
        QueryResultList<Entity> mappingResultList = new QueryResultListImpl<Entity>(null, mappings, Entity.class);
        ResponseBuilder rb = Response.ok(mappingResultList);
        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) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) 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) HashSet(java.util.HashSet) 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