Search in sources :

Example 16 with SiteException

use of org.apache.stanbol.entityhub.servicesapi.site.SiteException in project stanbol by apache.

the class ReferencedSiteRootResource 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(@PathParam(value = "site") String siteId, @QueryParam(value = "id") String id, @Context HttpHeaders headers) {
    Site site = getSite(siteId);
    log.debug("site/{}/entity Request", site.getId());
    log.debug("  > id       : " + id);
    log.debug("  > accept   : " + headers.getAcceptableMediaTypes());
    log.debug("  > mediaType: " + 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", new SiteResultData(site)));
            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();
        }
    }
    log.debug("handle Request for Entity {} of Site {}", id, site.getId());
    Entity entity;
    try {
        entity = site.getEntity(id);
    } catch (SiteException e) {
        log.error("ReferencedSiteException while accessing Site " + site.getConfiguration().getName() + " (id=" + site.getId() + ")", e);
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
    if (entity != null) {
        ResponseBuilder rb = Response.ok(entity);
        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.debug(" ... Entity {} not found on referenced site {}", id, site.getId());
        return Response.status(Status.NOT_FOUND).entity("Entity '" + id + "' not found on referenced site '" + site.getId() + "'\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    }
}
Also used : ManagedSite(org.apache.stanbol.entityhub.servicesapi.site.ManagedSite) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) 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) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) GET(javax.ws.rs.GET)

Example 17 with SiteException

use of org.apache.stanbol.entityhub.servicesapi.site.SiteException in project stanbol by apache.

the class ReferencedSiteRootResource method executeQuery.

/**
     * Executes the query parsed by {@link #queryEntities(String, File, HttpHeaders)} or created based
     * {@link #findEntity(String, String, String, int, int, HttpHeaders)}
     * 
     * @param query
     *            The query to execute
     * @param headers the request headers
     * @return the response (results of error)
     */
private Response executeQuery(Site site, FieldQuery query, HttpHeaders headers) throws WebApplicationException {
    MediaType mediaType = getAcceptableMediaType(headers, ENTITY_SUPPORTED_MEDIA_TYPES, APPLICATION_JSON_TYPE);
    if (query instanceof LDPathSelect && ((LDPathSelect) query).getLDPathSelect() != null) {
        //use the LDPath variant to process this query
        return executeLDPathQuery(site, query, ((LDPathSelect) query).getLDPathSelect(), mediaType, headers);
    } else {
        //use the default query execution
        QueryResultList<Representation> result;
        try {
            result = site.find(query);
        } 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();
        }
        ResponseBuilder rb = Response.ok(result);
        rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Aggregations

SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)17 Site (org.apache.stanbol.entityhub.servicesapi.site.Site)12 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)9 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)8 HashSet (java.util.HashSet)7 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)5 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)5 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)5 MediaType (javax.ws.rs.core.MediaType)4 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)4 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)4 IOException (java.io.IOException)3 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)3 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)3 ManagedSite (org.apache.stanbol.entityhub.servicesapi.site.ManagedSite)3 ArrayList (java.util.ArrayList)2 Path (javax.ws.rs.Path)2 EntityImpl (org.apache.stanbol.entityhub.core.model.EntityImpl)2 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)2 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)2