1 package ch.mimo.netty.example.icap.squidechoserver;
2
3 import org.jboss.netty.channel.ChannelHandlerContext;
4 import org.jboss.netty.channel.ExceptionEvent;
5 import org.jboss.netty.channel.MessageEvent;
6 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
7
8 import ch.mimo.netty.handler.codec.icap.DefaultIcapResponse;
9 import ch.mimo.netty.handler.codec.icap.IcapHeaders;
10 import ch.mimo.netty.handler.codec.icap.IcapMethod;
11 import ch.mimo.netty.handler.codec.icap.IcapRequest;
12 import ch.mimo.netty.handler.codec.icap.IcapResponse;
13 import ch.mimo.netty.handler.codec.icap.IcapResponseStatus;
14 import ch.mimo.netty.handler.codec.icap.IcapVersion;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public class SquidEchoHandler extends SimpleChannelUpstreamHandler {
34
35 @Override
36 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
37 Object message = e.getMessage();
38 IcapResponse response = null;
39 if(message instanceof IcapRequest) {
40 IcapRequest request = (IcapRequest)message;
41 System.out.println("");
42 System.out.println("---------------------------- receiving " + request.getMethod() + " ----------------------------");
43 System.out.print(message.toString());
44 if(request.getMethod().equals(IcapMethod.OPTIONS)) {
45 response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.OK);
46 response.addHeader("Options-TTL","3600");
47 response.addHeader("Service-ID","Test Icap Server");
48 response.addHeader("Allow","204");
49 response.addHeader("Preview","1024");
50 response.addHeader("Methods","REQMOD, RESPMOD");
51
52 } else if(request.isPreviewMessage()) {
53 response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.NO_CONTENT);
54 } else {
55 response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.OK);
56 response.addHeader(IcapHeaders.Names.ISTAG,"Echo-Server-1.0");
57 if(request.getMethod().equals(IcapMethod.REQMOD)) {
58 response.setHttpRequest(request.getHttpRequest());
59 }
60 response.setHttpResponse(request.getHttpResponse());
61 }
62 System.out.println("");
63 System.out.println("---------------------------- sending " + response.getStatus() + " ----------------------------");
64 System.out.print(response.toString());
65 ctx.getChannel().write(response);
66 }
67 }
68
69 @Override
70 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
71 System.out.println("");
72 System.out.println("---------------------------- exception ----------------------------");
73 e.getCause().printStackTrace();
74 }
75 }