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.preview;
    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.nio.NioClientSocketChannelFactory;
    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.DefaultIcapChunk;
    31  import ch.mimo.netty.handler.codec.icap.DefaultIcapChunkTrailer;
    32  import ch.mimo.netty.handler.codec.icap.DefaultIcapRequest;
    33  import ch.mimo.netty.handler.codec.icap.IcapChunk;
    34  import ch.mimo.netty.handler.codec.icap.IcapChunkTrailer;
    35  import ch.mimo.netty.handler.codec.icap.IcapHeaders;
    36  import ch.mimo.netty.handler.codec.icap.IcapMessageElementEnum;
    37  import ch.mimo.netty.handler.codec.icap.IcapMethod;
    38  import ch.mimo.netty.handler.codec.icap.IcapRequest;
    39  import ch.mimo.netty.handler.codec.icap.IcapVersion;
    40
    41  /**
    42   * Preview capable ICAP client that send a REQMOD request with a HTTP POST request and body as preview
    43   * to a server and prints the answer, waits for a 10 continue and sends the rest.
    44   *
    45   * @author Michael Mimo Moratti (mimo@mimo.ch)
    46   *
    47   */
    48  public class IcapClient {
    49  	public static void main(String[] args) {
    50  		int port = 8099;
    51  		String host = "localhost";
    52
    53          // Configure the client.
    54          ClientBootstrap bootstrap = new ClientBootstrap(
    55                  new NioClientSocketChannelFactory(
    56                          Executors.newCachedThreadPool(),
    57                          Executors.newCachedThreadPool()));
    58
    59          // Set up the event pipeline factory.
    60          bootstrap.setPipelineFactory(new IcapClientChannelPipeline());
    61
    62          // Start the connection attempt.
    63          ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,port));
    64
    65          // Wait until the connection attempt succeeds or fails.
    66          Channel channel = future.awaitUninterruptibly().getChannel();
    67          if (!future.isSuccess()) {
    68              future.getCause().printStackTrace();
    69              bootstrap.releaseExternalResources();
    70              return;
    71          }
    72
    73          // Prepare the ICAP request.
    74          IcapRequest request = new DefaultIcapRequest(IcapVersion.ICAP_1_0,IcapMethod.REQMOD,"/simple","localhost");
    75          request.setBody(IcapMessageElementEnum.REQBODY);
    76          request.addHeader(IcapHeaders.Names.PREVIEW,"50");
    77          HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,"/some/servers/uri");
    78          httpRequest.setHeader(HttpHeaders.Names.HOST,host);
    79          httpRequest.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    80          httpRequest.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    81          request.setHttpRequest(httpRequest);
    82
    83          IcapChunk previewChunk = new DefaultIcapChunk(ChannelBuffers.copiedBuffer("It is common not to understand why something happe".getBytes()));
    84          previewChunk.setPreviewChunk(true);
    85          IcapChunkTrailer previewTrailer = new DefaultIcapChunkTrailer(true,false);
    86
    87          // Send the ICAP request.
    88          channel.write(request);
    89          channel.write(previewChunk);
    90          channel.write(previewTrailer);
    91
    92          // Wait for the server to close the connection.
    93          channel.getCloseFuture().awaitUninterruptibly();
    94
    95          // Shut down executor threads to exit.
    96          bootstrap.releaseExternalResources();
    97  }
    98  }