1 16 package org.apache.commons.net.pop3; 17 18 import java.io.BufferedReader ; 19 import java.io.BufferedWriter ; 20 import java.io.EOFException ; 21 import java.io.IOException ; 22 import java.io.InputStreamReader ; 23 import java.io.OutputStreamWriter ; 24 import java.lang.StringBuffer ; 25 import java.util.Enumeration ; 26 import java.util.Vector ; 27 import org.apache.commons.net.MalformedServerReplyException; 28 import org.apache.commons.net.ProtocolCommandListener; 29 import org.apache.commons.net.ProtocolCommandSupport; 30 import org.apache.commons.net.SocketClient; 31 32 53 54 public class POP3 extends SocketClient 55 { 56 57 public static final int DEFAULT_PORT = 110; 58 62 public static final int DISCONNECTED_STATE = -1; 63 64 public static final int AUTHORIZATION_STATE = 0; 65 66 public static final int TRANSACTION_STATE = 1; 67 68 public static final int UPDATE_STATE = 2; 69 70 static final String _OK = "+OK"; 71 static final String _ERROR = "-ERR"; 72 73 private static final String __DEFAULT_ENCODING = "ISO-8859-1"; 77 78 private int __popState; 79 private BufferedWriter __writer; 80 private StringBuffer __commandBuffer; 81 82 BufferedReader _reader; 83 int _replyCode; 84 String _lastReplyLine; 85 Vector _replyLines; 86 87 91 protected ProtocolCommandSupport _commandSupport_; 92 93 97 public POP3() 98 { 99 setDefaultPort(DEFAULT_PORT); 100 __commandBuffer = new StringBuffer (); 101 __popState = DISCONNECTED_STATE; 102 _reader = null; 103 __writer = null; 104 _replyLines = new Vector (); 105 _commandSupport_ = new ProtocolCommandSupport(this); 106 } 107 108 private void __getReply() throws IOException 109 { 110 String line; 111 112 _replyLines.setSize(0); 113 line = _reader.readLine(); 114 115 if (line == null) 116 throw new EOFException ("Connection closed without indication."); 117 118 if (line.startsWith(_OK)) 119 _replyCode = POP3Reply.OK; 120 else if (line.startsWith(_ERROR)) 121 _replyCode = POP3Reply.ERROR; 122 else 123 throw new 124 MalformedServerReplyException( 125 "Received invalid POP3 protocol response from server."); 126 127 _replyLines.addElement(line); 128 _lastReplyLine = line; 129 130 if (_commandSupport_.getListenerCount() > 0) 131 _commandSupport_.fireReplyReceived(_replyCode, getReplyString()); 132 } 133 134 135 139 protected void _connectAction_() throws IOException 140 { 141 super._connectAction_(); 142 _reader = 143 new BufferedReader (new InputStreamReader (_input_, 144 __DEFAULT_ENCODING)); 145 __writer = 146 new BufferedWriter (new OutputStreamWriter (_output_, 147 __DEFAULT_ENCODING)); 148 __getReply(); 149 setState(AUTHORIZATION_STATE); 150 } 151 152 153 159 public void addProtocolCommandListener(ProtocolCommandListener listener) 160 { 161 _commandSupport_.addProtocolCommandListener(listener); 162 } 163 164 170 public void removeProtocolCommandistener(ProtocolCommandListener listener) 171 { 172 _commandSupport_.removeProtocolCommandListener(listener); 173 } 174 175 176 182 public void setState(int state) 183 { 184 __popState = state; 185 } 186 187 188 193 public int getState() 194 { 195 return __popState; 196 } 197 198 199 202 public void getAdditionalReply() throws IOException 203 { 204 String line; 205 206 line = _reader.readLine(); 207 while (line != null) 208 { 209 _replyLines.addElement(line); 210 if (line.equals(".")) 211 break; 212 line = _reader.readLine(); 213 } 214 } 215 216 217 225 public void disconnect() throws IOException 226 { 227 super.disconnect(); 228 _reader = null; 229 __writer = null; 230 _lastReplyLine = null; 231 _replyLines.setSize(0); 232 setState(DISCONNECTED_STATE); 233 } 234 235 236 243 public int sendCommand(String command, String args) throws IOException 244 { 245 String message; 246 247 __commandBuffer.setLength(0); 248 __commandBuffer.append(command); 249 250 if (args != null) 251 { 252 __commandBuffer.append(' '); 253 __commandBuffer.append(args); 254 } 255 __commandBuffer.append(SocketClient.NETASCII_EOL); 256 257 __writer.write(message = __commandBuffer.toString()); 258 __writer.flush(); 259 260 if (_commandSupport_.getListenerCount() > 0) 261 _commandSupport_.fireCommandSent(command, message); 262 263 __getReply(); 264 return _replyCode; 265 } 266 267 274 public int sendCommand(String command) throws IOException 275 { 276 return sendCommand(command, null); 277 } 278 279 287 public int sendCommand(int command, String args) throws IOException 288 { 289 return sendCommand(POP3Command._commands[command], args); 290 } 291 292 300 public int sendCommand(int command) throws IOException 301 { 302 return sendCommand(POP3Command._commands[command], null); 303 } 304 305 306 318 public String [] getReplyStrings() 319 { 320 String [] lines; 321 lines = new String [_replyLines.size()]; 322 _replyLines.copyInto(lines); 323 return lines; 324 } 325 326 338 public String getReplyString() 339 { 340 Enumeration en; 341 StringBuffer buffer = new StringBuffer (256); 342 343 en = _replyLines.elements(); 344 while (en.hasMoreElements()) 345 { 346 buffer.append((String )en.nextElement()); 347 buffer.append(SocketClient.NETASCII_EOL); 348 } 349 350 return buffer.toString(); 351 } 352 353 } 354 355 | Popular Tags |