当前位置: 首页>>代码示例>>Java>>正文


Java HttpPostRequestEncoder.finalizeRequest方法代码示例

本文整理汇总了Java中io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.finalizeRequest方法的典型用法代码示例。如果您正苦于以下问题:Java HttpPostRequestEncoder.finalizeRequest方法的具体用法?Java HttpPostRequestEncoder.finalizeRequest怎么用?Java HttpPostRequestEncoder.finalizeRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.netty.handler.codec.http.multipart.HttpPostRequestEncoder的用法示例。


在下文中一共展示了HttpPostRequestEncoder.finalizeRequest方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createRequest

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder; //导入方法依赖的package包/类
/**
 * Creates a {@link NettyMultipartRequest} with the given {@code headers} and {@code parts}.
 * @param headers the {@link HttpHeaders} that need to be added to the request.
 * @param parts the files that will form the parts of the request.
 * @return a {@link NettyMultipartRequest} containing all the {@code headers} and {@code parts}.
 * @throws Exception
 */
private NettyMultipartRequest createRequest(HttpHeaders headers, InMemoryFile[] parts) throws Exception {
  HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
  if (headers != null) {
    httpRequest.headers().set(headers);
  }
  HttpPostRequestEncoder encoder = createEncoder(httpRequest, parts);
  NettyMultipartRequest request =
      new NettyMultipartRequest(encoder.finalizeRequest(), new MockChannel(), NETTY_METRICS, Collections.emptySet(),
          Long.MAX_VALUE);
  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(PooledByteBufAllocator.DEFAULT));
  }
  return request;
}
 
开发者ID:linkedin,项目名称:ambry,代码行数:24,代码来源:NettyMultipartRequestTest.java

示例2: multipartPostTest

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder; //导入方法依赖的package包/类
/**
 * Tests the case where multipart upload is used.
 * @throws Exception
 */
@Test
public void multipartPostTest() throws Exception {
  Random random = new Random();
  ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(random.nextInt(128) + 128));
  HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
  httpRequest.headers().set(RestUtils.Headers.SERVICE_ID, "rawBytesPostTest");
  HttpPostRequestEncoder encoder = createEncoder(httpRequest, content);
  HttpRequest postRequest = encoder.finalizeRequest();
  List<ByteBuffer> contents = new ArrayList<ByteBuffer>();
  while (!encoder.isEndOfInput()) {
    // Sending null for ctx because the encoder is OK with that.
    contents.add(encoder.readChunk(PooledByteBufAllocator.DEFAULT).content().nioBuffer());
  }
  ByteBuffer receivedContent = doPostTest(postRequest, contents);
  compareContent(receivedContent, Collections.singletonList(content));
}
 
开发者ID:linkedin,项目名称:ambry,代码行数:21,代码来源:NettyMessageProcessorTest.java

示例3: getEncodedSize

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder; //导入方法依赖的package包/类
/**
 * Gets the encoded size for a set of bytes
 * @param bytes the bytes to encode.
 * @return the encoded size
 * @throws Exception
 */
private int getEncodedSize(byte[] bytes) throws Exception {
  int encodedSize = 0;
  InMemoryFile[] files = {new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(bytes))};
  HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
  HttpPostRequestEncoder encoder = createEncoder(httpRequest, files);
  encoder.finalizeRequest();
  while (!encoder.isEndOfInput()) {
    HttpContent httpContent = encoder.readChunk(PooledByteBufAllocator.DEFAULT);
    encodedSize += httpContent.content().readableBytes();
  }
  return encodedSize;
}
 
开发者ID:linkedin,项目名称:ambry,代码行数:19,代码来源:NettyMultipartRequestTest.java

示例4: formpost

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder; //导入方法依赖的package包/类
/**
 * Standard post without multipart but already support on Factory (memory management)
 *
 * @return the list of HttpData object (attribute and file) to be reused on next post
 */
private static List<InterfaceHttpData> formpost(
        Bootstrap bootstrap,
        String host, int port, URI uriSimple, File file, HttpDataFactory factory,
        List<Entry<String, String>> headers) throws Exception {
    // XXX /formpost
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();

    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriSimple.toASCIIString());

    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder =
            new HttpPostRequestEncoder(factory, request, false);  // false => not multipart

    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }

    // add Form attribute
    bodyRequestEncoder.addBodyAttribute("getform", "POST");
    bodyRequestEncoder.addBodyAttribute("info", "first value");
    bodyRequestEncoder.addBodyAttribute("secondinfo", "secondvalue ���&");
    bodyRequestEncoder.addBodyAttribute("thirdinfo", textArea);
    bodyRequestEncoder.addBodyAttribute("fourthinfo", textAreaLong);
    bodyRequestEncoder.addBodyFileUpload("myfile", file, "application/x-zip-compressed", false);

    // finalize request
    request = bodyRequestEncoder.finalizeRequest();

    // Create the bodylist to be reused on the last version with Multipart support
    List<InterfaceHttpData> bodylist = bodyRequestEncoder.getBodyListAttributes();

    // send request
    channel.write(request);

    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) { // could do either request.isChunked()
        // either do it through ChunkedWriteHandler
        channel.write(bodyRequestEncoder);
    }
    channel.flush();

    // Do not clear here since we will reuse the InterfaceHttpData on the next request
    // for the example (limit action on client side). Take this as a broadcast of the same
    // request on both Post actions.
    //
    // On standard program, it is clearly recommended to clean all files after each request
    // bodyRequestEncoder.cleanFiles();

    // Wait for the server to close the connection.
    channel.closeFuture().sync();
    return bodylist;
}
 
开发者ID:cowthan,项目名称:JavaAyo,代码行数:63,代码来源:HttpUploadClient.java

示例5: formpostmultipart

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder; //导入方法依赖的package包/类
/**
 * Multipart example
 */
private static void formpostmultipart(
        Bootstrap bootstrap, String host, int port, URI uriFile, HttpDataFactory factory,
        Iterable<Entry<String, String>> headers, List<InterfaceHttpData> bodylist) throws Exception {
    // XXX /formpostmultipart
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();

    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriFile.toASCIIString());

    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder =
            new HttpPostRequestEncoder(factory, request, true); // true => multipart

    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }

    // add Form attribute from previous request in formpost()
    bodyRequestEncoder.setBodyHttpDatas(bodylist);

    // finalize request
    bodyRequestEncoder.finalizeRequest();

    // send request
    channel.write(request);

    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) {
        channel.write(bodyRequestEncoder);
    }
    channel.flush();

    // Now no more use of file representation (and list of HttpData)
    bodyRequestEncoder.cleanFiles();

    // Wait for the server to close the connection.
    channel.closeFuture().sync();
}
 
开发者ID:cowthan,项目名称:JavaAyo,代码行数:46,代码来源:HttpUploadClient.java

示例6: formpostmultipart

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder; //导入方法依赖的package包/类
/**
 * Multipart example
 */
private static void formpostmultipart(
        Bootstrap bootstrap, String host, int port, URI uriFile, HttpDataFactory factory,
        List<Entry<String, String>> headers, List<InterfaceHttpData> bodylist) throws Exception {
    // XXX /formpostmultipart
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();

    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriFile.toASCIIString());

    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder =
            new HttpPostRequestEncoder(factory, request, true); // true => multipart

    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }

    // add Form attribute from previous request in formpost()
    bodyRequestEncoder.setBodyHttpDatas(bodylist);

    // finalize request
    bodyRequestEncoder.finalizeRequest();

    // send request
    channel.write(request);

    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) {
        channel.write(bodyRequestEncoder);
    }
    channel.flush();

    // Now no more use of file representation (and list of HttpData)
    bodyRequestEncoder.cleanFiles();

    // Wait for the server to close the connection.
    channel.closeFuture().sync();
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:46,代码来源:HttpUploadClient.java

示例7: sizeLimitationTest

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder; //导入方法依赖的package包/类
/**
 * Tests to make sure the max allowed size for multipart requests is enforced.
 * @throws Exception
 */
// Disabling test because the encoded size is different at different times and it is hard to predict. Need a better
// test
//@Test
public void sizeLimitationTest() throws Exception {
  int blobPartSize = 1024;
  byte[] bytes = TestUtils.getRandomBytes(blobPartSize);
  int encodedSize = getEncodedSize(bytes);
  long[] maxSizesAllowed = {encodedSize + 1, encodedSize, encodedSize - 1, 0};
  for (long maxSizeAllowed : maxSizesAllowed) {
    InMemoryFile[] files = {new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(bytes))};
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, files);
    NettyMultipartRequest request =
        new NettyMultipartRequest(encoder.finalizeRequest(), new MockChannel(), NETTY_METRICS, Collections.emptySet(),
            maxSizeAllowed);
    assertTrue("Request channel is not open", request.isOpen());
    long currentSizeAdded = 0;
    boolean failedToAdd = false;
    while (!encoder.isEndOfInput()) {
      HttpContent httpContent = encoder.readChunk(PooledByteBufAllocator.DEFAULT);
      int readableBytes = httpContent.content().readableBytes();
      if (currentSizeAdded + readableBytes <= maxSizeAllowed) {
        request.addContent(httpContent);
      } else {
        assertTrue("Max size [" + maxSizeAllowed + "] must be lesser than content size: " + encodedSize,
            maxSizeAllowed < encodedSize);
        try {
          request.addContent(httpContent);
          fail("Should have failed to add content of size [" + encodedSize
              + "] because it is over the max size allowed: " + maxSizeAllowed);
        } catch (RestServiceException e) {
          failedToAdd = true;
          assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestTooLarge, e.getErrorCode());
          break;
        }
      }
      currentSizeAdded += readableBytes;
    }
    assertEquals(
        "Success state not as expected. maxSizeAllowed=[" + maxSizeAllowed + "], encodedSize expected=[" + encodedSize
            + "], actual size added=[" + currentSizeAdded + "]", maxSizeAllowed < encodedSize, failedToAdd);
  }
}
 
开发者ID:linkedin,项目名称:ambry,代码行数:48,代码来源:NettyMultipartRequestTest.java


注:本文中的io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.finalizeRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。