Search in sources :

Example 81 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class BrowseJdbcTable method execute.

@POST
@RolesAllowed({ "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/jdbc/browse")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response execute(LinkedHashMap<String, Object> json) throws ApiException {
    String datasource = null, tableName = null, where = "", order = "";
    Boolean numberOfRowsOnly = false;
    int minRow = 1, maxRow = 100;
    for (Entry<String, Object> entry : json.entrySet()) {
        String key = entry.getKey();
        if (key.equalsIgnoreCase("datasource")) {
            datasource = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("table")) {
            tableName = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("where")) {
            where = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("order")) {
            order = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("numberOfRowsOnly")) {
            numberOfRowsOnly = Boolean.parseBoolean(entry.getValue().toString());
        }
        if (key.equalsIgnoreCase("minRow")) {
            if (entry.getValue() != "") {
                minRow = Integer.parseInt(entry.getValue().toString());
                minRow = Math.max(minRow, 0);
            }
        }
        if (key.equalsIgnoreCase("maxRow")) {
            if (entry.getValue() != "") {
                maxRow = Integer.parseInt(entry.getValue().toString());
                maxRow = Math.max(maxRow, 1);
            }
        }
    }
    if (datasource == null || tableName == null) {
        throw new ApiException("datasource and/or tableName not defined.", 400);
    }
    if (maxRow < minRow)
        throw new ApiException("Rownum max must be greater than or equal to Rownum min", 400);
    if (maxRow - minRow >= 100) {
        throw new ApiException("Difference between Rownum max and Rownum min must be less than hundred", 400);
    }
    if (!readAllowed(permissionRules, tableName))
        throw new ApiException("Access to table (" + tableName + ") not allowed", 400);
    // We have all info we need, lets execute the query!
    Map<String, Object> fieldDef = new LinkedHashMap<>();
    String result = "";
    String query = null;
    DirectQuerySender qs;
    try {
        qs = 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("select");
        qs.setSqlDialect("Oracle");
        qs.setBlobSmartGet(true);
        qs.setIncludeFieldDefinition(true);
        qs.configure(true);
        qs.open();
        try (Connection conn = qs.getConnection()) {
            ResultSet rs = null;
            try {
                rs = conn.getMetaData().getColumns(null, null, tableName, null);
                if (!rs.isBeforeFirst()) {
                    rs.close();
                    rs = conn.getMetaData().getColumns(null, null, tableName.toUpperCase(), null);
                }
                StringBuilder fielddefinition = new StringBuilder("<fielddefinition>");
                String field = null;
                if (!numberOfRowsOnly) {
                    field = "<field name=\"" + rnumColumnName + "\" type=\"INTEGER\" />";
                    fielddefinition.append(field);
                    fieldDef.put(rnumColumnName, "INTEGER");
                    while (rs.next()) {
                        field = "<field name=\"" + rs.getString(COLUMN_NAME) + "\" type=\"" + DB2XMLWriter.getFieldType(rs.getInt(DATA_TYPE)) + "\" size=\"" + rs.getInt(COLUMN_SIZE) + "\"/>";
                        fielddefinition.append(field);
                        fieldDef.put(rs.getString(COLUMN_NAME), DB2XMLWriter.getFieldType(rs.getInt(DATA_TYPE)) + "(" + rs.getInt(COLUMN_SIZE) + ")");
                    }
                } else {
                    field = "<field name=\"" + countColumnName + "\" type=\"INTEGER\" />";
                    fielddefinition.append(field);
                    fieldDef.put(countColumnName, "INTEGER");
                    if (StringUtils.isNotEmpty(order)) {
                        rs = conn.getMetaData().getColumns(null, null, tableName, order);
                        while (rs.next()) {
                            field = "<field name=\"" + rs.getString(COLUMN_NAME) + "\" type=\"" + DB2XMLWriter.getFieldType(rs.getInt(DATA_TYPE)) + "\" size=\"" + rs.getInt(COLUMN_SIZE) + "\"/>";
                            fielddefinition.append(field);
                            fieldDef.put(rs.getString(COLUMN_NAME), DB2XMLWriter.getFieldType(rs.getInt(DATA_TYPE)) + "(" + rs.getInt(COLUMN_SIZE) + ")");
                        }
                    }
                }
                fielddefinition.append("</fielddefinition>");
                String browseJdbcTableExecuteREQ = "<browseJdbcTableExecuteREQ>" + "<dbmsName>" + qs.getDbmsSupport().getDbmsName() + "</dbmsName>" + "<countColumnName>" + countColumnName + "</countColumnName>" + "<rnumColumnName>" + rnumColumnName + "</rnumColumnName>" + "<tableName>" + tableName + "</tableName>" + "<where>" + XmlUtils.encodeChars(where) + "</where>" + "<numberOfRowsOnly>" + numberOfRowsOnly + "</numberOfRowsOnly>" + "<order>" + order + "</order>" + "<rownumMin>" + minRow + "</rownumMin>" + "<rownumMax>" + maxRow + "</rownumMax>" + fielddefinition + "<maxColumnSize>1000</maxColumnSize>" + "</browseJdbcTableExecuteREQ>";
                URL url = ClassUtils.getResourceURL(DB2XML_XSLT);
                if (url != null) {
                    Transformer t = XmlUtils.createTransformer(url);
                    query = XmlUtils.transformXml(t, browseJdbcTableExecuteREQ);
                }
                result = qs.sendMessage(new Message(query), null).asString();
            } finally {
                if (rs != null) {
                    rs.close();
                }
            }
        }
    } catch (Throwable t) {
        throw new ApiException("An error occured on executing jdbc query [" + query + "]", t);
    } finally {
        qs.close();
    }
    List<Map<String, String>> resultMap = null;
    if (XmlUtils.isWellFormed(result)) {
        try {
            resultMap = new QueryOutputToListOfMaps().parseString(result);
        } catch (IOException | SAXException e) {
            throw new ApiException("Query result could not be parsed.", e);
        }
    }
    if (resultMap == null)
        throw new ApiException("Invalid query result [null].", 400);
    Map<String, Object> resultObject = new HashMap<String, Object>();
    resultObject.put("table", tableName);
    resultObject.put("query", XmlUtils.encodeChars(query));
    resultObject.put("fielddefinition", fieldDef);
    resultObject.put("result", resultMap);
    return Response.status(Response.Status.CREATED).entity(resultObject).build();
}
Also used : Transformer(javax.xml.transform.Transformer) Message(nl.nn.adapterframework.stream.Message) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Connection(java.sql.Connection) DirectQuerySender(nl.nn.adapterframework.jdbc.DirectQuerySender) QueryOutputToListOfMaps(nl.nn.adapterframework.jdbc.transformer.QueryOutputToListOfMaps) IOException(java.io.IOException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) SAXException(org.xml.sax.SAXException) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) 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)

Example 82 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class PipeLineSessionTest method testMessage.

@Test
public void testMessage() throws Exception {
    Message message1 = session.getMessage("key1");
    Message message2 = session.getMessage("key2");
    Message message3 = session.getMessage("key3");
    Message message4 = session.getMessage("key4");
    Message message5 = session.getMessage("doenst-exist");
    assertEquals("test1", message1.asString());
    assertEquals("test2", message2.asString());
    assertEquals("test3", message3.asString());
    assertEquals("test4", message4.asString());
    assertTrue(message5.isEmpty());
}
Also used : Message(nl.nn.adapterframework.stream.Message) Test(org.junit.Test)

Example 83 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class PipeLineTest method testDirectWrapperPipeSuccessForward.

// Should add tests to assertThat(configuration.getConfigWarning(0), StringEndsWith.endsWith("] has no pipe forwards defined"));
@Test
public void testDirectWrapperPipeSuccessForward() throws ConfigurationException, PipeRunException {
    TestConfiguration configuration = new TestConfiguration();
    PipeLine pipeline = configuration.createBean(PipeLine.class);
    PipeForward pf = configuration.createBean(PipeForward.class);
    pf.setName("success");
    pf.setPath("nextPipe");
    PipeForward toExit = configuration.createBean(PipeForward.class);
    toExit.setName("success");
    toExit.setPath("EXIT");
    DirectWrapperPipe pipe = configuration.createBean(DirectWrapperPipe.class);
    pipe.setName("DirectWrapperPipe");
    pipe.registerForward(pf);
    pipeline.addPipe(pipe);
    EchoPipe echoPipe = configuration.createBean(EchoPipe.class);
    echoPipe.setName("nextPipe");
    echoPipe.setPipeLine(pipeline);
    echoPipe.registerForward(toExit);
    pipeline.addPipe(echoPipe);
    PipeLineExit exit = configuration.createBean(PipeLineExit.class);
    exit.setPath("exit");
    exit.setState(ExitState.SUCCESS);
    pipeline.registerPipeLineExit(exit);
    pipeline.setOwner(pipe);
    pipeline.configure();
    CorePipeProcessor cpp = configuration.createBean(CorePipeProcessor.class);
    PipeLineSession ps = configuration.createBean(PipeLineSession.class);
    PipeRunResult pipeRunResult = cpp.processPipe(pipeline, pipe, new Message("<dummy/>"), ps);
    PipeForward pipeForward = pipeRunResult.getPipeForward();
    IForwardTarget target = pipeline.resolveForward(pipe, pipeForward);
    assertNotNull(target);
    configuration.close();
    configuration = null;
}
Also used : Message(nl.nn.adapterframework.stream.Message) EchoPipe(nl.nn.adapterframework.pipes.EchoPipe) CorePipeProcessor(nl.nn.adapterframework.processors.CorePipeProcessor) TestConfiguration(nl.nn.adapterframework.testutil.TestConfiguration) DirectWrapperPipe(nl.nn.adapterframework.extensions.esb.DirectWrapperPipe) Test(org.junit.Test)

Example 84 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class FileSystemActorExtraTest method fileSystemActorAppendActionWithDailyRollover.

@Test
public void fileSystemActorAppendActionWithDailyRollover() throws Exception {
    String filename = "rolloverDaily" + FILE1;
    String contents = "thanos car ";
    int numOfBackups = 12;
    int numOfWrites = 10;
    Date currentDate = new Date();
    Date firstDate;
    long millisPerDay = 1000L * 60L * 60L * 24L;
    if (_fileExists(filename)) {
        _deleteFile(null, filename);
    }
    createFile(null, filename, "thanos car ");
    setFileDate(null, filename, firstDate = new Date(currentDate.getTime() - (millisPerDay * numOfWrites)));
    PipeLineSession session = new PipeLineSession();
    ParameterList params = new ParameterList();
    params.add(ParameterBuilder.create().withName("contents").withSessionKey("appendActionwString"));
    params.configure();
    actor.setAction(FileSystemAction.APPEND);
    actor.setRotateDays(numOfBackups);
    actor.configure(fileSystem, params, owner);
    actor.open();
    Message message = new Message(filename);
    for (int i = 0; i < numOfWrites; i++) {
        setFileDate(null, filename, new Date(firstDate.getTime() + (millisPerDay * i)));
        session.put("appendActionwString", contents + i);
        ParameterValueList pvl = params.getValues(message, session);
        String result = (String) actor.doAction(message, pvl, null);
        TestAssertions.assertXpathValueEquals(filename, result, "file/@name");
    }
    for (int i = 1; i <= numOfWrites - 1; i++) {
        String formattedDate = DateUtils.format(new Date(firstDate.getTime() + (millisPerDay * i)), DateUtils.shortIsoFormat);
        String actualContentsi = readFile(null, filename + "." + formattedDate);
        assertEquals((contents + (i - 1)).trim(), actualContentsi.trim());
    }
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Message(nl.nn.adapterframework.stream.Message) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) ParameterList(nl.nn.adapterframework.parameters.ParameterList) Date(java.util.Date) Test(org.junit.Test)

Example 85 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class FileSystemPipeTest method fileSystemPipeMkdirActionTest.

// @Test
// public void fileSystemPipeMoveActionTestFolderToRoot() throws Exception {
// fileSystemPipeMoveActionTest("folder",null);
// }
// @Test
// public void fileSystemPipeMoveActionTestFolderToFolder() throws Exception {
// fileSystemPipeMoveActionTest("folder1","folder2");
// }
@Test
public void fileSystemPipeMkdirActionTest() throws Exception {
    String folder = "mkdir" + DIR1;
    if (_folderExists(folder)) {
        _deleteFolder(folder);
    }
    fileSystemPipe.setAction(FileSystemAction.MKDIR);
    fileSystemPipe.configure();
    fileSystemPipe.start();
    Message message = new Message(folder);
    PipeRunResult prr = fileSystemPipe.doPipe(message, null);
    String result = prr.getResult().asString();
    waitForActionToFinish();
    // test
    boolean actual = _folderExists(folder);
    // test
    assertEquals("result of pipe should be name of created folder", folder, result);
    assertTrue("Expected folder [" + folder + "] to be present", actual);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Message(nl.nn.adapterframework.stream.Message) Test(org.junit.Test)

Aggregations

Message (nl.nn.adapterframework.stream.Message)598 Test (org.junit.Test)385 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)220 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)114 IOException (java.io.IOException)112 SenderException (nl.nn.adapterframework.core.SenderException)97 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)54 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)54 Parameter (nl.nn.adapterframework.parameters.Parameter)52 PipeForward (nl.nn.adapterframework.core.PipeForward)41 Date (java.util.Date)37 TimeoutException (nl.nn.adapterframework.core.TimeoutException)31 UrlMessage (nl.nn.adapterframework.stream.UrlMessage)31 PipeRunException (nl.nn.adapterframework.core.PipeRunException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)29 InputStream (java.io.InputStream)29 ParameterList (nl.nn.adapterframework.parameters.ParameterList)28 ListenerException (nl.nn.adapterframework.core.ListenerException)27 ParameterException (nl.nn.adapterframework.core.ParameterException)25 SAXException (org.xml.sax.SAXException)19