KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > client > FetchCommand


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.imapserver.client;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.mail.Flags JavaDoc;
29 import javax.mail.MessagingException JavaDoc;
30 import javax.mail.Flags.Flag;
31 import javax.mail.internet.MimeMessage JavaDoc;
32
33 import org.apache.james.imapserver.client.fetch.FetchBody;
34
35 public class FetchCommand extends AbstractCommand {
36
37     final long from;
38
39     final long to;
40
41     boolean uid;
42
43     private MimeMessage JavaDoc[] msgs;
44
45     private long[] uids;
46
47     private boolean fetchFlags;
48
49     private boolean fetchRfc822Size;
50     
51     private FetchBody body;
52     
53     private boolean useParenthesis = true;
54     
55     private boolean oneSeqNumberOnly = false;
56
57     public FetchCommand(MimeMessage JavaDoc[] msgs, long from, long to) {
58         statusResponse = "OK FETCH completed.";
59         this.msgs = msgs;
60         this.from = from;
61         this.to = to;
62     }
63
64     public FetchCommand(MimeMessage JavaDoc[] msgs, long no) {
65         statusResponse = "OK FETCH completed.";
66         this.msgs = msgs;
67         this.from = no;
68         this.to = no;
69         this.oneSeqNumberOnly = true;
70     }
71
72     public void setUseParenthesis(boolean useParenthesis) {
73         this.useParenthesis = useParenthesis;
74     }
75     
76     public void setUids(long[] uids) {
77         this.uids=uids;
78         this.uid=true;
79     }
80
81     public String JavaDoc getCommand() {
82         String JavaDoc command = "";
83         if (uid) {
84             command += "UID ";
85         }
86         command += "fetch " + from;
87         if (!oneSeqNumberOnly) {
88             if (to > 0) {
89                 command += ":" + to;
90             } else {
91                 command += ":*";
92             }
93         }
94
95         command += " ";
96         if (useParenthesis) {
97             command += "(";
98         }
99         
100         String JavaDoc items = "";
101         // FLAGS
102
if (fetchFlags) {
103             items += " FLAGS";
104         }
105         // RFC822.SIZE
106
if (fetchRfc822Size) {
107             items += " RFC822.SIZE";
108         }
109         // BODY
110
if (body != null) {
111             items += " " + body.getCommand();
112         }
113
114         if (items.length() > 0) {
115             items = items.substring(1);
116         }
117         command += items;
118         if (useParenthesis) {
119             command += ")";
120         }
121         command += "\n";
122         return command;
123     }
124
125     private List JavaDoc getSelectedMessageNumbers() {
126         List JavaDoc selectedNumbers = new ArrayList JavaDoc();
127         if (uid) {
128             final long to;
129             if (this.to>0) {
130                 to=this.to;
131             } else {
132                 to=Long.MAX_VALUE;
133             }
134             for (int i=0; i< msgs.length; i++) {
135                 if (uids[i]>=from && uids[i]<=to) {
136                     selectedNumbers.add(new Integer JavaDoc((int)i+1));
137                 }
138             }
139
140         } else {
141             final long from;
142             if (this.from > 0) {
143                 from = this.from;
144             } else {
145                 from = 1;
146             }
147
148             final long to;
149             if (this.to > 0) {
150                 if (this.to > msgs.length) {
151                     to = msgs.length;
152                 } else {
153                     to = this.to;
154                 }
155             } else {
156                 to = msgs.length;
157             }
158
159             for (long i = from; i <= to; i++) {
160                 selectedNumbers.add(new Integer JavaDoc((int)i));
161             }
162         }
163         
164         return selectedNumbers;
165     }
166     public String JavaDoc getResultForMessageNumber(int no) throws MessagingException JavaDoc, IOException JavaDoc {
167         final MimeMessage JavaDoc mm = msgs[no-1];
168         String JavaDoc result = "";
169         
170         // FLAGS
171
if (fetchFlags) {
172             result +=" FLAGS ("+flagsToString(mm.getFlags())+")";
173         }
174         
175         // UID
176
if (uid) {
177             final long uid=uids[no-1];
178             result += " UID "+uid;
179         }
180         
181         // RFC822.SIZE
182
if (fetchRfc822Size) {
183             final int size=mm.getSize();
184             result += " RFC822.SIZE "+size;
185         }
186
187         // BODY
188
if (body!=null) {
189             result += " "+body.getResult(mm);
190         }
191         
192         if (result.length()>0) {
193              // without leading space
194
result=result.substring(1);
195         }
196         return result;
197     }
198     
199     public List JavaDoc getExpectedResponseList() throws MessagingException JavaDoc, IOException JavaDoc {
200         List JavaDoc responseList = new LinkedList JavaDoc();
201
202         List JavaDoc selectedNumbers = getSelectedMessageNumbers();
203         
204         for (Iterator JavaDoc it = selectedNumbers.iterator(); it.hasNext();) {
205             final int no=((Integer JavaDoc)it.next()).intValue();
206             String JavaDoc line = "* " + no + " FETCH (";
207             line += getResultForMessageNumber(no);
208             line += ")";
209             responseList.add(line);
210         }
211         return responseList;
212
213     }
214
215     public static String JavaDoc flagToString(Flag flag) {
216         if (flag.equals(Flag.ANSWERED)) {
217             return "\\Answered";
218         }
219         if (flag.equals(Flag.DELETED)) {
220             return "\\Deleted";
221         }
222         if (flag.equals(Flag.DRAFT)) {
223             return "\\Draft";
224         }
225         if (flag.equals(Flag.FLAGGED)) {
226             return "\\Flagged";
227         }
228         if (flag.equals(Flag.RECENT)) {
229             return "\\Recent";
230         }
231         if (flag.equals(Flag.SEEN)) {
232             return "\\Seen";
233         }
234         throw new IllegalArgumentException JavaDoc("unknown Flag: "+flag);
235
236     }
237     
238     public static String JavaDoc flagsToString(Flags JavaDoc flags) {
239         String JavaDoc result="";
240         Flag[] f=flags.getSystemFlags();
241         for (int i = 0; i < f.length; i++) {
242             result +=" "+flagToString(f[i]);
243         }
244         if (result.length()>0) {
245              // without leading space
246
result=result.substring(1);
247         }
248         return result;
249     }
250
251     public void setFetchFlags(boolean fetchFlags) {
252         this.fetchFlags=fetchFlags;
253         
254     }
255
256     public void setFetchRfc822Size(boolean fetchRfc822Size) {
257         this.fetchRfc822Size=fetchRfc822Size;
258         
259     }
260     
261
262     public void setFetchBody(FetchBody body) {
263         this.body=body;
264         
265     }
266
267 }
268
Popular Tags