Skip to content

Commit

Permalink
Apply spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
f-galland committed Dec 19, 2024
1 parent c1ee277 commit 299afe3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@ public void handlePage(SearchResponse searchResponse) throws IllegalStateExcepti
NamedXContentRegistry.EMPTY,
DeprecationHandler.IGNORE_DEPRECATIONS,
hit.getSourceRef(),
XContentType.JSON
);
XContentType.JSON);
// Parse the hit's order
Order order = Order.parseSearchHit(hit);
// Add the current order to the XContentBuilder array
assert order != null;
order.toXContent(builder,ToXContent.EMPTY_PARAMS);
order.toXContent(builder, ToXContent.EMPTY_PARAMS);
}
// Close the object and prepare it for delivery
builder.endArray();
Expand All @@ -149,20 +148,20 @@ public void handlePage(SearchResponse searchResponse) throws IllegalStateExcepti
} catch (IOException e) {
log.error("Error building payload from hit: {}", e.getMessage());
}
final SimpleHttpResponse response = deliverOrders(payload);
if (response == null) {
log.error("No reply from server.");
return;
}
log.info("Server replied with {}. Updating orders' status.", response.getCode());
Status status = Status.FAILURE;
if (List.of(RestStatus.CREATED, RestStatus.ACCEPTED, RestStatus.OK)
.contains(RestStatus.fromCode(response.getCode()))) {
status = Status.SENT;
}
for (SearchHit hit : searchHits) {
this.setSentStatus(hit, status);
}
final SimpleHttpResponse response = deliverOrders(payload);
if (response == null) {
log.error("No reply from server.");
return;
}
log.info("Server replied with {}. Updating orders' status.", response.getCode());
Status status = Status.FAILURE;
if (List.of(RestStatus.CREATED, RestStatus.ACCEPTED, RestStatus.OK)
.contains(RestStatus.fromCode(response.getCode()))) {
status = Status.SENT;
}
for (SearchHit hit : searchHits) {
this.setSentStatus(hit, status);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.*;
import org.opensearch.search.SearchHit;

import java.io.IOException;

import org.opensearch.search.SearchHit;
import reactor.util.annotation.NonNull;

public class Order implements ToXContent {
Expand All @@ -42,11 +42,15 @@ public class Order implements ToXContent {

/**
* Default constructor
*
* @param source String field representing the origin of the command order
* @param target Object containing the destination's type and id. It is handled by its own model class
* @param target Object containing the destination's type and id. It is handled by its own model
* class
* @param user The requester of the command
* @param action An object containing the actual executable plus arguments and version. Handled by its own model class
* @param documentId The document ID from the index that holds commands. Used by the agent to report back the results of the action
* @param action An object containing the actual executable plus arguments and version. Handled
* by its own model class
* @param documentId The document ID from the index that holds commands. Used by the agent to
* report back the results of the action
*/
public Order(
@NonNull String source,
Expand All @@ -61,29 +65,27 @@ public Order(
this.documentId = documentId;
}


/**
* Parses a SearchHit into an order as expected by a Wazuh Agent
*
* @param hit The SearchHit result of a search
* @return An Order Object in accordance with the data model
*/
public static Order parseSearchHit(SearchHit hit)
{
public static Order parseSearchHit(SearchHit hit) {
try {
XContentParser parser =
XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.IGNORE_DEPRECATIONS,
hit.getSourceRef(),
XContentType.JSON
);
XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.IGNORE_DEPRECATIONS,
hit.getSourceRef(),
XContentType.JSON);
Command command = null;
// Iterate over the JsonXContentParser's JsonToken until we hit null,
// which corresponds to end of data
while (parser.nextToken() != null) {
// Look for FIELD_NAME JsonToken s
if (!parser.currentToken().equals(XContentParser.Token.FIELD_NAME)) {
continue;
continue;
}
String fieldName = parser.currentName();
if (fieldName.equals(Command.COMMAND)) {
Expand All @@ -96,12 +98,11 @@ public static Order parseSearchHit(SearchHit hit)
// Create a new Order object with the Command's fields
assert command != null;
return new Order(
command.getSource(),
command.getTarget(),
command.getUser(),
command.getAction(),
hit.getId()
);
command.getSource(),
command.getTarget(),
command.getUser(),
command.getAction(),
hit.getId());
} catch (IOException e) {
log.error("Order could not be parsed: {}", e.getMessage());
}
Expand All @@ -110,14 +111,14 @@ public static Order parseSearchHit(SearchHit hit)

/**
* Used to serialize the Order's contents.
*
* @param builder The builder object we will add our Json to
* @param params Not used. Required by the interface.
* @return XContentBuilder with a Json object including this Order's fields
* @throws IOException Rethrown from IOException's XContentBuilder methods
*/
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params)
throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(SOURCE, this.source);
builder.field(USER, this.user);
Expand Down

0 comments on commit 299afe3

Please sign in to comment.