use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload in project jocean-http by isdom.
the class DefaultAttachmentBuilder method call.
@Override
public HttpData call(final Attachment attachment) {
final File file = new File(attachment.filename);
final DiskFileUpload filePayload = new DiskFileUpload(null != attachment.name ? attachment.name : FilenameUtils.getBaseName(attachment.filename), attachment.filename, attachment.contentType, null, null, file.length()) {
@Override
public Charset getCharset() {
return null;
}
};
try {
filePayload.setContent(file);
} catch (IOException e) {
LOG.warn("exception when filePayload.setContent, detail: {}", ExceptionUtils.exception2detail(e));
}
return filePayload;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload in project jocean-http by isdom.
the class HttpPostRequestEncoderTestCase method testDiskFileUploadEquals.
@Test
public final void testDiskFileUploadEquals() {
final DiskFileUpload f2 = new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
assertEquals(f2, f2);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload in project flink by apache.
the class HttpRequestHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
try {
if (msg instanceof HttpRequest) {
currentRequest = (HttpRequest) msg;
currentRequestPath = null;
if (currentDecoder != null) {
currentDecoder.destroy();
currentDecoder = null;
}
if (currentRequest.getMethod() == HttpMethod.GET || currentRequest.getMethod() == HttpMethod.DELETE) {
// directly delegate to the router
ctx.fireChannelRead(currentRequest);
} else if (currentRequest.getMethod() == HttpMethod.POST) {
// POST comes in multiple objects. First the request, then the contents
// keep the request and path for the remaining objects of the POST request
currentRequestPath = new QueryStringDecoder(currentRequest.getUri(), ENCODING).path();
currentDecoder = new HttpPostRequestDecoder(DATA_FACTORY, currentRequest, ENCODING);
} else {
throw new IOException("Unsupported HTTP method: " + currentRequest.getMethod().name());
}
} else if (currentDecoder != null && msg instanceof HttpContent) {
// received new chunk, give it to the current decoder
HttpContent chunk = (HttpContent) msg;
currentDecoder.offer(chunk);
try {
while (currentDecoder.hasNext()) {
InterfaceHttpData data = currentDecoder.next();
if (data.getHttpDataType() == HttpDataType.FileUpload && tmpDir != null) {
DiskFileUpload file = (DiskFileUpload) data;
if (file.isCompleted()) {
String name = file.getFilename();
File target = new File(tmpDir, UUID.randomUUID() + "_" + name);
if (!tmpDir.exists()) {
logExternalUploadDirDeletion(tmpDir);
checkAndCreateUploadDir(tmpDir);
}
file.renameTo(target);
QueryStringEncoder encoder = new QueryStringEncoder(currentRequestPath);
encoder.addParam("filepath", target.getAbsolutePath());
encoder.addParam("filename", name);
currentRequest.setUri(encoder.toString());
}
}
}
} catch (EndOfDataDecoderException ignored) {
}
if (chunk instanceof LastHttpContent) {
HttpRequest request = currentRequest;
currentRequest = null;
currentRequestPath = null;
currentDecoder.destroy();
currentDecoder = null;
// fire next channel handler
ctx.fireChannelRead(request);
}
} else {
ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
}
} catch (Throwable t) {
currentRequest = null;
currentRequestPath = null;
if (currentDecoder != null) {
currentDecoder.destroy();
currentDecoder = null;
}
if (ctx.channel().isActive()) {
byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING);
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
ctx.writeAndFlush(response);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload 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.DiskFileUpload 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);
}
}
Aggregations