1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.mimo.netty.example.icap.simple;
15
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.Executors;
18
19 import org.jboss.netty.bootstrap.ClientBootstrap;
20 import org.jboss.netty.buffer.ChannelBuffers;
21 import org.jboss.netty.channel.Channel;
22 import org.jboss.netty.channel.ChannelFuture;
23 import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory;
24 import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
25 import org.jboss.netty.handler.codec.http.HttpHeaders;
26 import org.jboss.netty.handler.codec.http.HttpMethod;
27 import org.jboss.netty.handler.codec.http.HttpRequest;
28 import org.jboss.netty.handler.codec.http.HttpVersion;
29
30 import ch.mimo.netty.handler.codec.icap.DefaultIcapRequest;
31 import ch.mimo.netty.handler.codec.icap.IcapMethod;
32 import ch.mimo.netty.handler.codec.icap.IcapRequest;
33 import ch.mimo.netty.handler.codec.icap.IcapVersion;
34
35
36
37
38
39
40
41
42 public class IcapClient {
43
44 public static void main(String[] args) {
45 int port = 8099;
46 String host = "localhost";
47
48
49 ClientBootstrap bootstrap = new ClientBootstrap(
50 new OioClientSocketChannelFactory(
51 Executors.newCachedThreadPool()));
52
53
54 bootstrap.setPipelineFactory(new IcapClientChannelPipeline());
55
56
57 ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,port));
58
59
60 Channel channel = future.awaitUninterruptibly().getChannel();
61 if (!future.isSuccess()) {
62 future.getCause().printStackTrace();
63 bootstrap.releaseExternalResources();
64 return;
65 }
66
67
68 IcapRequest request = new DefaultIcapRequest(IcapVersion.ICAP_1_0,IcapMethod.REQMOD,"/simple","localhost");
69 HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,"/some/servers/uri");
70 httpRequest.setHeader(HttpHeaders.Names.HOST,host);
71 httpRequest.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
72 httpRequest.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
73 httpRequest.setContent(ChannelBuffers.wrappedBuffer("This is the message body that contains all the necessary data to answer the ultimate question...".getBytes()));
74 request.setHttpRequest(httpRequest);
75
76
77 channel.write(request);
78
79
80 channel.getCloseFuture().awaitUninterruptibly();
81
82
83 bootstrap.releaseExternalResources();
84 }
85 }