我正在尝试实现一个简单的Grizzly Web服务器,该服务器在收到HTTP请求后将:
处理指定的过滤器
将请求回送给发件人
我正在从soapUI提交请求,并且看到了意外的行为:
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());
}
}
使用上面的代码,按原样,在启动soapUI请求时将触发handleAccept()方法。奇怪的是,除非我在RunServer中注释掉UTF-8字符串过滤器,否则handleRead()方法不会触发:
//filterChainBuilder.add(new StringFilter(Charset.forName("UTF-8")));
我无法弄清楚,如果有人能解释,我将不胜感激。
但是,更重要的是,MyEchoHandler中的service()方法也不会触发。
仅当我注释掉在RunServer中将FilterChain设置为NetworkListener的行时才会触发:
//listener.setFilterChain(filterChainBuilder.build());
当我确实避免使用FilterChain时,实际上确实得到了对soapUI的响应,但我确实需要FilterChain。
似乎我在这里遗漏了一部分要点,我应该避免使用处理程序,而是使用过滤器,但是我在使用过滤器时无法获得对soapUI的响应。
谁能帮助我找出我的方式的错误?
先感谢您!
最佳答案
当我也遇到这个问题时,确实花了我很多时间来弄清楚。
filterChainBuilder.add(new StringFilter(Charset.forName("UTF-8")));
StringFilter将字符串解码/编码为字节,因此使用特殊的长度前缀。如果传入消息无法解码,则会跳过该消息。因此,看起来方法'handleRead'不会触发。
关于java - 灰熊2.2.19 FilterChain与HttpHandler,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20963970/