use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.
the class EntityhubRootResource method getEntityMapping.
@GET
@Path("mapping/entity")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getEntityMapping(@QueryParam("id") String entity, @Context HttpHeaders headers) throws WebApplicationException {
log.debug("getEntityMapping() POST Request > entity: {} > accept: {}", entity, headers.getAcceptableMediaTypes());
Set<String> supported = new HashSet<String>(JerseyUtils.REPRESENTATION_SUPPORTED_MEDIA_TYPES);
supported.add(TEXT_HTML);
MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, APPLICATION_JSON_TYPE);
if (entity == null || entity.isEmpty()) {
//if HTML -> print the docu of the restfull service
if (TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
ResponseBuilder rb = Response.ok(new Viewable("mapping_entity", this));
rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
} else {
return Response.status(Status.BAD_REQUEST).entity("No entity given. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
//Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
Entity mapping;
try {
mapping = entityhub.getMappingBySource(entity);
} catch (EntityhubException e) {
throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
}
if (mapping == null) {
return Response.status(Status.NOT_FOUND).entity("No mapping found for entity '" + entity + "'.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
} else {
ResponseBuilder rb = Response.ok(mapping);
rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
}
use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.
the class EntityhubRootResource method getSymbolMappings.
@GET
@Path("mapping/symbol")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getSymbolMappings(@QueryParam("id") String symbol, @Context HttpHeaders headers) throws WebApplicationException {
log.debug("getSymbolMappings() POST Request > symbol: {} > accept: {}", symbol, headers.getAcceptableMediaTypes());
Set<String> supported = new HashSet<String>(JerseyUtils.REPRESENTATION_SUPPORTED_MEDIA_TYPES);
supported.add(TEXT_HTML);
MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, APPLICATION_JSON_TYPE);
if (symbol == null || symbol.isEmpty()) {
//if HTML -> print the docu of the restfull service
if (TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
ResponseBuilder rb = Response.ok(new Viewable("mapping_symbol", this));
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("No symbol given. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
//Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
Collection<Entity> mappings;
try {
mappings = entityhub.getMappingsByTarget(symbol);
} catch (EntityhubException e) {
throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
}
if (mappings == null || mappings.isEmpty()) {
return Response.status(Status.NOT_FOUND).entity("No mapping found for symbol '" + symbol + "'.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
} else {
// TODO: Implement Support for list of Signs, Representations and Strings
// For now use a pseudo QueryResultList
QueryResultList<Entity> mappingResultList = new QueryResultListImpl<Entity>(null, mappings, Entity.class);
ResponseBuilder rb = Response.ok(mappingResultList);
rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
}
use of org.apache.stanbol.entityhub.servicesapi.EntityhubException 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.servicesapi.EntityhubException 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();
}
use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.
the class EntityhubSearcher method lookup.
@Override
public Collection<? extends Entity> lookup(IRI field, Set<IRI> includeFields, List<String> search, String[] languages, Integer limit, Integer offset) throws EntitySearcherException {
Entityhub entityhub = getSearchService();
if (entityhub == null) {
throw new EntitySearcherException("The Entityhub is currently not active");
}
FieldQuery query = EntitySearcherUtils.createFieldQuery(entityhub.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 = entityhub.find(query);
} catch (EntityhubException e) {
throw new EntitySearcherException("Exception while searchign for " + search + '@' + Arrays.toString(languages) + "in the Entityhub", e);
}
if (!results.isEmpty()) {
Set<String> languagesSet = new HashSet<String>(Arrays.asList(languages));
Collection<Entity> entities = new ArrayList<Entity>(results.size());
for (Representation result : results) {
entities.add(new EntityhubEntity(result, null, languagesSet));
}
return entities;
} else {
return Collections.emptyList();
}
}
Aggregations