Java Code Examples for org.apache.commons.httpclient.protocol.Protocol#getProtocol()

The following examples show how to use org.apache.commons.httpclient.protocol.Protocol#getProtocol() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonsHttpTransportTests.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void testProtocolReplacement() throws Exception {
    final ProtocolSocketFactory socketFactory = getSocketFactory();
    CommonsHttpTransport.replaceProtocol(socketFactory, "https", 443);

    Protocol protocol = Protocol.getProtocol("https");
    assertThat(protocol, instanceOf(DelegatedProtocol.class));

    DelegatedProtocol delegatedProtocol = (DelegatedProtocol) protocol;
    assertThat(delegatedProtocol.getSocketFactory(), sameInstance(socketFactory));
    assertThat(delegatedProtocol.getOriginal(), sameInstance(original));

    // ensure we do not re-wrap a delegated protocol
    CommonsHttpTransport.replaceProtocol(socketFactory, "https", 443);
    protocol = Protocol.getProtocol("https");
    assertThat(protocol, instanceOf(DelegatedProtocol.class));

    delegatedProtocol = (DelegatedProtocol) protocol;
    assertThat(delegatedProtocol.getSocketFactory(), sameInstance(socketFactory));
    assertThat(delegatedProtocol.getOriginal(), sameInstance(original));
}
 
Example 2
Source File: HttpHostFactory.java    From http4e with Apache License 2.0 6 votes vote down vote up
/**
 * Get a Protocol for the given parameters. The default implementation
 * selects a protocol based only on the scheme. Subclasses can do fancier
 * things, such as select SSL parameters based on the host or port. This
 * method must not return null.
 */
protected Protocol getProtocol(HostConfiguration old, String scheme, String host, int port)
{
    final Protocol oldProtocol = old.getProtocol();
    if (oldProtocol != null) {
        final String oldScheme = oldProtocol.getScheme();
        if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) {
            // The old protocol has the desired scheme.
            return oldProtocol; // Retain it.
        }
    }
    Protocol newProtocol = (scheme != null && scheme.toLowerCase().endsWith("s")) ? httpsProtocol
            : httpProtocol;
    if (newProtocol == null) {
        newProtocol = Protocol.getProtocol(scheme);
    }
    return newProtocol;
}
 
Example 3
Source File: HttpClientFactory.java    From alfresco-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Get a host for the given parameters. This method need not be thread-safe. */
public HttpHost getHost(String host, int port, String scheme)
{
    if(scheme == null)
    {
        scheme = "http";
    }
    Protocol protocol = protocols.get(scheme);
    if(protocol == null)
    {
        protocol = Protocol.getProtocol("http");
        if(protocol == null)
        {
            throw new IllegalArgumentException("Unrecognised scheme parameter");
        }
    }

    return new HttpHost(host, port, protocol);
}
 
Example 4
Source File: Manager.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
private void createHttpClient() {
    Protocol protocol = Protocol.getProtocol("https");
    if (protocol == null) {
        // ZAP: Dont override an existing protocol - it causes problems with ZAP
        Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
        Protocol.registerProtocol("https", easyhttps);
    }
    initialState = new HttpState();

    MultiThreadedHttpConnectionManager connectionManager =
            new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(1000);
    connectionManager.getParams().setMaxTotalConnections(1000);

    // connectionManager.set

    httpclient = new HttpClient(connectionManager);
    // httpclient.

}
 
Example 5
Source File: HostConfigurationWithStickyProtocol.java    From http4e with Apache License 2.0 5 votes vote down vote up
/**
 * Select a Protocol to be used for the given host, port and scheme. The
 * current Protocol may be selected, if appropriate. This method need not be
 * thread-safe; the caller must synchronize if necessary.
 * <p>
 * This implementation returns the current Protocol if it has the given
 * scheme; otherwise it returns the Protocol registered for that scheme.
 */
protected Protocol getNewProtocol(String host, int port, String scheme)
{
    final Protocol oldProtocol = getProtocol();
    if (oldProtocol != null) {
        final String oldScheme = oldProtocol.getScheme();
        if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) {
            // The old {rotocol has the desired scheme.
            return oldProtocol; // Retain it.
        }
    }
    return Protocol.getProtocol(scheme);
}
 
Example 6
Source File: ProtocolAwareHostConfiguration.java    From elasticsearch-hadoop with Apache License 2.0 5 votes vote down vote up
protected Protocol keepProtocol(String host, int port, String scheme) {
    final Protocol oldProtocol = getProtocol();
    if (oldProtocol != null) {
        final String oldScheme = oldProtocol.getScheme();
        if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) {
            return oldProtocol;
        }
    }
    return Protocol.getProtocol(scheme);
}
 
Example 7
Source File: ICalendarService.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
public Protocol getProtocol(boolean isSslConnection) {

    if (isSslConnection) {
      return Protocol.getProtocol("https");
    } else {
      return Protocol.getProtocol("http");
    }
  }
 
Example 8
Source File: HttpConnection.java    From http4e with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }
}
 
Example 9
Source File: CommonsHttpTransportTests.java    From elasticsearch-hadoop with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
    original = Protocol.getProtocol("https");
}
 
Example 10
Source File: HttpConnection.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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;
    }
}
 
Example 11
Source File: HttpConnection.java    From http4e with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new HTTP connection for the given host and port via the 
 * given proxy host and port using the default protocol.
 *
 * @param proxyHost the host to proxy via
 * @param proxyPort the port to proxy via
 * @param host the host to connect to
 * @param port the port to connect to
 */
public HttpConnection(
    String proxyHost,
    int proxyPort,
    String host,
    int port) {
    this(proxyHost, proxyPort, host, null, port, Protocol.getProtocol("http"));
}
 
Example 12
Source File: HttpConnection.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Creates a new HTTP connection for the given host and port via the 
 * given proxy host and port using the default protocol.
 *
 * @param proxyHost the host to proxy via
 * @param proxyPort the port to proxy via
 * @param host the host to connect to
 * @param port the port to connect to
 */
public HttpConnection(
    String proxyHost,
    int proxyPort,
    String host,
    int port) {
    this(proxyHost, proxyPort, host, null, port, Protocol.getProtocol("http"));
}
 
Example 13
Source File: HostConfiguration.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Set the given host. Uses the default protocol("http") and its port.
 * 
 * @param host The host(IP or DNS name).
 */
public synchronized void setHost(final String host) {
    Protocol defaultProtocol = Protocol.getProtocol("http"); 
    setHost(host, defaultProtocol.getDefaultPort(), defaultProtocol);
}
 
Example 14
Source File: HttpHost.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * URI constructor for HttpHost.
 *   
 * @param uri the URI.
 */
public  HttpHost(final URI uri) throws URIException {
    this(uri.getHost(), uri.getPort(), Protocol.getProtocol(uri.getScheme()));
}
 
Example 15
Source File: HttpHost.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Constructor for HttpHost.
 *   
 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
 */
public HttpHost(final String hostname) {
    this(hostname, -1, Protocol.getProtocol("http"));
}
 
Example 16
Source File: HttpHost.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Constructor for HttpHost.
 *   
 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
 * @param port the port. Value <code>-1</code> can be used to set default protocol port
 */
public HttpHost(final String hostname, int port) {
    this(hostname, port, Protocol.getProtocol("http"));
}
 
Example 17
Source File: ProxyHost.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Constructor for ProxyHost.
 *   
 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
 * @param port the port. Value <code>-1</code> can be used to set default protocol port
 */
public ProxyHost(final String hostname, int port) {
    super(hostname, port, Protocol.getProtocol("http"));
}
 
Example 18
Source File: HostConfiguration.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Sets the given host, port and protocol
 * 
 * @param host the host(IP or DNS name)
 * @param port The port
 * @param protocol The protocol.
 */
public synchronized void setHost(final String host, int port, final String protocol) {
    this.host = new HttpHost(host, port, Protocol.getProtocol(protocol));
}
 
Example 19
Source File: HttpConnection.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Creates a new HTTP connection for the given host and port.
 *
 * @param host the host to connect to
 * @param port the port to connect to
 */
public HttpConnection(String host, int port) {
    this(null, -1, host, null, port, Protocol.getProtocol("http"));
}
 
Example 20
Source File: HttpConnection.java    From http4e with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new HTTP connection for the given host and port.
 *
 * @param host the host to connect to
 * @param port the port to connect to
 */
public HttpConnection(String host, int port) {
    this(null, -1, host, null, port, Protocol.getProtocol("http"));
}