KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > ImapResponse


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * 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 *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.imapserver;
19
20 import org.apache.james.imapserver.commands.ImapCommand;
21 import org.apache.james.imapserver.store.MessageFlags;
22 import org.apache.james.util.InternetPrintWriter;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 /**
28  * Class providing methods to send response messages from the server
29  * to the client.
30  */

31 public class ImapResponse implements ImapConstants
32 {
33     private PrintWriter writer;
34     private String JavaDoc tag = UNTAGGED;
35
36     public ImapResponse( OutputStream JavaDoc output )
37     {
38         this.writer = new InternetPrintWriter( output, true );
39     }
40
41     public void setTag( String JavaDoc tag )
42     {
43         this.tag = tag;
44     }
45
46     /**
47      * Writes a standard tagged OK response on completion of a command.
48      * Response is writen as:
49      * <pre> a01 OK COMMAND_NAME completed.</pre>
50      *
51      * @param command The ImapCommand which was completed.
52      */

53     public void commandComplete( ImapCommand command )
54     {
55         commandComplete( command, null );
56     }
57
58     /**
59      * Writes a standard tagged OK response on completion of a command,
60      * with a response code (eg READ-WRITE)
61      * Response is writen as:
62      * <pre> a01 OK [responseCode] COMMAND_NAME completed.</pre>
63      *
64      * @param command The ImapCommand which was completed.
65      * @param responseCode A string response code to send to the client.
66      */

67     public void commandComplete( ImapCommand command, String JavaDoc responseCode )
68     {
69         tag();
70         message( OK );
71         responseCode( responseCode );
72         commandName( command );
73         message( "completed." );
74         end();
75     }
76
77     /**
78      * Writes a standard NO response on command failure, together with a
79      * descriptive message.
80      * Response is writen as:
81      * <pre> a01 NO COMMAND_NAME failed. <reason></pre>
82      *
83      * @param command The ImapCommand which failed.
84      * @param reason A message describing why the command failed.
85      */

86     public void commandFailed( ImapCommand command, String JavaDoc reason )
87     {
88         commandFailed( command, null, reason );
89     }
90
91     /**
92      * Writes a standard NO response on command failure, together with a
93      * descriptive message.
94      * Response is writen as:
95      * <pre> a01 NO [responseCode] COMMAND_NAME failed. <reason></pre>
96      *
97      * @param command The ImapCommand which failed.
98      * @param responseCode The Imap response code to send.
99      * @param reason A message describing why the command failed.
100      */

101     public void commandFailed( ImapCommand command,
102                                String JavaDoc responseCode,
103                                String JavaDoc reason )
104     {
105         tag();
106         message( NO );
107         responseCode( responseCode );
108         commandName( command );
109         message( "failed." );
110         message( reason );
111         end();
112     }
113
114     /**
115      * Writes a standard BAD response on command error, together with a
116      * descriptive message.
117      * Response is writen as:
118      * <pre> a01 BAD <message></pre>
119      *
120      * @param message The descriptive error message.
121      */

122     public void commandError( String JavaDoc message )
123     {
124         tag();
125         message( BAD );
126         message( message );
127         end();
128     }
129
130     /**
131      * Writes a standard untagged BAD response, together with a descriptive message.
132      */

133     public void badResponse( String JavaDoc message )
134     {
135         untagged();
136         message( BAD );
137         message( message );
138         end();
139     }
140
141     /**
142      * Writes an untagged OK response, with the supplied response code,
143      * and an optional message.
144      * @param responseCode The response code, included in [].
145      * @param message The message to follow the []
146      */

147     public void okResponse( String JavaDoc responseCode, String JavaDoc message )
148     {
149         untagged();
150         message( OK );
151         responseCode( responseCode );
152         message( message );
153         end();
154     }
155
156     public void flagsResponse( MessageFlags flags )
157     {
158         untagged();
159         message( "FLAGS" );
160         message( flags.format() );
161         end();
162     }
163
164     public void existsResponse( int count )
165     {
166         untagged();
167         message( count );
168         message( "EXISTS" );
169         end();
170     }
171
172     public void recentResponse( int count )
173     {
174         untagged();
175         message( count );
176         message( "RECENT" );
177         end();
178     }
179
180     public void expungeResponse( int msn )
181     {
182         untagged();
183         message( msn );
184         message( "EXPUNGE" );
185         end();
186     }
187
188     public void fetchResponse( int msn, String JavaDoc msgData )
189     {
190         untagged();
191         message( msn );
192         message( "FETCH" );
193         message( "(" + msgData + ")" );
194         end();
195     }
196
197     public void commandResponse( ImapCommand command, String JavaDoc message )
198     {
199         untagged();
200         commandName( command );
201         message( message );
202         end();
203     }
204
205     /**
206      * Writes the message provided to the client, prepended with the
207      * request tag.
208      *
209      * @param message The message to write to the client.
210      */

211     public void taggedResponse( String JavaDoc message )
212     {
213         tag();
214         message( message );
215         end();
216     }
217
218     /**
219      * Writes the message provided to the client, prepended with the
220      * untagged marker "*".
221      *
222      * @param message The message to write to the client.
223      */

224     public void untaggedResponse( String JavaDoc message )
225     {
226         untagged();
227         message( message );
228         end();
229     }
230
231     private void untagged()
232     {
233         writer.print( UNTAGGED );
234     }
235
236     private void tag()
237     {
238         writer.print( tag );
239     }
240
241     private void commandName( ImapCommand command )
242     {
243         String JavaDoc name = command.getName();
244         writer.print( SP );
245         writer.print( name );
246     }
247
248     private void message( String JavaDoc message )
249     {
250         if ( message != null ) {
251             writer.print( SP );
252             writer.print( message );
253         }
254     }
255
256     private void message( int number )
257     {
258         writer.print( SP );
259         writer.print( number );
260     }
261
262     private void responseCode( String JavaDoc responseCode )
263     {
264         if ( responseCode != null ) {
265             writer.print( " [" );
266             writer.print( responseCode );
267             writer.print( "]" );
268         }
269     }
270
271     private void end()
272     {
273         writer.println();
274         writer.flush();
275     }
276
277 }
278
Popular Tags