KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > imap > IMAPResponse


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.imap;
37
38 import java.util.LinkedList JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.regex.Matcher JavaDoc;
41 import java.util.regex.Pattern JavaDoc;
42
43 import org.columba.ristretto.io.CharSequenceSource;
44 import org.columba.ristretto.io.Source;
45
46 /**
47  * This class represents a response from an IMAP server.
48  *
49  * @author Frederik Dietz <fdietz>, Timo Stich <tstich@users.sourceforge.net>
50  */

51 public class IMAPResponse {
52     
53     private static final Pattern JavaDoc literalPattern = Pattern.compile("^\\{(\\d+)\\}$");
54     private static final Matcher JavaDoc literalMatcher = literalPattern.matcher("");
55
56     protected List JavaDoc literals;
57
58     protected int preNumber;
59
60     protected String JavaDoc tag;
61
62     protected String JavaDoc source;
63
64     protected String JavaDoc responseMessage;
65
66     protected int responseType;
67
68     protected String JavaDoc responseSubType;
69
70     protected ResponseTextCode responseTextCode;
71     
72     /**
73      * Constant for responsetype STATUS. Returned by {@link #getResponseType()}.
74      */

75     public static final int RESPONSE_STATUS = 0;
76
77     /**
78      * Constant for responsetype MAILBOX_DATA. Returned by {@link #getResponseType()}.
79      */

80     public static final int RESPONSE_MAILBOX_DATA = 1;
81
82     /**
83      * Constant for responsetype MESSAGE_DATA. Returned by {@link #getResponseType()}.
84      */

85     public static final int RESPONSE_MESSAGE_DATA = 2;
86
87     /**
88      * Constant for responsetype CONTINUATION. Returned by {@link #getResponseType()}.
89      */

90     public static final int RESPONSE_CONTINUATION = 3;
91
92     /**
93      * Constructs an IMAPResponse with the given source.
94      *
95      * @param source
96      */

97     public IMAPResponse(String JavaDoc source) {
98         this.source = source;
99         
100         literals = new LinkedList JavaDoc();
101         preNumber = -1;
102     }
103
104     /**
105      * Returns the tag of the response.
106      * If the reponse has no tag null is returned.
107      *
108      * @see #isTagged()
109      *
110      * @return the tag of the reponse or null if not present.
111      */

112     public String JavaDoc getTag() {
113         return tag;
114     }
115
116
117     
118     /**
119      * Gets the source of the response.
120      *
121      * @return the response as String
122      */

123     public String JavaDoc getSource() {
124         StringBuffer JavaDoc cleanedup = new StringBuffer JavaDoc(source.length());
125         literalMatcher.reset(source);
126         
127         while (literalMatcher.find()) {
128             literalMatcher.appendReplacement(cleanedup, getData(literalMatcher.group()).toString());
129          }
130          literalMatcher.appendTail(cleanedup);
131         
132         return cleanedup.toString();
133     }
134
135
136     
137     /**
138      * Checks if the response is tagged.
139      *
140      * @return true if the response is tagged, else false
141      */

142     public boolean isTagged() {
143         return tag != null;
144     }
145
146     /**
147      * Checks if the response subtye is "OK".
148      * <p>Convienice method, uses {@link #getResponseSubType() }.
149      *
150      * @return true if the response subtye is "OK", else false
151      */

152     public boolean isOK() {
153         return responseSubType.equals("OK");
154     }
155
156     /**
157      * Checks if the response subtye is "NO".
158      * <p>Convienice method, uses {@link #getResponseSubType() }.
159      *
160      * @return true if the response subtye is "NO", else false
161      */

162     public boolean isNO() {
163         return responseSubType.equals("NO");
164     }
165
166     /**
167      * Checks if the response subtye is "BYE".
168      * <p>Convienice method, uses {@link #getResponseSubType() }.
169      *
170      * @return true if the response subtye is "BYE", else false
171      */

172     public boolean isBYE() {
173         return responseSubType.equals("BYE");
174     }
175
176     /**
177      * Checks if the response subtye is "BAD".
178      * <p>Convienice method, uses {@link #getResponseSubType() }.
179      *
180      * @return true if the response subtye is "BAD", else false
181      */

182     public boolean isBAD() {
183         return responseSubType.equals("BAD");
184     }
185
186     /**
187      * Sets the tag.
188      *
189      * @param string
190      */

191     public void setTag(String JavaDoc string) {
192         tag = string;
193     }
194
195     /**
196      * Gets the message.
197      * <p>
198      * Returns the message from the server response. This can be a human-readable
199      * message that might be used to show to the user if an error occured.
200      * For example if the answer is "A044 BAD No such command as "BLURDYBLOOP"" the
201      * response message is: "No such command as "BLURDYBLOOP"". <p>
202      * This can also be the data if the reponse is of type message or mailbox-data.
203      * For example if the answer is "* LIST (\Noselect) "/" ~/Mail/foo" the message
204      * is "(\Noselect) "/" ~/Mail/foo".
205      * <p>
206      * <b>Note:</b> If the message contains literals you can access them via {@link #getLiteral(int)}.
207      *
208      * @return the message of a response
209      */

210     public String JavaDoc getResponseMessage() {
211         return responseMessage;
212     }
213
214     /**
215      * Sets the message.
216      *
217      * @see #getResponseMessage()
218      *
219      * @param responseText
220      */

221     public void setResponseMessage(String JavaDoc responseText) {
222         this.responseMessage = responseText;
223     }
224
225     /**
226      * Gets the response type of this imap response.
227      * <p>
228      * There are four types of responses:
229      * <li> RESPONSE_STATUS . Example: * OK IMAP4rev1 server ready
230      * <li> RESPONSE_MAILBOX_DATA . Example : * LSUB () "." #news.comp.mail.misc
231      * <li> RESPONSE_MESSAGE_DATA . Example : * 23 FETCH (FLAGS (\Seen) RFC822.SIZE 44827)
232      * <li> RESPONSE_CONTINUATION . Example : + Ready for additional command text
233      *
234      * @return type of the response
235      */

236     public int getResponseType() {
237         return responseType;
238     }
239
240     /**
241      * Sets the response type of this imap response.
242      *
243      * @see #getResponseType()
244      *
245      * @param responseType
246      */

247     public void setResponseType(int responseType) {
248         this.responseType = responseType;
249     }
250
251     /**
252      * Gets the reponse subtype.
253      * <p>
254      * Example: For "* LIST (\Noselect) "/" ~/Mail/foo" the reponse subtype is LIST.
255      *
256      * @return the response subtype
257      */

258     public String JavaDoc getResponseSubType() {
259         return responseSubType;
260     }
261
262     /**
263      * Sets the reponse subtype.
264      * <p>
265      * Example: For "* LIST (\Noselect) "/" ~/Mail/foo" the reponse subtype is LIST.
266      *
267      * @param responseSubType
268      */

269     public void setResponseSubType(String JavaDoc responseSubType) {
270         this.responseSubType = responseSubType;
271     }
272
273     /**
274      * Gets the reponse text code.
275      * <p>
276      * The reponse text code is optional extra information to the response. It is
277      * null if no response text is present.<p>
278      * Example: For "A142 OK [READ-WRITE] SELECT completed" the text code is "READ-WRTE".
279      *
280      * @return the response text code
281      */

282     public ResponseTextCode getResponseTextCode() {
283         return responseTextCode;
284     }
285
286     /**
287      * Sets the reponse text code.
288      *
289      * @see #getResponseTextCode()
290      *
291      * @param responseTextCode
292      */

293     public void setResponseTextCode(ResponseTextCode responseTextCode) {
294         this.responseTextCode = responseTextCode;
295     }
296
297     /**
298      * @param restresponse
299      */

300     public void appendResponseText(String JavaDoc restresponse) {
301         source += restresponse;
302         responseMessage += restresponse;
303     }
304
305     /**
306      * Gets the Literal with a given index.
307      * @param index
308      *
309      * @return the Source of the Literal
310      */

311     public Source getLiteral(int index) {
312         return (Source) literals.get(index);
313     }
314
315     /**
316      * Add a literal to the response.
317      *
318      * @param literal
319      */

320     public void addLiteral(Source literal) {
321         literals.add(literal);
322     }
323
324     /**
325      * Gets the pre number.
326      * <p>
327      * Is can be any number that comes in a server response before a command
328      * specification.<p>
329      * Example: For "* 44 EXPUNGE" the pre number is 44.
330      *
331      * @return the pre number of the response or -1 if not present.
332      */

333     public int getPreNumber() {
334         return preNumber;
335     }
336
337     /**
338      * Sets the pre number.
339      *
340      * @param preNumber
341      */

342     public void setPreNumber(int preNumber) {
343         this.preNumber = preNumber;
344     }
345     
346     /**
347      * Gets the data.
348      * <p> Use this method to get the data if this can be quoted or a literal.
349      * For example if the input is {0} the first literal is returned. If the
350      * input is "bla" then bla is returned.
351      *
352      * @param data
353      * @return the Source of the data
354      */

355     public Source getData(CharSequence JavaDoc data) {
356         if( data.length() == 0 ) return new CharSequenceSource(data);
357         literalMatcher.reset(data);
358         if( literalMatcher.matches() ) {
359             return getLiteral(Integer.parseInt(literalMatcher.group(1)));
360         } else {
361             // remove ""
362
if( data.charAt(0) == '"') {
363                 return new CharSequenceSource( data.subSequence(1,data.length()-1) );
364             } else {
365                 return new CharSequenceSource( data );
366             }
367             
368         }
369     }
370
371 }
372
Popular Tags