1 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 ; 25 import java.io.OutputStream ; 26 27 31 public class ImapResponse implements ImapConstants 32 { 33 private PrintWriter writer; 34 private String tag = UNTAGGED; 35 36 public ImapResponse( OutputStream output ) 37 { 38 this.writer = new InternetPrintWriter( output, true ); 39 } 40 41 public void setTag( String tag ) 42 { 43 this.tag = tag; 44 } 45 46 53 public void commandComplete( ImapCommand command ) 54 { 55 commandComplete( command, null ); 56 } 57 58 67 public void commandComplete( ImapCommand command, String responseCode ) 68 { 69 tag(); 70 message( OK ); 71 responseCode( responseCode ); 72 commandName( command ); 73 message( "completed." ); 74 end(); 75 } 76 77 86 public void commandFailed( ImapCommand command, String reason ) 87 { 88 commandFailed( command, null, reason ); 89 } 90 91 101 public void commandFailed( ImapCommand command, 102 String responseCode, 103 String reason ) 104 { 105 tag(); 106 message( NO ); 107 responseCode( responseCode ); 108 commandName( command ); 109 message( "failed." ); 110 message( reason ); 111 end(); 112 } 113 114 122 public void commandError( String message ) 123 { 124 tag(); 125 message( BAD ); 126 message( message ); 127 end(); 128 } 129 130 133 public void badResponse( String message ) 134 { 135 untagged(); 136 message( BAD ); 137 message( message ); 138 end(); 139 } 140 141 147 public void okResponse( String responseCode, String 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 msgData ) 189 { 190 untagged(); 191 message( msn ); 192 message( "FETCH" ); 193 message( "(" + msgData + ")" ); 194 end(); 195 } 196 197 public void commandResponse( ImapCommand command, String message ) 198 { 199 untagged(); 200 commandName( command ); 201 message( message ); 202 end(); 203 } 204 205 211 public void taggedResponse( String message ) 212 { 213 tag(); 214 message( message ); 215 end(); 216 } 217 218 224 public void untaggedResponse( String 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 name = command.getName(); 244 writer.print( SP ); 245 writer.print( name ); 246 } 247 248 private void message( String 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 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 |