Search in sources :

Example 16 with EntityhubException

use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.

the class EntityhubRootResource method deleteEntity.

@DELETE
@Path("entity")
public Response deleteEntity(@QueryParam(value = "id") String id, @Context HttpHeaders headers) {
    MediaType accepted = getAcceptableMediaType(headers, JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES, MediaType.APPLICATION_JSON_TYPE);
    if (id == null || id.isEmpty()) {
        return Response.status(Status.BAD_REQUEST).entity("The Request does" + "not provide the id of the Entity to delete (parameter 'id').").header(HttpHeaders.ACCEPT, accepted).build();
    }
    //Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
    Entity entity;
    ResponseBuilder rb;
    try {
        if (id.equals("*")) {
            log.info("Deleting all Entities form the Entityhub");
            entityhub.deleteAll();
            rb = Response.status(Response.Status.OK);
        } else {
            entity = entityhub.delete(id);
            if (entity == null) {
                rb = Response.status(Status.NOT_FOUND).entity("An Entity with the" + "parsed id " + id + " is not managed by the Entityhub").header(HttpHeaders.ACCEPT, accepted);
            } else {
                rb = Response.ok(entity);
                rb.header(HttpHeaders.CONTENT_TYPE, accepted + "; charset=utf-8").header(HttpHeaders.ACCEPT, accepted);
            }
        }
    } catch (EntityhubException e) {
        rb = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).header(HttpHeaders.ACCEPT, accepted);
    }
    //addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) 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) DELETE(javax.ws.rs.DELETE)

Example 17 with EntityhubException

use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.

the class EntityhubRootResource method updateOrCreateEntity.

/**
     * Implements the creation/update of Representations within the Entityhub.
     * @param id the id of the resource to create or update. If not 
     * <code>null</code> all parsed Representations with other
     * ids will be ignored.
     * @param parsed the parsed representation(s)
     * @param method the {@link HttpMethod} used by the reuqest. Needed to create
     * the correct response.
     * @param create allow to create new Entities
     * @param update allow to update existing Entities
     * @param headers the HTTP headers of the request
     * @return the created/updated representation as response
     */
private Response updateOrCreateEntity(String id, Map<String, Representation> parsed, String method, boolean create, boolean update, HttpHeaders headers) {
    //Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
    MediaType accepted = getAcceptableMediaType(headers, JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES, MediaType.APPLICATION_JSON_TYPE);
    if (entityhub == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("The Entityhub is currently unavailable.").header(HttpHeaders.ACCEPT, accepted).build();
    }
    //(1) if an id is parsed we need to ignore all other representations
    if (id != null && !"*".equals(id)) {
        Representation r = parsed.get(id);
        if (r == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Parsed RDF data do not contain any " + "Information about the parsed id '%s'", id)).header(HttpHeaders.ACCEPT, accepted).build();
        } else {
            parsed = Collections.singletonMap(id, r);
        }
    }
    //First check if all parsed Representation can be created/updated
    if (!(create && update)) {
        //if both create and update are enabled skip this
        long start = System.currentTimeMillis();
        log.debug("   ... validate parsed Representation state (create: {}| update: {})", create, update);
        for (Entry<String, Representation> entry : parsed.entrySet()) {
            boolean exists;
            try {
                exists = entityhub.isRepresentation(entry.getKey());
            } catch (EntityhubException e) {
                log.error(String.format("Exception while checking the existance " + "of an Entity with id  %s in the Entityhub.", entry.getKey()), e);
                return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Unable to process Entity %s because of" + "an Error while checking the current version of that" + "Entity within the Entityhub (Message: %s)", entry.getKey(), e.getMessage())).header(HttpHeaders.ACCEPT, accepted).build();
            }
            if ((exists && !update) || (!exists && !create)) {
                return Response.status(Status.BAD_REQUEST).entity(String.format("Unable to %s an Entity '%s' becuase it %s and request parameter '%s' is set. " + " To allow both creating and updating of Entities you need to set " + "'%s=true' in the request", exists ? "update" : "create", entry.getKey(), exists ? "exists " : "does not exist", exists ? "update=false" : "create=false", exists ? "update" : "create")).header(HttpHeaders.ACCEPT, accepted).build();
            }
        }
        log.debug("      > checked {} entities in {}ms", parsed.size(), System.currentTimeMillis() - start);
    }
    //store the Representations
    //If someone parses data for more than a single Entity, but does not
    //provide an ID for the Entity to update, this will update/create all
    //the parsed entity. However the response can only return a single
    //Entity!
    //This can not be changed easily as long as there are no local URIs 
    //for remote Entiteis as suggested by
    // http://incubator.apache.org/stanbol/docs/trunk/entityhub/entityhubandlinkeddata.html
    Map<String, Entity> updated = new HashMap<String, Entity>();
    for (Representation representation : parsed.values()) {
        try {
            Entity entity = entityhub.store(representation);
            updated.put(entity.getId(), entity);
        } catch (EntityhubException e) {
            log.error(String.format("Exception while storing Entity %s" + "in the Entityhub.", representation), e);
        }
    }
    // created/updated entity
    if (updated.isEmpty()) {
        // No (valid) data parsed
        ResponseBuilder rb = Response.status(Status.NOT_MODIFIED);
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    } else {
        Entity entity = updated.values().iterator().next();
        if (method.equals(HttpMethod.POST)) {
            ResponseBuilder rb = Response.created(uriInfo.getAbsolutePathBuilder().queryParam("id", "{entityId}").build(entity.getId()));
            //addCORSOrigin(servletContext, rb, headers);
            return rb.build();
        } else {
            //return Response.noContent().build();
            //As alternative return the modified entity
            ResponseBuilder rb = Response.ok(entity);
            rb.header(HttpHeaders.CONTENT_TYPE, accepted + "; charset=utf-8");
            //addCORSOrigin(servletContext, rb, headers);
            return rb.build();
        }
    //            if (updated.size() == 1){
    //                return Response.status(createState? Status.CREATED:Status.OK)
    //                .entity(updated.values().iterator().next())
    //                .header(HttpHeaders.ACCEPT, accepted).build();
    //            } else { //implement serializer for list of Entitis!
    //                return Response.status(createState? Status.CREATED:Status.OK)
    //                .entity(updated.values())
    //                .header(HttpHeaders.ACCEPT, accepted).build();
    //            }
    }
//return Response.seeOther(uriInfo.getAbsolutePath()).build();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) HashMap(java.util.HashMap) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 18 with EntityhubException

use of org.apache.stanbol.entityhub.servicesapi.EntityhubException 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 19 with EntityhubException

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

EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)19 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)10 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)9 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)8 MediaType (javax.ws.rs.core.MediaType)7 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)7 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)7 HashSet (java.util.HashSet)6 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)6 Produces (javax.ws.rs.Produces)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 Graph (org.apache.clerezza.commons.rdf.Graph)3 Entityhub (org.apache.stanbol.entityhub.servicesapi.Entityhub)3 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)3 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IRI (org.apache.clerezza.commons.rdf.IRI)2