use of org.apache.stanbol.entityhub.ldpath.query.LDPathSelect in project stanbol by apache.
the class SiteManagerRootResource 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(SiteManager manager, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
QueryResultList<Representation> result;
ValueFactory vf = new RdfValueFactory(new IndexedGraph());
SiteManagerBackend backend = new SiteManagerBackend(manager);
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 a Query to the '/sites' endpoint:");
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 the Query " + "parsed to the '/sites' endpoint!", e);
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
}
//2. execute the query
// we need to adapt from Entity to Representation
//TODO: should we add the metadata to the result?
Iterator<Representation> resultIt = new AdaptingIterator<Entity, Representation>(manager.findEntities(query).iterator(), new AdaptingIterator.Adapter<Entity, Representation>() {
@Override
public Representation adapt(Entity value, Class<Representation> type) {
return value.getRepresentation();
}
}, Representation.class);
//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();
}
use of org.apache.stanbol.entityhub.ldpath.query.LDPathSelect 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();
}
use of org.apache.stanbol.entityhub.ldpath.query.LDPathSelect in project stanbol by apache.
the class SiteManagerRootResource method executeQuery.
/**
* Executes the query parsed by {@link #queryEntities(String, File, HttpHeaders)} or created based
* {@link #findEntity(String, String, String, int, int, HttpHeaders)}
*
* @param manager The {@link SiteManager}
* @param query
* The query to execute
* @param headers the request headers
* @return the response (results of error)
*/
private Response executeQuery(SiteManager manager, FieldQuery query, MediaType mediaType, HttpHeaders headers) throws WebApplicationException {
if (query instanceof LDPathSelect && ((LDPathSelect) query).getLDPathSelect() != null) {
//use the LDPath variant to process this query
return executeLDPathQuery(manager, query, ((LDPathSelect) query).getLDPathSelect(), mediaType, headers);
} else {
//use the default query execution
QueryResultList<Representation> result = manager.find(query);
ResponseBuilder rb = Response.ok(result);
rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
}
use of org.apache.stanbol.entityhub.ldpath.query.LDPathSelect in project stanbol by apache.
the class EntityhubRootResource method executeQuery.
/**
* Executes the query parsed by {@link #queryEntities(String, File, HttpHeaders)}
* or created based {@link #findEntity(String, String, String, String, HttpHeaders)
* @param query The query to execute
* @param headers The headers used to determine the media types
* @return the response (results of error)
*/
private Response executeQuery(FieldQuery query, HttpHeaders headers, MediaType acceptedMediaType) throws WebApplicationException {
//Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
if (query instanceof LDPathSelect && ((LDPathSelect) query).getLDPathSelect() != null) {
//use the LDPath variant to process this query
return executeLDPathQuery(entityhub, query, ((LDPathSelect) query).getLDPathSelect(), acceptedMediaType, headers);
} else {
//use the default query execution
QueryResultList<Representation> result;
try {
result = entityhub.find(query);
} catch (EntityhubException e) {
String message = String.format("Exception while performing the " + "FieldQuery on the EntityHub (message: %s)", e.getMessage());
log.error(message, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
ResponseBuilder rb = Response.ok(result);
rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
}
use of org.apache.stanbol.entityhub.ldpath.query.LDPathSelect in project stanbol by apache.
the class EntityhubRootResource 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(Entityhub entityhub, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
QueryResultList<Representation> result;
ValueFactory vf = new RdfValueFactory(new IndexedGraph());
EntityhubBackend backend = new EntityhubBackend(entityhub);
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 {
// go directly to the yard and query there for Representations
resultIt = entityhub.getYard().findRepresentation(query).iterator();
} catch (EntityhubException e) {
String message = String.format("Exception while performing the " + "FieldQuery on the EntityHub (message: %s)", 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