Ejemplo n.º 1
0
 /**
  * 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.
  * @param usermetadata the {@link ByteBuffer} that represents user metadata
  * @return a {@link HttpPostRequestEncoder} that can encode the {@code request} and {@code
  *     blobContent}.
  * @throws HttpPostRequestEncoder.ErrorDataEncoderException
  * @throws IOException
  */
 private HttpPostRequestEncoder createEncoder(
     HttpRequest request, ByteBuffer blobContent, ByteBuffer usermetadata)
     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);
   fileUpload =
       new MemoryFileUpload(
           RestUtils.MultipartPost.USER_METADATA_PART,
           RestUtils.MultipartPost.USER_METADATA_PART,
           "application/octet-stream",
           "",
           Charset.forName("UTF-8"),
           usermetadata.remaining());
   fileUpload.setContent(Unpooled.wrappedBuffer(usermetadata));
   encoder.addBodyHttpData(fileUpload);
   return encoder;
 }
Ejemplo n.º 2
0
  /**
   * Posts a blob with the given {@code headers} and {@code content}.
   *
   * @param headers the headers required.
   * @param content the content of the blob.
   * @param usermetadata the {@link ByteBuffer} that represents user metadata
   * @return the blob ID of the blob.
   * @throws Exception
   */
  private String multipartPostBlobAndVerify(
      HttpHeaders headers, ByteBuffer content, ByteBuffer usermetadata) throws Exception {
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, usermetadata);
    Queue<HttpObject> responseParts =
        nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus());
    assertTrue(
        "No Date header",
        HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null);
    assertTrue(
        "No " + RestUtils.Headers.CREATION_TIME,
        HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null);
    assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
    String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null);

    if (blobId == null) {
      fail("postBlobAndVerify did not return a blob ID");
    }
    discardContent(responseParts, 1);
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
    return blobId;
  }