Search in sources :

Example 11 with SiteException

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

the class SiteManagerImpl method find.

@Override
public QueryResultList<Representation> find(FieldQuery query) {
    log.debug("find with query{}", query);
    Set<Representation> representations = new HashSet<Representation>();
    //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<Representation> results = site.find(query);
                if (processedQuery == null) {
                    processedQuery = results.getQuery();
                }
                if (!results.isEmpty() && queryWithResults == null) {
                    processedQuery = results.getQuery();
                }
                for (Representation rep : results) {
                    if (!representations.contains(rep)) {
                        //do not override
                        representations.add(rep);
                    } else {
                        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<Representation>(//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, representations, Representation.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) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 12 with SiteException

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

the class ReferencedSiteRootResource method updateOrCreateEntity.

private Response updateOrCreateEntity(Site site, String id, Map<String, Representation> parsed, String requestMethod, boolean create, boolean update, HttpHeaders headers) {
    long start = System.currentTimeMillis();
    MediaType accepted = getAcceptableMediaType(headers, JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES, MediaType.APPLICATION_JSON_TYPE);
    ManagedSite managedSite;
    if (site instanceof ManagedSite) {
        managedSite = (ManagedSite) site;
    } else {
        ResponseBuilder builder = Response.status(Status.FORBIDDEN).entity(String.format("The Site '%s' is not managed and does not support " + "create/update nor delete operations", site.getId())).header(HttpHeaders.ACCEPT, accepted);
        //addCORSOrigin(servletContext, builder, headers);
        return builder.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) {
            ResponseBuilder builder = 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);
            //addCORSOrigin(servletContext, builder, headers);
            return builder.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
        log.debug("   ... validate parsed Representation state (create: {}| update: {})", create, update);
        for (Entry<String, Representation> entry : parsed.entrySet()) {
            boolean exists;
            try {
                exists = managedSite.getEntity(entry.getKey()) != null;
            } catch (SiteException e) {
                log.error(String.format("Exception while checking the existance " + "of an Entity with id  %s in the Entityhub.", entry.getKey()), e);
                ResponseBuilder builder = 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);
                //addCORSOrigin(servletContext, builder, headers);
                return builder.build();
            }
            if ((exists && !update) || (!exists && !create)) {
                ResponseBuilder builder = Response.status(Status.BAD_REQUEST).entity(String.format("Unable to %s an Entity %s becuase it %s and %s is deactivated. " + " You might want to set the '%s' parameter to TRUE in your Request", exists ? "update" : "create", entry.getKey(), exists ? "does already exists " : "does not", exists ? "updateing existing" : "creating new", exists ? "does already" : "does not exists", exists ? "update" : "create")).header(HttpHeaders.ACCEPT, accepted);
                //addCORSOrigin(servletContext, builder, headers);
                return builder.build();
            }
        }
    }
    long validateCompleted = System.currentTimeMillis();
    log.info("   ... validate request data {}ms", validateCompleted - start);
    try {
        managedSite.store(parsed.values());
    } catch (ManagedSiteException e) {
        log.error(String.format("Exception while storing parsed Representations " + "in the ManagedSite %s", managedSite.getId()), e);
        ResponseBuilder builder = Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unable to store parsed Entities to ManagedSite " + managedSite.getId() + " because of an error (Message: " + e.getMessage() + ")").header(HttpHeaders.ACCEPT, accepted);
        //addCORSOrigin(servletContext, builder, headers);
        return builder.build();
    }
    ResponseBuilder builder;
    if (create && parsed.size() == 1) {
        String createdId = parsed.keySet().iterator().next();
        URI created = uriInfo.getRequestUriBuilder().queryParam("id", createdId).build();
        builder = Response.created(created);
        builder.header(HttpHeaders.ACCEPT, accepted);
    } else {
        builder = Response.noContent();
    }
    log.info("   ... create/update {} entities in {}ms", parsed.size(), System.currentTimeMillis() - validateCompleted);
    //addCORSOrigin(servletContext, builder, headers);
    return builder.build();
}
Also used : ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) 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) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) ManagedSite(org.apache.stanbol.entityhub.servicesapi.site.ManagedSite) URI(java.net.URI)

Example 13 with SiteException

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

the class ReferencedSiteSearcher method get.

@Override
public Representation get(String id, Set<String> includeFields) {
    if (id == null || id.isEmpty()) {
        return null;
    }
    Entity entity;
    Site site = getSearchService();
    if (site == null) {
        throw new IllegalStateException("ReferencedSite " + siteId + " is currently not available");
    }
    try {
        entity = site.getEntity(id);
    } catch (SiteException e) {
        throw new IllegalStateException("Exception while getting " + id + " from the ReferencedSite " + site.getId(), e);
    }
    return entity == null ? null : entity.getRepresentation();
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 14 with SiteException

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

the class ReferencedSiteImpl method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws SiteException {
    List<Entity> results;
    if (siteConfiguration.getCacheStrategy() == CacheStrategy.all) {
        try {
            // When using the Cache, directly get the representations!
            QueryResultList<Representation> representations = cache.findRepresentation((query));
            results = new ArrayList<Entity>(representations.size());
            for (Representation result : representations) {
                Entity entity = new EntityImpl(getId(), result, null);
                results.add(entity);
                initEntityMetadata(entity, siteMetadata, singletonMap(RdfResourceEnum.isChached.getUri(), (Object) Boolean.TRUE));
            }
            return new QueryResultListImpl<Entity>(query, results, Entity.class);
        } catch (YardException e) {
            if (entitySearcher == null) {
                throw new SiteException("Unable to execute query on Cache " + siteConfiguration.getCacheId(), e);
            } else {
                log.warn(String.format("Error while performing query on Cache %s! Try to use remote site %s as fallback!", siteConfiguration.getCacheId(), siteConfiguration.getQueryUri()), e);
            }
        }
    }
    QueryResultList<String> entityIds;
    if (entitySearcher == null) {
        throw new SiteException(String.format("The ReferencedSite %s does not support queries!", getId()));
    }
    try {
        entityIds = entitySearcher.findEntities(query);
    } catch (IOException e) {
        throw new SiteException(String.format("Unable to execute query on remote site %s with entitySearcher %s!", siteConfiguration.getQueryUri(), siteConfiguration.getEntitySearcherType()), e);
    }
    int numResults = entityIds.size();
    List<Entity> entities = new ArrayList<Entity>(numResults);
    int errors = 0;
    SiteException lastError = null;
    for (String id : entityIds) {
        Entity entity;
        try {
            entity = getEntity(id);
            if (entity == null) {
                log.warn("Unable to create Entity for ID that was selected by an FieldQuery (id=" + id + ")");
            }
            entities.add(entity);
            // use the position in the list as resultSocre
            entity.getRepresentation().set(RdfResourceEnum.resultScore.getUri(), Float.valueOf((float) numResults));
        } catch (SiteException e) {
            lastError = e;
            errors++;
            log.warn(String.format("Unable to get Representation for Entity " + "%s. -> %d Error%s for %d Entities in QueryResult (Reason:%s)", id, errors, errors > 1 ? "s" : "", entityIds.size(), e.getMessage()));
        }
        // decrease numResults because it is used as resultScore for
        // entities
        numResults--;
    }
    if (lastError != null) {
        if (entities.isEmpty()) {
            throw new SiteException("Unable to get anly Representations for " + "Entities selected by the parsed Query (Root-Cause is the " + "last Exception trown)", lastError);
        } else {
            log.warn(String.format("Unable to get %d/%d Represetnations for selected Entities.", errors, entityIds.size()));
            log.warn("Stack trace of the last Exception:", lastError);
        }
    }
    return new QueryResultListImpl<Entity>(query, entities, Entity.class);
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ArrayList(java.util.ArrayList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 15 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 Entity> lookup(IRI field, Set<IRI> includeFields, List<String> search, String[] languages, Integer limit, Integer offset) 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");
    }
    queryStats.begin();
    FieldQuery query = EntitySearcherUtils.createFieldQuery(site.getQueryFactory(), field, includeFields, search, languages);
    if (limit != null && limit > 0) {
        query.setLimit(limit);
    } else if (this.limit != null) {
        query.setLimit(this.limit);
    }
    if (offset != null && offset.intValue() > 0) {
        query.setOffset(offset.intValue());
    }
    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);
    }
    queryStats.complete();
    if (!results.isEmpty()) {
        Set<String> languagesSet = new HashSet<String>(Arrays.asList(languages));
        Collection<Entity> entities = new ArrayList<Entity>(results.size());
        for (Representation result : results) {
            resultStats.begin();
            entities.add(new EntityhubEntity(result, null, languagesSet));
            resultStats.complete();
        }
        return entities;
    } else {
        return Collections.emptyList();
    }
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Entity(org.apache.stanbol.enhancer.engines.entitylinking.Entity) ArrayList(java.util.ArrayList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) 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