KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > imap > parser > IMAPResponseParser


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.parser;
37
38 import java.util.regex.Matcher JavaDoc;
39 import java.util.regex.Pattern JavaDoc;
40
41 import org.columba.ristretto.imap.IMAPResponse;
42 import org.columba.ristretto.parser.ParserException;
43
44 /**
45  * Main parser for the reponses received
46  * from the IMAP server.
47  *
48  * @author tstich
49  *
50  */

51 public class IMAPResponseParser {
52
53     
54     // Pattern to classify the response in tagged/untagged/continuation
55
// and to access the text of the reponse without the trailing CRLF
56
private static final Pattern JavaDoc response =
57         Pattern.compile("^((\\*)" + // group 2 is untagged
58
"|([0-9a-zA-Z]+)" + // group 3 is the tag
59
"|(\\+)) " + // group 4 is continuation
60
"([^\r\n]*)\r?\n?"); // group 5 is the rest of the response without a optional CRLF
61

62     
63     // Pattern to classify a status reponse.
64
private static final Pattern JavaDoc resp_cond =
65         Pattern.compile(
66             "^(OK|BAD|NO|BYE|PREAUTH) " + // group 1 is the status type
67
"(\\[(\\w+[^\\]]+)\\])?" + // group 3 is an optional text code
68
" ?([^\r\n]*)"); // group 4 is the message
69

70
71     // Pattern to classify a mailbox data response.
72
private static final Pattern JavaDoc mailbox_data =
73         Pattern.compile(
74             "^((FLAGS|LIST|LSUB|SEARCH|STATUS|CAPABILITY|NAMESPACE)" + // group2 contains a simple command with no prenumber
75
"|((\\d+) " + // group4 contains a prenumber
76
"(EXISTS|RECENT)))" + // group5 the command to the prenumber
77
" ?([^\r\n]*)"); // group6 contains the data
78

79             
80     // Pattern to classify a message data response.
81
private static final Pattern JavaDoc message_data =
82         Pattern.compile("^(\\d+) " + //group1 contains the prenumber
83
"(EXPUNGE|FETCH)" + //group2 contains the command
84
" ?([^\r\n]*)"); //group3 contains the data
85

86
87     // Pattern to classify a continuation response.
88
private static final Pattern JavaDoc resp_cont =
89         Pattern.compile("^(\\[(\\w+[^\\]]+)\\] )?" + // group2 contains optional text code
90
"([^\r\n]*)"); // group3 contains the message
91

92
93     /**
94      * Parses a raw string from a IMAP Server and returns the preparsed IMAPResponse.
95      *
96      * @param input
97      * @return the parsed Response
98      * @throws ParserException
99      */

100     public static IMAPResponse parse(CharSequence JavaDoc input) throws ParserException {
101         Matcher JavaDoc matcher = response.matcher(input);
102
103         // Is this a valid response?
104
if (matcher.matches()) {
105             IMAPResponse result = new IMAPResponse(input.toString());
106
107             // what kind of? (untagged)
108
if (matcher.group(2) != null) {
109
110                 // test for resp-cond (BYE/OK/BAD/PREAUTH)
111
Matcher JavaDoc respCode = resp_cond.matcher(matcher.group(5));
112                 if (respCode.matches()) {
113                     result.setResponseType(IMAPResponse.RESPONSE_STATUS);
114                     result.setResponseSubType(respCode.group(1));
115
116                     // do we have a responseCode?
117
if (respCode.group(3) != null) {
118                         result.setResponseTextCode(ResponseTextCodeParser.parse(respCode.group(3)));
119                     }
120
121                     // set reponse message
122
result.setResponseMessage(respCode.group(4));
123
124                     return result;
125                 }
126
127                 // test for mailbox-data (LIST|LSUB|FLAGS...)
128
Matcher JavaDoc mailboxData = mailbox_data.matcher(matcher.group(5));
129                 if (mailboxData.matches()) {
130                     result.setResponseType(IMAPResponse.RESPONSE_MAILBOX_DATA);
131
132                     // command without or with a pre number
133
if (mailboxData.group(2) != null) {
134                         result.setResponseSubType(mailboxData.group(1));
135                     } else {
136                         result.setPreNumber(Integer.parseInt(mailboxData.group(4)));
137                         result.setResponseSubType(mailboxData.group(5));
138                     }
139
140                     result.setResponseMessage(mailboxData.group(6));
141
142                     return result;
143                 }
144
145                 // test for message-data (EXPUNGE|FECTH)
146
Matcher JavaDoc messageData = message_data.matcher(matcher.group(5));
147                 if (messageData.matches()) {
148                     result.setResponseType(IMAPResponse.RESPONSE_MESSAGE_DATA);
149                     
150                     result.setPreNumber(Integer.parseInt(messageData.group(1)));
151                     result.setResponseSubType(messageData.group(2));
152                     
153                     result.setResponseMessage(messageData.group(3));
154
155                     return result;
156                 }
157
158                 // Not any of the above subtypes
159
throw new ParserException(
160                     "Unkown subytpe : " + result.getSource());
161             }
162
163             // what kind of? (tagged)
164
if (matcher.group(3) != null) {
165                 result.setTag(matcher.group(3));
166
167                 // test for resp-cond (BYE/OK/BAD/PREAUTH)
168
Matcher JavaDoc respCode = resp_cond.matcher(matcher.group(5));
169                 if (respCode.matches()) {
170                     result.setResponseType(IMAPResponse.RESPONSE_STATUS);
171                     result.setResponseSubType(respCode.group(1));
172
173                     // do we have a responseCode?
174
if (respCode.group(3) != null) {
175                         result.setResponseTextCode(ResponseTextCodeParser.parse(respCode.group(3)));
176                     }
177
178                     // set reponse text
179
result.setResponseMessage(respCode.group(4));
180
181                     return result;
182                 }
183
184                 // Not any of the above subtypes
185
throw new ParserException(
186                     "Unkown subytpe :" + result.getSource());
187             }
188
189             // what kind of? (continuation)
190
if (matcher.group(4) != null) {
191                 result.setResponseType(IMAPResponse.RESPONSE_CONTINUATION);
192
193                 Matcher JavaDoc respCont = resp_cont.matcher(matcher.group(5));
194                 if (respCont.matches()) {
195                     // do we have a responseCode?
196
if (respCont.group(2) != null) {
197                         result.setResponseTextCode(ResponseTextCodeParser.parse(respCont.group(2)));
198                     }
199
200                     // set reponse text
201
result.setResponseMessage(respCont.group(3));
202
203                     return result;
204                 }
205
206             }
207
208         }
209
210         throw new ParserException("Not a valid IMAP response : " + input);
211     }
212 }
213
Popular Tags