Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 30558189D0 for ; Sat, 2 Jan 2016 14:15:56 +0000 (UTC) Received: (qmail 98973 invoked by uid 500); 2 Jan 2016 14:15:56 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 98931 invoked by uid 500); 2 Jan 2016 14:15:56 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 98922 invoked by uid 99); 2 Jan 2016 14:15:56 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 02 Jan 2016 14:15:56 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 96A091A05C8 for ; Sat, 2 Jan 2016 14:15:55 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.247 X-Spam-Level: * X-Spam-Status: No, score=1.247 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.554, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id 4rN2qrkNzYTB for ; Sat, 2 Jan 2016 14:15:45 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with ESMTP id EFAA520534 for ; Sat, 2 Jan 2016 14:15:44 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 7721CE0428 for ; Sat, 2 Jan 2016 14:15:44 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 34FFE3A0296 for ; Sat, 2 Jan 2016 14:15:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160102141544.34FFE3A0296@svn01-us-west.apache.org> 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 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 @@ junit test + + org.mockito + mockito-core + test + 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 + * . + * + */ +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 registrations = new TreeMap(); // 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 registrations = new TreeMap(); + 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