KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > mail > server > POP3Worker


1 /**
2  * @author Chamil Thanthrimudalige
3  */

4 package org.apache.axis2.transport.mail.server;
5
6 import javax.mail.MessagingException JavaDoc;
7 import javax.mail.internet.MimeMessage JavaDoc;
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 JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 public class POP3Worker extends Thread JavaDoc{
18     private Socket JavaDoc socket;
19     private Storage st;
20     boolean doneProcess = false;
21     protected static Log log = LogFactory.getLog(POP3Server.class.getName());
22     int numDeleted = 0 ; //This is a small hack to get the deleting working with the ArrayList. To keep it simple.
23
public POP3Worker(Socket JavaDoc 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 JavaDoc s;
37             String JavaDoc ps; //ProcessedString
38
while(!doneProcess) {
39                 s = bufferedReader.readLine();
40                 processInput(s, printWriter);
41             }
42             socket.close();
43         } catch(Exception JavaDoc e) {
44             log.error(e);
45         }
46
47     }
48     ArrayList JavaDoc messages = new ArrayList JavaDoc();
49     private void processInput(String JavaDoc 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 JavaDoc user = "";
54         if(input==null) {
55             this.doneProcess = true; // This should not be happening
56
} else {
57             ArrayList JavaDoc tokens = new ArrayList JavaDoc();
58             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(input);
59             while (stk.hasMoreTokens()) {
60                 tokens.add(stk.nextToken());
61             }
62             if(((String JavaDoc)tokens.get(0)).equals(MailConstants.USER)) {
63                 user = (String JavaDoc)tokens.get(1);
64                 messages = st.popUserMails(user);
65                 printWriter.println(MailConstants.OK);
66             } else if(((String JavaDoc)tokens.get(0)).equals(MailConstants.PASS)) {
67                 printWriter.println(MailConstants.OK); // Passwords are not checked.
68
} 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"); // We take the maildrop size as one.
73
} else if(((String JavaDoc)tokens.get(0)).equals(MailConstants.LIST)) { // scan listing
74
if(tokens.size() > 1) {
75                     try {
76                         int optArg = Integer.parseInt((String JavaDoc)tokens.get(1));
77                         int messageArrayIndex = optArg - 1;
78                         if((messageArrayIndex < messages.size()) && (messageArrayIndex >= 0)) { // that is OK careful with numbering
79
printWriter.println(MailConstants.OK + messageArrayIndex + 1 + " 120"); // Mail size of 120 is just some number.
80
} else {
81                             printWriter.println(MailConstants.ERR + "no such message, only " + (messages.size() + 1) + " messages in maildrop");
82                         }
83                     } catch(NumberFormatException JavaDoc e) {
84                         e.printStackTrace();
85                         printWriter.println(MailConstants.ERR + "problem passing the index. Index submited was " + (String JavaDoc)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"); // List out all the messages with a message size octet of 120
92
}
93                     printWriter.println(".");
94                 }
95             } else if(((String JavaDoc)tokens.get(0)).equals(MailConstants.RETR)) {
96                 String JavaDoc i = (String JavaDoc)tokens.get(1);
97                 try {
98                     int index = Integer.parseInt(i);
99                     printWriter.println(MailConstants.OK);
100                     MimeMessage JavaDoc m = (MimeMessage JavaDoc)messages.get(index-1);
101
102                     m.writeTo(socket.getOutputStream());
103                     //System.out.println("\n\n\n\n ========This is the mail========");
104
//m.writeTo(System.out);//socket.getOutputStream());
105
//System.out.println("\n\n\n\n ========This is the mail========");
106

107                     socket.getOutputStream().write(CR_LF_DOT_CR_LF);// This is a bit of a hack to get it working. Have to find a bette way to handle this.
108
socket.getOutputStream().flush();
109                 } catch(NumberFormatException JavaDoc e) {
110                     printWriter.println(MailConstants.ERR);
111                 } catch (IOException e1) {
112                     printWriter.println(MailConstants.ERR);
113                 } catch (MessagingException JavaDoc e2) {
114                     printWriter.println(MailConstants.ERR);
115                 }
116             } else if(((String JavaDoc)tokens.get(0)).equals(MailConstants.DELE)) {
117                 String JavaDoc smIndex = (String JavaDoc)tokens.get(1);
118                 try {
119                     int mIndex = Integer.parseInt(smIndex)-1 - numDeleted; // When one mail is deleted the index of the other mails will reduce. Asumed that the delete will occure from bottom up.
120
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 JavaDoc e) {
128                     printWriter.println(MailConstants.ERR);
129                 }
130             } else if(((String JavaDoc)tokens.get(0)).equals(MailConstants.NOOP) || ((String JavaDoc)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