Example usage for io.netty.buffer Unpooled wrappedBuffer

List of usage examples for io.netty.buffer Unpooled wrappedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled wrappedBuffer.

Prototype

public static ByteBuf wrappedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

Usage

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Does the post test by sending the request and content to {@link NettyMessageProcessor} through an
 * {@link EmbeddedChannel} and returns the data stored in the {@link InMemoryRouter} as a result of the post.
 * @param postRequest the POST request as a {@link HttpRequest}.
 * @param contentToSend the content to be sent as a part of the POST.
 * @return the data stored in the {@link InMemoryRouter} as a result of the POST.
 * @throws InterruptedException/*from   w  ww  . j ava 2s.c  o m*/
 */
private ByteBuffer doPostTest(HttpRequest postRequest, List<ByteBuffer> contentToSend)
        throws InterruptedException {
    EmbeddedChannel channel = createChannel();

    // POST
    notificationSystem.reset();
    HttpHeaders.setHeader(postRequest, RestUtils.Headers.AMBRY_CONTENT_TYPE, "application/octet-stream");
    HttpHeaders.setKeepAlive(postRequest, false);
    channel.writeInbound(postRequest);
    if (contentToSend != null) {
        for (ByteBuffer content : contentToSend) {
            channel.writeInbound(new DefaultHttpContent(Unpooled.wrappedBuffer(content)));
        }
        channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    }
    if (!notificationSystem.operationCompleted.await(100, TimeUnit.MILLISECONDS)) {
        fail("Post did not succeed after 100ms. There is an error or timeout needs to increase");
    }
    assertNotNull("Blob id operated on cannot be null", notificationSystem.blobIdOperatedOn);
    return router.getActiveBlobs().get(notificationSystem.blobIdOperatedOn).getBlob();
}

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Creates a {@link HttpPostRequestEncoder} that encodes the given {@code request} and {@code blobContent}.
 * @param request the {@link HttpRequest} containing headers and other metadata about the request.
 * @param blobContent the {@link ByteBuffer} that represents the content of the blob.
 * @return a {@link HttpPostRequestEncoder} that can encode the {@code request} and {@code blobContent}.
 * @throws HttpPostRequestEncoder.ErrorDataEncoderException
 * @throws IOException//from  w ww .  j  a v  a  2s. c o  m
 */
private HttpPostRequestEncoder createEncoder(HttpRequest request, ByteBuffer blobContent)
        throws HttpPostRequestEncoder.ErrorDataEncoderException, IOException {
    HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false);
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(httpDataFactory, request, true);
    FileUpload fileUpload = new MemoryFileUpload(RestUtils.MultipartPost.BLOB_PART,
            RestUtils.MultipartPost.BLOB_PART, "application/octet-stream", "", Charset.forName("UTF-8"),
            blobContent.remaining());
    fileUpload.setContent(Unpooled.wrappedBuffer(blobContent));
    encoder.addBodyHttpData(fileUpload);
    return encoder;
}

From source file:com.github.ambry.rest.NettyMultipartRequestTest.java

License:Open Source License

/**
 * Tests that reference counts are correct when a {@link NettyMultipartRequest} is closed without being read.
 * @throws Exception/*from   w ww .jav a  2  s.c o m*/
 */
@Test
public void refCountsAfterCloseTest() throws Exception {
    NettyMultipartRequest requestCloseBeforePrepare = createRequest(null, null);
    NettyMultipartRequest requestCloseAfterPrepare = createRequest(null, null);
    List<HttpContent> httpContents = new ArrayList<HttpContent>(5);
    for (int i = 0; i < 5; i++) {
        HttpContent httpContent = new DefaultHttpContent(
                Unpooled.wrappedBuffer(RestTestUtils.getRandomBytes(10)));
        requestCloseBeforePrepare.addContent(httpContent);
        requestCloseAfterPrepare.addContent(httpContent);
        assertEquals("Reference count is not as expected", 3, httpContent.refCnt());
        httpContents.add(httpContent);
    }
    closeRequestAndValidate(requestCloseBeforePrepare);
    requestCloseAfterPrepare.prepare();
    closeRequestAndValidate(requestCloseAfterPrepare);
    for (HttpContent httpContent : httpContents) {
        assertEquals("Reference count is not as expected", 1, httpContent.refCnt());
    }
}

From source file:com.github.ambry.rest.NettyMultipartRequestTest.java

License:Open Source License

/**
 * Tests the expected behavior of operations after {@link NettyMultipartRequest#close()} has been called.
 * @throws Exception/*from  w  w  w  .jav a2 s .  co  m*/
 */
@Test
public void operationsAfterCloseTest() throws Exception {
    NettyMultipartRequest request = createRequest(null, null);
    request.prepare();
    closeRequestAndValidate(request);
    // close should be idempotent.
    request.close();

    try {
        request.readInto(new ByteBufferAsyncWritableChannel(), null).get();
        fail("Reading should have failed because request is closed");
    } catch (ExecutionException e) {
        assertEquals("Unexpected exception", ClosedChannelException.class, Utils.getRootCause(e).getClass());
    }

    try {
        request.prepare();
        fail("Preparing should have failed because request is closed");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestChannelClosed,
                e.getErrorCode());
    }

    try {
        request.addContent(new DefaultHttpContent(Unpooled.wrappedBuffer(RestTestUtils.getRandomBytes(10))));
        fail("Content addition should have failed because request is closed");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestChannelClosed,
                e.getErrorCode());
    }
}

From source file:com.github.ambry.rest.NettyMultipartRequestTest.java

License:Open Source License

/**
 * Tests different scenarios with {@link NettyMultipartRequest#prepare()}.
 * Currently tests:/*from w  ww .j a  v a  2  s . co m*/
 * 1. Idempotency of {@link NettyMultipartRequest#prepare()}.
 * 2. Exception scenarios of {@link NettyMultipartRequest#prepare()}.
 * @throws Exception
 */
@Test
public void prepareTest() throws Exception {
    // prepare half baked data
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, null);
    NettyMultipartRequest request = new NettyMultipartRequest(encoder.finalizeRequest(), nettyMetrics);
    assertTrue("Request channel is not open", request.isOpen());
    // insert random data
    HttpContent httpContent = new DefaultHttpContent(Unpooled.wrappedBuffer(RestTestUtils.getRandomBytes(10)));
    request.addContent(httpContent);
    // prepare should fail
    try {
        request.prepare();
        fail("Preparing request should have failed");
    } catch (HttpPostRequestDecoder.NotEnoughDataDecoderException e) {
        assertEquals("Reference count is not as expected", 1, httpContent.refCnt());
    } finally {
        closeRequestAndValidate(request);
    }

    // more than one blob part
    HttpHeaders httpHeaders = new DefaultHttpHeaders();
    httpHeaders.set(RestUtils.Headers.BLOB_SIZE, 256);
    InMemoryFile[] files = new InMemoryFile[2];
    files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART,
            ByteBuffer.wrap(RestTestUtils.getRandomBytes(256)));
    files[1] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART,
            ByteBuffer.wrap(RestTestUtils.getRandomBytes(256)));
    request = createRequest(httpHeaders, files);
    assertEquals("Request size does not match", 256, request.getSize());
    try {
        request.prepare();
        fail("Prepare should have failed because there was more than one " + RestUtils.MultipartPost.BLOB_PART);
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.MalformedRequest,
                e.getErrorCode());
    } finally {
        closeRequestAndValidate(request);
    }

    // more than one part named "part-1"
    files = new InMemoryFile[2];
    files[0] = new InMemoryFile("Part-1", ByteBuffer.wrap(RestTestUtils.getRandomBytes(256)));
    files[1] = new InMemoryFile("Part-1", ByteBuffer.wrap(RestTestUtils.getRandomBytes(256)));
    request = createRequest(null, files);
    try {
        request.prepare();
        fail("Prepare should have failed because there was more than one part named Part-1");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.MalformedRequest,
                e.getErrorCode());
    } finally {
        closeRequestAndValidate(request);
    }

    // size of blob does not match the advertized size
    httpHeaders = new DefaultHttpHeaders();
    httpHeaders.set(RestUtils.Headers.BLOB_SIZE, 256);
    files = new InMemoryFile[1];
    files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART,
            ByteBuffer.wrap(RestTestUtils.getRandomBytes(128)));
    request = createRequest(httpHeaders, files);
    try {
        request.prepare();
        fail("Prepare should have failed because there was more than one " + RestUtils.MultipartPost.BLOB_PART);
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode());
    } finally {
        closeRequestAndValidate(request);
    }

    // non fileupload (file attribute present)
    httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    HttpHeaders.setHeader(httpRequest, RestUtils.Headers.BLOB_SIZE, 256);
    files = new InMemoryFile[1];
    files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART,
            ByteBuffer.wrap(RestTestUtils.getRandomBytes(256)));
    encoder = createEncoder(httpRequest, files);
    encoder.addBodyAttribute("dummyKey", "dummyValue");
    request = new NettyMultipartRequest(encoder.finalizeRequest(), nettyMetrics);
    assertTrue("Request channel is not open", request.isOpen());
    while (!encoder.isEndOfInput()) {
        // Sending null for ctx because the encoder is OK with that.
        request.addContent(encoder.readChunk(null));
    }
    try {
        request.prepare();
        fail("Prepare should have failed because there was non fileupload");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode());
    } finally {
        closeRequestAndValidate(request);
    }
}

From source file:com.github.ambry.rest.NettyMultipartRequestTest.java

License:Open Source License

/**
 * Creates a {@link HttpPostRequestEncoder} that encodes the given {@code request} and {@code parts}.
 * @param request the {@link HttpRequest} containing headers and other metadata about the request.
 * @param parts the {@link InMemoryFile}s that will form the parts of the request.
 * @return a {@link HttpPostRequestEncoder} that can encode the {@code request} and {@code parts}.
 * @throws HttpPostRequestEncoder.ErrorDataEncoderException
 * @throws IOException/*from w  ww  .j  av  a2  s . c  o  m*/
 */
private HttpPostRequestEncoder createEncoder(HttpRequest request, InMemoryFile[] parts)
        throws HttpPostRequestEncoder.ErrorDataEncoderException, IOException {
    HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false);
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(httpDataFactory, request, true);
    if (parts != null) {
        for (InMemoryFile part : parts) {
            FileUpload fileUpload = new MemoryFileUpload(part.name, part.name, "application/octet-stream", "",
                    Charset.forName("UTF-8"), part.content.remaining());
            fileUpload.setContent(Unpooled.wrappedBuffer(part.content));
            encoder.addBodyHttpData(fileUpload);
        }
    }
    return encoder;
}

From source file:com.github.ambry.rest.NettyRequestTest.java

License:Open Source License

/**
 * Tests for behavior of multiple operations after {@link NettyRequest#close()} has been called. Some should be ok to
 * do and some should throw exceptions./*from   w w w  .  j a  v a2s.c  o  m*/
 * @throws Exception
 */
@Test
public void operationsAfterCloseTest() throws Exception {
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null);
    closeRequestAndValidate(nettyRequest);

    // operations that should be ok to do (does not include all operations).
    nettyRequest.close();

    // operations that will throw exceptions.
    AsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    ReadIntoCallback callback = new ReadIntoCallback();
    try {
        nettyRequest.readInto(writeChannel, callback).get();
        fail("Request channel has been closed, so read should have thrown ClosedChannelException");
    } catch (ExecutionException e) {
        Exception exception = (Exception) Utils.getRootCause(e);
        assertTrue("Exception is not ClosedChannelException", exception instanceof ClosedChannelException);
        assertEquals("Exceptions of callback and future differ", exception.getMessage(),
                callback.exception.getMessage());
    }

    try {
        byte[] content = RestTestUtils.getRandomBytes(1024);
        nettyRequest.addContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content)));
        fail("Request channel has been closed, so addContent() should have thrown ClosedChannelException");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestChannelClosed,
                e.getErrorCode());
    }
}

From source file:com.github.ambry.rest.NettyRequestTest.java

License:Open Source License

/**
 * Tests that {@link NettyRequest#close()} leaves any added {@link HttpContent} the way it was before it was added.
 * (i.e no reference count changes)./*from  w  w  w.  j a  v  a2  s  . co m*/
 * @throws RestServiceException
 */
@Test
public void closeTest() throws RestServiceException {
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null);
    Queue<HttpContent> httpContents = new LinkedBlockingQueue<HttpContent>();
    for (int i = 0; i < 5; i++) {
        ByteBuffer content = ByteBuffer.wrap(RestTestUtils.getRandomBytes(1024));
        HttpContent httpContent = new DefaultHttpContent(Unpooled.wrappedBuffer(content));
        nettyRequest.addContent(httpContent);
        httpContents.add(httpContent);
    }
    closeRequestAndValidate(nettyRequest);
    while (httpContents.peek() != null) {
        assertEquals("Reference count of http content has changed", 1, httpContents.poll().refCnt());
    }
}

From source file:com.github.ambry.rest.NettyRequestTest.java

License:Open Source License

/**
 * Tests different state transitions that can happen with {@link NettyRequest#addContent(HttpContent)} for GET
 * requests. Some transitions are valid and some should necessarily throw exceptions.
 * @throws RestServiceException/*from w ww .  j a va 2s  . com*/
 */
@Test
public void addContentForGetTest() throws RestServiceException {
    byte[] content = RestTestUtils.getRandomBytes(16);
    // adding non LastHttpContent to nettyRequest
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.GET, "/", null);
    try {
        nettyRequest.addContent(new DefaultHttpContent(Unpooled.wrappedBuffer(content)));
        fail("GET requests should not accept non-LastHTTPContent");
    } catch (IllegalStateException e) {
        // expected. nothing to do.
    }

    // adding LastHttpContent with some content to nettyRequest
    nettyRequest = createNettyRequest(HttpMethod.GET, "/", null);
    try {
        nettyRequest.addContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content)));
        fail("GET requests should not accept actual content in LastHTTPContent");
    } catch (IllegalStateException e) {
        // expected. nothing to do.
    }

    // should accept LastHttpContent just fine.
    nettyRequest = createNettyRequest(HttpMethod.GET, "/", null);
    nettyRequest.addContent(new DefaultLastHttpContent());

    // should not accept LastHttpContent after close
    nettyRequest = createNettyRequest(HttpMethod.GET, "/", null);
    nettyRequest.close();
    try {
        nettyRequest.addContent(new DefaultLastHttpContent());
        fail("Request channel has been closed, so addContent() should have thrown ClosedChannelException");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestChannelClosed,
                e.getErrorCode());
    }
}

From source file:com.github.ambry.rest.NettyResponseChannel.java

License:Open Source License

/**
 * Provided a cause, returns an error response with the right status and error message.
 * @param cause the cause of the error.//from ww  w .  jav  a  2  s.com
 * @return a {@link FullHttpResponse} with the error message that can be sent to the client.
 */
private FullHttpResponse getErrorResponse(Throwable cause) {
    HttpResponseStatus status;
    StringBuilder errReason = new StringBuilder();
    if (cause instanceof RestServiceException) {
        RestServiceErrorCode restServiceErrorCode = ((RestServiceException) cause).getErrorCode();
        status = getHttpResponseStatus(ResponseStatus.getResponseStatus(restServiceErrorCode));
        if (status == HttpResponseStatus.BAD_REQUEST) {
            errReason.append(" [").append(Utils.getRootCause(cause).getMessage()).append("]");
        }
    } else {
        nettyMetrics.internalServerErrorCount.inc();
        status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
    }
    String fullMsg = "Failure: " + status + errReason;
    logger.trace("Constructed error response for the client - [{}]", fullMsg);
    FullHttpResponse response;
    if (request != null && !request.getRestMethod().equals(RestMethod.HEAD)) {
        response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status,
                Unpooled.wrappedBuffer(fullMsg.getBytes()));
    } else {
        // for HEAD, we cannot send the actual body but we need to return what the length would have been if this was GET.
        // https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html (Section 9.4)
        response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
    }
    HttpHeaders.setDate(response, new GregorianCalendar().getTime());
    HttpHeaders.setContentLength(response, fullMsg.length());
    HttpHeaders.setHeader(response, HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    boolean keepAlive = request != null && !request.getRestMethod().equals(RestMethod.POST)
            && !CLOSE_CONNECTION_ERROR_STATUSES.contains(status);
    HttpHeaders.setKeepAlive(response, keepAlive);
    return response;
}