View Javadoc

    1   /*******************************************************************************
    2    * Copyright (c) 2011 Michael Mimo Moratti.
    3    *
    4    * Michael Mimo Moratti licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at:
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    * Unless required by applicable law or agreed to in writing, software
    9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    11   * License for the specific language governing permissions and limitations
    12   * under the License.
    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   * Simple ICAP client that send a REQMOD request with a HTTP POST request and body
    37   * to a server and prints the answer.
    38   *
    39   * @author Michael Mimo Moratti (mimo@mimo.ch)
    40   *
    41   */
    42  public class IcapClient {
    43
    44  	public static void main(String[] args) {
    45  			int port = 8099;
    46  			String host = "localhost";
    47
    48  	        // Configure the client.
    49  	        ClientBootstrap bootstrap = new ClientBootstrap(
    50  	                new OioClientSocketChannelFactory(
    51  	                        Executors.newCachedThreadPool()));
    52
    53  	        // Set up the event pipeline factory.
    54  	        bootstrap.setPipelineFactory(new IcapClientChannelPipeline());
    55
    56  	        // Start the connection attempt.
    57  	        ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,port));
    58
    59  	        // Wait until the connection attempt succeeds or fails.
    60  	        Channel channel = future.awaitUninterruptibly().getChannel();
    61  	        if (!future.isSuccess()) {
    62  	            future.getCause().printStackTrace();
    63  	            bootstrap.releaseExternalResources();
    64  	            return;
    65  	        }
    66
    67  	        // Prepare the ICAP request.
    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  	        // Send the ICAP request.
    77  	        channel.write(request);
    78
    79  	        // Wait for the server to close the connection.
    80  	        channel.getCloseFuture().awaitUninterruptibly();
    81
    82  	        // Shut down executor threads to exit.
    83  	        bootstrap.releaseExternalResources();
    84  	}
    85  }