View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.geronimo.javamail.store.imap.connection;
19  
20  import java.util.List;
21  
22  import javax.mail.Flags;
23  import javax.mail.Folder;
24  import javax.mail.MessagingException;
25  
26  import org.apache.geronimo.javamail.store.imap.connection.IMAPResponseTokenizer.Token;
27  
28  
29  /**
30   * Utility class to aggregate status responses for a mailbox.
31   */
32  public class IMAPMailboxStatus {
33      // the set of available flag values for this mailbox
34      public Flags availableFlags = null;
35      // the permanent flags for this mailbox.
36      public Flags permanentFlags = null;
37      // the open mode flags
38      public int mode = Folder.READ_WRITE;
39  
40      // number of messages in the box
41      public int messages = -1;
42      // the number of newly added messages
43      public int recentMessages = -1;
44      // the number of unseen messages
45      public int unseenMessages = -1;
46  
47      // the next UID for this mailbox
48      public long uidNext = -1L;
49      // the UID validity item
50      public long uidValidity = -1L;
51  
52      public IMAPMailboxStatus() {
53      }
54  
55  
56      /**
57       * Merge information from a server status message.  These
58       * messages are in the form "* NAME args".  We only handle
59       * STATUS and FLAGS messages here.
60       *
61       * @param source The parsed status message.
62       *
63       * @exception MessagingException
64       */
65      public void mergeStatus(IMAPStatusResponse source) throws MessagingException {
66          // update any of the values that have changed since the last. 
67          if (source.messages != -1) {
68              messages = source.messages; 
69          }
70          if (source.uidNext != -1L) {
71              uidNext = source.uidNext; 
72          }
73          if (source.uidValidity != -1L) {
74              uidValidity = source.uidValidity; 
75          }
76          if (source.recentMessages != -1) {
77              recentMessages = source.recentMessages; 
78          }
79          if (source.unseenMessages != -1) {
80              unseenMessages = source.unseenMessages; 
81          }
82      }
83      
84      /**
85       * Merge in the FLAGS response from an EXAMINE or 
86       * SELECT mailbox command.
87       * 
88       * @param response The returned FLAGS item.
89       * 
90       * @exception MessagingException
91       */
92      public void mergeFlags(IMAPFlagsResponse response) throws MessagingException {
93          if (response != null) {
94              availableFlags = response.getFlags(); 
95          }
96      }
97      
98      
99      public void mergeSizeResponses(List responses) throws MessagingException  
100       {  
101         for (int i = 0; i < responses.size(); i++) {
102             mergeStatus((IMAPSizeResponse)responses.get(i)); 
103         }
104     }
105     
106     
107     public void mergeOkResponses(List responses) throws MessagingException {
108         for (int i = 0; i < responses.size(); i++) {
109             mergeStatus((IMAPOkResponse)responses.get(i)); 
110         }
111     }
112 
113     
114     /**
115      * Gather mailbox status information from mailbox status
116      * messages.  These messages come in as untagged messages in the
117      * form "* nnn NAME".
118      *
119      * @param source The parse message information.
120      *
121      * @exception MessagingException
122      */
123     public void mergeStatus(IMAPSizeResponse source) throws MessagingException {
124         if (source != null) {
125             String name = source.getKeyword(); 
126 
127             // untagged exists response
128             if (source.isKeyword("EXISTS")) {
129                 messages = source.getSize();
130             }
131             // untagged resent response
132             else if (source.isKeyword("RECENT")) {
133                 recentMessages = source.getSize();
134             }
135         }
136     }
137 
138     
139 
140     
141     /**
142      * Gather mailbox status information from mailbox status
143      * messages.  These messages come in as untagged messages in the
144      * form "* OK [NAME args]".
145      *
146      * @param source The parse message information.
147      *
148      * @exception MessagingException
149      */
150     public void mergeStatus(IMAPOkResponse source) throws MessagingException {
151         if (source != null) {
152             String name = source.getKeyword(); 
153 
154             // untagged UIDVALIDITY response 
155             if (source.isKeyword("UIDVALIDITY")) {
156                 List arguments = source.getStatus(); 
157                 uidValidity = ((Token)arguments.get(0)).getLong(); 
158             }
159             // untagged UIDNEXT response 
160             if (source.isKeyword("UIDNEXT")) {
161                 List arguments = source.getStatus(); 
162                 uidNext = ((Token)arguments.get(0)).getLong(); 
163             }
164             // untagged unseen response
165             else if (source.isKeyword("UNSEEN")) {
166                 List arguments = source.getStatus(); 
167                 uidValidity = ((Token)arguments.get(0)).getInteger(); 
168             }
169         }
170     }
171 
172     
173     /**
174      * Gather mailbox status information from mailbox status
175      * messages.  These messages come in as untagged messages in the
176      * form "* OK [NAME args]".
177      *
178      * @param source The parse message information.
179      *
180      * @exception MessagingException
181      */
182     public void mergeStatus(IMAPPermanentFlagsResponse source) throws MessagingException {
183         if (source != null) {
184             // this is already parsed.          
185             permanentFlags = source.flags; 
186         }
187     }
188 }