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.List;
021
022 import javax.mail.MessagingException;
023
024 /**
025 * Utility class to aggregate status responses for a mailbox.
026 */
027 public class IMAPStatusResponse extends IMAPUntaggedResponse {
028 // the mail box name
029 public String mailbox;
030 // number of messages in the box
031 public int messages = -1;
032 // number of recent messages
033 public int recentMessages = -1;
034 // the number of unseen messages
035 public int unseenMessages = -1;
036 // the next UID for this mailbox
037 public long uidNext = -1L;
038 // the UID validity item
039 public long uidValidity = -1L;
040
041 public IMAPStatusResponse(byte[] data, IMAPResponseTokenizer source) throws MessagingException {
042 super("STATUS", data);
043
044 // the mail box name is supposed to be encoded, so decode it now.
045 mailbox = source.readEncodedString();
046
047 // parse the list of flag values
048 List flags = source.readStringList();
049
050 for (int i = 0; i < flags.size(); i += 2) {
051 String field = ((String)flags.get(i)).toUpperCase();
052 String stringValue = ((String)flags.get(i + 1));
053 long value;
054 try {
055 value = Long.parseLong(stringValue);
056 } catch (NumberFormatException e) {
057 throw new MessagingException("Invalid IMAP Status response", e);
058 }
059
060
061 if (field.equals("MESSAGES")) {
062 messages = (int)value;
063 }
064 else if (field.equals("RECENT")) {
065 recentMessages = (int)value;
066 }
067 else if (field.equals("UIDNEXT")) {
068 uidNext = value;
069 }
070 else if (field.equals("UIDVALIDITY")) {
071 uidValidity = value;
072 }
073 else if (field.equals("UNSEEN")) {
074 unseenMessages = (int)value;
075 }
076 }
077 }
078 }
079