From 070d148faad709c565b7b8c2daf436e8d6311e6e Mon Sep 17 00:00:00 2001 From: wuhuizuo Date: Mon, 30 Oct 2023 11:03:56 +0000 Subject: [PATCH] fix: sendCloudEvent method of sinks.HttpSink Should judge null when get jenkins http proxy setting. Fixes #9 --- .../java/io/jenkins/plugins/cdevents/sinks/HttpSink.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/cdevents/sinks/HttpSink.java b/src/main/java/io/jenkins/plugins/cdevents/sinks/HttpSink.java index 14d039c..30eefbb 100644 --- a/src/main/java/io/jenkins/plugins/cdevents/sinks/HttpSink.java +++ b/src/main/java/io/jenkins/plugins/cdevents/sinks/HttpSink.java @@ -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; @@ -26,8 +28,7 @@ public class HttpSink extends CDEventsSink { 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 { @@ -41,9 +42,10 @@ public void sendCloudEvent(CloudEvent cloudEvent) throws IOException { 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) .build(); CloseableHttpResponse response = client.execute(httpPost)) { JSONObject sinkResponse = new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8"));