public class IcapServerHandler extends SimpleChannelUpstreamHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
IcapRequest request = (IcapRequest)e.getMessage();
System.out.println(request.toString());
IcapResponse response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.OK);
IcapMessageElementEnum bodyType = request.getBodyType();
if(bodyType == null) {
bodyType = IcapMessageElementEnum.NULLBODY;
}
if(!request.getMethod().equals(IcapMethod.RESPMOD) & request.getHttpRequest() != null) {
request.getHttpRequest().addHeader(HttpHeaders.Names.VIA,"icap://my.icap.server");
response.setHttpRequest(request.getHttpRequest());
}
if(request.getHttpResponse() != null) {
request.getHttpResponse().addHeader(HttpHeaders.Names.VIA,"icap://my.icap.server");
response.setHttpResponse(request.getHttpResponse());
}
response.addHeader(IcapHeaders.Names.ISTAG,"SimpleServer-version-1.0");
ChannelBuffer buffer = null;
switch (bodyType) {
case NULLBODY:
// No body in request
break;
case REQBODY:
// http request body in request
buffer = request.getHttpRequest().getContent();
break;
case RESBODY:
// http response body in request
buffer = request.getHttpResponse().getContent();
break;
default:
// cannot reach here.
break;
}