]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/TestEmailProvider.java
Stabelize Test email sender
[gigi.git] / src / org / cacert / gigi / email / TestEmailProvider.java
1 package org.cacert.gigi.email;
2
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.net.InetAddress;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import java.util.Properties;
10
11 class TestEmailProvider extends EmailProvider {
12         ServerSocket servs;
13         Socket client;
14         DataOutputStream out;
15         DataInputStream in;
16         protected TestEmailProvider(Properties props) {
17                 try {
18                         servs = new ServerSocket(Integer.parseInt(props
19                                         .getProperty("emailProvider.port")), 10,
20                                         InetAddress.getByName("127.0.0.1"));
21                 } catch (IOException e) {
22                         e.printStackTrace();
23                 }
24         }
25         @Override
26         public synchronized void sendmail(String to, String subject,
27                         String message, String from, String replyto, String toname,
28                         String fromname, String errorsto, boolean extra) throws IOException {
29                 while (true) {
30                         assureLocalConnection();
31                         try {
32                                 out.writeUTF("mail");
33                                 write(to);
34                                 write(subject);
35                                 write(message);
36                                 write(from);
37                                 write(replyto);
38                                 out.flush();
39                                 return;
40                         } catch (IOException e) {
41                                 client = null;
42                         }
43                 }
44         }
45         private void assureLocalConnection() throws IOException {
46                 if (out != null) {
47                         try {
48                                 out.writeUTF("ping");
49                         } catch (IOException e) {
50                                 client = null;
51                         }
52                 }
53                 if (client == null || client.isClosed()) {
54                         client = servs.accept();
55                         out = new DataOutputStream(client.getOutputStream());
56                         in = new DataInputStream(client.getInputStream());
57                 }
58         }
59         @Override
60         public synchronized String checkEmailServer(int forUid, String address)
61                         throws IOException {
62                 while (true) {
63                         assureLocalConnection();
64                         try {
65                                 out.writeUTF("challengeAddrBox");
66                                 out.writeUTF(address);
67                                 return in.readUTF();
68                         } catch (IOException e) {
69                                 client = null;
70                         }
71                 }
72         }
73
74         private void write(String to) throws IOException {
75                 if (to == null) {
76                         out.writeUTF("<null>");
77                 } else {
78                         out.writeUTF(to);
79                 }
80         }
81
82 }