From 345e69dc9f67a675d7ca6e5888fad0e1025127bb Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Tue, 7 May 2013 09:23:40 +0000 Subject: [PATCH] Throw UnsupportedSchemeException instead of plain IOException in case of an unsupported protocol scheme git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1479825 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/conn/UnsupportedSchemeException.java | 51 +++++++++++++++++++ .../conn/HttpClientConnectionOperator.java | 17 ++++--- .../TestHttpClientConnectionOperator.java | 13 ++++- 3 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 httpclient/src/main/java/org/apache/http/conn/UnsupportedSchemeException.java diff --git a/httpclient/src/main/java/org/apache/http/conn/UnsupportedSchemeException.java b/httpclient/src/main/java/org/apache/http/conn/UnsupportedSchemeException.java new file mode 100644 index 000000000..268a0b80a --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/conn/UnsupportedSchemeException.java @@ -0,0 +1,51 @@ +/* + * ==================================================================== + * 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.conn; + +import java.io.IOException; + +import org.apache.http.annotation.Immutable; + +/** + * Signals failure to establish connection using an unknown protocol scheme. + * + * @since 4.3 + */ +@Immutable +public class UnsupportedSchemeException extends IOException { + + private static final long serialVersionUID = 3597127619218687636L; + + /** + * Creates a UnsupportedSchemeException with the specified detail message. + */ + public UnsupportedSchemeException(final String message) { + super(message); + } + +} diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java b/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java index b5f38d4a1..a42abd5b4 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java @@ -45,12 +45,11 @@ import org.apache.http.conn.HttpClientConnectionManager; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.conn.ManagedHttpClientConnection; import org.apache.http.conn.SchemePortResolver; +import org.apache.http.conn.UnsupportedSchemeException; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.LayeredConnectionSocketFactory; -import org.apache.http.conn.socket.PlainSocketFactory; import org.apache.http.protocol.HttpContext; import org.apache.http.util.Args; -import org.apache.http.util.Asserts; @Immutable class HttpClientConnectionOperator { @@ -94,7 +93,8 @@ class HttpClientConnectionOperator { final Lookup registry = getSocketFactoryRegistry(context); final ConnectionSocketFactory sf = registry.lookup(host.getSchemeName()); if (sf == null) { - throw new IOException("Unsupported scheme: " + host.getSchemeName()); + throw new UnsupportedSchemeException(host.getSchemeName() + + " protocol is not supported"); } final InetAddress[] addresses = this.dnsResolver.resolve(host.getHostName()); final int port = this.schemePortResolver.resolve(host); @@ -144,12 +144,15 @@ class HttpClientConnectionOperator { final HttpContext context) throws IOException { final HttpClientContext clientContext = HttpClientContext.adapt(context); final Lookup registry = getSocketFactoryRegistry(clientContext); - ConnectionSocketFactory sf = registry.lookup(host.getSchemeName()); + final ConnectionSocketFactory sf = registry.lookup(host.getSchemeName()); if (sf == null) { - sf = PlainSocketFactory.INSTANCE; + throw new UnsupportedSchemeException(host.getSchemeName() + + " protocol is not supported"); + } + if (!(sf instanceof LayeredConnectionSocketFactory)) { + throw new UnsupportedSchemeException(host.getSchemeName() + + " protocol does not support connection upgrade"); } - Asserts.check(sf instanceof LayeredConnectionSocketFactory, - "Socket factory must implement LayeredConnectionSocketFactory"); final LayeredConnectionSocketFactory lsf = (LayeredConnectionSocketFactory) sf; Socket sock = conn.getSocket(); try { diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java index 47e9c1267..97f82b7f9 100644 --- a/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java +++ b/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java @@ -38,6 +38,7 @@ import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.DnsResolver; import org.apache.http.conn.SchemePortResolver; import org.apache.http.conn.ManagedHttpClientConnection; +import org.apache.http.conn.UnsupportedSchemeException; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.LayeredConnectionSocketFactory; import org.apache.http.protocol.BasicHttpContext; @@ -198,10 +199,20 @@ public class TestHttpClientConnectionOperator { Mockito.verify(conn).bind(socket); } - @Test(expected=IllegalStateException.class) + @Test(expected=UnsupportedSchemeException.class) + public void testUpgradeUpsupportedScheme() throws Exception { + final HttpContext context = new BasicHttpContext(); + final HttpHost host = new HttpHost("somehost", -1, "httpsssss"); + Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory); + + connectionOperator.upgrade(conn, host, context); + } + + @Test(expected=UnsupportedSchemeException.class) public void testUpgradeNonLayeringScheme() throws Exception { final HttpContext context = new BasicHttpContext(); final HttpHost host = new HttpHost("somehost", -1, "http"); + Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory); connectionOperator.upgrade(conn, host, context); }