001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.util;
020:
021: import java.io.IOException;
022: import java.net.InetAddress;
023: import java.net.Socket;
024: import java.net.URL;
025: import java.net.URLConnection;
026: import java.net.URLStreamHandler;
027: import java.net.URLStreamHandlerFactory;
028: import java.net.UnknownHostException;
029:
030: import org.apache.commons.httpclient.ConnectTimeoutException;
031: import org.apache.commons.httpclient.params.HttpConnectionParams;
032: import org.apache.commons.httpclient.protocol.Protocol;
033: import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
034:
035: /**
036: * HttpClient protocol factory to generate Loopback HTTP sockets
037: */
038:
039: public class LoopbackHttpClientSocketFactory implements
040: ProtocolSocketFactory {
041:
042: public LoopbackHttpClientSocketFactory() {
043: super ();
044: }
045:
046: public Socket createSocket(String host, int port,
047: InetAddress clientHost, int clientPort) throws IOException,
048: UnknownHostException {
049: return new LoopbackHTTPSocket(host, port, clientHost,
050: clientPort);
051: }
052:
053: public Socket createSocket(String host, int port)
054: throws IOException, UnknownHostException {
055: return new LoopbackHTTPSocket(host, port);
056: }
057:
058: public Socket createSocket(String host, int port,
059: InetAddress localAddress, int localPort,
060: HttpConnectionParams params) throws IOException,
061: UnknownHostException, ConnectTimeoutException {
062: int timeout = params.getConnectionTimeout();
063: if (timeout == 0) {
064: return new LoopbackHTTPSocket(host, port, localAddress,
065: localPort);
066: } else {
067: return new LoopbackHTTPSocket(host, port, localAddress,
068: localPort, timeout);
069: }
070: }
071:
072: /**
073: * Convenience method to set up the necessary HttpClient protocol and URL handler.
074: *
075: * Only works for HttpClient, because it's not possible (or at least very difficult)
076: * to provide a different socket factory for the HttpURLConnection class.
077: */
078: public static void setup() {
079: final String LOOPBACK = "loopback"; // $NON-NLS-1$
080:
081: // This ensures tha HttpClient knows about the protocol
082: Protocol.registerProtocol(LOOPBACK, new Protocol(LOOPBACK,
083: new LoopbackHttpClientSocketFactory(), 1));
084:
085: // Now allow the URL handling to work.
086: URLStreamHandlerFactory ushf = new URLStreamHandlerFactory() {
087: public URLStreamHandler createURLStreamHandler(
088: String protocol) {
089: if (protocol.equalsIgnoreCase(LOOPBACK)) {
090: return new URLStreamHandler() {
091: protected URLConnection openConnection(URL u)
092: throws IOException {
093: return null;// not needed for HttpClient
094: }
095: };
096: }
097: return null;
098: }
099: };
100:
101: java.net.URL.setURLStreamHandlerFactory(ushf);
102: }
103: }
|