Search in sources :

Example 6 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project druid by druid-io.

the class KerberosJettyHttpClientProvider method get.

@Override
public HttpClient get() {
    final HttpClient httpClient = delegateProvider.get();
    httpClient.getAuthenticationStore().addAuthentication(new Authentication() {

        @Override
        public boolean matches(String type, URI uri, String realm) {
            return true;
        }

        @Override
        public Result authenticate(final Request request, ContentResponse response, Authentication.HeaderInfo headerInfo, Attributes context) {
            return new Result() {

                @Override
                public URI getURI() {
                    return request.getURI();
                }

                @Override
                public void apply(Request request) {
                    try {
                        // No need to set cookies as they are handled by Jetty Http Client itself.
                        URI uri = request.getURI();
                        if (DruidKerberosUtil.needToSendCredentials(httpClient.getCookieStore(), uri)) {
                            log.debug("No Auth Cookie found for URI[%s]. Existing Cookies[%s] Authenticating... ", uri, httpClient.getCookieStore().getCookies());
                            final String host = request.getHost();
                            DruidKerberosUtil.authenticateIfRequired(config);
                            UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();
                            String challenge = currentUser.doAs(new PrivilegedExceptionAction<String>() {

                                @Override
                                public String run() throws Exception {
                                    return DruidKerberosUtil.kerberosChallenge(host);
                                }
                            });
                            request.getHeaders().add(HttpHeaders.Names.AUTHORIZATION, "Negotiate " + challenge);
                        } else {
                            log.debug("Found Auth Cookie found for URI[%s].", uri);
                        }
                    } catch (Throwable e) {
                        Throwables.propagate(e);
                    }
                }
            };
        }
    });
    return httpClient;
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) Attributes(org.eclipse.jetty.util.Attributes) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) URI(java.net.URI) Authentication(org.eclipse.jetty.client.api.Authentication) HttpClient(org.eclipse.jetty.client.HttpClient) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 7 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project elastic-job by dangdangdotcom.

the class RestfulServerTest method sentRequest.

private static ContentExchange sentRequest(final String content) throws Exception {
    HttpClient httpClient = new HttpClient();
    try {
        httpClient.start();
        ContentExchange result = new ContentExchange();
        result.setMethod("POST");
        result.setRequestContentType(MediaType.APPLICATION_JSON);
        result.setRequestContent(new ByteArrayBuffer(content.getBytes("UTF-8")));
        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        result.setURL(URL);
        httpClient.send(result);
        result.waitForDone();
        return result;
    } finally {
        httpClient.stop();
    }
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) ContentExchange(org.eclipse.jetty.client.ContentExchange) ByteArrayBuffer(org.eclipse.jetty.io.ByteArrayBuffer)

Example 8 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project elastic-job by dangdangdotcom.

the class RestfulTestsUtil method sentGetRequest.

public static String sentGetRequest(final String url) throws Exception {
    HttpClient httpClient = new HttpClient();
    try {
        httpClient.start();
        ContentExchange contentExchange = new ContentExchange();
        contentExchange.setMethod("GET");
        contentExchange.setRequestContentType(MediaType.APPLICATION_JSON);
        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        contentExchange.setURL(url);
        httpClient.send(contentExchange);
        contentExchange.waitForDone();
        return contentExchange.getResponseContent();
    } finally {
        httpClient.stop();
    }
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) ContentExchange(org.eclipse.jetty.client.ContentExchange)

Example 9 with HttpClient

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

the class HttpConnectionOverFCGI method acquireBuffer.

private ByteBuffer acquireBuffer() {
    HttpClient client = destination.getHttpClient();
    ByteBufferPool bufferPool = client.getByteBufferPool();
    return bufferPool.acquire(client.getResponseBufferSize(), true);
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HttpClient(org.eclipse.jetty.client.HttpClient)

Example 10 with HttpClient

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

the class AbstractHttpClientServerTest method start.

public void start(Handler handler) throws Exception {
    server = new Server();
    ServerFCGIConnectionFactory fcgiConnectionFactory = new ServerFCGIConnectionFactory(new HttpConfiguration());
    serverBufferPool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged());
    connector = new ServerConnector(server, null, null, serverBufferPool, 1, Math.max(1, Runtime.getRuntime().availableProcessors() / 2), fcgiConnectionFactory);
    //        connector.setPort(9000);
    server.addConnector(connector);
    server.setHandler(handler);
    server.start();
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName(executor.getName() + "-client");
    client = new HttpClient(new HttpClientTransportOverFCGI(1, false, "") {

        @Override
        public HttpDestination newHttpDestination(Origin origin) {
            return new HttpDestinationOverFCGI(client, origin) {

                @Override
                protected DuplexConnectionPool newConnectionPool(HttpClient client) {
                    return new LeakTrackingConnectionPool(this, client.getMaxConnectionsPerDestination(), this) {

                        @Override
                        protected void leaked(LeakDetector.LeakInfo leakInfo) {
                            connectionLeaks.incrementAndGet();
                        }
                    };
                }
            };
        }
    }, null);
    client.setExecutor(executor);
    clientBufferPool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged());
    client.setByteBufferPool(clientBufferPool);
    client.start();
}
Also used : Origin(org.eclipse.jetty.client.Origin) HttpDestinationOverFCGI(org.eclipse.jetty.fcgi.client.http.HttpDestinationOverFCGI) Server(org.eclipse.jetty.server.Server) LeakTrackingByteBufferPool(org.eclipse.jetty.io.LeakTrackingByteBufferPool) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServerConnector(org.eclipse.jetty.server.ServerConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) LeakTrackingConnectionPool(org.eclipse.jetty.client.LeakTrackingConnectionPool) HttpClientTransportOverFCGI(org.eclipse.jetty.fcgi.client.http.HttpClientTransportOverFCGI)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)135 Test (org.junit.Test)90 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)70 Request (org.eclipse.jetty.client.api.Request)44 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)40 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)23 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)18 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)8 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)8 URI (java.net.URI)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HttpProxy (org.eclipse.jetty.client.HttpProxy)7 Before (org.junit.Before)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)6 InputStream (java.io.InputStream)5 File (java.io.File)4 ContentExchange (org.eclipse.jetty.client.ContentExchange)4