Search in sources :

Example 16 with Site

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

the class ReferencedSiteRootResource method findEntity.

@POST
@Path("/find")
public Response findEntity(@PathParam(value = "site") String siteId, @FormParam(value = "name") String name, @FormParam(value = "field") String parsedField, @FormParam(value = "lang") String language, // @FormParam(value="select") String select,
@FormParam(value = "limit") Integer limit, @FormParam(value = "offset") Integer offset, @FormParam(value = "ldpath") String ldpath, @Context HttpHeaders headers) {
    Site site = getSite(siteId);
    log.debug("site/{}/find Request", site.getId());
    Collection<String> supported = new HashSet<String>(JerseyUtils.QUERY_RESULT_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    final MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, MediaType.APPLICATION_JSON_TYPE);
    if (name == null || name.isEmpty()) {
        if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("find", 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("The name must not be null nor empty for find requests. Missing parameter name.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    final String property;
    if (parsedField == null) {
        property = DEFAULT_FIND_FIELD;
    } else {
        parsedField = parsedField.trim();
        if (parsedField.isEmpty()) {
            property = DEFAULT_FIND_FIELD;
        } else {
            property = nsPrefixService.getFullName(parsedField);
            if (property == null) {
                String messsage = String.format("The prefix '%s' of the parsed field '%' is not " + "mapped to any namespace. Please parse the full URI instead!\n", NamespaceMappingUtils.getPrefix(parsedField), parsedField);
                return Response.status(Status.BAD_REQUEST).entity(messsage).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
            }
        }
    }
    return executeQuery(site, createFieldQueryForFindRequest(name, property, language, limit == null || limit < 1 ? DEFAULT_FIND_RESULT_LIMIT : limit, offset, ldpath), headers);
}
Also used : ManagedSite(org.apache.stanbol.entityhub.servicesapi.site.ManagedSite) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) 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) POST(javax.ws.rs.POST)

Example 17 with Site

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

Example 18 with Site

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

the class SiteManagerImpl method getEntity.

@Override
public Entity getEntity(String entityId) {
    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) {
        Entity entity;
        try {
            entity = site.getEntity(entityId);
            if (entity != null) {
                log.debug("Return Representation of Site {} for Entity {}", site.getConfiguration().getName(), entityId);
                return entity;
            }
        } 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) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 19 with Site

use of org.apache.stanbol.entityhub.servicesapi.site.Site 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 20 with Site

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

the class ReferencedSiteReconcileResource method getSite.

private Site getSite(String siteId) throws WebApplicationException {
    Site site = _siteManager.getSite(siteId);
    if (site == null) {
        String message = String.format("ReferencedSite '%s' not active!", siteId);
        log.error(message);
        throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(message).build());
    }
    return site;
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

Site (org.apache.stanbol.entityhub.servicesapi.site.Site)20 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)12 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)9 HashSet (java.util.HashSet)8 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)6 Path (javax.ws.rs.Path)5 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)5 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)5 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)5 ManagedSite (org.apache.stanbol.entityhub.servicesapi.site.ManagedSite)5 MediaType (javax.ws.rs.core.MediaType)4 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)4 IRI (org.apache.clerezza.commons.rdf.IRI)3 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)3 ArrayList (java.util.ArrayList)2 GET (javax.ws.rs.GET)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Graph (org.apache.clerezza.commons.rdf.Graph)2 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)2 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)2