Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.http.util » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.jmeter.protocol.http.util;
   20   
   21   import java.io.IOException;
   22   import java.net.InetAddress;
   23   import java.net.Socket;
   24   import java.net.URL;
   25   import java.net.URLConnection;
   26   import java.net.URLStreamHandler;
   27   import java.net.URLStreamHandlerFactory;
   28   import java.net.UnknownHostException;
   29   
   30   import org.apache.commons.httpclient.ConnectTimeoutException;
   31   import org.apache.commons.httpclient.params.HttpConnectionParams;
   32   import org.apache.commons.httpclient.protocol.Protocol;
   33   import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
   34   
   35   /**
   36    * HttpClient protocol factory to generate Loopback HTTP sockets
   37    */
   38   
   39   public class LoopbackHttpClientSocketFactory implements ProtocolSocketFactory {
   40   
   41       public LoopbackHttpClientSocketFactory() {
   42           super();
   43       }
   44   
   45       public Socket createSocket(String host, int port, InetAddress clientHost,
   46               int clientPort) throws IOException, UnknownHostException {
   47           return new LoopbackHTTPSocket(host,port,clientHost,clientPort);
   48       }
   49   
   50       public Socket createSocket(String host, int port) throws IOException,
   51               UnknownHostException {
   52           return new LoopbackHTTPSocket(host,port);
   53       }
   54   
   55       public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
   56               HttpConnectionParams params)
   57       throws IOException, UnknownHostException, ConnectTimeoutException {
   58           int timeout = params.getConnectionTimeout();
   59           if (timeout == 0) {
   60               return new LoopbackHTTPSocket(host,port,localAddress,localPort);
   61           } else {
   62               return new LoopbackHTTPSocket(host,port,localAddress,localPort, timeout);
   63           }
   64       }
   65   
   66       /**
   67        * Convenience method to set up the necessary HttpClient protocol and URL handler.
   68        *
   69        * Only works for HttpClient, because it's not possible (or at least very difficult)
   70        * to provide a different socket factory for the HttpURLConnection class.
   71        */
   72       public static void setup(){
   73           final String LOOPBACK = "loopback"; // $NON-NLS-1$
   74   
   75           // This ensures tha HttpClient knows about the protocol
   76           Protocol.registerProtocol(LOOPBACK, new Protocol(LOOPBACK,new LoopbackHttpClientSocketFactory(),1));
   77   
   78           // Now allow the URL handling to work.
   79           URLStreamHandlerFactory ushf = new URLStreamHandlerFactory(){
   80               public URLStreamHandler createURLStreamHandler(String protocol) {
   81                   if (protocol.equalsIgnoreCase(LOOPBACK)){
   82                       return new URLStreamHandler(){
   83                           protected URLConnection openConnection(URL u) throws IOException {
   84                               return null;// not needed for HttpClient
   85                           }
   86                       };
   87                   }
   88                   return null;
   89               }
   90           };
   91   
   92           java.net.URL.setURLStreamHandlerFactory(ushf);
   93       }
   94   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.http.util » [javadoc | source]