1 4 package org.apache.axis2.transport.mail.server; 5 6 import javax.mail.MessagingException ; 7 import javax.mail.internet.MimeMessage ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import java.io.*; 13 import java.net.Socket ; 14 import java.util.ArrayList ; 15 import java.util.StringTokenizer ; 16 17 public class POP3Worker extends Thread { 18 private Socket socket; 19 private Storage st; 20 boolean doneProcess = false; 21 protected static Log log = LogFactory.getLog(POP3Server.class.getName()); 22 int numDeleted = 0 ; public POP3Worker(Socket socket, Storage st) { 24 this.socket = socket; 25 this.st = st; 26 } 27 28 public void run() { 29 try { 30 InputStream inputStream = socket.getInputStream(); 31 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 32 33 PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); 34 35 printWriter.println(MailConstants.OK+" POP3 server ready"); 36 String s; 37 String ps; while(!doneProcess) { 39 s = bufferedReader.readLine(); 40 processInput(s, printWriter); 41 } 42 socket.close(); 43 } catch(Exception e) { 44 log.error(e); 45 } 46 47 } 48 ArrayList messages = new ArrayList (); 49 private void processInput(String input, PrintWriter printWriter){ 50 int listLen = (MailConstants.LIST+" ").length(); 51 byte[] CR_LF = new byte[] {0x0D, 0x0A}; 52 byte[] CR_LF_DOT_CR_LF = new byte[] { 0x0D, 0x0A, '.', 0x0D, 0x0A }; 53 String user = ""; 54 if(input==null) { 55 this.doneProcess = true; } else { 57 ArrayList tokens = new ArrayList (); 58 StringTokenizer stk = new StringTokenizer (input); 59 while (stk.hasMoreTokens()) { 60 tokens.add(stk.nextToken()); 61 } 62 if(((String )tokens.get(0)).equals(MailConstants.USER)) { 63 user = (String )tokens.get(1); 64 messages = st.popUserMails(user); 65 printWriter.println(MailConstants.OK); 66 } else if(((String )tokens.get(0)).equals(MailConstants.PASS)) { 67 printWriter.println(MailConstants.OK); } else if(input.equals(MailConstants.QUIT)) { 69 printWriter.println(MailConstants.OK + "POP3 server signing off"); 70 doneProcess=true; 71 } else if(input.equals(MailConstants.STAT)) { 72 printWriter.println(MailConstants.OK + messages.size() + " 1"); } else if(((String )tokens.get(0)).equals(MailConstants.LIST)) { if(tokens.size() > 1) { 75 try { 76 int optArg = Integer.parseInt((String )tokens.get(1)); 77 int messageArrayIndex = optArg - 1; 78 if((messageArrayIndex < messages.size()) && (messageArrayIndex >= 0)) { printWriter.println(MailConstants.OK + messageArrayIndex + 1 + " 120"); } else { 81 printWriter.println(MailConstants.ERR + "no such message, only " + (messages.size() + 1) + " messages in maildrop"); 82 } 83 } catch(NumberFormatException e) { 84 e.printStackTrace(); 85 printWriter.println(MailConstants.ERR + "problem passing the index. Index submited was " + (String )tokens.get(1)); 86 } 87 } else { 88 printWriter.println(MailConstants.OK+messages.size()); 89 for(int i=0; i<messages.size(); i++) { 90 int messageIndex = i+1; 91 printWriter.println(messageIndex + " 120"); } 93 printWriter.println("."); 94 } 95 } else if(((String )tokens.get(0)).equals(MailConstants.RETR)) { 96 String i = (String )tokens.get(1); 97 try { 98 int index = Integer.parseInt(i); 99 printWriter.println(MailConstants.OK); 100 MimeMessage m = (MimeMessage )messages.get(index-1); 101 102 m.writeTo(socket.getOutputStream()); 103 107 socket.getOutputStream().write(CR_LF_DOT_CR_LF); socket.getOutputStream().flush(); 109 } catch(NumberFormatException e) { 110 printWriter.println(MailConstants.ERR); 111 } catch (IOException e1) { 112 printWriter.println(MailConstants.ERR); 113 } catch (MessagingException e2) { 114 printWriter.println(MailConstants.ERR); 115 } 116 } else if(((String )tokens.get(0)).equals(MailConstants.DELE)) { 117 String smIndex = (String )tokens.get(1); 118 try { 119 int mIndex = Integer.parseInt(smIndex)-1 - numDeleted; if((mIndex >= 0) && (mIndex < messages.size())) { 121 messages.remove(mIndex); 122 numDeleted++; 123 printWriter.println(MailConstants.OK); 124 } else { 125 printWriter.println(MailConstants.ERR); 126 } 127 } catch(NumberFormatException e) { 128 printWriter.println(MailConstants.ERR); 129 } 130 } else if(((String )tokens.get(0)).equals(MailConstants.NOOP) || ((String )tokens.get(0)).equals(MailConstants.RSET)) { 131 printWriter.println(MailConstants.OK); 132 } else { 133 printWriter.println(MailConstants.ERR); 134 } 135 } 136 } 137 } 138 | Popular Tags |