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 org.jboss.netty.channel.ChannelHandlerContext;
    17  import org.jboss.netty.channel.MessageEvent;
    18  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
    19
    20  import ch.mimo.netty.handler.codec.icap.DefaultIcapResponse;
    21  import ch.mimo.netty.handler.codec.icap.IcapChunk;
    22  import ch.mimo.netty.handler.codec.icap.IcapChunkTrailer;
    23  import ch.mimo.netty.handler.codec.icap.IcapRequest;
    24  import ch.mimo.netty.handler.codec.icap.IcapResponse;
    25  import ch.mimo.netty.handler.codec.icap.IcapResponseStatus;
    26  import ch.mimo.netty.handler.codec.icap.IcapVersion;
    27
    28  public class IcapServerHandler extends SimpleChannelUpstreamHandler {
    29
    30  	private boolean continueWasSent;
    31
    32  	@Override
    33  	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    34  		Object msg = e.getMessage();
    35  		if(msg instanceof IcapRequest) {
    36  			IcapRequest request = (IcapRequest)e.getMessage();
    37  			System.out.println(request.toString());
    38  		} else if(msg instanceof IcapChunkTrailer) {
    39  			System.out.println(msg.toString());
    40  			if(!continueWasSent) {
    41  				continueWasSent = true;
    42  				// sending 100 continue in order to receive the rest of the message
    43  				IcapResponse response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.CONTINUE);
    44  				ctx.getChannel().write(response);
    45  			} else {
    46  				// sending 204 No Content response
    47  				IcapResponse response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.NO_CONTENT);
    48  				ctx.getChannel().write(response);
    49  			}
    50  		} else if(msg instanceof IcapChunk) {
    51  			System.out.println(msg);
    52  		}
    53  	}
    54
    55  }