001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap.server.codec;
017    
018    import java.io.StringReader;
019    import java.util.LinkedList;
020    
021    import org.apache.commons.collections.CollectionUtils;
022    import org.jboss.netty.buffer.ChannelBuffer;
023    import org.jboss.netty.buffer.ChannelBuffers;
024    
025    import com.hs.mail.imap.parser.CommandParser;
026    import com.hs.mail.imap.parser.Token;
027    
028    /**
029     * 
030     * @author Won Chul Doh
031     * @since Jan 22, 2010
032     *
033     */
034    public class DefaultImapMessage implements ImapMessage {
035    
036        private ChannelBuffer literal = ChannelBuffers.EMPTY_BUFFER;
037            private long literalLength = -1;
038            private boolean needContinuationRequest = false;
039            protected LinkedList<Token> tokens = null;
040            
041        public DefaultImapMessage(String request) {
042                    CommandParser parser = new CommandParser(new StringReader(request));
043                    tokens = parser.command();
044                    if (CollectionUtils.isNotEmpty(tokens)) {
045                            Token last = tokens.get(tokens.size() - 1);
046                            if (last.isLiteral()) {
047                                    int length = Integer.parseInt(last.value);
048                                    setLiteralLength(length);
049                                    if (Token.Type.LITERAL == last.type) {
050                                            needContinuationRequest = true;
051                                    }
052                            }
053                    }
054            }
055        
056            public String getCommand() {
057                    return (tokens.size() > 0) ? tokens.get(1).value : null;
058            }
059        
060        public LinkedList<Token> getTokens() {
061                    return tokens;
062            }
063    
064            public ChannelBuffer getLiteral() {
065                    return literal;
066            }
067    
068            public void setLiteral(ChannelBuffer literal) {
069                    if (null == literal) {
070                            literal = ChannelBuffers.EMPTY_BUFFER;
071                    }
072                    this.literal = literal;
073            }
074    
075            public long getLiteralLength() {
076                    return literalLength;
077            }
078    
079            public void setLiteralLength(long literalLength) {
080                    this.literalLength = literalLength;
081            }
082    
083            public boolean isNeedContinuationRequest() {
084                    return needContinuationRequest;
085            }
086    
087    }