KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.axis2.transport.mail.server;
2
3 import org.apache.axis2.context.ConfigurationContext;
4
5 import javax.mail.*;
6 import javax.mail.internet.MimeMessage JavaDoc;
7 import java.io.*;
8 import java.net.Socket JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Properties JavaDoc;
11
12
13 /**
14  * @author Chamil Thanthrimudalige
15  * @author Chamikara Jayalath
16  */

17
18 public class SMTPWorker extends Thread JavaDoc {
19
20     BufferedReader reader = null;
21
22     BufferedWriter writer = null;
23     
24     ArrayList JavaDoc recivers = new ArrayList JavaDoc();
25
26     Storage st = null;
27     boolean runThread = true;
28
29     private MimeMessage JavaDoc mail = null;
30     private ConfigurationContext configurationContext = null;
31
32     String JavaDoc temp = "";
33     boolean dataWriting = false;
34     boolean transmitionEnd = false;
35     
36     boolean bodyData = false;
37     
38     public SMTPWorker(Socket JavaDoc socket, Storage st,ConfigurationContext configurationContext) {
39         try {
40             this.st = st;
41             this.configurationContext = configurationContext;
42             //get the streams from the socket and save in instance variables.
43
reader = new BufferedReader(new InputStreamReader(socket
44                     .getInputStream()));
45             writer = new BufferedWriter(new OutputStreamWriter(socket
46                     .getOutputStream()));
47         } catch (IOException ex) {
48             ex.printStackTrace();
49         }
50     }
51
52     //running the thread
53
public void run() {
54         try {
55             //do initial transmission.
56
initializeClient();
57             
58             //analyze all the inputs from client and work accordingly.
59
while (runThread) {
60                 String JavaDoc input = null;
61
62                 //get client input
63
input = reader.readLine();
64                 
65                 String JavaDoc retString = processInput(input);
66
67                 if (MailConstants.COMMAND_EXIT.equals(retString)){
68                     exitWorker();
69                 } else {
70                     if (retString != null) {
71                         send(retString); //Send the reply
72
}
73                     if(mail!=null && transmitionEnd) {
74                         exitWorker();
75                     }
76                 }
77             }
78             for (int idx = 0; idx < recivers.size();idx++){
79                 try {
80                     MailSorter mSort = new MailSorter(this.st,this.configurationContext);
81                     mSort.sort((String JavaDoc)recivers.get(idx), new MimeMessage JavaDoc(mail));
82                 } catch (MessagingException e1) {
83                     e1.printStackTrace();
84                 }
85             }
86             //
87

88         } catch (IOException e) {
89             System.out.println("ERROR: CLIENT CLOSED THE SOCKET");
90         }
91     }
92
93     private void send(String JavaDoc s) throws IOException{
94         writer.write(s);
95         writer.newLine();
96         writer.flush();
97     }
98     
99     private String JavaDoc processInput(String JavaDoc input) {
100         byte[] CR_LF = new byte[] {0x0D, 0x0A};
101         if(input==null) return MailConstants.COMMAND_UNKNOWN;
102         if(mail!=null && transmitionEnd) return MailConstants.COMMAND_TRANSMISSION_END;
103
104         if (input.startsWith("MAIL")) {
105             mail = new MimeMessage JavaDoc(Session.getInstance(new Properties JavaDoc(), new Authenticator() {
106                 protected PasswordAuthentication getPasswordAuthentication() {
107                     return null;
108                 }
109             }));
110
111             int start = input.indexOf("<") + 1;
112             int end;
113             
114             if(start<=0){
115                 start = input.indexOf("FROM:") + 5;
116                 end = input.length();
117             }else{
118                 end = input.indexOf(">");
119             }
120             
121             String JavaDoc from = input.substring(start,end);
122             
123             if(from!=null && !from.trim().equals("")){
124                 //TODO this is an ugly hack to get the from address in. There should be a better way to do this.
125
MailAddress mailFrom[] = new MailAddress[1];
126                 mailFrom[0] = new MailAddress(from);
127                 try {
128                     mail.addFrom(mailFrom);
129                 } catch (MessagingException e) {
130                     // TODO Auto-generated catch block
131
e.printStackTrace();
132                 }
133             }
134             
135             return MailConstants.MAIL_OK;
136
137         } if (input.startsWith("HELO")) {
138             return MailConstants.HELO_REPLY;
139
140         } else if (input.startsWith("RCPT")) {
141
142             String JavaDoc domain = MailConstants.SERVER_DOMAIN;
143             //System.out.println("RCPT:" + input);
144
//temp += input + "\n"; TODO Check this
145
int start = input.indexOf("<") + 1;
146             int end;
147             
148             if(start<=0){
149                 start = input.indexOf("TO:") + 3;
150 /* if(!input.endsWith(domain)){
151                     System.out.println("ERROR: wrong donmain name");
152                     return MailConstants.RCPT_ERROR;
153                 }*/

154             }else{
155 /* if(!input.endsWith(domain + ">")){
156                     System.out.println("ERROR: wrong donmain name");
157                     return MailConstants.RCPT_ERROR;
158                 }*/

159             }
160             
161             end = input.indexOf(">");
162             String JavaDoc toStr = input.substring(start,end);
163             
164             try {
165                 mail.addRecipient(Message.RecipientType.TO, new MailAddress(toStr));
166                 recivers.add(toStr);
167             } catch (MessagingException e) {
168                 // TODO Auto-generated catch block
169
e.printStackTrace();
170             }
171             return MailConstants.RCPT_OK;
172
173         } else if (input.equalsIgnoreCase("DATA")) {
174             dataWriting = true;
175             return MailConstants.DATA_START_SUCCESS;
176             
177         } else if (input.equalsIgnoreCase("QUIT")) {
178             dataWriting = true;
179             transmitionEnd = true;
180             return MailConstants.COMMAND_TRANSMISSION_END;
181             
182         } else if (input.equals(".")) {
183             dataWriting = false;
184             return MailConstants.DATA_END_SUCCESS;
185         } else if (input.equals("") && !bodyData) {
186             bodyData = true;
187             return null;
188         } else if(mail!=null && dataWriting){
189             try {
190                 if (bodyData) {
191                     temp += input;
192                     mail.setContent(temp, "text/plain");
193                     System.out.println("\n\n\n---------------" + temp + "---------------\n\n\n");
194                 } else {
195                     mail.addHeaderLine(input);
196                 }
197             } catch (MessagingException e) {
198                 // TODO Auto-generated catch block
199
e.printStackTrace();
200             }
201             return null;
202             
203         }else {
204             return MailConstants.COMMAND_UNKNOWN;
205         }
206
207     }
208    
209     //transmission is over. setting to exit
210
private void exitWorker() throws IOException {
211         reader.close();
212         writer.close();
213         runThread = false;
214     }
215     
216     //initializing the client by sending the initial message.
217
private void initializeClient() throws IOException {
218         if(writer!=null){
219             send("220 SMTP Server IS UP");
220         }
221     }
222 }
Popular Tags