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

fix: sendCloudEvent method of sinks.HttpSink #10

Merged
merged 1 commit into from
Dec 15, 2023
Merged
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
8 changes: 5 additions & 3 deletions src/main/java/io/jenkins/plugins/cdevents/sinks/HttpSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import hudson.ProxyConfiguration;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -26,24 +28,24 @@

private static final Logger LOGGER = Logger.getLogger("HttpSink");
private static final String sinkUrl = CDEventsGlobalConfig.get().getHttpSinkUrl();
private final String host = Jenkins.get().proxy.name;
private final int port = Jenkins.get().proxy.port;
private final ProxyConfiguration proxy = Jenkins.get().proxy;

@Override
public void sendCloudEvent(CloudEvent cloudEvent) throws IOException {
LOGGER.log(Level.INFO, "Now attempting to send to the HTTP endpoint " + sinkUrl
+ " the following CloudEvent " + cloudEvent);
HttpPost httpPost = new HttpPost(sinkUrl);
// TODO need to test the string conversion and using the Apache HTTP library is
// successful
StringEntity entity = new StringEntity(cloudEvent.toString());
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

HttpHost httpProxy = (proxy != null ? new HttpHost(proxy.name, proxy.port) : null);
try (CloseableHttpClient client = HttpClientBuilder.create()
.useSystemProperties()
.setProxy(new HttpHost(host, port))
.setProxy(httpProxy)

Check warning on line 48 in src/main/java/io/jenkins/plugins/cdevents/sinks/HttpSink.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 31-48 are not covered by tests
.build();
CloseableHttpResponse response = client.execute(httpPost)) {
JSONObject sinkResponse = new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
Expand Down