use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project ballerina by ballerina-lang.
the class Util method encodeBodyPart.
/**
* Encode a given body part and add it to multipart request encoder.
*
* @param nettyEncoder Helps encode multipart/form-data
* @param httpRequest Represent top level http request that should hold multiparts
* @param bodyPart Represent a ballerina body part
* @throws HttpPostRequestEncoder.ErrorDataEncoderException when an error occurs while encoding
*/
private static void encodeBodyPart(HttpPostRequestEncoder nettyEncoder, HttpRequest httpRequest, BStruct bodyPart) throws HttpPostRequestEncoder.ErrorDataEncoderException {
try {
InterfaceHttpData encodedData;
Channel byteChannel = EntityBodyHandler.getByteChannel(bodyPart);
FileUploadContentHolder contentHolder = new FileUploadContentHolder();
contentHolder.setRequest(httpRequest);
contentHolder.setBodyPartName(getBodyPartName(bodyPart));
contentHolder.setFileName(TEMP_FILE_NAME + TEMP_FILE_EXTENSION);
contentHolder.setContentType(MimeUtil.getBaseType(bodyPart));
contentHolder.setBodyPartFormat(org.ballerinalang.mime.util.Constants.BodyPartForm.INPUTSTREAM);
String contentTransferHeaderValue = HeaderUtil.getHeaderValue(bodyPart, HttpHeaderNames.CONTENT_TRANSFER_ENCODING.toString());
if (contentTransferHeaderValue != null) {
contentHolder.setContentTransferEncoding(contentTransferHeaderValue);
}
if (byteChannel != null) {
contentHolder.setContentStream(byteChannel.getInputStream());
encodedData = getFileUpload(contentHolder);
if (encodedData != null) {
nettyEncoder.addBodyHttpData(encodedData);
}
}
} catch (IOException e) {
log.error("Error occurred while encoding body part in ", e.getMessage());
}
}
use of io.netty.handler.codec.http.multipart.InterfaceHttpData 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;
}
use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project netty by netty.
the class HttpUploadServerHandler method readHttpDataChunkByChunk.
/**
* Example of reading request by chunk and getting values from chunk to chunk
*/
private void readHttpDataChunkByChunk() {
try {
while (decoder.hasNext()) {
InterfaceHttpData data = decoder.next();
if (data != null) {
// check if current HttpData is a FileUpload and previously set as partial
if (partialContent == data) {
logger.info(" 100% (FinalSize: " + partialContent.length() + ")");
partialContent = null;
}
// new value
writeHttpData(data);
}
}
// Check partial decoding for a FileUpload
InterfaceHttpData data = decoder.currentPartialHttpData();
if (data != null) {
StringBuilder builder = new StringBuilder();
if (partialContent == null) {
partialContent = (HttpData) data;
if (partialContent instanceof FileUpload) {
builder.append("Start FileUpload: ").append(((FileUpload) partialContent).getFilename()).append(" ");
} else {
builder.append("Start Attribute: ").append(partialContent.getName()).append(" ");
}
builder.append("(DefinedSize: ").append(partialContent.definedLength()).append(")");
}
if (partialContent.definedLength() > 0) {
builder.append(" ").append(partialContent.length() * 100 / partialContent.definedLength()).append("% ");
logger.info(builder.toString());
} else {
builder.append(" ").append(partialContent.length()).append(" ");
logger.info(builder.toString());
}
}
} catch (EndOfDataDecoderException e1) {
// end
responseContent.append("\r\n\r\nEND OF CONTENT CHUNK BY CHUNK\r\n\r\n");
}
}
use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project flink by apache.
the class FileUploadHandler method channelRead0.
@Override
protected void channelRead0(final ChannelHandlerContext ctx, final HttpObject msg) throws Exception {
try {
if (msg instanceof HttpRequest) {
final HttpRequest httpRequest = (HttpRequest) msg;
LOG.trace("Received request. URL:{} Method:{}", httpRequest.getUri(), httpRequest.getMethod());
if (httpRequest.getMethod().equals(HttpMethod.POST)) {
if (HttpPostRequestDecoder.isMultipart(httpRequest)) {
LOG.trace("Initializing multipart file upload.");
checkState(currentHttpPostRequestDecoder == null);
checkState(currentHttpRequest == null);
checkState(currentUploadDir == null);
currentHttpPostRequestDecoder = new HttpPostRequestDecoder(DATA_FACTORY, httpRequest);
currentHttpRequest = ReferenceCountUtil.retain(httpRequest);
// make sure that we still have a upload dir in case that it got deleted in
// the meanwhile
RestServerEndpoint.createUploadDir(uploadDir, LOG, false);
currentUploadDir = Files.createDirectory(uploadDir.resolve(UUID.randomUUID().toString()));
} else {
ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
}
} else {
ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
}
} else if (msg instanceof HttpContent && currentHttpPostRequestDecoder != null) {
LOG.trace("Received http content.");
// make sure that we still have a upload dir in case that it got deleted in the
// meanwhile
RestServerEndpoint.createUploadDir(uploadDir, LOG, false);
final HttpContent httpContent = (HttpContent) msg;
currentHttpPostRequestDecoder.offer(httpContent);
while (httpContent != LastHttpContent.EMPTY_LAST_CONTENT && currentHttpPostRequestDecoder.hasNext()) {
final InterfaceHttpData data = currentHttpPostRequestDecoder.next();
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.FileUpload) {
final DiskFileUpload fileUpload = (DiskFileUpload) data;
checkState(fileUpload.isCompleted());
// wrapping around another File instantiation is a simple way to remove any
// path information - we're
// solely interested in the filename
final Path dest = currentUploadDir.resolve(new File(fileUpload.getFilename()).getName());
fileUpload.renameTo(dest.toFile());
LOG.trace("Upload of file {} into destination {} complete.", fileUpload.getFilename(), dest.toString());
} else if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
final Attribute request = (Attribute) data;
// this could also be implemented by using the first found Attribute as the
// payload
LOG.trace("Upload of attribute {} complete.", request.getName());
if (data.getName().equals(HTTP_ATTRIBUTE_REQUEST)) {
currentJsonPayload = request.get();
} else {
handleError(ctx, "Received unknown attribute " + data.getName() + '.', HttpResponseStatus.BAD_REQUEST, null);
return;
}
}
}
if (httpContent instanceof LastHttpContent) {
LOG.trace("Finalizing multipart file upload.");
ctx.channel().attr(UPLOADED_FILES).set(new FileUploads(currentUploadDir));
if (currentJsonPayload != null) {
currentHttpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, currentJsonPayload.length);
currentHttpRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);
ctx.fireChannelRead(currentHttpRequest);
ctx.fireChannelRead(httpContent.replace(Unpooled.wrappedBuffer(currentJsonPayload)));
} else {
currentHttpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);
currentHttpRequest.headers().remove(HttpHeaders.Names.CONTENT_TYPE);
ctx.fireChannelRead(currentHttpRequest);
ctx.fireChannelRead(LastHttpContent.EMPTY_LAST_CONTENT);
}
reset();
}
} else {
ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
}
} catch (Exception e) {
handleError(ctx, "File upload failed.", HttpResponseStatus.INTERNAL_SERVER_ERROR, e);
}
}
use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project vert.x by eclipse.
the class Http1xServerRequest method handleException.
void handleException(Throwable t) {
HttpEventHandler handler = null;
Http1xServerResponse resp = null;
InterfaceHttpData upload = null;
synchronized (conn) {
if (!isEnded()) {
handler = eventHandler;
if (decoder != null) {
upload = decoder.currentPartialHttpData();
}
}
if (!response.ended()) {
if (METRICS_ENABLED) {
reportRequestReset(t);
}
resp = response;
}
}
if (resp != null) {
resp.handleException(t);
}
if (upload instanceof NettyFileUpload) {
((NettyFileUpload) upload).handleException(t);
}
if (handler != null) {
handler.handleException(t);
}
}
Aggregations