You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ab...@apache.org on 2019/07/03 22:45:54 UTC

[nifi-minifi-cpp] branch master updated (acc6da8 -> a49fe09)

This is an automated email from the ASF dual-hosted git repository.

aboda pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git.


    from acc6da8  MINIFICPP-937: resolve state file issues and rollover issues. Add test coverage
     new 77f3c98  MINIFICPP-925 - Hex encoder and decoder
     new a49fe09  MINIFICPP-925 - Fix TailFile hang on long lines

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 extensions/bootstrap/docs/generatec2docs.h         |  11 +-
 extensions/pcap/CapturePacket.cpp                  |   4 +-
 extensions/pcap/CapturePacket.h                    |   2 +-
 extensions/sftp/client/SFTPClient.cpp              |   9 +-
 .../standard-processors/processors/HashContent.h   |  16 +-
 .../processors/LogAttribute.cpp                    |  27 +-
 .../standard-processors/processors/LogAttribute.h  |  32 +--
 .../standard-processors/processors/TailFile.cpp    |   5 +-
 .../tests/integration/TailFileCronTest.cpp         |   2 +-
 .../tests/integration/TailFileTest.cpp             |   2 +-
 .../tests/unit/TailFileTests.cpp                   | 132 +++++++++-
 libminifi/CMakeLists.txt                           |   2 +-
 .../include/{io/Sockets.h => core/Deprecated.h}    |  21 +-
 libminifi/include/core/ProcessSession.h            |   4 +-
 libminifi/include/utils/StringUtils.h              | 281 +++++++++++----------
 libminifi/src/core/ProcessSession.cpp              | 147 ++++++-----
 libminifi/src/utils/StringUtils.cpp                | 162 ++++++++++++
 libminifi/test/pcap-tests/CMakeLists.txt           |   8 +-
 libminifi/test/pcap-tests/PcapTest.cpp             |  23 +-
 libminifi/test/unit/StringUtilsTests.cpp           |  61 +++++
 20 files changed, 656 insertions(+), 295 deletions(-)
 copy libminifi/include/{io/Sockets.h => core/Deprecated.h} (71%)
 create mode 100644 libminifi/src/utils/StringUtils.cpp


[nifi-minifi-cpp] 02/02: MINIFICPP-925 - Fix TailFile hang on long lines

Posted by ab...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aboda pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git

commit a49fe09c12efee4ca9c5db37dc8aca80dbf7eafc
Author: Daniel Bakai <ba...@gmail.com>
AuthorDate: Sun Jun 30 19:16:21 2019 +0200

    MINIFICPP-925 - Fix TailFile hang on long lines
    
    Signed-off-by: Arpad Boda <ab...@apache.org>
    
    This closes #596
---
 .../processors/LogAttribute.cpp                    |  27 ++--
 .../standard-processors/processors/LogAttribute.h  |  32 ++---
 .../standard-processors/processors/TailFile.cpp    |   5 +-
 .../tests/integration/TailFileCronTest.cpp         |   2 +-
 .../tests/integration/TailFileTest.cpp             |   2 +-
 .../tests/unit/TailFileTests.cpp                   | 132 +++++++++++++++++-
 libminifi/CMakeLists.txt                           |   2 +-
 libminifi/include/core/Deprecated.h                |  29 ++++
 libminifi/include/core/ProcessSession.h            |   4 +-
 libminifi/src/core/ProcessSession.cpp              | 147 ++++++++++++---------
 10 files changed, 276 insertions(+), 106 deletions(-)

diff --git a/extensions/standard-processors/processors/LogAttribute.cpp b/extensions/standard-processors/processors/LogAttribute.cpp
index 6ffa397..cc44f25 100644
--- a/extensions/standard-processors/processors/LogAttribute.cpp
+++ b/extensions/standard-processors/processors/LogAttribute.cpp
@@ -135,35 +135,34 @@ void LogAttribute::onTrigger(const std::shared_ptr<core::ProcessContext> &contex
     }
     if (logPayload && flow->getSize() <= 1024 * 1024) {
       message << "\n" << "Payload:" << "\n";
-      ReadCallback callback(flow->getSize());
+      ReadCallback callback(logger_, flow->getSize());
       session->read(flow, &callback);
-      for (unsigned int i = 0, j = 0; i < callback.read_size_; i++) {
-        message << std::hex << callback.buffer_[i];
-        j++;
-        if (j == 80) {
-          message << '\n';
-          j = 0;
-        }
+
+      auto payload_hex = utils::StringUtils::to_hex(callback.buffer_.data(), callback.buffer_.size());
+      for (size_t i = 0; i < payload_hex.size(); i += 80) {
+        message << payload_hex.substr(i, 80) << '\n';
       }
+    } else {
+      message << "\n";
     }
-    message << "\n" << dashLine;
+    message << dashLine;
     std::string output = message.str();
 
     switch (level) {
       case LogAttrLevelInfo:
-        logger_->log_info("%s", output);
+        logging::LOG_INFO(logger_) << output;
         break;
       case LogAttrLevelDebug:
-        logger_->log_debug("%s", output);
+        logging::LOG_DEBUG(logger_) << output;
         break;
       case LogAttrLevelError:
-        logger_->log_error("%s", output);
+        logging::LOG_ERROR(logger_) << output;
         break;
       case LogAttrLevelTrace:
-        logger_->log_trace("%s", output);
+        logging::LOG_TRACE(logger_) << output;
         break;
       case LogAttrLevelWarn:
-        logger_->log_warn("%s", output);
+        logging::LOG_WARN(logger_) << output;
         break;
       default:
         break;
diff --git a/extensions/standard-processors/processors/LogAttribute.h b/extensions/standard-processors/processors/LogAttribute.h
index 5d03e11..a8f00eb 100644
--- a/extensions/standard-processors/processors/LogAttribute.h
+++ b/extensions/standard-processors/processors/LogAttribute.h
@@ -89,27 +89,23 @@ class LogAttribute : public core::Processor {
   // Nest Callback Class for read stream
   class ReadCallback : public InputStreamCallback {
    public:
-    ReadCallback(uint64_t size)
-        : read_size_(0) {
-      buffer_size_ = size;
-      buffer_ = new uint8_t[buffer_size_];
-    }
-    ~ReadCallback() {
-      if (buffer_)
-        delete[] buffer_;
+    ReadCallback(std::shared_ptr<logging::Logger> logger, uint64_t size)
+        : logger_(std::move(logger))
+        , buffer_(size)  {
     }
     int64_t process(std::shared_ptr<io::BaseStream> stream) {
-      int64_t ret = 0;
-      ret = stream->read(buffer_, buffer_size_);
-      if (!stream)
-        read_size_ = stream->getSize();
-      else
-        read_size_ = buffer_size_;
-      return ret;
+      if (buffer_.size() == 0U) {
+        return 0U;
+      }
+      int ret = stream->read(buffer_.data(), buffer_.size());
+      if (ret != buffer_.size()) {
+        logger_->log_error("%zu bytes were requested from the stream but %d bytes were read. Rolling back.", buffer_.size(), ret);
+        throw Exception(PROCESSOR_EXCEPTION, "Failed to read the entire FlowFile.");
+      }
+      return buffer_.size();
     }
-    uint8_t *buffer_;
-    uint64_t buffer_size_;
-    uint64_t read_size_;
+    std::shared_ptr<logging::Logger> logger_;
+    std::vector<uint8_t> buffer_;
   };
 
  public:
diff --git a/extensions/standard-processors/processors/TailFile.cpp b/extensions/standard-processors/processors/TailFile.cpp
index e6f1824..2fd0861 100644
--- a/extensions/standard-processors/processors/TailFile.cpp
+++ b/extensions/standard-processors/processors/TailFile.cpp
@@ -72,7 +72,8 @@ core::Property TailFile::StateFile("State File", "Specifies the file that should
                                    " what data has been ingested so that upon restart NiFi can resume from where it left off",
                                    "TailFileState");
 core::Property TailFile::Delimiter("Input Delimiter", "Specifies the character that should be used for delimiting the data being tailed"
-                                   "from the incoming file.",
+                                   "from the incoming file."
+                                   "If none is specified, data will be ingested as it becomes available.",
                                    "");
 
 core::Property TailFile::TailMode(
@@ -416,7 +417,7 @@ void TailFile::onTrigger(const std::shared_ptr<core::ProcessContext> &context, c
         }
         logger_->log_debug("Looking for delimiter 0x%X", delim);
         std::vector<std::shared_ptr<FlowFileRecord>> flowFiles;
-        session->import(fullPath, flowFiles, true, state.second.currentTailFilePosition_, delim);
+        session->import(fullPath, flowFiles, state.second.currentTailFilePosition_, delim);
         logger_->log_info("%u flowfiles were received from TailFile input", flowFiles.size());
 
         for (auto ffr : flowFiles) {
diff --git a/extensions/standard-processors/tests/integration/TailFileCronTest.cpp b/extensions/standard-processors/tests/integration/TailFileCronTest.cpp
index 2b5ee77..589c92c 100644
--- a/extensions/standard-processors/tests/integration/TailFileCronTest.cpp
+++ b/extensions/standard-processors/tests/integration/TailFileCronTest.cpp
@@ -73,7 +73,7 @@ class TailFileTestHarness : public IntegrationBase {
   virtual void runAssertions() {
     assert(LogTestController::getInstance().contains("5 flowfiles were received from TailFile input") == true);
     assert(LogTestController::getInstance().contains("Looking for delimiter 0xA") == true);
-    assert(LogTestController::getInstance().contains("li\\ne5") == true);
+    assert(LogTestController::getInstance().contains(utils::StringUtils::to_hex("li\\ne5")) == true);
   }
 
  protected:
diff --git a/extensions/standard-processors/tests/integration/TailFileTest.cpp b/extensions/standard-processors/tests/integration/TailFileTest.cpp
index 2b5ee77..589c92c 100644
--- a/extensions/standard-processors/tests/integration/TailFileTest.cpp
+++ b/extensions/standard-processors/tests/integration/TailFileTest.cpp
@@ -73,7 +73,7 @@ class TailFileTestHarness : public IntegrationBase {
   virtual void runAssertions() {
     assert(LogTestController::getInstance().contains("5 flowfiles were received from TailFile input") == true);
     assert(LogTestController::getInstance().contains("Looking for delimiter 0xA") == true);
-    assert(LogTestController::getInstance().contains("li\\ne5") == true);
+    assert(LogTestController::getInstance().contains(utils::StringUtils::to_hex("li\\ne5")) == true);
   }
 
  protected:
diff --git a/extensions/standard-processors/tests/unit/TailFileTests.cpp b/extensions/standard-processors/tests/unit/TailFileTests.cpp
index 71a11bc..416d3cf 100644
--- a/extensions/standard-processors/tests/unit/TailFileTests.cpp
+++ b/extensions/standard-processors/tests/unit/TailFileTests.cpp
@@ -25,6 +25,9 @@
 #include <string>
 #include <iostream>
 #include <set>
+#include <algorithm>
+#include <random>
+#include <cstdlib>
 #include "FlowController.h"
 #include "TestBase.h"
 #include "core/Core.h"
@@ -70,10 +73,13 @@ TEST_CASE("TailFileWithDelimiter", "[tailfiletest2]") {
 
   testController.runSession(plan, false);
   auto records = plan->getProvenanceRecords();
-  std::shared_ptr<core::FlowFile> record = plan->getCurrentFlowFile();
-  REQUIRE(record == nullptr);
   REQUIRE(records.size() == 2);
 
+  testController.runSession(plan, false);
+
+  REQUIRE(LogTestController::getInstance().contains("Logged 1 flow files"));
+  REQUIRE(LogTestController::getInstance().contains("Size:" + std::to_string(NEWLINE_FILE.find_first_of('\n')) + " Offset:0"));
+
   LogTestController::getInstance().reset();
 
   // Delete the test and state file.
@@ -323,6 +329,8 @@ TEST_CASE("TailFileWithOutDelimiter", "[tailfiletest2]") {
   // Create and write to the test file
 
   TestController testController;
+  LogTestController::getInstance().setTrace<minifi::processors::TailFile>();
+  LogTestController::getInstance().setDebug<core::ProcessSession>();
   LogTestController::getInstance().setDebug<minifi::processors::LogAttribute>();
 
   std::shared_ptr<TestPlan> plan = testController.createPlan();
@@ -354,12 +362,128 @@ TEST_CASE("TailFileWithOutDelimiter", "[tailfiletest2]") {
 
   testController.runSession(plan, false);
   auto records = plan->getProvenanceRecords();
-  std::shared_ptr<core::FlowFile> record = plan->getCurrentFlowFile();
-  REQUIRE(record == nullptr);
   REQUIRE(records.size() == 2);
 
   testController.runSession(plan, false);
 
+  REQUIRE(LogTestController::getInstance().contains("Logged 1 flow files"));
+  REQUIRE(LogTestController::getInstance().contains("Size:" + std::to_string(NEWLINE_FILE.size()) + " Offset:0"));
+
+  LogTestController::getInstance().reset();
+
+  // Delete the test and state file.
+  remove(TMP_FILE);
+  remove(STATE_FILE);
+}
+
+TEST_CASE("TailFileLongWithDelimiter", "[tailfiletest2]") {
+  std::string line1("foo");
+  std::string line2(8050, 0);
+  std::mt19937 gen(std::random_device { }());
+  std::generate_n(line2.begin(), line2.size(), [&]() -> char {
+    return 32 + gen() % (127 - 32);
+  });
+  std::string line3("bar");
+  std::string line4("buzz");
+
+  // Create and write to the test file
+  std::ofstream tmpfile;
+  tmpfile.open(TMP_FILE);
+  tmpfile << line1 << "\n" << line2 << "\n" << line3 << "\n" << line4;
+  tmpfile.close();
+
+  TestController testController;
+  LogTestController::getInstance().setTrace<TestPlan>();
+  LogTestController::getInstance().setTrace<processors::TailFile>();
+  LogTestController::getInstance().setTrace<processors::LogAttribute>();
+  LogTestController::getInstance().setTrace<core::ProcessSession>();
+
+  std::shared_ptr<TestPlan> plan = testController.createPlan();
+  std::shared_ptr<core::Processor> tailfile = plan->addProcessor("TailFile", "tailfileProc");
+
+  char format[] = "/tmp/gt.XXXXXX";
+  char *dir = testController.createTempDirectory(format);
+
+  plan->setProperty(tailfile, org::apache::nifi::minifi::processors::TailFile::FileName.getName(), TMP_FILE);
+  plan->setProperty(tailfile, org::apache::nifi::minifi::processors::TailFile::StateFile.getName(), STATE_FILE);
+  plan->setProperty(tailfile, org::apache::nifi::minifi::processors::TailFile::Delimiter.getName(), "\n");
+
+  std::shared_ptr<core::Processor> log_attr = plan->addProcessor("LogAttribute", "Log", core::Relationship("success", "description"), true);
+  plan->setProperty(log_attr, processors::LogAttribute::FlowFilesToLog.getName(), "0");
+  plan->setProperty(log_attr, processors::LogAttribute::LogPayload.getName(), "true");
+
+  testController.runSession(plan, true);
+
+  REQUIRE(LogTestController::getInstance().contains("Logged 3 flow files"));
+  REQUIRE(LogTestController::getInstance().contains(utils::StringUtils::to_hex(line1)));
+  auto line2_hex = utils::StringUtils::to_hex(line2);
+  std::stringstream line2_hex_lines;
+  for (size_t i = 0; i < line2_hex.size(); i += 80) {
+    line2_hex_lines << line2_hex.substr(i, 80) << '\n';
+  }
+  REQUIRE(LogTestController::getInstance().contains(line2_hex_lines.str()));
+  REQUIRE(LogTestController::getInstance().contains(utils::StringUtils::to_hex(line3)));
+  REQUIRE(false == LogTestController::getInstance().contains(utils::StringUtils::to_hex(line4), std::chrono::seconds(0)));
+
+
+  LogTestController::getInstance().reset();
+
+  // Delete the test and state file.
+  remove(TMP_FILE);
+  remove(STATE_FILE);
+}
+
+TEST_CASE("TailFileWithDelimiterMultipleDelimiters", "[tailfiletest2]") {
+  // Test having two delimiters on the buffer boundary
+  std::string line1(4097, '\n');
+  std::mt19937 gen(std::random_device { }());
+  std::generate_n(line1.begin(), 4095, [&]() -> char {
+  return 32 + gen() % (127 - 32);
+  });
+  std::string line2("foo");
+  std::string line3("bar");
+  std::string line4("buzz");
+
+  // Create and write to the test file
+  std::ofstream tmpfile;
+  tmpfile.open(TMP_FILE);
+  tmpfile << line1 << "\n" << line2 << "\n" << line3 << "\n" << line4;
+  tmpfile.close();
+
+  TestController testController;
+  LogTestController::getInstance().setTrace<TestPlan>();
+  LogTestController::getInstance().setTrace<processors::TailFile>();
+  LogTestController::getInstance().setTrace<processors::LogAttribute>();
+  LogTestController::getInstance().setTrace<core::ProcessSession>();
+
+  std::shared_ptr<TestPlan> plan = testController.createPlan();
+  std::shared_ptr<core::Processor> tailfile = plan->addProcessor("TailFile", "tailfileProc");
+  auto id = tailfile->getUUIDStr();
+
+  char format[] = "/tmp/gt.XXXXXX";
+  char *dir = testController.createTempDirectory(format);
+
+  plan->setProperty(tailfile, org::apache::nifi::minifi::processors::TailFile::FileName.getName(), TMP_FILE);
+  plan->setProperty(tailfile, org::apache::nifi::minifi::processors::TailFile::StateFile.getName(), STATE_FILE);
+  plan->setProperty(tailfile, org::apache::nifi::minifi::processors::TailFile::Delimiter.getName(), "\n");
+
+  std::shared_ptr<core::Processor> log_attr = plan->addProcessor("LogAttribute", "Log", core::Relationship("success", "description"), true);
+  plan->setProperty(log_attr, processors::LogAttribute::FlowFilesToLog.getName(), "0");
+  plan->setProperty(log_attr, processors::LogAttribute::LogPayload.getName(), "true");
+
+  testController.runSession(plan, true);
+
+  REQUIRE(LogTestController::getInstance().contains("Logged 5 flow files"));
+  auto line1_hex = utils::StringUtils::to_hex(line1.substr(0, 4095));
+  std::stringstream line1_hex_lines;
+  for (size_t i = 0; i < line1_hex.size(); i += 80) {
+    line1_hex_lines << line1_hex.substr(i, 80) << '\n';
+  }
+  REQUIRE(LogTestController::getInstance().contains(line1_hex_lines.str()));
+  REQUIRE(LogTestController::getInstance().contains(utils::StringUtils::to_hex(line2)));
+  REQUIRE(LogTestController::getInstance().contains(utils::StringUtils::to_hex(line3)));
+  REQUIRE(false == LogTestController::getInstance().contains(utils::StringUtils::to_hex(line4), std::chrono::seconds(0)));
+
   LogTestController::getInstance().reset();
 
   // Delete the test and state file.
diff --git a/libminifi/CMakeLists.txt b/libminifi/CMakeLists.txt
index dd91442..9959890 100644
--- a/libminifi/CMakeLists.txt
+++ b/libminifi/CMakeLists.txt
@@ -189,4 +189,4 @@ endif()
 set_property(TARGET core-minifi-shared PROPERTY POSITION_INDEPENDENT_CODE ON)
 set_property(TARGET minifi-shared PROPERTY POSITION_INDEPENDENT_CODE ON)
 #endif()
-endif(ENABLE_PYTHON AND NOT STATIC_BUILD)
\ No newline at end of file
+endif(ENABLE_PYTHON AND NOT STATIC_BUILD)
diff --git a/libminifi/include/core/Deprecated.h b/libminifi/include/core/Deprecated.h
new file mode 100644
index 0000000..faed844
--- /dev/null
+++ b/libminifi/include/core/Deprecated.h
@@ -0,0 +1,29 @@
+/**
+
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef LIBMINIFI_INCLUDE_CORE_DEPRECATED_H_
+#define LIBMINIFI_INCLUDE_CORE_DEPRECATED_H_
+
+#ifdef _MSC_VER
+#define DEPRECATED __declspec(deprecated)
+#elif defined(__GNUC__) | defined(__clang__)
+#define DEPRECATED __attribute__((__deprecated__))
+#else
+#define DEPRECATED
+#endif
+
+#endif /* LIBMINIFI_INCLUDE_CORE_DEPRECATED_H_ */
diff --git a/libminifi/include/core/ProcessSession.h b/libminifi/include/core/ProcessSession.h
index 52462e9..7bcd7f5 100644
--- a/libminifi/include/core/ProcessSession.h
+++ b/libminifi/include/core/ProcessSession.h
@@ -31,6 +31,7 @@
 #include "FlowFileRecord.h"
 #include "Exception.h"
 #include "core/logging/LoggerConfiguration.h"
+#include "core/Deprecated.h"
 #include "FlowFile.h"
 #include "WeakReference.h"
 #include "provenance/Provenance.h"
@@ -110,7 +111,8 @@ class ProcessSession : public ReferenceContainer {
   void importFrom(io::DataStream &stream, const std::shared_ptr<core::FlowFile> &flow);
   // import from the data source.
   void import(std::string source, const std::shared_ptr<core::FlowFile> &flow, bool keepSource = true, uint64_t offset = 0);
-  void import(std::string source, std::vector<std::shared_ptr<FlowFileRecord>> &flows, bool keepSource, uint64_t offset, char inputDelimiter);
+  DEPRECATED void import(std::string source, std::vector<std::shared_ptr<FlowFileRecord>> &flows, bool keepSource, uint64_t offset, char inputDelimiter);
+  void import(const std::string& source, std::vector<std::shared_ptr<FlowFileRecord>> &flows, uint64_t offset, char inputDelimiter);
 
   /**
    * Exports the data stream to a file
diff --git a/libminifi/src/core/ProcessSession.cpp b/libminifi/src/core/ProcessSession.cpp
index 40ee900..42271b8 100644
--- a/libminifi/src/core/ProcessSession.cpp
+++ b/libminifi/src/core/ProcessSession.cpp
@@ -384,8 +384,7 @@ void ProcessSession::read(const std::shared_ptr<core::FlowFile> &flow, InputStre
 void ProcessSession::importFrom(io::DataStream &stream, const std::shared_ptr<core::FlowFile> &flow) {
   std::shared_ptr<ResourceClaim> claim = std::make_shared<ResourceClaim>(process_context_->getContentRepository());
   size_t max_read = getpagesize();
-  std::vector<uint8_t> charBuffer;
-  charBuffer.resize(max_read);
+  std::vector<uint8_t> charBuffer(max_read);
 
   try {
     auto startTime = getTimeMillis();
@@ -450,12 +449,10 @@ void ProcessSession::importFrom(io::DataStream &stream, const std::shared_ptr<co
 
 void ProcessSession::import(std::string source, const std::shared_ptr<core::FlowFile> &flow, bool keepSource, uint64_t offset) {
   std::shared_ptr<ResourceClaim> claim = std::make_shared<ResourceClaim>(process_context_->getContentRepository());
-  int size = getpagesize();
-  std::vector<uint8_t> charBuffer;
-  charBuffer.resize(size);
+  size_t size = getpagesize();
+  std::vector<uint8_t> charBuffer(size);
 
   try {
-    //  std::ofstream fs;
     auto startTime = getTimeMillis();
     std::ifstream input;
     input.open(source.c_str(), std::fstream::in | std::fstream::binary);
@@ -537,63 +534,82 @@ void ProcessSession::import(std::string source, const std::shared_ptr<core::Flow
   }
 }
 
-void ProcessSession::import(std::string source, std::vector<std::shared_ptr<FlowFileRecord>> &flows, bool keepSource, uint64_t offset, char inputDelimiter) {
+void ProcessSession::import(const std::string& source, std::vector<std::shared_ptr<FlowFileRecord>> &flows, uint64_t offset, char inputDelimiter) {
   std::shared_ptr<ResourceClaim> claim;
+  std::shared_ptr<io::BaseStream> stream;
   std::shared_ptr<FlowFileRecord> flowFile;
-  int size = getpagesize();
-  std::vector<char> charBuffer;
-  charBuffer.resize(size);
 
+  std::vector<uint8_t> buffer(getpagesize());
   try {
-    // Open the input file and seek to the appropriate location.
-    std::ifstream input;
-    logger_->log_debug("Opening %s", source);
-    input.open(source.c_str(), std::fstream::in | std::fstream::binary);
-    if (input.is_open() && input.good()) {
-      if (offset != 0) {
+    try {
+      std::ifstream input;
+      logger_->log_debug("Opening %s", source);
+      input.open(source.c_str(), std::fstream::in | std::fstream::binary);
+      if (!input.is_open() || !input.good()) {
+        input.close();
+        throw Exception(FILE_OPERATION_EXCEPTION, "File Import Error");
+      }
+      if (offset != 0U) {
         input.seekg(offset, input.beg);
         if (!input.good()) {
-          logger_->log_error("Seeking to %d failed for file %s (does file/filesystem support seeking?)", offset, source);
+          logger_->log_error("Seeking to %lu failed for file %s (does file/filesystem support seeking?)", offset, source);
           throw Exception(FILE_OPERATION_EXCEPTION, "File Import Error");
         }
       }
-      while (input.good() && !input.eof()) {
-        bool invalidWrite = false;
-        uint64_t startTime = getTimeMillis();
-        input.getline(charBuffer.data(), size, inputDelimiter);
-
-        if (input.eof() || input.fail()) {
+      uint64_t startTime = 0U;
+      while (input.good()) {
+        input.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
+        std::streamsize read = input.gcount();
+        if (read < 0) {
+          throw Exception(FILE_OPERATION_EXCEPTION, "std::ifstream::gcount returned negative value");
+        }
+        if (read == 0) {
           logger_->log_trace("Finished reading input %s", source);
           break;
         }
-        flowFile = std::static_pointer_cast<FlowFileRecord>(create());
-        claim = std::make_shared<ResourceClaim>(process_context_->getContentRepository());
-
-        size_t bufsize = strlen(charBuffer.data());
-        std::shared_ptr<io::BaseStream> stream = process_context_->getContentRepository()->write(claim);
-        if (nullptr == stream) {
-          logger_->log_debug("Stream is null");
-          rollback();
-          return;
-        }
-
-        if (input.good()) {
-          if (stream->write(reinterpret_cast<uint8_t*>(charBuffer.data()), bufsize) < 0) {
-            invalidWrite = true;
+        uint8_t* begin = buffer.data();
+        uint8_t* end = begin + read;
+        while (true) {
+          uint8_t* delimiterPos = std::find(begin, end, static_cast<uint8_t>(inputDelimiter));
+          int len = delimiterPos - begin;
+
+          /*
+           * We do not want to process the rest of the buffer after the last delimiter if
+           *  - we have reached EOF in the file (we would discard it anyway)
+           *  - there is nothing to process (the last character in the buffer is a delimiter)
+           */
+          if (delimiterPos == end && (input.eof() || len == 0)) {
             break;
           }
-        } else {
-          if (stream->write(reinterpret_cast<uint8_t*>(charBuffer.data()), input.gcount()) < 0) {
-            invalidWrite = true;
-            break;
+
+          /* Create claim and stream if needed and append data */
+          if (claim == nullptr) {
+            startTime = getTimeMillis();
+            claim = std::make_shared<ResourceClaim>(process_context_->getContentRepository());
+          }
+          if (stream == nullptr) {
+            stream = process_context_->getContentRepository()->write(claim);
+          }
+          if (stream == nullptr) {
+            logger_->log_error("Stream is null");
+            rollback();
+            return;
+          }
+          if (stream->write(begin, len) != len) {
+            logger_->log_error("Error while writing");
+            stream->closeStream();
+            throw Exception(FILE_OPERATION_EXCEPTION, "File Export Error creating Flowfile");
           }
-        }
 
-        if (!invalidWrite) {
+          /* Create a FlowFile if we reached a delimiter */
+          if (delimiterPos == end) {
+            break;
+          }
+          flowFile = std::static_pointer_cast<FlowFileRecord>(create());
           flowFile->setSize(stream->getSize());
           flowFile->setOffset(0);
           if (flowFile->getResourceClaim() != nullptr) {
-            // Remove the old claim
+            /* Remove the old claim */
             flowFile->getResourceClaim()->decreaseFlowFileRecordOwnedCount();
             flowFile->clearResourceClaim();
           }
@@ -606,37 +622,40 @@ void ProcessSession::import(std::string source, std::vector<std::shared_ptr<Flow
           uint64_t endTime = getTimeMillis();
           provenance_report_->modifyContent(flowFile, details, endTime - startTime);
           flows.push_back(flowFile);
-        } else {
-          logger_->log_debug("Error while writing");
-          stream->closeStream();
-          throw Exception(FILE_OPERATION_EXCEPTION, "File Export Error creating Flowfile");
+
+          /* Reset these to start processing the next FlowFile with a clean slate */
+          flowFile.reset();
+          stream.reset();
+          claim.reset();
+
+          /* Skip delimiter */
+          begin = delimiterPos + 1;
         }
       }
-      input.close();
-      logger_->log_trace("Closed input %s, keeping source ? %i", source, keepSource);
-      if (!keepSource)
-        std::remove(source.c_str());
-    } else {
-      input.close();
-      throw Exception(FILE_OPERATION_EXCEPTION, "File Import Error");
+    } catch (std::exception &exception) {
+      logger_->log_debug("Caught Exception %s", exception.what());
+      throw;
+    } catch (...) {
+      logger_->log_debug("Caught Exception during process session write");
+      throw;
     }
-  } catch (std::exception &exception) {
-    if (flowFile && flowFile->getResourceClaim() == claim) {
-      flowFile->getResourceClaim()->decreaseFlowFileRecordOwnedCount();
-      flowFile->clearResourceClaim();
-    }
-    logger_->log_debug("Caught Exception %s", exception.what());
-    throw;
   } catch (...) {
-    if (flowFile && flowFile->getResourceClaim() == claim) {
+    if (flowFile != nullptr && claim != nullptr && flowFile->getResourceClaim() == claim) {
       flowFile->getResourceClaim()->decreaseFlowFileRecordOwnedCount();
       flowFile->clearResourceClaim();
     }
-    logger_->log_debug("Caught Exception during process session write");
     throw;
   }
 }
 
+void ProcessSession::import(std::string source, std::vector<std::shared_ptr<FlowFileRecord>> &flows, bool keepSource, uint64_t offset, char inputDelimiter) {
+  import(source, flows, offset, inputDelimiter);
+  logger_->log_trace("Closed input %s, keeping source ? %i", source, keepSource);
+  if (!keepSource) {
+    std::remove(source.c_str());
+  }
+}
+
 bool ProcessSession::exportContent(const std::string &destination, const std::string &tmpFile, const std::shared_ptr<core::FlowFile> &flow, bool keepContent) {
   logger_->log_debug("Exporting content of %s to %s", flow->getUUIDStr(), destination);
 


[nifi-minifi-cpp] 01/02: MINIFICPP-925 - Hex encoder and decoder

Posted by ab...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aboda pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git

commit 77f3c98c8121be1090e49e0c2df8c29873cc63c0
Author: Daniel Bakai <ba...@gmail.com>
AuthorDate: Sun Jun 30 19:05:59 2019 +0200

    MINIFICPP-925 - Hex encoder and decoder
    
    Signed-off-by: Arpad Boda <ab...@apache.org>
---
 extensions/bootstrap/docs/generatec2docs.h         |  11 +-
 extensions/pcap/CapturePacket.cpp                  |   4 +-
 extensions/pcap/CapturePacket.h                    |   2 +-
 extensions/sftp/client/SFTPClient.cpp              |   9 +-
 .../standard-processors/processors/HashContent.h   |  16 +-
 libminifi/include/utils/StringUtils.h              | 281 +++++++++++----------
 libminifi/src/utils/StringUtils.cpp                | 162 ++++++++++++
 libminifi/test/pcap-tests/CMakeLists.txt           |   8 +-
 libminifi/test/pcap-tests/PcapTest.cpp             |  23 +-
 libminifi/test/unit/StringUtilsTests.cpp           |  61 +++++
 10 files changed, 399 insertions(+), 178 deletions(-)

diff --git a/extensions/bootstrap/docs/generatec2docs.h b/extensions/bootstrap/docs/generatec2docs.h
index bcb3a3e..637900c 100644
--- a/extensions/bootstrap/docs/generatec2docs.h
+++ b/extensions/bootstrap/docs/generatec2docs.h
@@ -24,15 +24,6 @@
 #include <iomanip>
 #include "utils/StringUtils.h"
 
-std::string toHex(const std::string& s) {
-  std::ostringstream ret;
-
-  for (std::string::size_type i = 0; i < s.length(); ++i)
-    ret << std::hex << std::setfill('0') << std::setw(2) << (int) s[i];
-
-  return ret.str();
-}
-
 int generateC2Docs(const std::string &inputfile, const std::string &output) {
   std::ifstream inf(inputfile);
   std::string input((std::istreambuf_iterator<char>(inf)), std::istreambuf_iterator<char>());
@@ -81,7 +72,7 @@ int generateC2Docs(const std::string &inputfile, const std::string &output) {
     auto description = input.substr(0, nextBlock);
 
     auto desc = org::apache::nifi::minifi::utils::StringUtils::trim(description);
-    outputFile << "     extensions.insert(std::make_pair(\"" << processor << "\",utils::StringUtils::hex_ascii(\"" << toHex(desc) << "\")));\n";
+    outputFile << "     extensions.insert(std::make_pair(\"" << processor << "\",utils::StringUtils::from_hex(\"" << org::apache::nifi::minifi::utils::StringUtils::to_hex(desc) << "\")));\n";
   }
 
   outputFile << "}\n    return extensions[feature]; \n"
diff --git a/extensions/pcap/CapturePacket.cpp b/extensions/pcap/CapturePacket.cpp
index 6a07e2e..1b5d2e7 100644
--- a/extensions/pcap/CapturePacket.cpp
+++ b/extensions/pcap/CapturePacket.cpp
@@ -55,7 +55,7 @@ std::shared_ptr<utils::IdGenerator> CapturePacket::id_generator_ = utils::IdGene
 core::Property CapturePacket::BaseDir(core::PropertyBuilder::createProperty("Base Directory")->withDescription("Scratch directory for PCAP files")->withDefaultValue<std::string>("/tmp/")->build());
 
 core::Property CapturePacket::BatchSize(core::PropertyBuilder::createProperty("Batch Size")->withDescription("The number of packets to combine within a given PCAP")->withDefaultValue<uint64_t>(50)->build());
-core::Property CapturePacket::NetworkController("Network Controller", "Regular expression of the network controller(s) to which we will attach", ".*");
+core::Property CapturePacket::NetworkControllers("Network Controllers", "Regular expression of the network controller(s) to which we will attach", ".*");
 core::Property CapturePacket::CaptureBluetooth(core::PropertyBuilder::createProperty("Capture Bluetooth")->withDescription("True indicates that we support bluetooth interfaces")->withDefaultValue<bool>(false)->build());
 
 const char *CapturePacket::ProcessorName = "CapturePacket";
@@ -116,7 +116,7 @@ void CapturePacket::initialize() {
   // Set the supported properties
   std::set<core::Property> properties;
   properties.insert(BatchSize);
-  properties.insert(NetworkController);
+  properties.insert(NetworkControllers);
   properties.insert(BaseDir);
   properties.insert(CaptureBluetooth);
   setSupportedProperties(properties);
diff --git a/extensions/pcap/CapturePacket.h b/extensions/pcap/CapturePacket.h
index 1ec145d..6dfd276 100644
--- a/extensions/pcap/CapturePacket.h
+++ b/extensions/pcap/CapturePacket.h
@@ -109,7 +109,7 @@ class CapturePacket : public core::Processor {
   // Processor Name
   static const char *ProcessorName;
   static core::Property BatchSize;
-  static core::Property NetworkController;
+  static core::Property NetworkControllers;
   static core::Property BaseDir;
   static core::Property CaptureBluetooth;
   // Supported Relationships
diff --git a/extensions/sftp/client/SFTPClient.cpp b/extensions/sftp/client/SFTPClient.cpp
index bc27d5a..d2a2716 100644
--- a/extensions/sftp/client/SFTPClient.cpp
+++ b/extensions/sftp/client/SFTPClient.cpp
@@ -381,14 +381,15 @@ bool SFTPClient::connect() {
     if (fingerprint == nullptr) {
       logger_->log_warn("Cannot get remote server fingerprint");
     } else {
-      std::stringstream fingerprint_hex;
+      auto fingerprint_hex = utils::StringUtils::to_hex(reinterpret_cast<const uint8_t*>(fingerprint), 20);
+      std::stringstream fingerprint_hex_colon;
       for (size_t i = 0; i < 20; i++) {
-        fingerprint_hex << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(static_cast<uint8_t>(fingerprint[i]));
+        fingerprint_hex_colon << fingerprint_hex.substr(i * 2, 2);
         if (i != 19) {
-          fingerprint_hex << ":";
+          fingerprint_hex_colon << ":";
         }
       }
-      logger_->log_debug("SHA1 host key fingerprint for %s is %s", hostname_.c_str(), fingerprint_hex.str().c_str());
+      logger_->log_debug("SHA1 host key fingerprint for %s is %s", hostname_.c_str(), fingerprint_hex_colon.str().c_str());
     }
   }
 
diff --git a/extensions/standard-processors/processors/HashContent.h b/extensions/standard-processors/processors/HashContent.h
index bcfdd34..80a0bdb 100644
--- a/extensions/standard-processors/processors/HashContent.h
+++ b/extensions/standard-processors/processors/HashContent.h
@@ -38,21 +38,13 @@
 #include "core/ProcessSession.h"
 #include "core/Resource.h"
 #include "io/BaseStream.h"
+#include "utils/StringUtils.h"
 
 using HashReturnType = std::pair<std::string, int64_t>;
 
 namespace {
 #define HASH_BUFFER_SIZE 16384
 
-  std::string digestToString(const unsigned char * const digest, size_t size) {
-    std::stringstream ss;
-    for(size_t i = 0; i < size; i++)
-    {
-      ss << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
-    }
-    return ss.str();
-  }
-
   HashReturnType MD5Hash(const std::shared_ptr<org::apache::nifi::minifi::io::BaseStream>& stream) {
     HashReturnType ret_val;
     ret_val.second = 0;
@@ -72,7 +64,7 @@ namespace {
     if (ret_val.second > 0) {
       unsigned char digest[MD5_DIGEST_LENGTH];
       MD5_Final(digest, &context);
-      ret_val.first = digestToString(digest, MD5_DIGEST_LENGTH);
+      ret_val.first = utils::StringUtils::to_hex(digest, MD5_DIGEST_LENGTH, true /*uppercase*/);
     }
     return ret_val;
   }
@@ -96,7 +88,7 @@ namespace {
     if (ret_val.second > 0) {
       unsigned char digest[SHA_DIGEST_LENGTH];
       SHA1_Final(digest, &context);
-      ret_val.first = digestToString(digest, SHA_DIGEST_LENGTH);
+      ret_val.first = utils::StringUtils::to_hex(digest, SHA_DIGEST_LENGTH, true /*uppercase*/);
     }
     return ret_val;
   }
@@ -120,7 +112,7 @@ namespace {
     if (ret_val.second > 0) {
       unsigned char digest[SHA256_DIGEST_LENGTH];
       SHA256_Final(digest, &context);
-      ret_val.first = digestToString(digest, SHA256_DIGEST_LENGTH);
+      ret_val.first = utils::StringUtils::to_hex(digest, SHA256_DIGEST_LENGTH, true /*uppercase*/);
     }
     return ret_val;
   }
diff --git a/libminifi/include/utils/StringUtils.h b/libminifi/include/utils/StringUtils.h
index 326a048..6ecbebd 100644
--- a/libminifi/include/utils/StringUtils.h
+++ b/libminifi/include/utils/StringUtils.h
@@ -82,12 +82,7 @@ class StringUtils {
    * @param input input string
    * @param output output string.
    */
-  static bool StringToBool(std::string input, bool &output) {
-
-    std::transform(input.begin(), input.end(), input.begin(), ::tolower);
-    std::istringstream(input) >> std::boolalpha >> output;
-    return output;
-  }
+  static bool StringToBool(std::string input, bool &output);
 
   // Trim String utils
 
@@ -96,9 +91,7 @@ class StringUtils {
    * @param s incoming string
    * @returns modified string
    */
-  static std::string trim(std::string s) {
-    return trimRight(trimLeft(s));
-  }
+  static std::string trim(std::string s);
 
   /**
    * Trims left most part of a string
@@ -132,25 +125,7 @@ class StringUtils {
     }
   }
 
-  static std::vector<std::string> split(const std::string &str, const std::string &delimiter) {
-    std::vector<std::string> result;
-    auto curr = str.begin();
-    auto end = str.end();
-    auto is_func = [delimiter](int s) {
-      return delimiter.at(0) == s;
-    };
-    while (curr != end) {
-      curr = std::find_if_not(curr, end, is_func);
-      if (curr == end) {
-        break;
-      }
-      auto next = std::find_if(curr, end, is_func);
-      result.push_back(std::string(curr, next));
-      curr = next;
-    }
-
-    return result;
-  }
+  static std::vector<std::string> split(const std::string &str, const std::string &delimiter);
 
   /**
    * Converts a string to a float
@@ -158,82 +133,11 @@ class StringUtils {
    * @param output output float
    * @param cp failure policy
    */
-  static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN) {
-    try {
-      output = std::stof(input);
-    } catch (const std::invalid_argument &ie) {
-      switch (cp) {
-        case RETURN:
-        case NOTHING:
-          return false;
-        case EXIT:
-          exit(1);
-        case EXCEPT:
-          throw ie;
-      }
-    } catch (const std::out_of_range &ofr) {
-      switch (cp) {
-        case RETURN:
-        case NOTHING:
-          return false;
-        case EXIT:
-          exit(1);
-        case EXCEPT:
-          throw ofr;
-
-      }
-    }
+  static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN);
 
-    return true;
-
-  }
-
-  static std::string replaceEnvironmentVariables(std::string& original_string) {
-    int32_t beg_seq = 0;
-    int32_t end_seq = 0;
-    std::string source_string = original_string;
-    do {
-      beg_seq = source_string.find("${", beg_seq);
-      if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') {
-        beg_seq += 2;
-        continue;
-      }
-      if (beg_seq < 0)
-        break;
-      end_seq = source_string.find("}", beg_seq + 2);
-      if (end_seq < 0)
-        break;
-      if (end_seq - (beg_seq + 2) < 0) {
-        beg_seq += 2;
-        continue;
-      }
-      const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2));
-      const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1);
-      if (env_field.empty()) {
-        continue;
-      }
-      const auto strVal = std::getenv(env_field.c_str());
-      std::string env_value;
-      if (strVal != nullptr)
-        env_value = strVal;
-      source_string = replaceAll(source_string, env_field_wrapped, env_value);
-      beg_seq = 0;  // restart
-    } while (beg_seq >= 0);
+  static std::string replaceEnvironmentVariables(std::string& original_string);
 
-    source_string = replaceAll(source_string, "\\$", "$");
-
-    return source_string;
-  }
-
-  static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) {
-    std::size_t loc = 0;
-    std::size_t lastFound;
-    while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) {
-      source_string.replace(lastFound, from_string.size(), to_string);
-      loc = lastFound + to_string.size();
-    }
-    return source_string;
-  }
+  static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string);
 
   inline static bool endsWithIgnoreCase(const std::string &value, const std::string & endString) {
     if (endString.size() > value.size())
@@ -257,39 +161,15 @@ class StringUtils {
     }
     return newString;
   }
-
-  static std::string replaceMap(std::string source_string, const std::map<std::string, std::string> &replace_map) {
-    auto result_string = source_string;
-
-    std::vector<std::pair<size_t, std::pair<size_t, std::string>>> replacements;
-    for (const auto &replace_pair : replace_map) {
-      size_t replace_pos = 0;
-      while ((replace_pos = source_string.find(replace_pair.first, replace_pos)) != std::string::npos) {
-        replacements.emplace_back(std::make_pair(replace_pos, std::make_pair(replace_pair.first.length(), replace_pair.second)));
-        replace_pos += replace_pair.first.length();
-      }
-    }
-
-    std::sort(replacements.begin(), replacements.end(), [](const std::pair<size_t, std::pair<size_t, std::string>> a,
-        const std::pair<size_t, std::pair<size_t, std::string>> &b) {
-      return a.first > b.first;
-    });
-
-    for (const auto &replacement : replacements) {
-      result_string = source_string.replace(replacement.first, replacement.second.first, replacement.second.second);
-    }
-
-    return result_string;
-  }
-
+  
   /**
- * Concatenates strings stored in an arbitrary container using the provided separator.
- * @tparam TChar char type of the string (char or wchar_t)
- * @tparam U arbitrary container which has string or wstring value type
- * @param separator that is inserted between each elements. Type should match the type of strings in container.
- * @param container that contains the strings to be concatenated
- * @return the result string
- */
+   * Concatenates strings stored in an arbitrary container using the provided separator.
+   * @tparam TChar char type of the string (char or wchar_t)
+   * @tparam U arbitrary container which has string or wstring value type
+   * @param separator that is inserted between each elements. Type should match the type of strings in container.
+   * @param container that contains the strings to be concatenated
+   * @return the result string
+   */
   template<class TChar, class U, typename std::enable_if<std::is_same<typename U::value_type, std::basic_string<TChar>>::value>::type* = nullptr>
   static std::basic_string<TChar> join(const std::basic_string<TChar>& separator, const U& container) {
     typedef typename U::const_iterator ITtype;
@@ -347,6 +227,139 @@ class StringUtils {
     return join(std::basic_string<TChar>(separator), container);
   };
 
+  /**
+   * Hexdecodes the hexencoded string in data, ignoring every character that is not [0-9a-fA-F]
+   * @param data the output buffer where the hexdecoded bytes will be written. Must be at least length / 2 bytes long.
+   * @param data_length pointer to the length of data the data buffer. It will be filled with the length of the decoded bytes.
+   * @param hex the hexencoded string
+   * @param hex_length the length of hex
+   * @return true on success
+   */
+  inline static bool from_hex(uint8_t* data, size_t* data_length, const char* hex, size_t hex_length) {
+    if (*data_length < hex_length / 2) {
+      return false;
+    }
+    uint8_t n1;
+    bool found_first_nibble = false;
+    *data_length = 0;
+    for (size_t i = 0; i < hex_length; i++) {
+      const uint8_t byte = static_cast<uint8_t>(hex[i]);
+      if (byte > 127) {
+        continue;
+      }
+      uint8_t n = hex_lut[byte];
+      if (n != SKIP) {
+        if (found_first_nibble) {
+          data[(*data_length)++] = n1 << 4 | n;
+          found_first_nibble = false;
+        } else {
+          n1 = n;
+          found_first_nibble = true;
+        }
+      }
+    }
+    if (found_first_nibble) {
+      return false;
+    }
+    return true;
+  }
+
+  /**
+   * Hexdecodes a string
+   * @param hex the hexencoded string
+   * @param hex_length the length of hex
+   * @return the vector containing the hexdecoded bytes
+   */
+  inline static std::vector<uint8_t> from_hex(const char* hex, size_t hex_length) {
+    std::vector<uint8_t> decoded(hex_length / 2);
+    size_t data_length = decoded.size();
+    if (!from_hex(decoded.data(), &data_length, hex, hex_length)) {
+      throw std::runtime_error("Hexencoded string is malformatted");
+    }
+    decoded.resize(data_length);
+    return decoded;
+  }
+
+  /**
+   * Hexdecodes a string
+   * @param hex the hexencoded string
+   * @return the hexdecoded string
+   */
+  inline static std::string from_hex(const std::string& hex) {
+    auto data = from_hex(hex.data(), hex.length());
+    return std::string(reinterpret_cast<char*>(data.data()), data.size());
+  }
+
+  /**
+   * Hexencodes bytes and writes the result to hex
+   * @param hex the output buffer where the hexencoded string will be written. Must be at least length * 2 bytes long.
+   * @param data the bytes to be hexencoded
+   * @param length the length of data. Must not be larger than std::numeric_limits<size_t>::max()
+   * @param uppercase whether the hexencoded string should be upper case
+   */
+  inline static void to_hex(char* hex, const uint8_t* data, size_t length, bool uppercase) {
+    for (size_t i = 0; i < length; i++) {
+      hex[i * 2] = nibble_to_hex(data[i] >> 4, uppercase);
+      hex[i * 2 + 1] = nibble_to_hex(data[i] & 0xf, uppercase);
+    }
+  }
+
+  /**
+   * Creates a hexencoded string from data
+   * @param data the bytes to be hexencoded
+   * @param length the length of the data
+   * @param uppercase whether the hexencoded string should be upper case
+   * @return the hexencoded string
+   */
+  inline static std::string to_hex(const uint8_t* data, size_t length, bool uppercase = false) {
+    if (length > (std::numeric_limits<size_t>::max)() / 2) {
+      throw std::length_error("Data is too large to be hexencoded");
+    }
+    std::vector<char> buf(length * 2);
+    to_hex(buf.data(), data, length, uppercase);
+    return std::string(buf.data(), buf.size());
+  }
+
+  /**
+   * Hexencodes a string
+   * @param str the string to be hexencoded
+   * @param uppercase whether the hexencoded string should be upper case
+   * @return the hexencoded string
+   */
+  inline static std::string to_hex(const std::string& str, bool uppercase = false) {
+    return to_hex(reinterpret_cast<const uint8_t*>(str.data()), str.length(), uppercase);
+  }
+
+  static std::string replaceMap(std::string source_string, const std::map<std::string, std::string> &replace_map);
+
+ private:
+  inline static char nibble_to_hex(uint8_t nibble, bool uppercase) {
+    if (nibble < 10) {
+      return '0' + nibble;
+    } else {
+      return (uppercase ? 'A' : 'a') + nibble - 10;
+    }
+  }
+
+  static constexpr uint8_t SKIP = 255;
+  static constexpr uint8_t hex_lut[128] =
+      {SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+       0x08, 0x09, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
+       SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP};
+
 };
 
 } /* namespace utils */
diff --git a/libminifi/src/utils/StringUtils.cpp b/libminifi/src/utils/StringUtils.cpp
new file mode 100644
index 0000000..788327b
--- /dev/null
+++ b/libminifi/src/utils/StringUtils.cpp
@@ -0,0 +1,162 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+bool StringUtils::StringToBool(std::string input, bool &output) {
+  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
+  std::istringstream(input) >> std::boolalpha >> output;
+  return output;
+}
+
+std::string StringUtils::trim(std::string s) {
+  return trimRight(trimLeft(s));
+}
+
+std::vector<std::string> StringUtils::split(const std::string &str, const std::string &delimiter) {
+  std::vector<std::string> result;
+  auto curr = str.begin();
+  auto end = str.end();
+  auto is_func = [delimiter](int s) {
+    return delimiter.at(0) == s;
+  };
+  while (curr != end) {
+    curr = std::find_if_not(curr, end, is_func);
+    if (curr == end) {
+      break;
+    }
+    auto next = std::find_if(curr, end, is_func);
+    result.push_back(std::string(curr, next));
+    curr = next;
+  }
+
+  return result;
+}
+
+bool StringUtils::StringToFloat(std::string input, float &output, FailurePolicy cp /*= RETURN*/) {
+  try {
+    output = std::stof(input);
+  } catch (const std::invalid_argument &ie) {
+    switch (cp) {
+      case RETURN:
+      case NOTHING:
+        return false;
+      case EXIT:
+        exit(1);
+      case EXCEPT:
+        throw ie;
+    }
+  } catch (const std::out_of_range &ofr) {
+    switch (cp) {
+      case RETURN:
+      case NOTHING:
+        return false;
+      case EXIT:
+        exit(1);
+      case EXCEPT:
+        throw ofr;
+    }
+  }
+
+  return true;
+}
+
+std::string StringUtils::replaceEnvironmentVariables(std::string& original_string) {
+  int32_t beg_seq = 0;
+  int32_t end_seq = 0;
+  std::string source_string = original_string;
+  do {
+    beg_seq = source_string.find("${", beg_seq);
+    if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') {
+      beg_seq += 2;
+      continue;
+    }
+    if (beg_seq < 0)
+      break;
+    end_seq = source_string.find("}", beg_seq + 2);
+    if (end_seq < 0)
+      break;
+    if (end_seq - (beg_seq + 2) < 0) {
+      beg_seq += 2;
+      continue;
+    }
+    const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2));
+    const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1);
+    if (env_field.empty()) {
+      continue;
+    }
+    const auto strVal = std::getenv(env_field.c_str());
+    std::string env_value;
+    if (strVal != nullptr)
+      env_value = strVal;
+    source_string = replaceAll(source_string, env_field_wrapped, env_value);
+    beg_seq = 0;  // restart
+  } while (beg_seq >= 0);
+
+  source_string = replaceAll(source_string, "\\$", "$");
+
+  return source_string;
+}
+
+std::string& StringUtils::replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) {
+  std::size_t loc = 0;
+  std::size_t lastFound;
+  while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) {
+    source_string.replace(lastFound, from_string.size(), to_string);
+    loc = lastFound + to_string.size();
+  }
+  return source_string;
+}
+
+std::string StringUtils::replaceMap(std::string source_string, const std::map<std::string, std::string> &replace_map) {
+  auto result_string = source_string;
+
+  std::vector<std::pair<size_t, std::pair<size_t, std::string>>> replacements;
+  for (const auto &replace_pair : replace_map) {
+    size_t replace_pos = 0;
+    while ((replace_pos = source_string.find(replace_pair.first, replace_pos)) != std::string::npos) {
+      replacements.emplace_back(std::make_pair(replace_pos, std::make_pair(replace_pair.first.length(), replace_pair.second)));
+      replace_pos += replace_pair.first.length();
+    }
+  }
+
+  std::sort(replacements.begin(), replacements.end(), [](const std::pair<size_t, std::pair<size_t, std::string>> a,
+                                                         const std::pair<size_t, std::pair<size_t, std::string>> &b) {
+    return a.first > b.first;
+  });
+
+  for (const auto &replacement : replacements) {
+    result_string = source_string.replace(replacement.first, replacement.second.first, replacement.second.second);
+  }
+
+  return result_string;
+}
+
+constexpr uint8_t StringUtils::SKIP;
+constexpr uint8_t StringUtils::hex_lut[128];
+
+} /* namespace utils */
+} /* namespace minifi */
+} /* namespace nifi */
+} /* namespace apache */
+} /* namespace org */
diff --git a/libminifi/test/pcap-tests/CMakeLists.txt b/libminifi/test/pcap-tests/CMakeLists.txt
index 14b4528..f66333c 100644
--- a/libminifi/test/pcap-tests/CMakeLists.txt
+++ b/libminifi/test/pcap-tests/CMakeLists.txt
@@ -29,17 +29,17 @@ FOREACH(testfile ${PCAP_TESTS})
   	target_include_directories(${testfilename} PRIVATE BEFORE "${CMAKE_BINARY_DIR}/extensions/pcap/pcap++/Dist/header/")
     createTests("${testfilename}")
     if(APPLE)    
-    	target_link_libraries ("${testfilename}" -Wl,-all_load minifi-pcap )
+        target_link_libraries ("${testfilename}" -Wl,-all_load minifi-pcap minifi-standard-processors)
 	else()
-  		target_link_libraries ("${testfilename}" -Wl,--whole-archive minifi-pcap -Wl,--no-whole-archive)
+        target_link_libraries ("${testfilename}" -Wl,--whole-archive minifi-pcap minifi-standard-processors -Wl,--no-whole-archive)
   	endif()
   MATH(EXPR PCAP_INT_TEST_COUNT "${PCAP_INT_TEST_COUNT}+1")
 ENDFOREACH()
 
 message("-- Finished building ${PCAP_INT_TEST_COUNT} libPCAP test file(s)...")
 if(APPLE)    
-	add_test(NAME PcapTest COMMAND PcapTest "${TEST_RESOURCES}/TestPcap.yml"  "${TEST_RESOURCES}/")
+    add_test(NAME PcapTest COMMAND PcapTest "${TEST_RESOURCES}/TestPcap.yml"  "${TEST_RESOURCES}/")
 else()
-	add_test(NAME PcapTest COMMAND PcapTest "${TEST_RESOURCES}/TestPcapLinux.yml"  "${TEST_RESOURCES}/")
+    add_test(NAME PcapTest COMMAND PcapTest "${TEST_RESOURCES}/TestPcapLinux.yml"  "${TEST_RESOURCES}/")
 endif()
 
diff --git a/libminifi/test/pcap-tests/PcapTest.cpp b/libminifi/test/pcap-tests/PcapTest.cpp
index 3c6fd27..eea0364 100644
--- a/libminifi/test/pcap-tests/PcapTest.cpp
+++ b/libminifi/test/pcap-tests/PcapTest.cpp
@@ -40,6 +40,7 @@
 #include "../unit/ProvenanceTestHelper.h"
 #include "io/StreamFactory.h"
 #include "core/ConfigurableComponent.h"
+#include "core/state/ProcessorController.h"
 #include "../integration/IntegrationBase.h"
 #include "CapturePacket.h"
 
@@ -71,20 +72,20 @@ class PcapTestHarness : public IntegrationBase {
     assert(LogTestController::getInstance().contains("Accepting ") == true && LogTestController::getInstance().contains("because it matches .*") );
   }
 
-  void queryRootProcessGroup(std::shared_ptr<core::ProcessGroup> pg) {
-    std::shared_ptr<core::Processor> proc = pg->findProcessor("pcap");
-    assert(proc != nullptr);
-
-    auto inv = std::dynamic_pointer_cast<minifi::processors::CapturePacket>(proc);
-    assert(inv != nullptr);
-
-    configuration->set(minifi::processors::CapturePacket::BaseDir.getName(), dir);
-    configuration->set(minifi::processors::CapturePacket::NetworkController.getName(), ".*");
-    configuration->set("nifi.c2.enable", "false");
+  void updateProperties(std::shared_ptr<minifi::FlowController> fc) {
+    auto components = fc->getComponents("pcap");
+    for (const auto& component : components) {
+      auto proccontroller = std::dynamic_pointer_cast<minifi::state::ProcessorController>(component);
+      if (proccontroller) {
+        auto processor = proccontroller->getProcessor();
+        processor->setProperty(minifi::processors::CapturePacket::BaseDir.getName(), dir);
+        processor->setProperty(minifi::processors::CapturePacket::NetworkControllers.getName(), ".*");
+      }
+    }
   }
 
  protected:
-  char *dir;
+  std::string dir;
   TestController testController;
 };
 
diff --git a/libminifi/test/unit/StringUtilsTests.cpp b/libminifi/test/unit/StringUtilsTests.cpp
index 6486f29..16704b4 100644
--- a/libminifi/test/unit/StringUtilsTests.cpp
+++ b/libminifi/test/unit/StringUtilsTests.cpp
@@ -20,6 +20,9 @@
 #include <list>
 #include <vector>
 #include <cstdlib>
+#include <random>
+#include <algorithm>
+#include <cstdint>
 #include "../TestBase.h"
 #include "core/Core.h"
 #include "utils/StringUtils.h"
@@ -112,3 +115,61 @@ TEST_CASE("TestStringUtils::testJoin", "[test string join]") {
 
   REQUIRE(StringUtils::join("this separator wont appear", std::vector<std::string>()) == "");
 }
+
+TEST_CASE("TestStringUtils::testHexEncode", "[test hex encode]") {
+  REQUIRE("" == StringUtils::to_hex(""));
+  REQUIRE("6f" == StringUtils::to_hex("o"));
+  REQUIRE("666f6f626172" == StringUtils::to_hex("foobar"));
+  REQUIRE("000102030405060708090a0b0c0d0e0f" == StringUtils::to_hex({0x00, 0x01, 0x02, 0x03,
+                                                                     0x04, 0x05, 0x06, 0x07,
+                                                                     0x08, 0x09, 0x0a, 0x0b,
+                                                                     0x0c, 0x0d, 0x0e, 0x0f}));
+  REQUIRE("6F" == StringUtils::to_hex("o", true /*uppercase*/));
+  REQUIRE("666F6F626172" == StringUtils::to_hex("foobar", true /*uppercase*/));
+  REQUIRE("000102030405060708090A0B0C0D0E0F" == StringUtils::to_hex({0x00, 0x01, 0x02, 0x03,
+                                                                     0x04, 0x05, 0x06, 0x07,
+                                                                     0x08, 0x09, 0x0a, 0x0b,
+                                                                     0x0c, 0x0d, 0x0e, 0x0f}, true /*uppercase*/));
+}
+
+TEST_CASE("TestStringUtils::testHexDecode", "[test hex decode]") {
+  REQUIRE("" == StringUtils::from_hex(""));
+  REQUIRE("o" == StringUtils::from_hex("6f"));
+  REQUIRE("o" == StringUtils::from_hex("6F"));
+  REQUIRE("foobar" == StringUtils::from_hex("666f6f626172"));
+  REQUIRE("foobar" == StringUtils::from_hex("666F6F626172"));
+  REQUIRE("foobar" == StringUtils::from_hex("66:6F:6F:62:61:72"));
+  REQUIRE("foobar" == StringUtils::from_hex("66 6F 6F 62 61 72"));
+  REQUIRE(std::string({0x00, 0x01, 0x02, 0x03,
+                       0x04, 0x05, 0x06, 0x07,
+                       0x08, 0x09, 0x0a, 0x0b,
+                       0x0c, 0x0d, 0x0e, 0x0f}) == StringUtils::from_hex("000102030405060708090a0b0c0d0e0f"));
+  REQUIRE(std::string({0x00, 0x01, 0x02, 0x03,
+                       0x04, 0x05, 0x06, 0x07,
+                       0x08, 0x09, 0x0a, 0x0b,
+                       0x0c, 0x0d, 0x0e, 0x0f}) == StringUtils::from_hex("000102030405060708090A0B0C0D0E0F"));
+  try {
+    StringUtils::from_hex("666f6f62617");
+    abort();
+  } catch (std::exception& e) {
+    REQUIRE(std::string("Hexencoded string is malformatted") == e.what());
+  }
+  try {
+    StringUtils::from_hex("666f6f6261 7");
+    abort();
+  } catch (std::exception& e) {
+    REQUIRE(std::string("Hexencoded string is malformatted") == e.what());
+  }
+}
+
+TEST_CASE("TestStringUtils::testHexEncodeDecode", "[test hex encode decode]") {
+  std::mt19937 gen(std::random_device { }());
+  const bool uppercase = gen() % 2;
+  const size_t length = gen() % 1024;
+  std::vector<uint8_t> data(length);
+  std::generate_n(data.begin(), data.size(), [&]() -> uint8_t {
+    return gen() % 256;
+  });
+  auto hex = utils::StringUtils::to_hex(data.data(), data.size(), uppercase);
+  REQUIRE(data == utils::StringUtils::from_hex(hex.data(), hex.size()));
+}