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
This commit is contained in:
parent
0ba38201ad
commit
345e69dc9f
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<ConnectionSocketFactory> 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<ConnectionSocketFactory> 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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue