You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DummyMailServer.java 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant;
  19. import java.io.BufferedReader;
  20. import java.io.BufferedWriter;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.io.OutputStreamWriter;
  24. import java.net.ServerSocket;
  25. import java.net.Socket;
  26. import java.util.concurrent.Callable;
  27. /**
  28. * A utility class that pretends to be a mail transfer agent. This
  29. * has minimal functionality and is meant to be used only in tests
  30. */
  31. public class DummyMailServer implements Runnable, Callable<Void> {
  32. private final String host;
  33. private final int port;
  34. private StringBuilder sb = null;
  35. private volatile boolean stop = false;
  36. ServerSocket ssock = null;
  37. Socket sock = null;
  38. BufferedWriter out = null;
  39. BufferedReader in = null;
  40. private boolean data = false; // state engine: false=envelope, true=message
  41. public DummyMailServer(int port) {
  42. this("localhost", port);
  43. }
  44. public DummyMailServer(final String host, final int port) {
  45. this.host = host;
  46. this.port = port;
  47. }
  48. public int getPort() {
  49. return this.port;
  50. }
  51. public String getHost() {
  52. return this.host;
  53. }
  54. public void run() {
  55. call();
  56. }
  57. public Void call() {
  58. try {
  59. ssock = new ServerSocket(port);
  60. sock = ssock.accept(); // wait for connection
  61. in = new BufferedReader(new InputStreamReader(
  62. sock.getInputStream()));
  63. out = new BufferedWriter(new OutputStreamWriter(
  64. sock.getOutputStream()));
  65. sb = new StringBuilder();
  66. send("220 test SMTP EmailTaskTest\r\n");
  67. while (!stop) {
  68. String response = in.readLine();
  69. if (response == null) {
  70. stop = true;
  71. break;
  72. }
  73. sb.append(response).append("\r\n");
  74. if (!data && response.startsWith("EHLO")) {
  75. // reject Extended HLO semantics, since we don't support it
  76. send("500 EHLO unsupported\r\n");
  77. } else if (!data && response.startsWith("HELO")) {
  78. send("250 " + host + " Hello " + host + " "
  79. + "[127.0.0.1], pleased to meet you\r\n");
  80. } else if (!data && response.startsWith("MAIL")) {
  81. send("250\r\n");
  82. } else if (!data && response.startsWith("RCPT")) {
  83. send("250\r\n");
  84. } else if (!data && response.startsWith("DATA")) {
  85. send("354\r\n");
  86. data = true;
  87. } else if (data && response.equals(".")) {
  88. send("250\r\n");
  89. data = false;
  90. } else if (!data && response.startsWith("QUIT")) {
  91. send("221\r\n");
  92. stop = true;
  93. } else if (!data) {
  94. send("500 5.5.1 Command unrecognized: \""
  95. + response + "\"\r\n");
  96. stop = true;
  97. } else {
  98. // sb.append(response + "\r\n");
  99. }
  100. } // while
  101. } catch (IOException ioe) {
  102. if (stop) {
  103. // asked to stop, so ignore the exception and move on
  104. return null;
  105. }
  106. throw new BuildException(ioe);
  107. } finally {
  108. disconnect();
  109. }
  110. return null;
  111. }
  112. private void send(String retmsg) throws IOException {
  113. out.write(retmsg);
  114. out.flush();
  115. sb.append(retmsg);
  116. }
  117. public void disconnect() {
  118. this.stop = true;
  119. if (out != null) {
  120. try {
  121. out.flush();
  122. out.close();
  123. out = null;
  124. } catch (IOException e) {
  125. // ignore
  126. }
  127. }
  128. if (in != null) {
  129. try {
  130. in.close();
  131. in = null;
  132. } catch (IOException e) {
  133. // ignore
  134. }
  135. }
  136. if (sock != null) {
  137. try {
  138. sock.close();
  139. sock = null;
  140. } catch (IOException e) {
  141. // ignore
  142. }
  143. }
  144. if (ssock != null) {
  145. try {
  146. ssock.close();
  147. ssock = null;
  148. } catch (IOException e) {
  149. // ignore
  150. }
  151. }
  152. }
  153. public synchronized String getResult() {
  154. this.stop = true;
  155. return sb.toString();
  156. }
  157. /**
  158. * Starts and returns a new dummy mail server to be used in tests
  159. *
  160. * @return
  161. */
  162. public static DummyMailServer startMailServer() {
  163. return startMailServer("localhost");
  164. }
  165. /**
  166. * Starts and returns a new dummy mail server to be used in tests
  167. *
  168. * @param host The host on which the mail server will open a server socket to listen on
  169. * @return
  170. */
  171. public static DummyMailServer startMailServer(final String host) {
  172. final int port = TestHelper.getMaybeAvailablePort();
  173. final DummyMailServer mailServer = new DummyMailServer(host, port);
  174. final Thread thread = new Thread(mailServer);
  175. thread.setDaemon(true);
  176. thread.start();
  177. return mailServer;
  178. }
  179. }