Skip to content

Commit

Permalink
Merge pull request #16 from marcellodesales/master
Browse files Browse the repository at this point in the history
JENKINS-25558: Notifications Plugin does not honor the "http_proxy" env Variable
  • Loading branch information
hagzag committed Feb 25, 2015
2 parents d3d00a5 + da26771 commit c5c81d0
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 c5c81d0

Please sign in to comment.