use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder 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 org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project jocean-http by isdom.
the class DefaultSignalClient method assembleOutgoing.
private Outgoing assembleOutgoing(final HttpRequest request, final BodyForm body, final Attachment[] attachments) throws Exception {
if (0 == attachments.length) {
final LastHttpContent lastContent = buildLastContent(request, body);
return new Outgoing(Observable.<Object>just(request, lastContent), lastContent.content().readableBytes(), new Action0() {
@Override
public void call() {
ReferenceCountUtil.release(request);
ReferenceCountUtil.release(lastContent);
}
});
} else {
// Use the PostBody encoder
final HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(_DATA_FACTORY, request, true, CharsetUtil.UTF_8, // true => multipart
EncoderMode.HTML5);
final long signalSize = addSignalToMultipart(postRequestEncoder, body);
final long attachmentSize = addAttachmentsToMultipart(postRequestEncoder, attachments);
final long total = signalSize + attachmentSize;
// finalize request
final HttpRequest request4send = postRequestEncoder.finalizeRequest();
final Action0 toRelease = new Action0() {
@Override
public void call() {
ReferenceCountUtil.release(request4send);
RxNettys.releaseObjects(postRequestEncoder.getBodyListAttributes());
}
};
return postRequestEncoder.isChunked() ? new Outgoing(Observable.<Object>just(request4send, postRequestEncoder), total, toRelease) : new Outgoing(Observable.<Object>just(request4send), total, toRelease);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project ballerina by ballerina-lang.
the class Util method prepareRequestWithMultiparts.
/**
* Prepare carbon request message with multiparts.
*
* @param outboundRequest Represent outbound carbon request
* @param requestStruct Ballerina request struct which contains multipart data
*/
private static void prepareRequestWithMultiparts(HTTPCarbonMessage outboundRequest, BStruct requestStruct) {
BStruct entityStruct = requestStruct.getNativeData(MESSAGE_ENTITY) != null ? (BStruct) requestStruct.getNativeData(MESSAGE_ENTITY) : null;
if (entityStruct != null) {
BRefValueArray bodyParts = entityStruct.getNativeData(BODY_PARTS) != null ? (BRefValueArray) entityStruct.getNativeData(BODY_PARTS) : null;
if (bodyParts != null) {
HttpDataFactory dataFactory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
setDataFactory(dataFactory);
try {
HttpPostRequestEncoder nettyEncoder = new HttpPostRequestEncoder(dataFactory, outboundRequest.getNettyHttpRequest(), true);
for (int i = 0; i < bodyParts.size(); i++) {
BStruct bodyPart = (BStruct) bodyParts.get(i);
encodeBodyPart(nettyEncoder, outboundRequest.getNettyHttpRequest(), bodyPart);
}
nettyEncoder.finalizeRequest();
requestStruct.addNativeData(MULTIPART_ENCODER, nettyEncoder);
} catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
log.error("Error occurred while creating netty request encoder for multipart data binding", e.getMessage());
}
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project netty by netty.
the class HttpUploadClient method formpostmultipart.
/**
* 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(SocketUtils.socketAddress(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 = // true => multipart
new HttpPostRequestEncoder(factory, request, true);
// 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();
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project netty by netty.
the class HttpUploadClient method formpost.
/**
* 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(SocketUtils.socketAddress(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 = // false => not multipart
new HttpPostRequestEncoder(factory, request, false);
// 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;
}
Aggregations