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.handler.codec.icap;
    15
    16  import java.util.List;
    17
    18  import org.jboss.netty.buffer.ChannelBuffer;
    19  import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
    20  import org.jboss.netty.handler.codec.http.HttpResponse;
    21  import org.jboss.netty.handler.codec.http.HttpResponseStatus;
    22  import org.jboss.netty.handler.codec.http.HttpVersion;
    23
    24  /**
    25   * Decoder State that reads http response initial and headers.
    26   *
    27   * @author Michael Mimo Moratti (mimo@mimo.ch)
    28   *
    29   * @see IcapMessageDecoder
    30   * @see StateEnum
    31   */
    32  public class ReadHttpResponseInitalAndHeadersState extends State<Object> {
    33
    34  	public ReadHttpResponseInitalAndHeadersState(String name) {
    35  		super(name);
    36  	}
    37
    38  	@Override
    39  	public void onEntry(ChannelBuffer buffer, IcapMessageDecoder icapMessageDecoder) throws DecodingException {
    40  		if(icapMessageDecoder.message == null) {
    41  			throw new IllegalArgumentException("This state requires a valid IcapMessage instance");
    42  		}
    43  		if(icapMessageDecoder.message.getEncapsulatedHeader() == null) {
    44  			throw new IllegalArgumentException("This state requires a valid Encapsulation header instance");
    45  		}
    46  	}
    47
    48  	@Override
    49  	public StateReturnValue execute(ChannelBuffer buffer, IcapMessageDecoder icapMessageDecoder) throws DecodingException {
    50  		String line = IcapDecoderUtil.readLine(buffer,icapMessageDecoder.maxInitialLineLength);
    51  		String[] initialLine = IcapDecoderUtil.splitInitialLine(line);
    52  		HttpResponse message = new DefaultHttpResponse(HttpVersion.valueOf(initialLine[0]),HttpResponseStatus.valueOf(Integer.parseInt(initialLine[1])));
    53  		icapMessageDecoder.message.setHttpResponse(message);
    54  		List<String[]> headerList = IcapDecoderUtil.readHeaders(buffer,icapMessageDecoder.maxHttpHeaderSize);
    55  		for(String[] header : headerList) {
    56  			message.addHeader(header[0],header[1]);
    57  		}
    58  		Encapsulated encapsulated = icapMessageDecoder.message.getEncapsulatedHeader();
    59  		encapsulated.setEntryAsProcessed(encapsulated.getNextEntry());
    60  		if(encapsulated.getNextEntry() != null && encapsulated.getNextEntry().equals(IcapMessageElementEnum.REQHDR)) {
    61  			return StateReturnValue.createIrrelevantResult();
    62  		}
    63  		return StateReturnValue.createRelevantResult(icapMessageDecoder.message);
    64  	}
    65
    66  	@Override
    67  	public StateEnum onExit(ChannelBuffer buffer, IcapMessageDecoder icapMessageDecoder, Object decisionInformation) throws DecodingException {
    68  		Encapsulated encapsulated = icapMessageDecoder.message.getEncapsulatedHeader();
    69  		IcapMessageElementEnum entry = encapsulated.getNextEntry();
    70  		if(entry != null) {
    71  			if(entry.equals(IcapMessageElementEnum.REQHDR)) {
    72  				return StateEnum.READ_HTTP_REQUEST_INITIAL_AND_HEADERS;
    73  			}
    74  			if(entry.equals(IcapMessageElementEnum.REQBODY)) {
    75  				return StateEnum.READ_CHUNK_SIZE_STATE;
    76  			}
    77  			if(entry.equals(IcapMessageElementEnum.RESBODY)) {
    78  				return StateEnum.READ_CHUNK_SIZE_STATE;
    79  			}
    80  		}
    81  		return null;
    82  	}
    83
    84  }