Search in sources :

Example 1 with ProtocolHandlers

use of org.eclipse.jetty.client.ProtocolHandlers in project jetty.project by eclipse.

the class AbstractProxyServlet method createHttpClient.

/**
     * <p>Creates a {@link HttpClient} instance, configured with init parameters of this servlet.</p>
     * <p>The init parameters used to configure the {@link HttpClient} instance are:</p>
     * <table>
     * <caption>Init Parameters</caption>
     * <thead>
     * <tr>
     * <th>init-param</th>
     * <th>default</th>
     * <th>description</th>
     * </tr>
     * </thead>
     * <tbody>
     * <tr>
     * <td>maxThreads</td>
     * <td>256</td>
     * <td>The max number of threads of HttpClient's Executor.  If not set, or set to the value of "-", then the
     * Jetty server thread pool will be used.</td>
     * </tr>
     * <tr>
     * <td>maxConnections</td>
     * <td>32768</td>
     * <td>The max number of connections per destination, see {@link HttpClient#setMaxConnectionsPerDestination(int)}</td>
     * </tr>
     * <tr>
     * <td>idleTimeout</td>
     * <td>30000</td>
     * <td>The idle timeout in milliseconds, see {@link HttpClient#setIdleTimeout(long)}</td>
     * </tr>
     * <tr>
     * <td>timeout</td>
     * <td>60000</td>
     * <td>The total timeout in milliseconds, see {@link Request#timeout(long, java.util.concurrent.TimeUnit)}</td>
     * </tr>
     * <tr>
     * <td>requestBufferSize</td>
     * <td>HttpClient's default</td>
     * <td>The request buffer size, see {@link HttpClient#setRequestBufferSize(int)}</td>
     * </tr>
     * <tr>
     * <td>responseBufferSize</td>
     * <td>HttpClient's default</td>
     * <td>The response buffer size, see {@link HttpClient#setResponseBufferSize(int)}</td>
     * </tr>
     * </tbody>
     * </table>
     *
     * @return a {@link HttpClient} configured from the {@link #getServletConfig() servlet configuration}
     * @throws ServletException if the {@link HttpClient} cannot be created
     */
protected HttpClient createHttpClient() throws ServletException {
    ServletConfig config = getServletConfig();
    HttpClient client = newHttpClient();
    // Redirects must be proxied as is, not followed.
    client.setFollowRedirects(false);
    // Must not store cookies, otherwise cookies of different clients will mix.
    client.setCookieStore(new HttpCookieStore.Empty());
    Executor executor;
    String value = config.getInitParameter("maxThreads");
    if (value == null || "-".equals(value)) {
        executor = (Executor) getServletContext().getAttribute("org.eclipse.jetty.server.Executor");
        if (executor == null)
            throw new IllegalStateException("No server executor for proxy");
    } else {
        QueuedThreadPool qtp = new QueuedThreadPool(Integer.parseInt(value));
        String servletName = config.getServletName();
        int dot = servletName.lastIndexOf('.');
        if (dot >= 0)
            servletName = servletName.substring(dot + 1);
        qtp.setName(servletName);
        executor = qtp;
    }
    client.setExecutor(executor);
    value = config.getInitParameter("maxConnections");
    if (value == null)
        value = "256";
    client.setMaxConnectionsPerDestination(Integer.parseInt(value));
    value = config.getInitParameter("idleTimeout");
    if (value == null)
        value = "30000";
    client.setIdleTimeout(Long.parseLong(value));
    value = config.getInitParameter("timeout");
    if (value == null)
        value = "60000";
    _timeout = Long.parseLong(value);
    value = config.getInitParameter("requestBufferSize");
    if (value != null)
        client.setRequestBufferSize(Integer.parseInt(value));
    value = config.getInitParameter("responseBufferSize");
    if (value != null)
        client.setResponseBufferSize(Integer.parseInt(value));
    try {
        client.start();
        // Content must not be decoded, otherwise the client gets confused.
        client.getContentDecoderFactories().clear();
        // Pass traffic to the client, only intercept what's necessary.
        ProtocolHandlers protocolHandlers = client.getProtocolHandlers();
        protocolHandlers.clear();
        protocolHandlers.put(new ProxyContinueProtocolHandler());
        return client;
    } catch (Exception x) {
        throw new ServletException(x);
    }
}
Also used : ServletConfig(javax.servlet.ServletConfig) HttpCookieStore(org.eclipse.jetty.util.HttpCookieStore) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) UnknownHostException(java.net.UnknownHostException) UnavailableException(javax.servlet.UnavailableException) ServletException(javax.servlet.ServletException) Executor(java.util.concurrent.Executor) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) ProtocolHandlers(org.eclipse.jetty.client.ProtocolHandlers)

Aggregations

UnknownHostException (java.net.UnknownHostException)1 Executor (java.util.concurrent.Executor)1 TimeoutException (java.util.concurrent.TimeoutException)1 ServletConfig (javax.servlet.ServletConfig)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 ProtocolHandlers (org.eclipse.jetty.client.ProtocolHandlers)1 HttpCookieStore (org.eclipse.jetty.util.HttpCookieStore)1 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)1