OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / test / com / dumbster / smtp / SimpleSmtpServer.java
1 /*\r
2  * Dumbster: a dummy SMTP server.\r
3  * Copyright (C) 2003, Jason Paul Kitchen\r
4  * lilnottsman@yahoo.com\r
5  *\r
6  * This library is free software; you can redistribute it and/or\r
7  * modify it under the terms of the GNU Lesser General Public\r
8  * License as published by the Free Software Foundation; either\r
9  * version 2.1 of the License, or (at your option) any later version.\r
10  *\r
11  * This library is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
14  * Lesser General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU Lesser General Public\r
17  * License along with this library; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
19  */\r
20 package com.dumbster.smtp;\r
21 \r
22 import java.io.BufferedReader;\r
23 import java.io.IOException;\r
24 import java.io.InputStreamReader;\r
25 import java.io.InterruptedIOException;\r
26 import java.io.PrintWriter;\r
27 import java.net.ServerSocket;\r
28 import java.net.Socket;\r
29 import java.util.ArrayList;\r
30 import java.util.Iterator;\r
31 import java.util.List;\r
32 \r
33 /**\r
34  * Dummy SMTP server for testing purposes.\r
35  */\r
36 public class SimpleSmtpServer implements Runnable {\r
37 \r
38         /** Stores all of the email received since this instance started up. */\r
39         private List receivedMail;\r
40 \r
41         /** Default SMTP port is 25. */\r
42         public static final int DEFAULT_SMTP_PORT = 25;\r
43 \r
44         /** Indicates whether this server is stopped or not. */\r
45         private volatile boolean stopped = true;\r
46 \r
47         /** Indicates if a stop request has been sent to this server. */\r
48         private volatile boolean doStop = false;\r
49 \r
50         /** Port the server listens on - set to the default SMTP port initially. */\r
51         private int port = DEFAULT_SMTP_PORT;\r
52 \r
53         /**\r
54          * Constructor.\r
55          */\r
56         public SimpleSmtpServer(int port) {\r
57                 receivedMail = new ArrayList();\r
58                 this.port = port;\r
59         }\r
60 \r
61         /**\r
62          * Main loop of the SMTP server.\r
63          */\r
64         public void run() {\r
65                 stopped = false;\r
66                 ServerSocket serverSocket = null;\r
67                 try {\r
68                         serverSocket = new ServerSocket(port);\r
69                         serverSocket.setSoTimeout(500); // Block for maximum of 1.5 seconds\r
70 \r
71                         // Server: loop until stopped\r
72                         while (!doStop) {\r
73                                 Socket socket = null;\r
74                                 try {\r
75                                         socket = serverSocket.accept();\r
76                                 } catch (InterruptedIOException iioe) {\r
77                                         if (socket != null) {\r
78                                                 socket.close();\r
79                                         }\r
80                                         continue; // Non-blocking socket timeout occurred: try accept() again\r
81                                 }\r
82 \r
83                                 // Get the input and output streams\r
84                                 BufferedReader input = new BufferedReader(new InputStreamReader(socket\r
85                                                 .getInputStream()));\r
86                                 PrintWriter out = new PrintWriter(socket.getOutputStream());\r
87 \r
88                                 // Initialize the state machine\r
89                                 SmtpState smtpState = SmtpState.CONNECT;\r
90                                 SmtpRequest smtpRequest = new SmtpRequest(SmtpActionType.CONNECT, "", smtpState);\r
91 \r
92                                 // Execute the connection request\r
93                                 SmtpResponse smtpResponse = smtpRequest.execute();\r
94 \r
95                                 // Send initial response\r
96                                 sendResponse(out, smtpResponse);\r
97                                 smtpState = smtpResponse.getNextState();\r
98 \r
99                                 SmtpMessage msg = new SmtpMessage();\r
100 \r
101                                 while (smtpState != SmtpState.CONNECT) {\r
102                                         String line = input.readLine();\r
103 \r
104                                         if (line == null) {\r
105                                                 break;\r
106                                         }\r
107                                         // Create request from client input and current state\r
108                                         SmtpRequest request = SmtpRequest.createRequest(line, smtpState);\r
109                                         // Execute request and create response object\r
110                                         SmtpResponse response = request.execute();\r
111                                         // Move to next internal state\r
112                                         smtpState = response.getNextState();\r
113                                         // Send reponse to client\r
114                                         sendResponse(out, response);\r
115 \r
116                                         // Store input in message\r
117                                         msg.store(response, request.getParams());\r
118                                 }\r
119 \r
120                                 receivedMail.add(msg);\r
121                                 socket.close();\r
122                         }\r
123                 } catch (Exception e) {\r
124                         // e.printStackTrace();\r
125                 } finally {\r
126                         if (serverSocket != null) {\r
127                                 try {\r
128                                         serverSocket.close();\r
129                                 } catch (IOException e) {\r
130                                         e.printStackTrace();\r
131                                 }\r
132                         }\r
133                 }\r
134                 stopped = true;\r
135         }\r
136 \r
137         /**\r
138          * Send response to client.\r
139          * @param out socket output stream\r
140          * @param smtpResponse response object\r
141          */\r
142         private void sendResponse(PrintWriter out, SmtpResponse smtpResponse) {\r
143                 if (smtpResponse.getCode() > 0) {\r
144                         out.print(smtpResponse.getCode() + " " + smtpResponse.getMessage() + "\r\n");\r
145                         out.flush();\r
146                 }\r
147         }\r
148 \r
149         /**\r
150          * Get email received by this instance since start up.\r
151          * @return List of String\r
152          */\r
153         public Iterator getReceivedEmail() {\r
154                 return receivedMail.iterator();\r
155         }\r
156 \r
157         /**\r
158          * Get the number of messages received.\r
159          * @return size of received email list\r
160          */\r
161         public int getReceievedEmailSize() {\r
162                 return receivedMail.size();\r
163         }\r
164 \r
165         /**\r
166          * Forces the server to stop after processing the current request.\r
167          */\r
168         public void stop() {\r
169                 doStop = true;\r
170                 while (!isStopped()) {} // Wait for email server to stop\r
171         }\r
172 \r
173         /**\r
174          * Indicates whether this server is stopped or not.\r
175          * @return true iff this server is stopped\r
176          */\r
177         public boolean isStopped() {\r
178                 return stopped;\r
179         }\r
180 \r
181         /**\r
182          * Creates an instance of SimpleSmtpServer and starts it. Will listen on the default port.\r
183          * @return a reference to the SMTP server\r
184          */\r
185         public static SimpleSmtpServer start() {\r
186                 return start(DEFAULT_SMTP_PORT);\r
187         }\r
188 \r
189         /**\r
190          * Creates an instance of SimpleSmtpServer and starts it.\r
191          * @param port port number the server should listen to\r
192          * @return a reference to the SMTP server\r
193          */\r
194         public static SimpleSmtpServer start(int port) {\r
195                 SimpleSmtpServer server = new SimpleSmtpServer(port);\r
196                 Thread t = new Thread(server);\r
197                 t.start();\r
198                 return server;\r
199         }\r
200 \r
201 }