Search in sources :

Example 1 with ByteArrayBodyGenerator

use of org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator in project async-http-client by AsyncHttpClient.

the class CustomHeaderProxyTest method testHttpProxy.

@Test
public void testHttpProxy() throws Exception {
    AsyncHttpClientConfig config = config().setFollowRedirect(true).setProxyServer(proxyServer("localhost", port1).setCustomHeaders((req) -> new DefaultHttpHeaders().add(customHeaderName, customHeaderValue)).build()).setUseInsecureTrustManager(true).build();
    try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) {
        Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get();
        assertEquals(r.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 2 with ByteArrayBodyGenerator

use of org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator in project async-http-client by AsyncHttpClient.

the class HttpsProxyTest method testNoDirectRequestBodyWithProxy.

@Test
public void testNoDirectRequestBodyWithProxy() throws Exception {
    AsyncHttpClientConfig config = config().setFollowRedirect(true).setProxyServer(proxyServer("localhost", port1).build()).setUseInsecureTrustManager(true).build();
    try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) {
        Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get();
        assertEquals(r.getStatusCode(), 200);
    }
}
Also used : ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) Test(org.testng.annotations.Test)

Example 3 with ByteArrayBodyGenerator

use of org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator in project async-http-client by AsyncHttpClient.

the class ByteArrayBodyGeneratorTest method testMultipleReads.

@Test(groups = "standalone")
public void testMultipleReads() throws IOException {
    final int srcArraySize = (3 * chunkSize) + 42;
    final byte[] srcArray = new byte[srcArraySize];
    random.nextBytes(srcArray);
    final ByteArrayBodyGenerator babGen = new ByteArrayBodyGenerator(srcArray);
    final Body body = babGen.createBody();
    final ByteBuf chunkBuffer = Unpooled.buffer(chunkSize);
    int reads = 0;
    int bytesRead = 0;
    while (body.transferTo(chunkBuffer) != BodyState.STOP) {
        reads += 1;
        bytesRead += chunkBuffer.readableBytes();
        chunkBuffer.clear();
    }
    assertEquals(reads, 4, "reads to drain generator");
    assertEquals(bytesRead, srcArraySize, "bytes read");
}
Also used : ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) ByteBuf(io.netty.buffer.ByteBuf) Body(org.asynchttpclient.request.body.Body) Test(org.testng.annotations.Test)

Example 4 with ByteArrayBodyGenerator

use of org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator in project async-http-client by AsyncHttpClient.

the class ByteArrayBodyGeneratorTest method testSingleRead.

@Test(groups = "standalone")
public void testSingleRead() throws IOException {
    final int srcArraySize = chunkSize - 1;
    final byte[] srcArray = new byte[srcArraySize];
    random.nextBytes(srcArray);
    final ByteArrayBodyGenerator babGen = new ByteArrayBodyGenerator(srcArray);
    final Body body = babGen.createBody();
    final ByteBuf chunkBuffer = Unpooled.buffer(chunkSize);
    // should take 1 read to get through the srcArray
    body.transferTo(chunkBuffer);
    assertEquals(chunkBuffer.readableBytes(), srcArraySize, "bytes read");
    chunkBuffer.clear();
    assertEquals(body.transferTo(chunkBuffer), BodyState.STOP, "body at EOF");
}
Also used : ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) ByteBuf(io.netty.buffer.ByteBuf) Body(org.asynchttpclient.request.body.Body) Test(org.testng.annotations.Test)

Example 5 with ByteArrayBodyGenerator

use of org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator in project camel by apache.

the class DefaultAhcBinding method populateBody.

protected void populateBody(RequestBuilder builder, AhcEndpoint endpoint, Exchange exchange) throws CamelExchangeException {
    Message in = exchange.getIn();
    if (in.getBody() == null) {
        return;
    }
    String contentType = ExchangeHelper.getContentType(exchange);
    BodyGenerator body = in.getBody(BodyGenerator.class);
    String charset = IOHelper.getCharsetName(exchange, false);
    if (body == null) {
        try {
            Object data = in.getBody();
            if (data != null) {
                if (contentType != null && AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
                    if (!endpoint.getComponent().isAllowJavaSerializedObject()) {
                        throw new CamelExchangeException("Content-type " + AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed", exchange);
                    }
                    // serialized java object
                    Serializable obj = in.getMandatoryBody(Serializable.class);
                    // write object to output stream
                    ByteArrayOutputStream bos = new ByteArrayOutputStream(endpoint.getBufferSize());
                    AhcHelper.writeObjectToStream(bos, obj);
                    byte[] bytes = bos.toByteArray();
                    body = new ByteArrayBodyGenerator(bytes);
                    IOHelper.close(bos);
                } else if (data instanceof File || data instanceof GenericFile) {
                    // file based (could potentially also be a FTP file etc)
                    File file = in.getBody(File.class);
                    if (file != null) {
                        body = new FileBodyGenerator(file);
                    }
                } else if (data instanceof String) {
                    // (for example application/x-www-form-urlencoded forms being sent)
                    if (charset != null) {
                        body = new ByteArrayBodyGenerator(((String) data).getBytes(charset));
                    } else {
                        body = new ByteArrayBodyGenerator(((String) data).getBytes());
                    }
                }
                // fallback as input stream
                if (body == null) {
                    // force the body as an input stream since this is the fallback
                    InputStream is = in.getMandatoryBody(InputStream.class);
                    body = new InputStreamBodyGenerator(is);
                }
            }
        } catch (UnsupportedEncodingException e) {
            throw new CamelExchangeException("Error creating BodyGenerator from message body", exchange, e);
        } catch (IOException e) {
            throw new CamelExchangeException("Error serializing message body", exchange, e);
        }
    }
    if (body != null) {
        log.trace("Setting body {}", body);
        builder.setBody(body);
    }
    if (charset != null) {
        log.trace("Setting body charset {}", charset);
        builder.setCharset(Charset.forName(charset));
    }
    // must set content type, even if its null, otherwise it may default to
    // application/x-www-form-urlencoded which may not be your intention
    log.trace("Setting Content-Type {}", contentType);
    if (ObjectHelper.isNotEmpty(contentType)) {
        builder.setHeader(Exchange.CONTENT_TYPE, contentType);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) Serializable(java.io.Serializable) Message(org.apache.camel.Message) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BodyGenerator(org.asynchttpclient.request.body.generator.BodyGenerator) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) GenericFile(org.apache.camel.component.file.GenericFile)

Aggregations

ByteArrayBodyGenerator (org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator)5 Test (org.testng.annotations.Test)4 ByteBuf (io.netty.buffer.ByteBuf)2 Body (org.asynchttpclient.request.body.Body)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 Message (org.apache.camel.Message)1 GenericFile (org.apache.camel.component.file.GenericFile)1 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)1 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)1 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)1 Response (org.asynchttpclient.Response)1