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