use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData 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.InterfaceHttpData in project netty by netty.
the class HttpUploadClient method main.
public static void main(String[] args) throws Exception {
String postSimple, postFile, get;
if (BASE_URL.endsWith("/")) {
postSimple = BASE_URL + "formpost";
postFile = BASE_URL + "formpostmultipart";
get = BASE_URL + "formget";
} else {
postSimple = BASE_URL + "/formpost";
postFile = BASE_URL + "/formpostmultipart";
get = BASE_URL + "/formget";
}
URI uriSimple = new URI(postSimple);
String scheme = uriSimple.getScheme() == null ? "http" : uriSimple.getScheme();
String host = uriSimple.getHost() == null ? "127.0.0.1" : uriSimple.getHost();
int port = uriSimple.getPort();
if (port == -1) {
if ("http".equalsIgnoreCase(scheme)) {
port = 80;
} else if ("https".equalsIgnoreCase(scheme)) {
port = 443;
}
}
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
System.err.println("Only HTTP(S) is supported.");
return;
}
final boolean ssl = "https".equalsIgnoreCase(scheme);
final SslContext sslCtx;
if (ssl) {
sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} else {
sslCtx = null;
}
URI uriFile = new URI(postFile);
File file = new File(FILE);
if (!file.canRead()) {
throw new FileNotFoundException(FILE);
}
// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
// setup the factory: here using a mixed memory/disk based on size threshold
// Disk if MINSIZE exceed
HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
// should delete file on exit (in normal exit)
DiskFileUpload.deleteOnExitTemporaryFile = true;
// system temp directory
DiskFileUpload.baseDirectory = null;
// should delete file on exit (in normal exit)
DiskAttribute.deleteOnExitTemporaryFile = true;
// system temp directory
DiskAttribute.baseDirectory = null;
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).handler(new HttpUploadClientInitializer(sslCtx));
// Simple Get form: no factory used (not usable)
List<Entry<String, String>> headers = formget(b, host, port, get, uriSimple);
if (headers == null) {
factory.cleanAllHttpData();
return;
}
// Simple Post form: factory used for big attributes
List<InterfaceHttpData> bodylist = formpost(b, host, port, uriSimple, file, factory, headers);
if (bodylist == null) {
factory.cleanAllHttpData();
return;
}
// Multipart Post form: factory used
formpostmultipart(b, host, port, uriFile, factory, headers, bodylist);
} finally {
// Shut down executor threads to exit.
group.shutdownGracefully();
// Really clean all temporary files if they still exist
factory.cleanAllHttpData();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project vert.x by eclipse.
the class Http2ServerRequest method notifyException.
private void notifyException(Throwable failure) {
InterfaceHttpData upload = null;
HttpEventHandler handler;
synchronized (conn) {
if (postRequestDecoder != null) {
upload = postRequestDecoder.currentPartialHttpData();
}
handler = eventHandler;
}
if (handler != null) {
handler.handleException(failure);
}
if (upload instanceof NettyFileUpload) {
((NettyFileUpload) upload).handleException(failure);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project vert.x by eclipse.
the class Http2ServerRequest method handleEnd.
void handleEnd(MultiMap trailers) {
HttpEventHandler handler;
synchronized (conn) {
streamEnded = true;
ended = true;
if (postRequestDecoder != null) {
try {
postRequestDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
while (postRequestDecoder.hasNext()) {
InterfaceHttpData data = postRequestDecoder.next();
if (data instanceof Attribute) {
Attribute attr = (Attribute) data;
try {
formAttributes().add(attr.getName(), attr.getValue());
} catch (Exception e) {
// Will never happen, anyway handle it somehow just in case
handleException(e);
} finally {
attr.release();
}
}
}
} catch (HttpPostRequestDecoder.EndOfDataDecoderException e) {
// ignore this as it is expected
} catch (Exception e) {
handleException(e);
} finally {
postRequestDecoder.destroy();
}
}
handler = eventHandler;
}
if (handler != null) {
handler.handleEnd();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project cloudstack by apache.
the class HttpUploadServerHandler method readFileUploadData.
private HttpResponseStatus readFileUploadData() throws IOException {
while (decoder.hasNext()) {
InterfaceHttpData data = decoder.next();
if (data != null) {
try {
logger.info("BODY FileUpload: " + data.getHttpDataType().name() + ": " + data);
if (data.getHttpDataType() == HttpDataType.FileUpload) {
FileUpload fileUpload = (FileUpload) data;
if (fileUpload.isCompleted()) {
requestProcessed = true;
String format = ImageStoreUtil.checkTemplateFormat(fileUpload.getFile().getAbsolutePath(), fileUpload.getFilename());
if (StringUtils.isNotBlank(format)) {
String errorString = "File type mismatch between the sent file and the actual content. Received: " + format;
logger.error(errorString);
responseContent.append(errorString);
storageResource.updateStateMapWithError(uuid, errorString);
return HttpResponseStatus.BAD_REQUEST;
}
String status = storageResource.postUpload(uuid, fileUpload.getFile().getName(), processTimeout);
if (status != null) {
responseContent.append(status);
storageResource.updateStateMapWithError(uuid, status);
return HttpResponseStatus.INTERNAL_SERVER_ERROR;
} else {
responseContent.append("upload successful.");
return HttpResponseStatus.OK;
}
}
}
} finally {
data.release();
}
}
}
responseContent.append("received entity is not a file");
return HttpResponseStatus.UNPROCESSABLE_ENTITY;
}
Aggregations