Skip to content

Writing a Query Result Modifier

Pradeeban Kathiravelu edited this page Aug 24, 2018 · 1 revision

Writing a simple Query Result Modifier

Follow the documentation for creating a Data Provider and create a new plugin project.

Edit MANIFEST entries

After creating a brand new project, $PROJECT_HOME/META-INF/MANIFEST.MF must be modified to import packages that are required by this project to develop a QRM. These packages are as follows.

 

Import-Package: com.google.gson;version="2.1.0",

 com.google.gson.annotations;version="2.1.0",

 com.google.gson.internal;version="2.1.0",

 com.google.gson.internal.bind;version="2.1.0",

 com.google.gson.reflect;version="2.1.0",

 com.google.gson.stream;version="2.1.0",

 edu.emory.cci.bindaas.framework.api,

 edu.emory.cci.bindaas.framework.event,

 edu.emory.cci.bindaas.framework.event.listeners,

 edu.emory.cci.bindaas.framework.model,

 edu.emory.cci.bindaas.framework.provider.exception,

 edu.emory.cci.bindaas.framework.util,

 org.apache.commons.logging;version="1.1.1",

 org.osgi.framework;version="1.3.0"

 

One more entry needs to be added in order to tell Bindaas where to pick-up spring configuration files :

 

Spring-Context: META-INF/spring/*.xml

Writing LoggingQRM

public class LoggingQRM implements IQueryResultModifier{
    private Log log = LogFactory.getLog(getClass());
    @Override
    public JsonObject getDocumentation() {
        return new JsonObject();
    }

    @Override
    public void validate() throws ModifierException {
    }

    @Override
    public String getDescriptiveName() {
        return "Plugin for logging query results";
    }

    @Override
    public QueryResult modifyQueryResult(QueryResult queryResult,JsonObject dataSource, RequestContext requestContext,JsonObject modifierProperties, Map<String, String> queryParams) throws AbstractHttpCodeException {
        String data;
        try {
            // intercepting the response and logging it
            data = IOUtils.toString(queryResult.getData());
            log.info("Request received from [" + requestContext.getUser() + "]");
            log.info("Response [" + data + "]");
            // overriding the response by a custom message
            queryResult.setMimeType(StandardMimeType.TEXT.toString());
            queryResult.setData(new ByteArrayInputStream(new String("Response from the QueryHandler was consumed by LoggingQRM").getBytes()));
            return queryResult;
        } catch (IOException e) {
            log.error("Execution of LoggingQRM failed");
            throw new QueryExecutionFailedException(getClass().getName(), 1);
        }
    }
}

Clone this wiki locally