1 19 20 package org.apache.james.test.mock.util; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStream ; 26 import java.net.ServerSocket ; 27 import java.net.Socket ; 28 29 32 public class MockSpamd implements Runnable { 33 34 37 public final static String GTUBE = "-SPAM-"; 38 39 public final static String NOT_SPAM = "Spam: False ; 3 / 5"; 40 41 public final static String SPAM = "Spam: True ; 1000 / 5"; 42 43 BufferedReader in; 44 45 OutputStream out; 46 47 Socket spamd; 48 49 ServerSocket socket; 50 51 57 public MockSpamd(int port) throws IOException { 58 socket = new ServerSocket (port); 59 } 60 61 64 public void run() { 65 boolean spam = false; 66 67 try { 68 69 spamd = socket.accept(); 71 72 in = new BufferedReader (new InputStreamReader (spamd 73 .getInputStream())); 74 out = spamd.getOutputStream(); 75 76 String line = null; 77 78 while ((line = in.readLine()) != null) { 80 if (line.indexOf(GTUBE) >= 0) { 81 spam = true; 82 } 83 } 84 if (spam) { 85 out.write(SPAM.getBytes()); 86 out.flush(); 87 } else { 88 out.write(NOT_SPAM.getBytes()); 89 out.flush(); 90 } 91 92 in.close(); 93 out.close(); 94 spamd.close(); 95 socket.close(); 96 97 } catch (IOException e) { 98 e.printStackTrace(); 100 } 101 102 } 103 } 104 | Popular Tags |