Search in sources :

Example 1 with SiteException

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

the class ReferencedSiteSearcher method lookup.

@Override
public Collection<? extends Representation> lookup(String field, Set<String> includeFields, List<String> search, String... languages) throws IllegalStateException {
    //build the query and than return the result
    Site site = getSearchService();
    if (site == null) {
        throw new IllegalStateException("ReferencedSite " + siteId + " is currently not available");
    }
    FieldQuery query = EntitySearcherUtils.createFieldQuery(site.getQueryFactory(), field, includeFields, search, languages);
    if (limit != null) {
        query.setLimit(limit);
    }
    QueryResultList<Representation> results;
    try {
        results = site.find(query);
    } catch (SiteException e) {
        throw new IllegalStateException("Exception while searchign for " + search + '@' + Arrays.toString(languages) + "in the ReferencedSite " + site.getId(), e);
    }
    return results.results();
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 2 with SiteException

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

the class SiteManagerImpl method findIds.

@Override
public QueryResultList<String> findIds(FieldQuery query) {
    log.debug("findIds for query{}", query);
    // We need to search all referenced Sites
    Set<String> entityIds = new HashSet<String>();
    //TODO: The QueryResultList expects that the query as executed is added
    //to the response. However when executing queries on multiple site they
    //might support a different set of features and therefore execute
    //different variants. For now I return simple the query as executed by
    //the first Site that contributes results
    FieldQuery processedQuery = null;
    FieldQuery queryWithResults = null;
    for (Site site : referencedSites) {
        if (site.supportsSearch()) {
            log.debug(" > query site {}", site.getId());
            try {
                QueryResultList<String> results = site.findReferences(query);
                if (processedQuery == null) {
                    processedQuery = results.getQuery();
                }
                if (!results.isEmpty() && queryWithResults == null) {
                    processedQuery = results.getQuery();
                }
                for (String entityId : results) {
                    entityIds.add(entityId);
                }
            } catch (SiteException e) {
                log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
            }
        } else {
            log.debug(" > Site {} does not support queries", site.getId());
        }
    }
    return new QueryResultListImpl<String>(//use the query with results
    queryWithResults != null ? //use the query with results
    queryWithResults : //if not a processed
    processedQuery != null ? //if not a processed
    processedQuery : //else the parsed one
    query, entityIds.iterator(), String.class);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 3 with SiteException

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

the class SiteManagerImpl method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) {
    log.debug("findEntities for query{}", query);
    //TODO: The QueryResultList expects that the query as executed is added
    //to the response. However when executing queries on multiple site they
    //might support a different set of features and therefore execute
    //different variants. For now I return simple the query as executed by
    //the first Site that contributes results
    FieldQuery processedQuery = null;
    FieldQuery queryWithResults = null;
    Set<Entity> entities = new HashSet<Entity>();
    for (Site site : referencedSites) {
        if (site.supportsSearch()) {
            //do not search on sites that do not support it
            log.debug(" > query site {}", site.getId());
            try {
                QueryResultList<Entity> results = site.findEntities(query);
                if (processedQuery == null) {
                    processedQuery = results.getQuery();
                }
                if (!results.isEmpty() && queryWithResults == null) {
                    processedQuery = results.getQuery();
                }
                for (Entity rep : results) {
                    if (!entities.contains(rep)) {
                        //do not override
                        entities.add(rep);
                    } else {
                        //TODO: find a solution for this problem
                        //      e.g. allow to add the site for entities
                        log.info("Entity {} found on more than one Referenced Site" + " -> Representation of Site {} is ignored", rep.getId(), site.getConfiguration().getName());
                    }
                }
            } catch (SiteException e) {
                log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
            }
        } else {
            log.debug(" > Site {} does not support queries", site.getId());
        }
    }
    return new QueryResultListImpl<Entity>(//use the query with results
    queryWithResults != null ? //use the query with results
    queryWithResults : //if not a processed
    processedQuery != null ? //if not a processed
    processedQuery : //else the parsed one
    query, entities, Entity.class);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 4 with SiteException

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

the class SiteManagerImpl method getContent.

@Override
public InputStream getContent(String entityId, String contentType) {
    Collection<Site> sites = getSitesByEntityPrefix(entityId);
    if (sites.isEmpty()) {
        log.info("No Referenced Site registered for Entity {}", entityId);
        log.debug("Registered Prefixes {}", prefixList);
        return null;
    }
    for (Site site : sites) {
        InputStream content;
        try {
            content = site.getContent(entityId, contentType);
            if (content != null) {
                log.debug("Return Content of type {} for Entity {} from referenced site {}", new Object[] { contentType, entityId, site.getConfiguration().getName() });
                return content;
            }
        } catch (SiteException e) {
            log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
        }
    }
    log.debug("Entity {} not found on any of the following Sites {}", entityId, sites);
    return null;
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) InputStream(java.io.InputStream) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 5 with SiteException

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

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