Search in sources :

Example 1 with LDPathParseException

use of org.apache.marmotta.ldpath.exception.LDPathParseException in project stanbol by apache.

the class TrackingDereferencerBase method setLdPath.

/**
     * Setter for the LDPath program used for dereferencing Entities
     * @param ldpathProgramStr the LDPath program as String
     * @throws ConfigurationException if parsing the LDPath program fails
     */
public void setLdPath(String ldpathProgramStr) throws ConfigurationException {
    if (ldpathProgramStr == null || StringUtils.isBlank(ldpathProgramStr)) {
        ldpathProgram = null;
    } else {
        //validate the parsed LDPath program
        //when this method is called the real RDFBackend will not be available.
        //however we would like to parse/validate the parsed LDPath program
        //So we will create a pseudo RDFBackend sufficient to be used with the
        //parser
        final RDFBackend<Object> parseBackend = new ParseBackend<T>(valueFactory);
        //NOTE: calling execute(..) an this parseLdPath or even the 
        //ldpathProgram will result in UnsupportedOperationException
        //but parsing is OK
        EntityhubLDPath parseLdPath = new EntityhubLDPath(parseBackend, valueFactory);
        try {
            ldpathProgram = parseLdPath.parseProgram(new StringReader(ldpathProgramStr));
        } catch (LDPathParseException e) {
            log.error("Unable to parse LDPath pogram: \n {}", ldpathProgramStr);
            throw new ConfigurationException(DereferenceConstants.DEREFERENCE_ENTITIES_LDPATH, "Unable to parse configured LDPath program ", e);
        }
        //finally validate if all mappings of the programm do use a URI as key
        for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : ldpathProgram.getFields()) {
            try {
                new URI(mapping.getFieldName());
            } catch (URISyntaxException e) {
                throw new ConfigurationException(DereferenceConstants.DEREFERENCE_ENTITIES_LDPATH, "Parsed LDPath MUST use valid URIs as field names (invalid field name: '" + mapping.getFieldName() + "' | selector: '" + mapping.getSelector().getPathExpression(parseBackend) + "')!");
            }
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ConfigurationException(org.osgi.service.cm.ConfigurationException) StringReader(java.io.StringReader) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)

Example 2 with LDPathParseException

use of org.apache.marmotta.ldpath.exception.LDPathParseException 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();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SiteManagerBackend(org.apache.stanbol.entityhub.ldpath.backend.SiteManagerBackend) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Example 3 with LDPathParseException

use of org.apache.marmotta.ldpath.exception.LDPathParseException 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();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) SiteBackend(org.apache.stanbol.entityhub.ldpath.backend.SiteBackend) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Example 4 with LDPathParseException

use of org.apache.marmotta.ldpath.exception.LDPathParseException 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();
}
Also used : EntityhubBackend(org.apache.stanbol.entityhub.ldpath.backend.EntityhubBackend) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Example 5 with LDPathParseException

use of org.apache.marmotta.ldpath.exception.LDPathParseException in project stanbol by apache.

the class LdpathProcessor method setConfiguration.

@Override
public void setConfiguration(Map<String, Object> config) {
    indexingConfig = (IndexingConfig) config.get(IndexingConfig.KEY_INDEXING_CONFIG);
    //parse the ldpath
    final File ldpathFile;
    Object value = config.get(PARAMETER_LD_PATH);
    if (value != null && !value.toString().isEmpty()) {
        ldpathFile = indexingConfig.getConfigFile(value.toString());
        if (ldpathFile == null || !ldpathFile.exists()) {
            throw new IllegalArgumentException("Configured '" + PARAMETER_LD_PATH + "' file was not found!");
        }
        if (!ldpathFile.isFile()) {
            throw new IllegalArgumentException("Configured '" + PARAMETER_LD_PATH + "' file exists but is not a File!");
        }
    } else {
        throw new IllegalArgumentException("Missing required configuration '" + PARAMETER_LD_PATH + "' - the file containing the LDPath program used by this " + LdpathProcessor.class.getSimpleName() + "!");
    }
    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);
    }
    value = config.get(PARAMETER_APPEND);
    if (value instanceof Boolean) {
        this.appendMode = ((Boolean) value).booleanValue();
    } else if (value != null && !value.toString().isEmpty()) {
        this.appendMode = Boolean.parseBoolean(value.toString());
    } else {
        this.appendMode = DEFAULT_APPEND_MODE;
    }
}
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) File(java.io.File) FileInputStream(java.io.FileInputStream)

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