import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.security.Key;
+import java.security.cert.Certificate;
import java.util.Properties;
/**
private DataInputStream in;
+ private EmailProvider target;
+
protected TestEmailProvider(Properties props) {
+ try {
+ String name = props.getProperty("emailProvider.test.target");
+ if (name != null) {
+ Class<?> c = Class.forName(name);
+ target = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(props);
+ }
+ } catch (ReflectiveOperationException e) {
+ e.printStackTrace();
+ }
try {
servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10, InetAddress.getByName("127.0.0.1"));
} catch (IOException e) {
@Override
public synchronized void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
while (true) {
- assureLocalConnection();
+ if ( !assureLocalConnection() && target != null) {
+ target.sendmail(to, subject, message, from, replyto, toname, fromname, errorsto, extra);
+ return;
+ }
try {
+ if (out == null) {
+ continue;
+ }
out.writeUTF("mail");
write(to);
write(subject);
}
}
- private void assureLocalConnection() throws IOException {
+ private boolean assureLocalConnection() throws IOException {
if (out != null) {
try {
out.writeUTF("ping");
}
}
if (client == null || client.isClosed()) {
- client = servs.accept();
+ servs.setSoTimeout(2000);
+ try {
+ client = servs.accept();
+ } catch (SocketTimeoutException e) {
+ return false;
+ }
out = new DataOutputStream(client.getOutputStream());
in = new DataInputStream(client.getInputStream());
}
+ return true;
}
@Override
public synchronized String checkEmailServer(int forUid, String address) throws IOException {
while (true) {
- assureLocalConnection();
+ if ( !assureLocalConnection() && target != null) {
+ return checkEmailServer(forUid, address);
+ }
try {
out.writeUTF("challengeAddrBox");
out.writeUTF(address);
}
}
+ @Override
+ protected void init(Certificate c, Key k) {
+ super.init(c, k);
+ if (target != null) {
+ target.init(c, k);
+ }
+ }
}