hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1722632 - in /httpcomponents/httpclient/branches/4.5.x/httpclient-osgi: pom.xml src/main/java/org/apache/http/osgi/impl/OSGiHttpRoutePlanner.java src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java
Date Sat, 02 Jan 2016 14:15:43 GMT
Author: olegk
Date: Sat Jan  2 14:15:43 2016
New Revision: 1722632

URL: http://svn.apache.org/viewvc?rev=1722632&view=rev
Log:
HTTPCLIENT-1710 - fixes - examine all exceptions, correct capture groups count
Contributed by 212427891 <munene.kiruja at ge.com>

Added:
    httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java
  (with props)
Modified:
    httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/pom.xml
    httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiHttpRoutePlanner.java

Modified: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/pom.xml?rev=1722632&r1=1722631&r2=1722632&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/pom.xml (original)
+++ httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/pom.xml Sat Jan  2 14:15:43 2016
@@ -98,6 +98,11 @@
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>

Modified: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiHttpRoutePlanner.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiHttpRoutePlanner.java?rev=1722632&r1=1722631&r2=1722632&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiHttpRoutePlanner.java
(original)
+++ httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiHttpRoutePlanner.java
Sat Jan  2 14:15:43 2016
@@ -73,7 +73,7 @@ final class OSGiHttpRoutePlanner extends
     @Override
     protected HttpHost determineProxy(final HttpHost target, final HttpRequest request, final
HttpContext context) throws HttpException {
         ProxyConfiguration proxyConfiguration = null;
-
+        HttpHost proxyHost = null;
         for (final ServiceRegistration registration : registeredConfigurations.values())
{
             final Object proxyConfigurationObject = bundleContext.getService(registration.getReference());
             if (proxyConfigurationObject != null) {
@@ -82,15 +82,16 @@ final class OSGiHttpRoutePlanner extends
                     for (final String exception : proxyConfiguration.getProxyExceptions())
{
                         if (createMatcher(exception).matches(target.getHostName())) {
                             return null;
-                        } else {
-                            return new HttpHost(proxyConfiguration.getHostname(), proxyConfiguration.getPort());
                         }
                     }
+                    if (null == proxyHost) {
+                        proxyHost = new HttpHost(proxyConfiguration.getHostname(), proxyConfiguration.getPort());
+                    }
                 }
             }
         }
 
-        return null;
+        return proxyHost;
     }
 
     private static HostMatcher createMatcher(final String name) {
@@ -174,7 +175,7 @@ final class OSGiHttpRoutePlanner extends
                         final int i4 = toInt(nameMatcher.group(4), 255);
                         final int ip = i1 << 24 | i2 << 16 | i3 << 8 |
i4;
 
-                        int mask = toInt(nameMatcher.group(6), 32);
+                        int mask = toInt(nameMatcher.group(4), 32);
                         mask = (mask == 32) ? -1 : -1 - (-1 >>> mask);
 
                         return new NetworkAddress(ip, mask);

Added: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java?rev=1722632&view=auto
==============================================================================
--- httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java
(added)
+++ httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java
Sat Jan  2 14:15:43 2016
@@ -0,0 +1,160 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.osgi.impl;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.InetAddress;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.http.HttpHost;
+import org.apache.http.osgi.services.ProxyConfiguration;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+
+/**
+ * @since 4.4.3
+ */
+
+public class TestOSGiHttpRoutePlanner  {
+
+    final ProxyConfiguration pc1 = new ProxyConfiguration() {
+        @Override
+        public boolean isEnabled() {return true; }
+        @Override
+        public String getHostname() {return "proxy1"; }
+        @Override
+        public int getPort() { return 8080; }
+        @Override
+        public String getUsername() { return ""; }
+        @Override
+        public String getPassword() {return ""; }
+        @Override
+        public String[] getProxyExceptions() { return new String[]{"localhost", "127.0.0.1",
".apache.org"}; }
+    };
+
+    final ProxyConfiguration pc2 = new ProxyConfiguration() {
+        @Override
+        public boolean isEnabled() {return true; }
+        @Override
+        public String getHostname() {return "proxy2"; }
+        @Override
+        public int getPort() { return 9090; }
+        @Override
+        public String getUsername() { return ""; }
+        @Override
+        public String getPassword() {return ""; }
+        @Override
+        public String[] getProxyExceptions() { return new String[]{"localhost", "127.0.0.1",
".oracle.com", "12.34.34.8"}; }
+    };
+
+    @Test
+    public void testDeterminProxy() throws Exception {
+        final ServiceReference sRef1 = mock(ServiceReference.class);
+        final ServiceRegistration sReg1 = mock(ServiceRegistration.class);
+        when(sReg1.getReference()).thenReturn(sRef1);
+        final BundleContext bc = mock(BundleContext.class);
+        when(bc.getService(sRef1)).thenReturn(this.pc1);
+
+        final Map<String, ServiceRegistration> registrations = new TreeMap<String,
ServiceRegistration>(); // TreeMap for order
+        registrations.put("foo1", sReg1);
+
+        OSGiHttpRoutePlanner planner = new OSGiHttpRoutePlanner(bc, registrations);
+
+        HttpHost proxy = planner.determineProxy(new HttpHost("localhost", 8090), null, null);
+        assertNull(proxy);
+
+        proxy = planner.determineProxy(new HttpHost("there", 9090), null, null);
+        assertNotNull(proxy);
+        assertTrue(proxy.getHostName().equals("proxy1"));
+
+        proxy = planner.determineProxy(new HttpHost("10.2.144.23", 4554), null, null);
+        assertNotNull(proxy);
+        assertTrue(proxy.getHostName().equals("proxy1"));
+
+        final InetAddress addr = InetAddress.getByName("localhost");
+        proxy = planner.determineProxy(new HttpHost(addr, 4554), null, null);
+        assertNull(proxy);
+
+        proxy = planner.determineProxy(new HttpHost("hc.apache.org", 4554), null, null);
+        assertNull(proxy);
+
+
+        // test with more than one registration of proxyConfiguration
+        final ServiceReference sRef2 = mock(ServiceReference.class);
+        final ServiceRegistration sReg2 = mock(ServiceRegistration.class);
+        when(sReg2.getReference()).thenReturn(sRef2);
+        when(bc.getService(sRef2)).thenReturn(this.pc2);
+        registrations.put("foo2", sReg2);
+
+        planner = new OSGiHttpRoutePlanner(bc, registrations);
+        proxy = planner.determineProxy(new HttpHost("localhost", 8090), null, null);
+        assertNull(proxy);
+
+        proxy = planner.determineProxy(new HttpHost("there", 9090), null, null);
+        assertNotNull(proxy);
+        assertTrue(proxy.getHostName().equals("proxy1")); // the first one
+
+        proxy = planner.determineProxy(new HttpHost(addr, 4554), null, null);
+        assertNull(proxy);
+
+        proxy = planner.determineProxy(new HttpHost("hc.apache.org", 4554), null, null);
+        assertNull(proxy);
+
+        proxy = planner.determineProxy(new HttpHost("docs.oracle.com", 4554), null, null);
+        assertNull(proxy);
+    }
+
+    @Test
+    public void testMasking() throws Exception {
+        final ServiceReference sRef2 = mock(ServiceReference.class);
+        final ServiceRegistration sReg2 = mock(ServiceRegistration.class);
+        when(sReg2.getReference()).thenReturn(sRef2);
+        final BundleContext bc = mock(BundleContext.class);
+        when(bc.getService(sRef2)).thenReturn(this.pc2);
+        final Map<String, ServiceRegistration> registrations = new TreeMap<String,
ServiceRegistration>();
+        registrations.put("foo2", sReg2);
+
+        final OSGiHttpRoutePlanner planner = new OSGiHttpRoutePlanner(bc, registrations);
+
+        HttpHost proxy = planner.determineProxy(new HttpHost("12.34.34.2", 4554), null, null);
+        assertNotNull(proxy);
+        assertTrue(proxy.getHostName().equals("proxy2"));
+
+        proxy = planner.determineProxy(new HttpHost("12.34.34.8", 4554), null, null);
+        assertNotNull(proxy);
+    }
+
+}

Propchange: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/test/java/org/apache/http/osgi/impl/TestOSGiHttpRoutePlanner.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message