FilterChain vs HttpHandler (2.2.19)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

FilterChain vs HttpHandler (2.2.19)

pepperjack77
This post has NOT been accepted by the mailing list yet.

I'm attempting to implement a simple web server that, upon receipt of an HTTP request, will:

  • Process the specified filters
  • Echo the request back to the sender

I am submitting the request from soapUI and seeing behavior that I don't expect:

RunServer.java:
    public class RunServer{
        public static final String HOST = "localhost";
        public static final int PORT = 5151;
        
        public static void main(String[] args) {
            final HttpServer server = HttpServer.createSimpleServer();
            
            NetworkListener listener = new NetworkListener("echoCalls", "localhost", 5151);
            
            FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless();

            filterChainBuilder.add(new TransportFilter());

            filterChainBuilder.add(new StringFilter(Charset.forName("UTF-8")));
            //filterChainBuilder.add(new StringFilter(Charset.forName("ISO-8859-1")));
            //filterChainBuilder.add(new StringFilter(Charset.forName("ASCII")));

            filterChainBuilder.add(new MyEchoFilter());
            
            listener.setFilterChain(filterChainBuilder.build());

            server.addListener(listener);
            
            server.getServerConfiguration().addHttpHandler(new MyEchoHandler(), "/echo");
            
            try{
                server.start();
                System.out.println("Press any key to stop the server...");
                System.in.read();
            }catch(IOException ioe){
                System.err.println(ioe.toString());
            }finally{
                server.stop();
            }
            
        }

    }
MyEchoFilter.java:
    public class MyEchoFilter extends BaseFilter{

        public NextAction handleRead(FilterChainContext ctx)throws IOException{
            
            System.err.println(">>>MyEchoFilter.handleRead()...");

            //|why not working? no response, eventual time out. soapui just hangs up after return NextAction...
            
            final Object peerAddress = ctx.getAddress();
            final Object message = ctx.getMessage();
            ctx.write(peerAddress, message, null);
            
            return ctx.getStopAction();
        }
        
        public NextAction handleWrite(FilterChainContext ctx) throws IOException{
            System.err.println(">>>MyEchoFilter.handleWrite()...");
            
            return ctx.getStopAction();
        }

        public NextAction handleConnect(FilterChainContext ctx) throws IOException{
            System.err.println(">>>MyEchoFilter.handleConnect()...");
            
            return ctx.getStopAction();
        }

        public NextAction handleAccept(FilterChainContext ctx) throws IOException{
            System.err.println(">>>MyEchoFilter.handleAccept()...");
            
            return ctx.getStopAction();
        }

        public NextAction handleClose(FilterChainContext ctx) throws IOException{
            System.err.println(">>>MyEchoFilter.handleClose()...");
            
            return ctx.getStopAction();
        }

    }
MyEchoHandler:
    public class MyEchoHandler extends HttpHandler{

        public void service(Request req, Response res) throws Exception{
            
            System.err.println(">>>handler service method...");

            BufferedReader requestStream = new BufferedReader(new InputStreamReader(req.getInputStream()));
            
            String reqStrLn;
            StringBuffer callContent = new StringBuffer();
            while((reqStrLn = requestStream.readLine()) != null) callContent.append(reqStrLn);
            
            res.setContentLength(callContent.length());
            res.getWriter().write(callContent.toString());
        }
    }

With the code above, as-is, the handleAccept() method is fired upon initiating a soapUI request. Strangely, the handleRead() method does not fire, unless I comment out the UTF-8 string filter, in RunServer:

    //filterChainBuilder.add(new StringFilter(Charset.forName("UTF-8")));

I can't figure that out, if anyone can explain, I would appreciate it.

But, more importantly, the service() method in MyEchoHandler does not fire either.

That only fires if I comment out the line that sets the FilterChain to the NetworkListener, in RunServer:

    //listener.setFilterChain(filterChainBuilder.build());

When I do avoid using a FilterChain, I actually do get a response back to soapUI, but I really need the FilterChain.

It may seem like I'm missing part of the point here, that I should be avoiding handlers all together, in favor of using filters, but I haven't been able to get a response back to soapUI while using filters.

Can anyone help me pinpoint the error of my ways?

Thank you in advance!