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 org.jboss.netty.buffer.ChannelBuffer;
17  
18  /**
19   * Decoder State that reads one huge chunk into many smaller chunks
20   * 
21   * @author Michael Mimo Moratti (mimo@mimo.ch)
22   *
23   * @see IcapMessageDecoder
24   * @see StateEnum
25   */
26  public class ReadChunkedContentAsChunksState extends State<Object> {
27  
28  	public ReadChunkedContentAsChunksState(String name) {
29  		super(name);
30  	}
31  	
32  	@Override
33  	public void onEntry(ChannelBuffer buffer, IcapMessageDecoder icapMessageDecoder) throws DecodingException {
34  	}
35  
36  	@Override
37  	public StateReturnValue execute(ChannelBuffer buffer, IcapMessageDecoder icapMessageDecoder) throws DecodingException {
38  		IcapChunk chunk = null;
39  		if(icapMessageDecoder.currentChunkSize > icapMessageDecoder.maxChunkSize) {
40  			chunk = new DefaultIcapChunk(buffer.readBytes(icapMessageDecoder.maxChunkSize));
41  			icapMessageDecoder.currentChunkSize -= icapMessageDecoder.maxChunkSize;
42  		} else {
43  			chunk = new DefaultIcapChunk(buffer.readBytes(icapMessageDecoder.currentChunkSize));
44  			icapMessageDecoder.currentChunkSize = 0;
45  		}
46  		chunk.setPreviewChunk(icapMessageDecoder.message.isPreviewMessage());
47  		if(chunk.isLast()) {
48  			icapMessageDecoder.currentChunkSize = 0;
49  			return StateReturnValue.createRelevantResult(new Object[]{chunk,new DefaultIcapChunkTrailer()}); 
50  		}
51  		return StateReturnValue.createRelevantResult(chunk);
52  	}
53  
54  	@Override
55  	public StateEnum onExit(ChannelBuffer buffer, IcapMessageDecoder icapMessageDecoder, Object decisionInformation) throws DecodingException {
56  		if(icapMessageDecoder.currentChunkSize == 0) {
57  			return StateEnum.READ_CHUNK_DELIMITER_STATE;
58  		}
59  		return StateEnum.READ_CHUNKED_CONTENT_AS_CHUNKS_STATE;
60  	}
61  
62  }