Search in sources :

Example 1 with AbstractQueryOutputTransformer

use of nl.nn.adapterframework.jdbc.transformer.AbstractQueryOutputTransformer in project iaf by ibissource.

the class ExecuteJdbcQuery method execute.

@POST
@RolesAllowed({ "IbisTester" })
@Path("/jdbc/query")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response execute(LinkedHashMap<String, Object> json) throws ApiException {
    String datasource = null, resultType = null, query = null, queryType = null, result = "", returnType = MediaType.APPLICATION_XML;
    boolean avoidLocking = false, trimSpaces = false;
    for (Entry<String, Object> entry : json.entrySet()) {
        String key = entry.getKey();
        if (key.equalsIgnoreCase("datasource")) {
            datasource = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("resultType")) {
            resultType = entry.getValue().toString().toLowerCase();
            if (resultType.equalsIgnoreCase("csv")) {
                returnType = MediaType.TEXT_PLAIN;
            }
            if (resultType.equalsIgnoreCase("json")) {
                returnType = MediaType.APPLICATION_JSON;
            }
        }
        if (key.equalsIgnoreCase("avoidLocking")) {
            avoidLocking = Boolean.parseBoolean(entry.getValue().toString());
        }
        if (key.equalsIgnoreCase("trimSpaces")) {
            trimSpaces = Boolean.parseBoolean(entry.getValue().toString());
        }
        if (key.equalsIgnoreCase("query")) {
            query = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("queryType")) {
            queryType = entry.getValue().toString();
        }
    }
    if ("AUTO".equals(queryType)) {
        // defaults to other
        queryType = "other";
        // if it matches, set it to select
        String[] commands = new String[] { "select", "show" };
        for (String command : commands) {
            if (query.toLowerCase().startsWith(command)) {
                queryType = "select";
                break;
            }
        }
    }
    if (datasource == null || resultType == null || query == null) {
        throw new ApiException("Missing data, datasource, resultType and query are expected.", 400);
    }
    secLog.info(String.format("executing query [%s] on datasource [%s] queryType [%s] avoidLocking [%s]", query, datasource, queryType, avoidLocking));
    // We have all info we need, lets execute the query!
    DirectQuerySender qs;
    try {
        qs = (DirectQuerySender) getIbisContext().createBeanAutowireByName(DirectQuerySender.class);
    } catch (Exception e) {
        throw new ApiException("An error occured on creating or closing the connection", e);
    }
    try {
        qs.setName("QuerySender");
        qs.setDatasourceName(datasource);
        qs.setQueryType(queryType);
        qs.setTrimSpaces(trimSpaces);
        qs.setAvoidLocking(avoidLocking);
        qs.setBlobSmartGet(true);
        qs.setPrettyPrint(true);
        qs.configure(true);
        qs.open();
        Message message = qs.sendMessage(new Message(query), null);
        if (resultType.equalsIgnoreCase("csv")) {
            AbstractQueryOutputTransformer filter = new QueryOutputToCSV();
            result = filter.parse(message);
        } else if (resultType.equalsIgnoreCase("json")) {
            AbstractQueryOutputTransformer filter = new QueryOutputToJson();
            result = filter.parse(message);
        } else {
            result = message.asString();
        }
    } catch (Throwable t) {
        throw new ApiException("Error executing query", t);
    } finally {
        qs.close();
    }
    return Response.status(Response.Status.CREATED).type(returnType).entity(result).build();
}
Also used : QueryOutputToJson(nl.nn.adapterframework.jdbc.transformer.QueryOutputToJson) Message(nl.nn.adapterframework.stream.Message) QueryOutputToCSV(nl.nn.adapterframework.jdbc.transformer.QueryOutputToCSV) DirectQuerySender(nl.nn.adapterframework.jdbc.DirectQuerySender) AbstractQueryOutputTransformer(nl.nn.adapterframework.jdbc.transformer.AbstractQueryOutputTransformer) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 DirectQuerySender (nl.nn.adapterframework.jdbc.DirectQuerySender)1 AbstractQueryOutputTransformer (nl.nn.adapterframework.jdbc.transformer.AbstractQueryOutputTransformer)1 QueryOutputToCSV (nl.nn.adapterframework.jdbc.transformer.QueryOutputToCSV)1 QueryOutputToJson (nl.nn.adapterframework.jdbc.transformer.QueryOutputToJson)1 Message (nl.nn.adapterframework.stream.Message)1