use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project ambry by linkedin.
the class FrontendIntegrationTest method createEncoder.
/**
* 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;
}
use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project jocean-http by isdom.
the class HttpPostRequestEncoderTestCase method testGetBodyListAttributes.
@Test
public final void testGetBodyListAttributes() throws Exception {
final HttpDataFactory factory = new DefaultHttpDataFactory(false);
final HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
// Use the PostBody encoder
final HttpPostRequestEncoder postRequestEncoder = // true => multipart
new HttpPostRequestEncoder(factory, request, true);
final MemoryFileUpload f1 = new MemoryFileUpload("m1", "m1", "application/json", null, null, 100);
final DiskFileUpload f2 = new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
final DiskFileUpload f3 = new DiskFileUpload("d2", "d2", "application/json", null, null, 100);
postRequestEncoder.addBodyHttpData(f1);
postRequestEncoder.addBodyHttpData(f2);
postRequestEncoder.addBodyHttpData(f3);
final List<InterfaceHttpData> attrs = postRequestEncoder.getBodyListAttributes();
final InterfaceHttpData[] datas = new InterfaceHttpData[] { f1, f2, f3 };
for (int idx = 0; idx < datas.length; idx++) {
assertEquals(datas[idx], attrs.toArray(new InterfaceHttpData[0])[idx]);
}
}
use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project jocean-http by isdom.
the class DefaultSignalClient method addSignalToMultipart.
private long addSignalToMultipart(final HttpPostRequestEncoder postRequestEncoder, final BodyForm body) throws IOException, ErrorDataEncoderException {
if (null != body) {
final MemoryFileUpload signalPayload = new MemoryFileUpload("__signal", "__signal", body.contentType(), null, null, body.length()) {
@Override
public Charset getCharset() {
return null;
}
};
signalPayload.setContent(body.content().retain());
postRequestEncoder.addBodyHttpData(signalPayload);
return body.length();
} else {
// TODO ?
return 0;
}
}
use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project jocean-http by isdom.
the class AttachmentBuilder4InMemory method call.
@Override
public HttpData call(final Attachment attachment) {
if (attachment instanceof AttachmentInMemory) {
final AttachmentInMemory inmemory = (AttachmentInMemory) attachment;
final MemoryFileUpload fileupload = new MemoryFileUpload(inmemory.filename, inmemory.filename, inmemory.contentType, null, null, inmemory.content().length) {
@Override
public Charset getCharset() {
return null;
}
};
try {
fileupload.setContent(Unpooled.wrappedBuffer(inmemory.content()));
} catch (IOException e) {
LOG.warn("exception when fileupload.setContent, detail: {}", ExceptionUtils.exception2detail(e));
}
return fileupload;
} else {
throw new RuntimeException("attachment " + attachment + " is not AttachmentInMemory instance.");
}
}
use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project ambry by linkedin.
the class FrontendIntegrationTestBase method createEncoder.
/**
* 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
*/
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", "", StandardCharsets.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", "", StandardCharsets.UTF_8, usermetadata.remaining());
fileUpload.setContent(Unpooled.wrappedBuffer(usermetadata));
encoder.addBodyHttpData(fileUpload);
return encoder;
}
Aggregations