Skip to content

Commit

Permalink
JENKINS-25558: Notifications Plugin does not honor the "http_proxy" e…
Browse files Browse the repository at this point in the history
…nvironment variable: Fix

This commit partially fixes the problem by using an HTTP Proxy without authentication. In
addition, it does not provide test cases, but it does work properly. The message after using
a new build of this plugin was as follows:

Started by user mdesales
Notifying endpoint 'HTTP:https://zapier.com/hooks/catch/o54xyk/'
Building in workspace /app/installs/jenkins/jobs/Status Notification/workspace
Notifying endpoint 'HTTP:https://zapier.com/hooks/catch/o54xyk/'
Finished: SUCCESS

(Sorry, in a rush to get things done and I can try providing a test with an internal
 http proxy server).

*	modified:   src/main/java/com/tikal/hudson/plugins/notification/Protocol.java
- Verifying if the environment variable "http_proxy" is set. If so, decorate the connection
  with the proxy information before making the HTTP request.
  • Loading branch information
marcellodesales committed Nov 12, 2014
1 parent 399725c commit da26771
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/main/java/com/tikal/hudson/plugins/notification/Protocol.java
Expand Up @@ -14,10 +14,20 @@
package com.tikal.hudson.plugins.notification;


import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;

import javax.xml.bind.DatatypeConverter;


public enum Protocol {
Expand Down Expand Up @@ -53,7 +63,27 @@ protected void send(String url, byte[] data, int timeout, boolean isJson) throws
throw new IllegalArgumentException("Not an http(s) url: " + url);
}

HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection();
// Verifying if the HTTP_PROXY is available
final String httpProxyUrl = System.getenv().get("http_proxy");
URL proxyUrl = null;
if (httpProxyUrl != null && httpProxyUrl.length() > 0) {
proxyUrl = new URL(httpProxyUrl);
if (!proxyUrl.getProtocol().startsWith("http")) {
throw new IllegalArgumentException("Not an http(s) url: " + httpProxyUrl);
}
}

HttpURLConnection connection = null;
if (proxyUrl == null) {
connection = (HttpURLConnection) targetUrl.openConnection();

} else {
// Proxy connection to the address provided
final int proxyPort = proxyUrl.getPort() > 0 ? proxyUrl.getPort() : 80;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrl.getHost(), proxyPort));
connection = (HttpURLConnection) targetUrl.openConnection(proxy);
}

connection.setRequestProperty("Content-Type", String.format( "application/%s;charset=UTF-8", isJson ? "json" : "xml" ));
String userInfo = targetUrl.getUserInfo();
if (null != userInfo) {
Expand Down

0 comments on commit da26771

Please sign in to comment.