Example #1
0
 /**
  * Converts the given text using the supplied method.
  *
  * @param method The method to use for conversion
  * @param text The text to be converted
  * @return The converted text
  */
 public static String convertText(final ConversionMethod method, final String text) {
   switch (method) {
     case PLAIN:
       {
         return text;
       }
     case HEX_ENCODE:
       {
         return ConversionUtils.stringToHex(text);
       }
     case HEX_DECODE:
       {
         return ConversionUtils.hexToStringNoException(text);
       }
     case BASE_64_ENCODE:
       {
         return ConversionUtils.stringToBase64(text);
       }
     case BASE_64_DECODE:
       {
         return ConversionUtils.base64ToString(text);
       }
     default:
       return text;
   }
 }
  /**
   * Turns the given list of LoggedMqttMessages into ReceivedMqttMessages.
   *
   * @param list List of logged messages to progress
   * @param progress The progress updater to call with updated progress
   * @param current Current progress
   * @param max Maximum progress value
   * @return List of MQTT message objects (ReceivedMqttMessage)
   */
  public static List<BaseMqttMessage> processMessageLog(
      final List<LoggedMqttMessage> list,
      final ProgressUpdater progress,
      final long current,
      final long max) {
    final List<BaseMqttMessage> mqttMessageList = new ArrayList<BaseMqttMessage>();
    long item = 0;

    // Process the messages
    for (final LoggedMqttMessage loggedMessage : list) {
      if (progress != null) {
        item++;
        if (item % 1000 == 0) {
          progress.update(current + item, max);
        }
      }

      final MqttMessage mqttMessage = new MqttMessage();

      if (logger.isTraceEnabled()) {
        logger.trace("Processing message with payload {}", loggedMessage.getValue());
      }

      if (Boolean.TRUE.equals(loggedMessage.isEncoded())) {
        mqttMessage.setPayload(Base64.decodeBase64(loggedMessage.getValue()));
      } else {
        mqttMessage.setPayload(ConversionUtils.stringToArray(loggedMessage.getValue()));
      }

      mqttMessage.setQos(loggedMessage.getQos() == null ? 0 : loggedMessage.getQos());
      mqttMessage.setRetained(
          loggedMessage.isRetained() == null ? false : loggedMessage.isRetained());

      mqttMessageList.add(
          new BaseMqttMessage(
              loggedMessage.getId(),
              loggedMessage.getTopic(),
              mqttMessage,
              new Date(loggedMessage.getTimestamp())));
    }
    logger.info("Message log - processed {} MQTT messages", list.size());

    return mqttMessageList;
  }