Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor HttpClient and add unit tests #102

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/
package com.wazuh.commandmanager;

import com.wazuh.commandmanager.config.reader.ConfigReader;
import com.wazuh.commandmanager.index.CommandIndex;
import com.wazuh.commandmanager.rest.action.RestPostCommandAction;
import com.wazuh.commandmanager.scheduler.JobScheduler;
import com.wazuh.commandmanager.utils.httpclient.HttpRestClient;
import com.wazuh.commandmanager.utils.httpclient.HttpRestClientDemo;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNodes;
Expand All @@ -32,6 +32,7 @@
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -65,8 +66,11 @@ public Collection<Object> createComponents(
Supplier<RepositoriesService> repositoriesServiceSupplier
) {
this.commandIndex = new CommandIndex(client, clusterService, threadPool);
ConfigReader configReader = new ConfigReader("httpbin.org", 80, "/post", "admin", "admin");
JobScheduler jobScheduler = new JobScheduler(threadPool, configReader);

// HttpRestClient stuff
String uri = "https://httpbin.org/post";
String payload = "{\"message\": \"Hello world!\"}";
HttpRestClientDemo.run(uri, payload);
return Collections.emptyList();
}

Expand All @@ -81,4 +85,15 @@ public List<RestHandler> getRestHandlers(
) {
return Collections.singletonList(new RestPostCommandAction(this.commandIndex));
}

/**
* Close the resources opened by this plugin.
*
* @throws IOException if the plugin failed to close its resources
*/
@Override
public void close() throws IOException {
super.close();
HttpRestClient.getInstance().stopHttpAsyncClient();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

/**
* Class to manage the Command Manager index and index template.
*/
public class CommandIndex implements IndexingOperationListener {

private static final Logger logger = LogManager.getLogger(CommandIndex.class);
Expand All @@ -53,7 +56,7 @@ public CommandIndex(Client client, ClusterService clusterService, ThreadPool thr
}

/**
* @param command: A Command model object
* @param command A Command model object
* @return A CompletableFuture with the RestStatus response from the operation
*/
public CompletableFuture<RestStatus> asyncCreate(Command command) {
Expand Down Expand Up @@ -95,7 +98,10 @@ public CompletableFuture<RestStatus> asyncCreate(Command command) {
}

/**
* @return
* Checks for the existence of the given index template in the cluster.
*
* @param template_name index template name within the resources folder
* @return whether the index template exists.
*/
public boolean indexTemplateExists(String template_name) {
Map<String, IndexTemplateMetadata> templates = this.clusterService
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package com.wazuh.commandmanager.utils.httpclient;

import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class HttpResponseCallback implements FutureCallback<SimpleHttpResponse> {

private static final Logger log = LogManager.getLogger(HttpResponseCallback.class);

/**
* The Http get request.
*/
SimpleHttpRequest httpRequest;

/**
* The Error message.
*/
String errorMessage;

public HttpResponseCallback(SimpleHttpRequest httpRequest,
String errorMessage) {
this.httpRequest = httpRequest;
this.errorMessage = errorMessage;
}

@Override
public void completed(SimpleHttpResponse response) {
log.debug("{}->{}", httpRequest, new StatusLine(response));
log.debug("Got response: {}", response.getBody());
}

@Override
public void failed(Exception ex) {
log.error("{}->{}", httpRequest, ex);
// throw new HttpException(errorMessage, ex);
}

@Override
public void cancelled() {
log.debug(httpRequest + " cancelled");
}
}
Loading