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  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.mail.MessagingException;
23  
24  /**
25   * Util class to represent a composite FETCH response from an IMAP server.  The
26   * response may have information about multiple message dataItems.
27   *
28   * @version $Rev: 594520 $ $Date: 2007-11-13 07:57:39 -0500 (Tue, 13 Nov 2007) $
29   */
30  public class IMAPFetchResponse extends IMAPUntaggedResponse {
31      // parsed sections within the FETCH response structure 
32      protected List dataItems = new ArrayList();
33      // the message number to which this applies 
34      public int sequenceNumber; 
35  
36      public IMAPFetchResponse(int sequenceNumber, byte[] data, IMAPResponseTokenizer source) throws MessagingException {
37          super("FETCH", data);
38          
39          this.sequenceNumber = sequenceNumber; 
40  
41          // fetch responses are a list, even if there is just a single member.
42          source.checkLeftParen();
43  
44          // loop until we find the list end.
45          while (source.notListEnd()) {
46              // the response names are coded as ATOMS.  The BODY one's use a special 
47              // syntax, so we need to use the expanded delimiter set to pull this out. 
48              String itemName = source.readAtom(true).toUpperCase();
49  
50              if (itemName.equals("ENVELOPE")) {
51                  dataItems.add(new IMAPEnvelope(source));
52              }
53              else if (itemName.equals("BODYSTRUCTURE")) {
54                  dataItems.add(new IMAPBodyStructure(source));
55              }
56              else if (itemName.equals("FLAGS")) {
57                  dataItems.add(new IMAPFlags(source));
58              }
59              else if (itemName.equals("INTERNALDATE")) {
60                  dataItems.add(new IMAPInternalDate(source));
61              }
62              else if (itemName.equals("UID")) {
63                  dataItems.add(new IMAPUid(sequenceNumber, source));
64              }
65              else if (itemName.equals("RFC822")) {
66                  // all of the RFC822 items are of form 
67                  // "RFC822.name".  We used the expanded parse above because 
68                  // the BODY names include some complicated bits.  If we got one 
69                  // of the RFC822 sections, then parse the rest of the name using 
70                  // the old rules, which will pull in the rest of the name from the period. 
71                  itemName = source.readAtom(false).toUpperCase();
72                  if (itemName.equals(".SIZE")) {
73                      dataItems.add(new IMAPMessageSize(source));
74                  }
75                  else if (itemName.equals(".HEADER")) {
76                      dataItems.add(new IMAPInternetHeader(source.readByteArray()));
77                  }
78                  else if (itemName.equals(".TEXT")) {
79                      dataItems.add(new IMAPMessageText(source.readByteArray()));
80                  }
81              }
82              // this is just the body alone. Specific body segments 
83              // have a more complex naming structure.  Believe it or  
84              // not, 
85              else if (itemName.equals("BODY")) {
86                  // time to go parse out the section information from the 
87                  // name.  
88                  IMAPBodySection section = new IMAPBodySection(source); 
89                  
90                  switch (section.section) {
91                      case IMAPBodySection.BODY:
92                          // a "full body cast".  Just grab the binary data 
93                          dataItems.add(new IMAPBody(section, source.readByteArray())); 
94                          break; 
95                          
96                      case IMAPBodySection.HEADERS:
97                      case IMAPBodySection.HEADERSUBSET:
98                      case IMAPBodySection.MIME:
99                          // these 3 are all variations of a header request
100                         dataItems.add(new IMAPInternetHeader(section, source.readByteArray())); 
101                         break; 
102                         
103                     case IMAPBodySection.TEXT:
104                         // just the text portion of the body 
105                         // a "full body cast".  Just grab the binary data 
106                         dataItems.add(new IMAPMessageText(section, source.readByteArray())); 
107                         break; 
108                 }
109             }
110         }
111         // swallow the terminating right paren
112         source.checkRightParen(); 
113     }
114     
115     /**
116      * Retrieve the sequence number for the FETCH item. 
117      * 
118      * @return The message sequence number this FETCH applies to. 
119      */
120     public int getSequenceNumber() {
121         return sequenceNumber; 
122     }
123 
124     /**
125      * Get the section count.
126      *
127      * @return The number of sections in the response.
128      */
129     public int getCount() {
130         return dataItems.size();
131     }
132 
133     /**
134      * Get the complete set of response dataItems.
135      *
136      * @return The List of IMAPFetchResponse values.
137      */
138     public List getDataItems() {
139         return dataItems;
140     }
141 
142 
143     /**
144      * Fetch a particular response type from the response dataItems.
145      *
146      * @param type   The target FETCH type.
147      *
148      * @return The first IMAPFetchDataItem item that matches the response type.
149      */
150     public IMAPFetchDataItem getDataItem(int type) {
151         for (int i = 0; i < dataItems.size(); i ++) {
152             IMAPFetchDataItem item = (IMAPFetchDataItem)dataItems.get(i);
153             if (item.isType(type)) {
154                 return item;
155             }
156         }
157         return null;
158     }
159     
160 }
161