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.message.builder.ext;
017    
018    import java.util.LinkedList;
019    
020    import javax.mail.Quota;
021    
022    import com.hs.mail.imap.message.builder.ImapRequestBuilder;
023    import com.hs.mail.imap.message.request.ImapRequest;
024    import com.hs.mail.imap.message.request.ext.SetQuotaRequest;
025    import com.hs.mail.imap.parser.Token;
026    import com.hs.mail.imap.server.codec.ImapMessage;
027    
028    /**
029     * 
030     * @author Won Chul Doh
031     * @since Apr 19, 2010
032     *
033     */
034    public class SetQuotaRequestBuilder extends ImapRequestBuilder {
035    
036            @Override
037            public ImapRequest createRequest(String tag, String command,
038                            ImapMessage message) {
039                    LinkedList<Token> tokens = message.getTokens();
040                    Quota quota = new Quota(tokens.remove().value);
041                    Token token = tokens.peek(); // consume '('
042                    if (token.type == Token.Type.LPAREN) {
043                            tokens.remove(); // remove left parenthesis
044                            do {
045                                    if ((token = tokens.remove()).type == Token.Type.RPAREN)
046                                            break;
047                                    String name = token.value;
048                                    long limit = Long.parseLong(tokens.remove().value);
049                                    quota.setResourceLimit(name, limit);
050                            } while (true);
051                    }
052                    return new SetQuotaRequest(tag, command, quota);
053            }
054    
055    }