Search in sources :

Example 6 with LDPathParseException

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) FileInputStream(java.io.FileInputStream)

Example 7 with LDPathParseException

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;
    }
}
Also used : ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) StringReader(java.io.StringReader) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) DereferenceConfigurationException(org.apache.stanbol.enhancer.engines.dereference.DereferenceConfigurationException) HashSet(java.util.HashSet)

Example 8 with LDPathParseException

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();
}
Also used : IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) MediaType(javax.ws.rs.core.MediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)8 HashSet (java.util.HashSet)5 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)5 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)4 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)4 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)4 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)3 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)3 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)2 File (java.io.File)1 LinkedHashSet (java.util.LinkedHashSet)1 MediaType (javax.ws.rs.core.MediaType)1