org.openstatic.smtp.PlaceboSmtpServer.java Source code

Java tutorial

Introduction

Here is the source code for org.openstatic.smtp.PlaceboSmtpServer.java

Source

/*
Copyright (C) 2010 Brian Dunigan
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.openstatic.smtp;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Hashtable;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.PrintStream;
import java.io.OutputStream;
import org.json.*;

public class PlaceboSmtpServer extends Thread {
    private int port;
    private String myName;
    private String domain;
    private PrintStream debug_stream;
    private boolean debug_mode;
    private boolean show_data;
    private boolean keep_running;
    private ServerSocket ss;
    private int routing_mode;
    private LinkedBlockingQueue<SmtpMessage> message_queue;

    public static void main(String[] args) {
        PlaceboSmtpServer smtp = new PlaceboSmtpServer(args[0]);
        smtp.setDebug(true);
        smtp.start();

        try {
            while (true) {
                SmtpMessage nm = smtp.getNextMessage();
                System.err.println(nm.toJSONObject().toString());
            }
        } catch (Exception e) {
            System.err.println(e.toString() + "/" + e.getMessage());
            e.printStackTrace(System.err);
        }
    }

    public PlaceboSmtpServer(String domain) {
        this(25, domain);
    }

    public PlaceboSmtpServer(int port, String domain) {
        this.routing_mode = 0;
        this.port = port;
        this.myName = "Placebo:" + String.valueOf(this.port);
        this.domain = domain;
        this.debug_stream = System.err;
        this.keep_running = true;
        this.message_queue = new LinkedBlockingQueue<SmtpMessage>();
    }

    public void setPort(int value) {
        this.port = value;
        this.myName = "Placebo:" + String.valueOf(this.port);
    }

    public void setDomain(String dm) {
        this.domain = dm;
    }

    public String getDomain() {
        return this.domain;
    }

    public void run() {
        ss = null;
        try {
            ss = new ServerSocket(this.port);
            logln(this.myName, "Init on port " + String.valueOf(this.port));
        } catch (Exception n) {
            logln(this.myName, "Init Failed on port " + String.valueOf(this.port) + " / " + n.getMessage());
        }
        logln(this.myName, "Entering Server Loop");
        this.keep_running = true;
        while (this.keep_running) {
            try {
                logln(this.myName, "Waiting for next Socket....");
                Socket new_connection = ss.accept();
                logln(this.myName, "Connection Accepted " + new_connection.getInetAddress().getCanonicalHostName());
                SmtpRequestThread rt = new SmtpRequestThread(new_connection, this);
                rt.start();
            } catch (Exception x) {
                logln(this.myName, x.toString() + " / " + x.getMessage());
                x.printStackTrace(System.err);
            }
        }
    }

    public void queueMessage(SmtpMessage new_message) {
        try {
            this.message_queue.put(new_message);
        } catch (Exception e) {

        }
    }

    public SmtpMessage getNextMessage() {
        try {
            return message_queue.take();
        } catch (Exception e) {
            return null;
        }
    }

    public void setDebug(boolean value) {
        this.debug_mode = value;
    }

    public void setShowData(boolean value) {
        this.show_data = value;
    }

    public boolean isDebug() {
        return this.debug_mode;
    }

    public boolean isShowData() {
        return this.show_data;
    }

    public void setDebugStream(OutputStream value) {
        this.debug_stream = new PrintStream(value);
    }

    public void setDebugStream(PrintStream value) {
        this.debug_stream = value;
    }

    public PrintStream getDebugStream() {
        return this.debug_stream;
    }

    public void logln(String msg) {
        if (this.debug_mode) {
            this.debug_stream.println(msg.replaceAll("\r", "").replaceAll("\n", ""));
        }
    }

    public void logln(String caption, String msg) {
        if (this.debug_mode) {
            this.debug_stream.println("[" + caption + "]" + msg.replaceAll("\r", "").replaceAll("\n", ""));
        }
    }

    private static String setSizedString(String value, int size) {
        if (value == null) {
            return getPaddingSpace(size);
        } else if (value.length() == size) {
            return value;
        } else if (value.length() > size) {
            return value.substring(0, size);
        } else if (value.length() < size) {
            return value + getPaddingSpace(size - value.length());
        } else {
            return null;
        }
    }

    public void shutdown() {
        try {
            ss.close();
            this.keep_running = false;
            this.interrupt();
        } catch (Exception e) {
        }
    }

    private static String getPaddingSpace(int value) {
        StringBuffer x = new StringBuffer("");
        for (int n = 0; n < value; n++) {
            x.append(" ");
        }
        return x.toString();
    }
}