You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by as...@apache.org on 2009/03/12 17:56:18 UTC

svn commit: r752933 - in /synapse/trunk/java: modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOSender.java repository/conf/axis2.xml

Author: asankha
Date: Thu Mar 12 16:56:18 2009
New Revision: 752933

URL: http://svn.apache.org/viewvc?rev=752933&view=rev
Log:
fix SYNAPSE-481 for HTTP proxying

Modified:
    synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOSender.java
    synapse/trunk/java/repository/conf/axis2.xml

Modified: synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOSender.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOSender.java?rev=752933&r1=752932&r2=752933&view=diff
==============================================================================
--- synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOSender.java (original)
+++ synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOSender.java Thu Mar 12 16:56:18 2009
@@ -27,6 +27,7 @@
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.description.TransportOutDescription;
+import org.apache.axis2.description.Parameter;
 import org.apache.axis2.handlers.AbstractHandler;
 import org.apache.axis2.transport.MessageFormatter;
 import org.apache.axis2.transport.OutTransportInfo;
@@ -63,8 +64,7 @@
 import java.net.InetSocketAddress;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.*;
 
 /**
  * NIO transport sender for Axis2 based on HttpCore and NIO extensions
@@ -91,6 +91,16 @@
     private MetricsCollector metrics = new MetricsCollector();
     /** state of the listener */
     private int state = BaseConstants.STOPPED;
+    /** The proxy host */
+    private String proxyHost = null;
+    /** The proxy port */
+    private int proxyPort = 80;
+    /** The list of hosts for which the proxy should be bypassed */
+    private String[] proxyBypassList = null;
+    /** The list of known hosts to bypass proxy */
+    private List<String> knownDirectHosts = new ArrayList<String>();
+    /** The list of known hosts to go via proxy */
+    private List<String> knownProxyHosts = new ArrayList<String>();
 
     /**
      * Initialize the transport sender, and execute reactor in new seperate thread
@@ -105,6 +115,35 @@
         sslContext = getSSLContext(transportOut);
         sslIOSessionHandler = getSSLIOSessionHandler(transportOut);
 
+        // configure proxy settings - only supports HTTP right now (See SYNAPSE-418)
+        if (sslContext == null) {
+            Parameter proxyHostParam = transportOut.getParameter("http.proxyHost");
+            if (proxyHostParam != null || System.getProperty("http.proxyHost") != null) {
+                if (proxyHostParam != null) {
+                    proxyHost = (String) proxyHostParam.getValue();
+                } else {
+                    proxyHost = System.getProperty("http.proxyHost");
+                }
+
+                Parameter proxyPortParam = transportOut.getParameter("http.proxyPort");
+                if (proxyPortParam != null) {
+                    proxyPort = Integer.parseInt((String) proxyPortParam.getValue());
+                } else if (System.getProperty("http.proxyPort") != null) {
+                    proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
+                }
+
+                Parameter bypassList = transportOut.getParameter("http.nonProxyHosts");
+                if (bypassList != null) {
+                    proxyBypassList = ((String) bypassList.getValue()).split("\\|");
+                } else if (System.getProperty("http.nonProxyHosts") != null) {
+                    proxyBypassList = (System.getProperty("http.nonProxyHosts")).split("\\|");
+                }
+
+                log.info("HTTP Sender using Proxy : "
+                    + proxyHost + ":" + proxyPort + " bypassing : " + Arrays.toString(proxyBypassList));
+            }
+        }
+
         HttpParams params = getClientParameters();
         try {
             String prefix = (sslContext == null ? "http" : "https") + "-Sender I/O dispatcher";
@@ -294,6 +333,7 @@
     private void sendAsyncRequest(EndpointReference epr, MessageContext msgContext) throws AxisFault {
         try {
             URL url = new URL(epr.getAddress());
+            String host = url.getHost();
             int port = url.getPort();
             if (port == -1) {
                 // use default
@@ -303,7 +343,7 @@
                     port = 443;
                 }
             }
-            HttpHost httpHost = new HttpHost(url.getHost(), port, url.getProtocol());
+            HttpHost httpHost = new HttpHost(host, port, url.getProtocol());
 
             Axis2HttpRequest axis2Req = new Axis2HttpRequest(epr, httpHost, msgContext);
             Object timeout = msgContext.getProperty(NhttpConstants.SEND_TIMEOUT);
@@ -311,25 +351,43 @@
                 axis2Req.setTimeout( (int) ((Long) timeout).longValue());
             }
 
-            NHttpClientConnection conn = ConnectionPool.getConnection(url.getHost(), port);
+            // do we have a proxy configured?
+            if (proxyHost != null) {
+                // but are we supposed to bypass for this host?
+                if (knownProxyHosts.contains(host)) {
+                    // this has already been found to be a proxy host
+                    host = proxyHost;
+                    port = proxyPort;
+                } else if (knownDirectHosts.contains(host)) {
+                    // do nothing, let this request go directly bypassing proxy
+                } else {
+                    // we are encountering this host:port pair for the first time
+                    if (!isBypass(host)) {
+                        host = proxyHost;
+                        port = proxyPort;
+                    }
+                }
+            }
+            
+            NHttpClientConnection conn = ConnectionPool.getConnection(host, port);
 
             if (conn == null) {
-                ioReactor.connect(new InetSocketAddress(url.getHost(), port),
+                ioReactor.connect(new InetSocketAddress(host, port),
                     null, axis2Req, sessionRequestCallback);
                 if (log.isDebugEnabled()) {
-                    log.debug("A new connection established to : " + url.getHost() + ":" + port);
+                    log.debug("A new connection established to : " + host + ":" + port);
                 }
             } else {
                 try {
                     handler.submitRequest(conn, axis2Req);
                     if (log.isDebugEnabled()) {
-                        log.debug("An existing connection reused to : " + url.getHost() + ":" + port);
+                        log.debug("An existing connection reused to : " + host + ":" + port);
                     }                    
                 } catch (ConnectionClosedException e) {
-                    ioReactor.connect(new InetSocketAddress(url.getHost(), port),
+                    ioReactor.connect(new InetSocketAddress(host, port),
                         null, axis2Req, sessionRequestCallback);
                     if (log.isDebugEnabled()) {
-                        log.debug("A new connection established to : " + url.getHost() + ":" + port);
+                        log.debug("A new connection established to : " + host + ":" + port);
                     }
                 }
             }
@@ -551,6 +609,17 @@
     }
 
     // -------------- utility methods -------------
+    private boolean isBypass(String hostName) {
+        for (String entry : proxyBypassList) {
+            if (hostName.matches(entry)) {
+                knownDirectHosts.add(hostName);
+                return true;
+            }
+        }
+        knownProxyHosts.add(hostName);
+        return false;
+    }
+
     private void handleException(String msg, Exception e) throws AxisFault {
         log.error(msg, e);
         throw new AxisFault(msg, e);

Modified: synapse/trunk/java/repository/conf/axis2.xml
URL: http://svn.apache.org/viewvc/synapse/trunk/java/repository/conf/axis2.xml?rev=752933&r1=752932&r2=752933&view=diff
==============================================================================
--- synapse/trunk/java/repository/conf/axis2.xml (original)
+++ synapse/trunk/java/repository/conf/axis2.xml Thu Mar 12 16:56:18 2009
@@ -222,6 +222,9 @@
 
     <transportSender name="http"  class="org.apache.synapse.transport.nhttp.HttpCoreNIOSender">
         <parameter name="non-blocking" locked="false">true</parameter>
+        <!--parameter name="http.proxyHost" locked="false">localhost</parameter>
+        <parameter name="http.proxyPort" locked="false">3128</parameter>
+        <parameter name="http.nonProxyHosts" locked="false">localhost|moon|sun</parameter-->
     </transportSender>
     <transportSender name="https" class="org.apache.synapse.transport.nhttp.HttpCoreNIOSSLSender">
         <parameter name="non-blocking" locked="false">true</parameter>