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.DefaultHttpRequest;
    20  import org.jboss.netty.handler.codec.http.HttpMethod;
    21  import org.jboss.netty.handler.codec.http.HttpRequest;
    22  import org.jboss.netty.handler.codec.http.HttpVersion;
    23
    24  /**
    25   * Decoder State that reads http request initial and headers.
    26   *
    27   * @author Michael Mimo Moratti (mimo@mimo.ch)
    28   *
    29   * @see IcapMessageDecoder
    30   * @see StateEnum
    31   */
    32  public class ReadHttpRequestInitialAndHeadersState extends State<Object> {
    33
    34  	public ReadHttpRequestInitialAndHeadersState(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  		HttpRequest message = new DefaultHttpRequest(HttpVersion.valueOf(initialLine[2]),HttpMethod.valueOf(initialLine[0]),initialLine[1]);
    53  		icapMessageDecoder.message.setHttpRequest(message);
    54  		List<String[]> headerList = IcapDecoderUtil.readHeaders(buffer,icapMessageDecoder.maxHttpHeaderSize);
    55  		message.clearHeaders();
    56  		for(String[] header : headerList) {
    57  			message.addHeader(header[0],header[1]);
    58  		}
    59  		Encapsulated encapsulated = icapMessageDecoder.message.getEncapsulatedHeader();
    60  		encapsulated.setEntryAsProcessed(encapsulated.getNextEntry());
    61  		if(encapsulated.getNextEntry() != null && encapsulated.getNextEntry().equals(IcapMessageElementEnum.RESHDR)) {
    62  			return StateReturnValue.createIrrelevantResult();
    63  		}
    64  		return StateReturnValue.createRelevantResult(icapMessageDecoder.message);
    65  	}
    66
    67  	@Override
    68  	public StateEnum onExit(ChannelBuffer buffer, IcapMessageDecoder icapMessageDecoder, Object decisionInformation) throws DecodingException {
    69  		Encapsulated encapsulated = icapMessageDecoder.message.getEncapsulatedHeader();
    70  		IcapMessageElementEnum entry = encapsulated.getNextEntry();
    71  		if(entry != null) {
    72  			if(entry.equals(IcapMessageElementEnum.RESHDR)) {
    73  				return StateEnum.READ_HTTP_RESPONSE_INITIAL_AND_HEADERS;
    74  			}
    75  			if(entry.equals(IcapMessageElementEnum.REQBODY)) {
    76  				return StateEnum.READ_CHUNK_SIZE_STATE;
    77  			}
    78  			if(entry.equals(IcapMessageElementEnum.RESBODY)) {
    79  				return StateEnum.READ_CHUNK_SIZE_STATE;
    80  			}
    81  		}
    82  		return null;
    83  	}
    84
    85  }