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