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);
}
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();
}
}
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;
}
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);
}
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;
}
Aggregations