public class SquidEchoHandler extends SimpleChannelUpstreamHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Object message = e.getMessage();
IcapResponse response = null;
if(message instanceof IcapRequest) {
IcapRequest request = (IcapRequest)message;
System.out.println("");
System.out.println("---------------------------- receiving " + request.getMethod() + " ----------------------------");
System.out.print(message.toString());
if(request.getMethod().equals(IcapMethod.OPTIONS)) {
response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.OK);
response.addHeader("Options-TTL","3600");
response.addHeader("Service-ID","Test Icap Server");
response.addHeader("Allow","204");
response.addHeader("Preview","1024");
response.addHeader("Methods","REQMOD, RESPMOD");
} else if(request.isPreviewMessage()) {
response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.NO_CONTENT);
} else {
response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.OK);
response.addHeader(IcapHeaders.Names.ISTAG,"Echo-Server-1.0");
if(request.getMethod().equals(IcapMethod.REQMOD)) {
response.setHttpRequest(request.getHttpRequest());
}
response.setHttpResponse(request.getHttpResponse());
}
System.out.println("");
System.out.println("---------------------------- sending " + response.getStatus() + " ----------------------------");
System.out.print(response.toString());
ctx.getChannel().write(response);
}
}