Search in sources :

Example 21 with BytesContentProvider

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

the class AsyncQueryForwardingServlet method sendProxyRequest.

@Override
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
    proxyRequest.timeout(httpClientConfig.getReadTimeout().getMillis(), TimeUnit.MILLISECONDS);
    proxyRequest.idleTimeout(httpClientConfig.getReadTimeout().getMillis(), TimeUnit.MILLISECONDS);
    final Query query = (Query) clientRequest.getAttribute(QUERY_ATTRIBUTE);
    if (query != null) {
        final ObjectMapper objectMapper = (ObjectMapper) clientRequest.getAttribute(OBJECTMAPPER_ATTRIBUTE);
        try {
            proxyRequest.content(new BytesContentProvider(objectMapper.writeValueAsBytes(query)));
        } catch (JsonProcessingException e) {
            Throwables.propagate(e);
        }
    }
    super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
}
Also used : Query(io.druid.query.Query) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 22 with BytesContentProvider

use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.

the class DigestPostTest method testServerWithHttpClientStringContent.

@Test
public void testServerWithHttpClientStringContent() throws Exception {
    String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
    HttpClient client = new HttpClient();
    try {
        AuthenticationStore authStore = client.getAuthenticationStore();
        authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
        client.start();
        Request request = client.newRequest(srvUrl);
        request.method(HttpMethod.POST);
        request.content(new BytesContentProvider(__message.getBytes("UTF8")));
        _received = null;
        request = request.timeout(5, TimeUnit.SECONDS);
        ContentResponse response = request.send();
        Assert.assertEquals(__message, _received);
        Assert.assertEquals(200, response.getStatus());
    } finally {
        client.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) URI(java.net.URI) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 23 with BytesContentProvider

use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.

the class JdbcLoginServiceTest method testPut.

@Test
public void testPut() throws Exception {
    try {
        startClient();
        Request request = _client.newRequest(_baseUri.resolve("output.txt"));
        request.method(HttpMethod.PUT);
        request.content(new BytesContentProvider(_content.getBytes()));
        ContentResponse response = request.send();
        int responseStatus = response.getStatus();
        boolean statusOk = (responseStatus == 200 || responseStatus == 201);
        assertTrue(statusOk);
        String content = IO.toString(new FileInputStream(new File(_docRoot, "output.txt")));
        assertEquals(_content, content);
    } finally {
        stopClient();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 24 with BytesContentProvider

use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.

the class JdbcLoginServiceTest method testPost.

@Test
public void testPost() throws Exception {
    try {
        startClient();
        Request request = _client.newRequest(_baseUri.resolve("test"));
        request.method(HttpMethod.POST);
        request.content(new BytesContentProvider(_content.getBytes()));
        ContentResponse response = request.send();
        assertEquals(HttpStatus.OK_200, response.getStatus());
        assertEquals(_content, _testServer.getTestHandler().getRequestContent());
    } finally {
        stopClient();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) Test(org.junit.Test)

Example 25 with BytesContentProvider

use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.

the class HttpClientTest method test_POST_WithContent_TracksProgress.

@Test
public void test_POST_WithContent_TracksProgress() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            consume(request.getInputStream(), true);
        }
    });
    final AtomicInteger progress = new AtomicInteger();
    ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).onRequestContent((request, buffer) -> {
        byte[] bytes = new byte[buffer.remaining()];
        Assert.assertEquals(1, bytes.length);
        buffer.get(bytes);
        Assert.assertEquals(bytes[0], progress.getAndIncrement());
    }).content(new BytesContentProvider(new byte[] { 0 }, new byte[] { 1 }, new byte[] { 2 }, new byte[] { 3 }, new byte[] { 4 })).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(5, progress.get());
}
Also used : Arrays(java.util.Arrays) EndPoint(org.eclipse.jetty.io.EndPoint) TestingDir(org.eclipse.jetty.toolchain.test.TestingDir) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ServerSocket(java.net.ServerSocket) HttpCookie(java.net.HttpCookie) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) URI(java.net.URI) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) SocketAddressResolver(org.eclipse.jetty.util.SocketAddressResolver) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) FuturePromise(org.eclipse.jetty.util.FuturePromise) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BadMessageException(org.eclipse.jetty.http.BadMessageException) Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ArrayList(java.util.ArrayList) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) Assume(org.junit.Assume) NoSuchElementException(java.util.NoSuchElementException) OutputStream(java.io.OutputStream) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) AtomicLong(java.util.concurrent.atomic.AtomicLong) URLEncoder(java.net.URLEncoder) HttpMethod(org.eclipse.jetty.http.HttpMethod) Rule(org.junit.Rule) Paths(java.nio.file.Paths) HttpField(org.eclipse.jetty.http.HttpField) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) Exchanger(java.util.concurrent.Exchanger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)40 HttpServletRequest (javax.servlet.http.HttpServletRequest)32 IOException (java.io.IOException)31 Test (org.junit.Test)30 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)29 HttpServletResponse (javax.servlet.http.HttpServletResponse)28 ServletException (javax.servlet.ServletException)27 Request (org.eclipse.jetty.client.api.Request)17 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 Request (org.eclipse.jetty.server.Request)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Response (org.eclipse.jetty.client.api.Response)11 Result (org.eclipse.jetty.client.api.Result)11 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)10 InputStream (java.io.InputStream)9 ByteBuffer (java.nio.ByteBuffer)9 HttpServlet (javax.servlet.http.HttpServlet)9 InterruptedIOException (java.io.InterruptedIOException)8 Random (java.util.Random)8