View Javadoc

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   * This handler will enable squid to send all traffic and the responses from this handler will always
18   * allow squid to proceed with the current request or response.
19   * 
20   * The following behavior is standard for this handler:
21   * 
22   * - OPTIONS: responds 204 is valid TTL 1hr and REQ RESPMOD is accepted.
23   * - REQMOD:  HTTP request is returned.
24   * - RESPMOD: HTTP response is returned.
25   * 
26   * - Preview: always responds with 204 No Content. 
27   * 
28   * This handler was used to integration test together with squid version 3.1.6
29   * 
30   * @author Michael Mimo Moratti (mimo@mimo.ch)
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  }