From a2f965e6f6a543092609935d3ee6b2e19241eed5 Mon Sep 17 00:00:00 2001 From: f-galland Date: Thu, 19 Dec 2024 13:49:32 -0300 Subject: [PATCH] Clean up and refactor orders code --- .../jobscheduler/SearchThread.java | 37 +----------- .../wazuh/commandmanager/model/Command.java | 57 +++++++++--------- .../com/wazuh/commandmanager/model/Order.java | 33 ++++++----- .../wazuh/commandmanager/model/Orders.java | 58 +++++++++++++++++++ 4 files changed, 106 insertions(+), 79 deletions(-) create mode 100644 plugins/command-manager/src/main/java/com/wazuh/commandmanager/model/Orders.java diff --git a/plugins/command-manager/src/main/java/com/wazuh/commandmanager/jobscheduler/SearchThread.java b/plugins/command-manager/src/main/java/com/wazuh/commandmanager/jobscheduler/SearchThread.java index 462b5f3..d9a773d 100644 --- a/plugins/command-manager/src/main/java/com/wazuh/commandmanager/jobscheduler/SearchThread.java +++ b/plugins/command-manager/src/main/java/com/wazuh/commandmanager/jobscheduler/SearchThread.java @@ -28,9 +28,6 @@ import org.opensearch.client.Client; import org.opensearch.common.action.ActionFuture; import org.opensearch.common.unit.TimeValue; -import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentHelper; -import org.opensearch.common.xcontent.XContentType; import org.opensearch.core.action.ActionListener; import org.opensearch.core.rest.RestStatus; import org.opensearch.core.xcontent.*; @@ -42,7 +39,6 @@ import org.opensearch.search.builder.SearchSourceBuilder; import org.opensearch.search.sort.SortOrder; -import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.security.AccessController; @@ -53,10 +49,7 @@ import java.util.concurrent.ExecutionException; import com.wazuh.commandmanager.CommandManagerPlugin; -import com.wazuh.commandmanager.model.Command; -import com.wazuh.commandmanager.model.Document; -import com.wazuh.commandmanager.model.Order; -import com.wazuh.commandmanager.model.Status; +import com.wazuh.commandmanager.model.*; import com.wazuh.commandmanager.settings.PluginSettings; import com.wazuh.commandmanager.utils.httpclient.AuthHttpRestClient; @@ -121,33 +114,7 @@ public static T getNestedObject(Map map, String key, Class. + */ +package com.wazuh.commandmanager.model; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.common.xcontent.XContentFactory; +import org.opensearch.core.xcontent.ToXContent; +import org.opensearch.core.xcontent.XContentBuilder; +import org.opensearch.search.SearchHit; +import org.opensearch.search.SearchHits; + +import java.io.IOException; +import java.util.Objects; + +public class Orders { + + private static final Logger log = LogManager.getLogger(Orders.class); + + public static String getOrders(SearchHits searchHits) { + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + // Start an XContentBuilder array named "orders" + builder.startObject(); + builder.startArray(Order.ORDERS); + // Iterate over search results + for (SearchHit hit : searchHits) { + // Parse the hit's order + Order order = Order.parseSearchHit(hit); + // Add the current order to the XContentBuilder array + Objects.requireNonNull(order).toXContent(builder, ToXContent.EMPTY_PARAMS); + } + // Close the object and prepare it for delivery + builder.endArray(); + builder.endObject(); + return builder.toString(); + } catch (IOException e) { + log.error("Error building payload from hit: {}", e.getMessage()); + } catch (NullPointerException e) { + log.error( + "Exception found when building order payload. Null Order: {}", e.getMessage()); + } + return null; + } +}