@Override
  public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
      return;
    }

    final ProcessorLog logger = getLogger();

    boolean encode = context.getProperty(MODE).getValue().equalsIgnoreCase(ENCODE_MODE);
    String encoding = context.getProperty(ENCODING).getValue();
    StreamCallback encoder = null;

    // Select the encoder/decoder to use
    if (encode) {
      if (encoding.equalsIgnoreCase(BASE64_ENCODING)) {
        encoder = new EncodeBase64();
      } else if (encoding.equalsIgnoreCase(BASE32_ENCODING)) {
        encoder = new EncodeBase32();
      } else if (encoding.equalsIgnoreCase(HEX_ENCODING)) {
        encoder = new EncodeHex();
      }
    } else {
      if (encoding.equalsIgnoreCase(BASE64_ENCODING)) {
        encoder = new DecodeBase64();
      } else if (encoding.equalsIgnoreCase(BASE32_ENCODING)) {
        encoder = new DecodeBase32();
      } else if (encoding.equalsIgnoreCase(HEX_ENCODING)) {
        encoder = new DecodeHex();
      }
    }

    if (encoder == null) {
      logger.warn(
          "Unknown operation: {} {}", new Object[] {encode ? "encode" : "decode", encoding});
      return;
    }

    try {
      final StopWatch stopWatch = new StopWatch(true);
      flowFile = session.write(flowFile, encoder);

      logger.info("Successfully {} {}", new Object[] {encode ? "encoded" : "decoded", flowFile});
      session
          .getProvenanceReporter()
          .modifyContent(flowFile, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
      session.transfer(flowFile, REL_SUCCESS);
    } catch (Exception e) {
      logger.error(
          "Failed to {} {} due to {}", new Object[] {encode ? "encode" : "decode", flowFile, e});
      session.transfer(flowFile, REL_FAILURE);
    }
  }
Exemple #2
0
  @Override
  public void onTrigger(final ProcessContext context, final ProcessSession session)
      throws ProcessException {
    final BlockingQueue<String> partitionIds = this.partitionNames;
    final String partitionId = partitionIds.poll();
    if (partitionId == null) {
      getLogger().debug("No partitions available");
      return;
    }

    final StopWatch stopWatch = new StopWatch(true);
    try {

      final Iterable<EventData> receivedEvents = receiveEvents(context, partitionId);
      if (receivedEvents == null) {
        return;
      }

      for (final EventData eventData : receivedEvents) {
        if (null != eventData) {

          final Map<String, String> attributes = new HashMap<>();
          FlowFile flowFile = session.create();
          EventData.SystemProperties systemProperties = eventData.getSystemProperties();

          if (null != systemProperties) {
            attributes.put(
                "eventhub.enqueued.timestamp",
                String.valueOf(eventData.getSystemProperties().getEnqueuedTime()));
            attributes.put("eventhub.offset", eventData.getSystemProperties().getOffset());
            attributes.put(
                "eventhub.sequence",
                String.valueOf(eventData.getSystemProperties().getSequenceNumber()));
          }

          attributes.put("eventhub.name", context.getProperty(EVENT_HUB_NAME).getValue());
          attributes.put("eventhub.partition", partitionId);

          flowFile = session.putAllAttributes(flowFile, attributes);
          flowFile =
              session.write(
                  flowFile,
                  out -> {
                    out.write(eventData.getBody());
                  });

          session.transfer(flowFile, REL_SUCCESS);

          final String namespace = context.getProperty(NAMESPACE).getValue();
          final String eventHubName = context.getProperty(EVENT_HUB_NAME).getValue();
          final String consumerGroup = context.getProperty(CONSUMER_GROUP).getValue();
          final String transitUri =
              "amqps://"
                  + namespace
                  + ".servicebus.windows.net"
                  + "/"
                  + eventHubName
                  + "/ConsumerGroups/"
                  + consumerGroup
                  + "/Partitions/"
                  + partitionId;
          session
              .getProvenanceReporter()
              .receive(flowFile, transitUri, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
        }
      }
    } finally {
      partitionIds.offer(partitionId);
    }
  }