KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > test > mock > util > MockSpamd


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.test.mock.util;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.ServerSocket JavaDoc;
27 import java.net.Socket JavaDoc;
28
29 /**
30  * This class can be used to run a mocked SPAMD daemom
31  */

32 public class MockSpamd implements Runnable JavaDoc {
33
34     /**
35      * Mailcontent which is 100% spam
36      */

37     public final static String JavaDoc GTUBE = "-SPAM-";
38
39     public final static String JavaDoc NOT_SPAM = "Spam: False ; 3 / 5";
40
41     public final static String JavaDoc SPAM = "Spam: True ; 1000 / 5";
42
43     BufferedReader JavaDoc in;
44
45     OutputStream JavaDoc out;
46
47     Socket JavaDoc spamd;
48
49     ServerSocket JavaDoc socket;
50
51     /**
52      * Init the mocked SPAMD daemon
53      *
54      * @param port The port on which the mocked SPAMD daemon will be bind
55      * @throws IOException
56      */

57     public MockSpamd(int port) throws IOException JavaDoc {
58         socket = new ServerSocket JavaDoc(port);
59     }
60
61     /**
62      * @see java.lang.Runnable#run()
63      */

64     public void run() {
65         boolean spam = false;
66         
67         try {
68
69             // Accept connections
70
spamd = socket.accept();
71
72             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(spamd
73                     .getInputStream()));
74             out = spamd.getOutputStream();
75
76             String JavaDoc line = null;
77
78             // Parse the message
79
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 JavaDoc e) {
98             // Should not happen
99
e.printStackTrace();
100         }
101
102     }
103 }
104
Popular Tags