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 1 commit
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 @@ -95,6 +95,8 @@ public CompletableFuture<RestStatus> asyncCreate(Command command) {
}

/**
*
* @param template_name
* @return
*/
public boolean indexTemplateExists(String template_name) {
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