1
votes

Here's my situation:

  1. Client sends a POST request to Netty Server.
  2. Netty processes the POST request and If the server determines that a response needs to be sent It sends a response back to the client. Else The server has to send a POST request to another end point, get the response and send that response back to the client.

So far I have got the incoming POST request working. To send the outgoing POST request this is what I'm doing in my Handler.

private void sendHttpPost(String input, ChannelHandlerContext ctx) {
        try {
            String url = "http://localhost";
            URI uri = new URI(url);
            Bootstrap b = new Bootstrap();
            b.group(new NioEventLoopGroup())
            .channel(NioServerSocketChannel.class)
            .handler(new PostRequestHandler())
            .option(ChannelOption.AUTO_READ, false);
           Channel f = b.connect("REMOTE_HOST", 8888).sync().channel();       
           HttpRequest postReq = new DefaultHttpRequest(HttpVersion.HTTP_1_1, 
                                        HttpMethod.POST, uri.getRawPath());
           postReq.headers().set(HttpHeaders.Names.HOST, "localhost");
           postReq.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
           postReq.headers().set(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded");
           f.writeAndFlush(postReq);

           // Wait for the server to close the connection.
           f.closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        }           
    }

This is obviously wrong because I'm starting a new Bootstrap process within the handler. However, I can't send a HttpRequest without a channel. I can't reuse the existing Channel in the ChannelHandlerContext.

What's the correct way to do this ? How can I fork a new HttpPostRequest. Any help will be greatly appreciated.

1
You can't use the existing channel to connect to another server since that channel already connected with your original client.I don't see anything wrong with this code. And as per your requirement, bootstrapping a client in server's handler is quite ok. You got this program work right? - Sudheera
@Sudheera I do get an unsupportedoperation exception and this code doesn't work .java.lang.UnsupportedOperationException at io.netty.channel.socket.nio.NioServerSocketChannel.doConnect(NioServerSocketChannel.java:135) at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.connect(AbstractNioChannel.java:172) at io.netty.channel.DefaultChannelPipeline$HeadHandler.connect(DefaultChannelPipeline.java:1003) at io.netty.channel.DefaultChannelHandlerContext.invokeConnect0(DefaultChannelHandlerContext.java:527) (can't paste the full stack trace) - Pradeep Banavara
can you post the full handler class here? - Sudheera
Here't the URL with the full handler pastebin.com/jPARd1z8 - Pradeep Banavara
sorry I didn't spotted this before but the address "REMOTE_HOST" is wrong i guess..? - Sudheera

1 Answers

0
votes

Use .channel(NioSocketChannel.class) instead of .channel(NioServerSocketChannel.class) in your client bootstrap code. That's it.