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.nio.charset.Charset;
    17
    18  import org.jboss.netty.buffer.ChannelBuffer;
    19  import org.jboss.netty.channel.ChannelHandlerContext;
    20  import org.jboss.netty.channel.MessageEvent;
    21  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
    22  import org.jboss.netty.handler.codec.http.HttpHeaders;
    23
    24  import ch.mimo.netty.handler.codec.icap.DefaultIcapResponse;
    25  import ch.mimo.netty.handler.codec.icap.IcapHeaders;
    26  import ch.mimo.netty.handler.codec.icap.IcapMessageElementEnum;
    27  import ch.mimo.netty.handler.codec.icap.IcapRequest;
    28  import ch.mimo.netty.handler.codec.icap.IcapResponse;
    29  import ch.mimo.netty.handler.codec.icap.IcapResponseStatus;
    30  import ch.mimo.netty.handler.codec.icap.IcapVersion;
    31
    32  public class IcapServerHandler extends SimpleChannelUpstreamHandler {
    33
    34  	@Override
    35  	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    36  		IcapRequest request = (IcapRequest)e.getMessage();
    37
    38  		System.out.println(request.toString());
    39
    40  		IcapResponse response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.OK);
    41  		IcapMessageElementEnum bodyType = request.getBody();
    42
    43  		if(request.getHttpRequest() != null) {
    44  			request.getHttpRequest().addHeader(HttpHeaders.Names.VIA,"icap://my.icap.server");
    45  			response.setHttpRequest(request.getHttpRequest());
    46  		}
    47  		if(request.getHttpResponse() != null) {
    48  			request.getHttpResponse().addHeader(HttpHeaders.Names.VIA,"icap://my.icap.server");
    49  			response.setHttpResponse(request.getHttpResponse());
    50  		}
    51  		response.addHeader(IcapHeaders.Names.ISTAG,"SimpleServer-version-1.0");
    52
    53  		ChannelBuffer buffer = null;
    54  		switch (bodyType) {
    55  		case NULLBODY:
    56  			// No body in request
    57  			break;
    58  		case REQBODY:
    59  			// http request body in request
    60  			buffer = request.getHttpRequest().getContent();
    61  			break;
    62  		case RESBODY:
    63  			// http response body in request
    64  			buffer = request.getHttpResponse().getContent();
    65  			break;
    66  		default:
    67  			// cannot reach here.
    68  			break;
    69  		}
    70
    71  		/*
    72  		 * There is also a convenience method that extracts a body from any http message.
    73  		 * @See IcapChunkAggregator#extractHttpBodyContentFromIcapMessage(IcapMessage message).
    74  		 */
    75
    76  		if(buffer != null) {
    77  			System.out.println(buffer.toString(Charset.defaultCharset()));
    78  		}
    79
    80  		ctx.getChannel().write(response);
    81  	}
    82
    83  }