KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > protocol > FetchResponse


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)FetchResponse.java 1.10 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.imap.protocol;
29
30 import java.io.*;
31 import java.util.*;
32 import com.sun.mail.util.*;
33 import com.sun.mail.iap.*;
34
35 /**
36  * This class represents a response obtained from the input stream
37  * of an IMAP server.
38  *
39  * @version 1.10, 05/08/29
40  * @author John Mani
41  */

42
43 public class FetchResponse extends IMAPResponse {
44     private Item[] items;
45
46     public FetchResponse(Protocol p)
47         throws IOException, ProtocolException {
48     super(p);
49     parse();
50     }
51
52     public FetchResponse(IMAPResponse r)
53         throws IOException, ProtocolException {
54     super(r);
55     parse();
56     }
57
58     public int getItemCount() {
59     return items.length;
60     }
61
62     public Item getItem(int index) {
63     return items[index];
64     }
65
66     public Item getItem(Class JavaDoc c) {
67     for (int i = 0; i < items.length; i++) {
68         if (c.isInstance(items[i]))
69         return items[i];
70     }
71
72     return null;
73     }
74
75     public static Item getItem(Response[] r, int msgno, Class JavaDoc c) {
76     if (r == null)
77         return null;
78
79     for (int i = 0; i < r.length; i++) {
80
81         if (r[i] == null ||
82         !(r[i] instanceof FetchResponse) ||
83         ((FetchResponse)r[i]).getNumber() != msgno)
84         continue;
85
86         FetchResponse f = (FetchResponse)r[i];
87         for (int j = 0; j < f.items.length; j++) {
88         if (c.isInstance(f.items[j]))
89             return f.items[j];
90         }
91     }
92
93     return null;
94     }
95
96     private final static char[] HEADER = {'.','H','E','A','D','E','R'};
97     private final static char[] TEXT = {'.','T','E','X','T'};
98
99     
100     private void parse() throws ParsingException {
101     skipSpaces();
102     if (buffer[index] != '(')
103         throw new ParsingException(
104         "error in FETCH parsing, missing '(' at index " + index);
105
106     Vector v = new Vector();
107     Item i = null;
108     do {
109         index++; // skip '(', or SPACE
110

111         if (index >= size)
112         throw new ParsingException(
113         "error in FETCH parsing, ran off end of buffer, size " + size);
114
115         switch(buffer[index]) {
116         case 'E':
117         if (match(ENVELOPE.name)) {
118             index += ENVELOPE.name.length; // skip "ENVELOPE"
119
i = new ENVELOPE(this);
120         }
121         break;
122         case 'F':
123         if (match(FLAGS.name)) {
124             index += FLAGS.name.length; // skip "FLAGS"
125
i = new FLAGS((IMAPResponse)this);
126         }
127         break;
128         case 'I':
129         if (match(INTERNALDATE.name)) {
130             index += INTERNALDATE.name.length; // skip "INTERNALDATE"
131
i = new INTERNALDATE(this);
132         }
133         break;
134         case 'B':
135         if (match(BODY.name)) {
136             if (buffer[index+4] == '[') {
137             index += BODY.name.length; // skip "BODY"
138
i = new BODY(this);
139             }
140             else {
141             if (match(BODYSTRUCTURE.name))
142                 index += BODYSTRUCTURE.name.length;
143                 // skip "BODYSTRUCTURE"
144
else
145                 index += BODY.name.length; // skip "BODY"
146
i = new BODYSTRUCTURE(this);
147             }
148         }
149         break;
150         case 'R':
151         if (match(RFC822SIZE.name)) {
152             index += RFC822SIZE.name.length; // skip "RFC822.SIZE"
153
i = new RFC822SIZE(this);
154         }
155         else {
156             if (match(RFC822DATA.name)) {
157             index += RFC822DATA.name.length;
158             if (match(HEADER))
159                 index += HEADER.length; // skip ".HEADER"
160
else if (match(TEXT))
161                 index += TEXT.length; // skip ".TEXT"
162
i = new RFC822DATA(this);
163             }
164         }
165         break;
166         case 'U':
167         if (match(UID.name)) {
168             index += UID.name.length;
169             i = new UID(this);
170         }
171         break;
172         default:
173         }
174         if (i != null)
175         v.addElement(i);
176     } while (buffer[index] != ')');
177
178     index++; // skip ')'
179
items = new Item[v.size()];
180     v.copyInto(items);
181     }
182
183     /*
184      * itemName is the name of the IMAP item to compare against.
185      * NOTE that itemName *must* be all uppercase.
186      */

187     private boolean match(char[] itemName) {
188     int len = itemName.length;
189     for (int i = 0, j = index; i < len;)
190         // IMAP tokens are case-insensitive. We store itemNames in
191
// uppercase, so convert operand to uppercase before comparing.
192
if (Character.toUpperCase((char)buffer[j++]) != itemName[i++])
193         return false;
194     return true;
195     }
196 }
197
Popular Tags