Exemplo n.º 1
0
 /**
  * Sets the host to connect to.
  *
  * @param host the host to connect to. Parameter value must be non-null.
  * @throws IllegalStateException if the connection is already open
  */
 public void setHost(String host) throws IllegalStateException {
   if (host == null) {
     throw new IllegalArgumentException("host parameter is null");
   }
   assertNotOpen();
   hostName = host;
 }
Exemplo n.º 2
0
  /**
   * Sets the protocol used to establish the connection
   *
   * @param protocol The protocol to use.
   * @throws IllegalStateException if the connection is already open
   */
  public void setProtocol(Protocol protocol) {
    assertNotOpen();

    if (protocol == null) {
      throw new IllegalArgumentException("protocol is null");
    }

    protocolInUse = protocol;
  }
Exemplo n.º 3
0
  /**
   * Establishes a connection to the specified host and port (via a proxy if specified). The
   * underlying socket is created from the {@link ProtocolSocketFactory}.
   *
   * @throws IOException if an attempt to establish the connection results in an I/O error.
   */
  public void open() throws IOException {
    LOG.trace("enter HttpConnection.open()");

    final String host = (proxyHostName == null) ? hostName : proxyHostName;
    final int port = (proxyHostName == null) ? portNumber : proxyPortNumber;
    assertNotOpen();

    if (LOG.isDebugEnabled()) {
      LOG.debug("Open connection to " + host + ":" + port);
    }

    try {
      if (this.socket == null) {
        usingSecureSocket = isSecure() && !isProxied();
        // use the protocol's socket factory unless this is a secure
        // proxied connection
        ProtocolSocketFactory socketFactory = null;
        if (isSecure() && isProxied()) {
          Protocol defaultprotocol = Protocol.getProtocol("http");
          socketFactory = defaultprotocol.getSocketFactory();
        } else {
          socketFactory = this.protocolInUse.getSocketFactory();
        }
        this.socket = socketFactory.createSocket(host, port, localAddress, 0, this.params);
      }

      /*
      "Nagling has been broadly implemented across networks,
      including the Internet, and is generally performed by default
      - although it is sometimes considered to be undesirable in
      highly interactive environments, such as some client/server
      situations. In such cases, nagling may be turned off through
      use of the TCP_NODELAY sockets option." */

      socket.setTcpNoDelay(this.params.getTcpNoDelay());
      socket.setSoTimeout(this.params.getSoTimeout());

      int linger = this.params.getLinger();
      if (linger >= 0) {
        socket.setSoLinger(linger > 0, linger);
      }

      int sndBufSize = this.params.getSendBufferSize();
      if (sndBufSize >= 0) {
        socket.setSendBufferSize(sndBufSize);
      }
      int rcvBufSize = this.params.getReceiveBufferSize();
      if (rcvBufSize >= 0) {
        socket.setReceiveBufferSize(rcvBufSize);
      }
      int outbuffersize = socket.getSendBufferSize();
      if ((outbuffersize > 2048) || (outbuffersize <= 0)) {
        outbuffersize = 2048;
      }
      int inbuffersize = socket.getReceiveBufferSize();
      if ((inbuffersize > 2048) || (inbuffersize <= 0)) {
        inbuffersize = 2048;
      }
      inputStream = new BufferedInputStream(socket.getInputStream(), inbuffersize);
      outputStream = new BufferedOutputStream(socket.getOutputStream(), outbuffersize);
      isOpen = true;
    } catch (IOException e) {
      // Connection wasn't opened properly
      // so close everything out
      closeSocketAndStreams();
      throw e;
    }
  }
Exemplo n.º 4
0
 /**
  * Set the local address used when creating the connection. If unset or <tt>null</tt>, the default
  * address is used.
  *
  * @param localAddress the local address to use
  */
 public void setLocalAddress(InetAddress localAddress) {
   assertNotOpen();
   this.localAddress = localAddress;
 }
Exemplo n.º 5
0
 /**
  * Sets the port of the host to proxy through.
  *
  * @param port the port of the host to proxy through.
  * @throws IllegalStateException if the connection is already open
  */
 public void setProxyPort(int port) throws IllegalStateException {
   assertNotOpen();
   proxyPortNumber = port;
 }
Exemplo n.º 6
0
 /**
  * Sets the host to proxy through.
  *
  * @param host the host to proxy through.
  * @throws IllegalStateException if the connection is already open
  */
 public void setProxyHost(String host) throws IllegalStateException {
   assertNotOpen();
   proxyHostName = host;
 }
Exemplo n.º 7
0
 /**
  * Sets the virtual host to target.
  *
  * @param host the virtual host name that should be used instead of physical host name when
  *     sending HTTP requests. Virtual host name can be set to <tt> null</tt> if virtual host name
  *     is not to be used
  * @throws IllegalStateException if the connection is already open
  * @deprecated no longer applicable
  */
 @Deprecated
 public void setVirtualHost(String host) throws IllegalStateException {
   assertNotOpen();
 }