use of org.apache.marmotta.ldpath.exception.LDPathParseException in project stanbol by apache.
the class LdpathSourceProcessor method parseLdPathProgram.
/**
*
*/
@SuppressWarnings("unchecked")
private void parseLdPathProgram(File ldpathFile) {
Reader in = null;
try {
in = new InputStreamReader(new FileInputStream(ldpathFile), Charset.forName("UTF-8"));
this.program = ldPath.parseProgram(in);
log.info("ldpath program: \n{}\n", program.getPathExpression(backend));
} catch (IOException e) {
throw new IllegalStateException("Unabwle to read LDPath program from configured file '" + ldpathFile + "'!", e);
} catch (LDPathParseException e) {
throw new IllegalStateException("Unable to parse LDPath program from configured file '" + ldpathFile + "'!", e);
} finally {
IOUtils.closeQuietly(in);
}
}
use of org.apache.marmotta.ldpath.exception.LDPathParseException in project stanbol by apache.
the class EntityhubDereferenceContext method initLdPath.
protected void initLdPath(String program) throws DereferenceConfigurationException {
TrackingDereferencerBase<?> dereferencer = getEntityhubDereferencer();
ValueFactory valueFactory = dereferencer.getValueFactory();
Program<Object> ldpathProgram;
if (!StringUtils.isBlank(program)) {
@SuppressWarnings("rawtypes") RDFBackend<Object> parseBackend = new ParseBackend<Object>(valueFactory);
EntityhubLDPath parseLdPath = new EntityhubLDPath(parseBackend, valueFactory);
try {
ldpathProgram = parseLdPath.parseProgram(new StringReader(program));
} catch (LDPathParseException e) {
log.error("Unable to parse Context LDPath pogram: \n {}", program);
throw new DereferenceConfigurationException("Unable to parse context LDPath program !", e, dereferencer.getClass(), DEREFERENCE_ENTITIES_LDPATH);
}
//finally validate if all mappings of the program do use a URI as key
//also store used fieldNames as we need them later
Set<String> contextFields = new HashSet<String>();
for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : ldpathProgram.getFields()) {
try {
new URI(mapping.getFieldName());
contextFields.add(mapping.getFieldName());
} catch (URISyntaxException e) {
throw new DereferenceConfigurationException("Parsed LDPath MUST use valid URIs as field names (invalid field name: '" + mapping.getFieldName() + "' | selector: '" + mapping.getSelector().getPathExpression(parseBackend) + "')!", dereferencer.getClass(), DereferenceConstants.DEREFERENCE_ENTITIES_LDPATH);
}
}
//append the mappings configured for the engine
if (dereferencer.getLdPathProgram() != null) {
for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : dereferencer.getLdPathProgram().getFields()) {
if (!contextFields.contains(mapping.getFieldName())) {
ldpathProgram.addMapping(mapping);
}
//else ignore mappings for fields specified in the context
}
}
} else {
//no context specific - use the one of the config
ldpathProgram = dereferencer.getLdPathProgram();
}
if (ldpathProgram != null && !ldpathProgram.getFields().isEmpty()) {
this.ldpathProgram = ldpathProgram;
} else {
this.ldpathProgram = null;
}
}
use of org.apache.marmotta.ldpath.exception.LDPathParseException in project stanbol by apache.
the class LDPathHelper method handleLDPathRequest.
/**
* Processes LDPath requests as supported by the {@link SiteManagerRootResource},
* {@link ReferencedSiteRootResource}, {@link EntityhubRootResource}.
* @param resource The resource used as context when sending RESTful Service API
* {@link Viewable} as response entity.
* @param backend The {@link RDFBackend} implementation
* @param ldpath the parsed LDPath program
* @param contexts the Entities to execute the LDPath program
* @param headers the parsed HTTP headers (used to determine the accepted
* content type for the response
* @param servletContext The Servlet context needed for CORS support
* @return the Response {@link Status#BAD_REQUEST} or {@link Status#OK}.
*/
public static Response handleLDPathRequest(BaseStanbolResource resource, RDFBackend<Object> backend, String ldpath, Set<String> contexts, HttpHeaders headers) {
Collection<String> supported = new HashSet<String>(JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES);
supported.add(TEXT_HTML);
final MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, MediaType.APPLICATION_JSON_TYPE);
boolean printDocu = false;
//remove null and "" element
contexts.remove(null);
contexts.remove("");
if (contexts == null || contexts.isEmpty()) {
if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
printDocu = true;
} else {
return Response.status(Status.BAD_REQUEST).entity("No context was provided by the Request. Missing parameter context.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
if (!printDocu & (ldpath == null || ldpath.isEmpty())) {
if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
printDocu = true;
} else {
return Response.status(Status.BAD_REQUEST).entity("No ldpath program was provided by the Request. Missing or empty parameter ldpath.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
if (printDocu) {
//a missing parameter and the content type is compatible to HTML
ResponseBuilder rb = Response.ok(new Viewable("ldpath", resource));
rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
} else if (acceptedMediaType.equals(TEXT_HTML_TYPE)) {
//HTML is only supported for documentation
return Response.status(Status.NOT_ACCEPTABLE).entity("The requested content type " + TEXT_HTML + " is not supported.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
Graph data;
try {
data = executeLDPath(backend, ldpath, contexts);
} catch (LDPathParseException e) {
log.warn("Unable to parse LDPath program:\n" + ldpath, e);
return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
ResponseBuilder rb = Response.ok(data);
rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
Aggregations