I'm having some troubles to get the type of the inputs from a POST received in netty. I have a class that read all the received attributes from the post request, and I want to filter out the attributes corresponding to inputs of submit type from forms. That is, if I have this form:
<form action="https://127.0.0.1:10005/firmarMultiplesDocumentos" name="" method="POST" enctype="multipart/form-data">
File: <input type="file" name="File1" /><br>
NHC: <input type="text" name="NHC" value="555555" /><br>
<input type="submit" name="Send" />
</form>
I want to get only the File1 and NHC attributes and discard the Send attribute.
If it helps, this is my channel definition:
secureBossGroup = new NioEventLoopGroup();
secureWorkerGroup = new NioEventLoopGroup();
secureServerBootstrap = new ServerBootstrap();
secureServerBootstrap.group(secureBossGroup, secureWorkerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addFirst(getSSLContext().newHandler(ch.alloc()));
ch.pipeline().addLast(new HttpServerCodec());
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(100 * 1024 * 1024));
ch.pipeline().addLast(new CustomChannelHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
And this is part of my CustomChannelHandler:
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = fullHttpRequest = (FullHttpRequest) msg;
try {
decoder = new HttpPostRequestDecoder(dataFactory, request);
decoder.setDiscardThreshold(0);
} catch (Exception e) {
// Error Handler
}
}
if (decoder != null) {
if (msg instanceof HttpContent) {
HttpContent chunk = (HttpContent) msg;
try {
decoder.offer(chunk);
} catch (ErrorDataDecoderException e) {
//Error handler
}
// Read data as it becomes available, chunk by chunk.
readChunkByChunk(ctx);
if (chunk instanceof LastHttpContent) {
readChunkByChunk(ctx);
try {
prepareResponse(ctx);
} catch (Exception e){
// Error handler
}
resetPostRequestDecoder();
}
}
} else {
// Error handler
}
}
private void readChunkByChunk(ChannelHandlerContext ctx) {
try {
while (decoder.hasNext()) {
InterfaceHttpData data = decoder.next();
if (data != null) {
try {
processChunk(ctx, data);
} catch (IOException e) {
// Error handler
} finally {
data.release();
}
}
}
} catch (EndOfDataDecoderException e) {
// No more data to decode, that's fine
}
}
private void processChunk(ChannelHandlerContext ctx, InterfaceHttpData data) throws IOException {
LOGGER.debug("HTTP Data Name: {}, Type: {}" + data.getName() + data.getHttpDataType());
switch (data.getHttpDataType()) {
case Attribute:
Attribute attrib = (Attribute) data;
try {
int bytes = attrib.getByteBuf().readableBytes();
String name = attrib.getName();
readData = attrib.getByteBuf().toString(CharsetUtil.UTF_8);
// Attribute Handling
} catch (IOException e) {
// Error handler
}
break;
case FileUpload:
// FileUpload stuff
Now, all the fields of the form go through the "Attribute" case on processChunk, so I was guessing if it would be possible to get the their type to filter the submit fields from forms.
Thanks, Cris.
I believe types are not passed to the sever by the client when it sends the request based on a form. So there is no way to know type=text, submit or file... Except for file since there is a special handling...
Look at w3 example at the end here.