001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.geronimo.javamail.store.imap.connection;
019    
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.List;
023    
024    import javax.mail.MessagingException;
025    
026    import org.apache.geronimo.javamail.store.imap.connection.IMAPResponseTokenizer.Token; 
027    import org.apache.geronimo.javamail.util.ResponseFormatException; 
028    
029    /**
030     * Util class to represent a NAMESPACE response from a IMAP server
031     *
032     * @version $Rev: 594520 $ $Date: 2007-11-13 07:57:39 -0500 (Tue, 13 Nov 2007) $
033     */
034    
035    public class IMAPNamespaceResponse extends IMAPUntaggedResponse {
036        // the personal namespaces defined 
037        public List personalNamespaces; 
038        // the other use name spaces this user has access to. 
039        public List otherUserNamespaces; 
040        // the list of shared namespaces 
041        public List sharedNamespaces; 
042        
043        // construct a default IMAPNamespace response for return when the server doesn't support this. 
044        public IMAPNamespaceResponse() 
045        {
046            super("NAMESPACE", null); 
047            // fill in default lists to simplify processing 
048            personalNamespaces = Collections.EMPTY_LIST; 
049            otherUserNamespaces = Collections.EMPTY_LIST; 
050            sharedNamespaces = Collections.EMPTY_LIST; 
051        }
052    
053        /**
054         * Construct a LIST response item.  This can be either 
055         * a response from a LIST command or an LSUB command, 
056         * and will be tagged accordingly.
057         * 
058         * @param type   The type of resonse (LIST or LSUB).
059         * @param data   The raw response data.
060         * @param source The tokenizer source.
061         * 
062         * @exception MessagingException
063         */
064        public IMAPNamespaceResponse(byte[] data, IMAPResponseTokenizer source) throws MessagingException {
065            super("NAMESPACE", data); 
066            // the namespace response is a set of 3 items, which will be either NIL or a "list of lists".  
067            // if the item exists, then there will be a set of list parens, with 1 or more subitems inside. 
068            // Each of the subitems will consist of a namespace prefix and the hierarchy delimiter for that 
069            // particular namespace. 
070            personalNamespaces = parseNamespace(source); 
071            otherUserNamespaces = parseNamespace(source); 
072            sharedNamespaces = parseNamespace(source); 
073        }
074        
075        private List parseNamespace(IMAPResponseTokenizer source) throws MessagingException {
076            Token token = source.next(true); 
077            // is this token the NIL token?
078            if (token.getType() == Token.NIL) {
079                // no items at this position. 
080                return null; 
081            }
082            if (token.getType() != '(') {
083                throw new ResponseFormatException("Missing '(' in response");
084            }
085            
086            // ok, we're processing a namespace list.  Create a list and populate it with IMAPNamespace 
087            // items. 
088            
089            List namespaces = new ArrayList(); 
090            
091            while (source.notListEnd()) {
092                namespaces.add(new IMAPNamespace(source)); 
093            }
094            // this should always pass, since it terminated the loop 
095            source.checkRightParen(); 
096            return namespaces; 
097        }
098    }