Added a new POPClient class with SSL support and also added SSL support to HttpServer.Added a DEBUG variable to control the output of FTPClient, POP3Client and SMTPClient.

This commit is contained in:
Ziver Koc 2008-12-05 15:13:26 +00:00
parent 354c459aa5
commit 2da05b24d5
4 changed files with 343 additions and 25 deletions

View file

@ -25,6 +25,8 @@ import zutil.MultiPrintStream;
* TODO: file info, rename, Active mode * TODO: file info, rename, Active mode
*/ */
public class FTPClient extends Thread{ public class FTPClient extends Thread{
public static boolean DEBUG = false;
public static final int FTP_ACTIVE = 0; public static final int FTP_ACTIVE = 0;
public static final int FTP_PASSIVE = 1; public static final int FTP_PASSIVE = 1;
public static final int FTP_PORT = 21; public static final int FTP_PORT = 21;
@ -90,8 +92,8 @@ public class FTPClient extends Thread{
readCommand(true); readCommand(true);
sendCommand("USER "+user); sendCommand("USER "+user);
sendNoReplyCommand("PASS "+pass, false); sendNoReplyCommand("PASS "+pass, false);
System.out.println("PASS ***"); if(DEBUG)System.out.println("PASS ***");
String tmp = readMultipleCommands(true); String tmp = readMultipleCommands(DEBUG);
if(parseReturnCode(tmp) == FTPC_LOGIN_NO){ if(parseReturnCode(tmp) == FTPC_LOGIN_NO){
close(); close();
throw new AccountException(tmp); throw new AccountException(tmp);
@ -112,7 +114,7 @@ public class FTPClient extends Thread{
* @throws IOException * @throws IOException
*/ */
public synchronized int sendCommand(String cmd) throws IOException{ public synchronized int sendCommand(String cmd) throws IOException{
return parseReturnCode(sendCommand(cmd, true)); return parseReturnCode(sendCommand(cmd, DEBUG));
} }
/** /**
@ -203,7 +205,7 @@ public class FTPClient extends Thread{
LinkedList<String> list = new LinkedList<String>(); LinkedList<String> list = new LinkedList<String>();
BufferedReader data_in = getDataInputStream(); BufferedReader data_in = getDataInputStream();
sendCommand("NLST "+path, true); sendCommand("NLST "+path, DEBUG);
String tmp = ""; String tmp = "";
while((tmp = data_in.readLine()) != null){ while((tmp = data_in.readLine()) != null){
@ -211,7 +213,7 @@ public class FTPClient extends Thread{
} }
data_in.close(); data_in.close();
readCommand(true); readCommand(DEBUG);
return list; return list;
} }
@ -237,7 +239,7 @@ public class FTPClient extends Thread{
} }
data_in.close(); data_in.close();
readCommand(true); readCommand(DEBUG);
return info.toString(); return info.toString();
} }
@ -250,10 +252,10 @@ public class FTPClient extends Thread{
*/ */
public void sendFile(String path, String data) throws IOException{ public void sendFile(String path, String data) throws IOException{
PrintStream data_out = getDataOutputStream(); PrintStream data_out = getDataOutputStream();
sendCommand("STOR "+path, true); sendCommand("STOR "+path, DEBUG);
data_out.println(data); data_out.println(data);
data_out.close(); data_out.close();
readCommand(true); readCommand(DEBUG);
} }
/** /**
@ -278,7 +280,7 @@ public class FTPClient extends Thread{
*/ */
private BufferedReader getFile(String path) throws IOException{ private BufferedReader getFile(String path) throws IOException{
BufferedReader ret = getDataInputStream(); BufferedReader ret = getDataInputStream();
sendCommand("RETR "+path, true); sendCommand("RETR "+path, DEBUG);
return ret; return ret;
} }
@ -297,7 +299,7 @@ public class FTPClient extends Thread{
while((tmp = file_in.readLine()) != null){ while((tmp = file_in.readLine()) != null){
file_out.println(tmp); file_out.println(tmp);
} }
readCommand(true); readCommand(DEBUG);
} }
/** /**
@ -440,7 +442,7 @@ public class FTPClient extends Thread{
* @throws IOException * @throws IOException
*/ */
private int setPassiveMode() throws IOException{ private int setPassiveMode() throws IOException{
String tmp = sendCommand("PASV", true); String tmp = sendCommand("PASV", DEBUG);
if(parseReturnCode(tmp) != FTPC_ENTERING_PASSIVE){ if(parseReturnCode(tmp) != FTPC_ENTERING_PASSIVE){
throw new IOException(tmp); throw new IOException(tmp);
} }
@ -479,7 +481,7 @@ public class FTPClient extends Thread{
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void close() throws IOException{ public void close() throws IOException{
sendCommand("QUIT", true); sendCommand("QUIT", DEBUG);
in.close(); in.close();
out.close(); out.close();
socket.close(); socket.close();

View file

@ -0,0 +1,317 @@
package zutil.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
/**
* A simple class that connects and logs in to a POP3
* server and then can read and delete messages.
* INFO: http://pages.prodigy.net/michael_santovec/pop3telnet.htm
*
* @author Ziver
*
*/
public class POP3Client {
public static boolean DEBUG = false;
public static final int POP3_PORT = 110;
public static final int POP3_SSL_PORT = 995;
private BufferedReader in;
private PrintStream out;
private Socket socket;
/**
* Connect to a POP3 server without username
*
* @param host The hostname of the server
* @throws UnknownHostException
* @throws IOException
*/
public POP3Client(String host) throws UnknownHostException, IOException{
this(host, POP3_PORT, null, null, false);
}
/**
* Connect to a POP3 server with username and password
*
* @param host The hostname of the server
* @param user The username
* @param password the password
* @throws UnknownHostException
* @throws IOException
*/
public POP3Client(String host, String user, String password) throws UnknownHostException, IOException{
this(host, POP3_PORT, user, password, false);
}
/**
* Connects to a POP3 server with username and password and SSL
*
* @param host The hostname of the server
* @param user The username
* @param password the password
* @param ssl If SSL should be used
* @throws UnknownHostException
* @throws IOException
*/
public POP3Client(String host, String user, String password, boolean ssl) throws UnknownHostException, IOException{
this(host, (ssl ? POP3_SSL_PORT : POP3_PORT), user, password, ssl);
}
/**
* Connects to a POP3 server with username and password and
* SSL and with costume port.
*
* @param host The hostname of the server
* @param port The port number to connect to on the server
* @param user The username
* @param password the password
* @param ssl If SSL should be used
* @throws UnknownHostException
* @throws IOException
*/
public POP3Client(String host, int port, String user, String password, boolean ssl) throws UnknownHostException, IOException{
if(ssl) connectSSL(host, port);
else connect(host, port);
if(user != null){
login(user, password);
}
}
/**
* Connects to the server
*
* @param host The hostname of the server
* @param port The port to connect to on the server
* @throws UnknownHostException
* @throws IOException
*/
private void connect(String host, int port) throws UnknownHostException, IOException{
socket = new Socket(host, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
readCommand(true);
}
/**
* Connects to the server with SSL.
* http://www.exampledepot.com/egs/javax.net.ssl/Client.html
*
* @param host The hostname of the server
* @param port The port to connect to on the server
* @throws UnknownHostException
* @throws IOException
*/
private void connectSSL(String host, int port) throws UnknownHostException, IOException{
SocketFactory socketFactory = SSLSocketFactory.getDefault();
socket = socketFactory.createSocket(host, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
readCommand(DEBUG);
}
/**
* Logs in to the POP3 server with the username and password if the password is set
*
* @param user The user name
* @param password The password or null if no password is required
* @throws IOException
*/
private void login(String user, String password) throws IOException{
sendCommand("USER "+user);
if(password != null){
sendNoReplyCommand("PASS "+password, false);
if(DEBUG)System.out.println("PASS ***");
readCommand(DEBUG);
}
}
/**
* Returns the number of messages that is on the server
*
* @return Message count
* @throws IOException
*/
public int getMessageCount() throws IOException{
String msg = sendCommand("STAT", DEBUG);
return Integer.parseInt(
msg.substring(
msg.indexOf(' ')+1,
msg.indexOf(' ', msg.indexOf(' ')+1)));
}
/**
* Retrieves the message with the given id.
*
* @param id The id of the message to get
* @return The message
* @throws IOException
*/
public String getMessage(int id) throws IOException{
sendCommand("RETR "+id);
return readMultipleLines(DEBUG);
}
/**
* Returns the title of the message
*
* @param id The message id
* @return The title
* @throws IOException
*/
public String getMessageTitle(int id) throws IOException{
String tmp = getMessageHeader(id);
String tmp2 = tmp.toLowerCase();
if(tmp2.contains("subject:")){
return tmp.substring(
tmp2.indexOf("subject:")+8,
tmp2.indexOf('\n',
tmp2.indexOf("subject:")));
}
else
return null;
}
/**
* Returns the header of the given message id.
*
* @param id The id of the message to get
* @return The message
* @throws IOException
*/
public String getMessageHeader(int id) throws IOException{
sendCommand("TOP "+id+" 0");
return readMultipleLines(DEBUG);
}
/**
* Deletes the message with the given id
*
* @param id The id of the message to be deleted
* @throws IOException
*/
public void delete(int id) throws IOException{
sendCommand("DELE "+id);
}
//*********************** IO Stuff *********************************************
/**
* Sends the given line to the server and returns a status integer
*
* @param cmd The command to send
* @return The return code from the server
* @throws IOException if the cmd fails
*/
private boolean sendCommand(String cmd) throws IOException{
return parseReturnCode(sendCommand(cmd, DEBUG));
}
/**
* Sends the given line to the server and returns the last line
*
* @param cmd The command to send
* @param print To print out the received lines
* @return Last String line from the server
* @throws IOException
*/
private String sendCommand(String cmd, boolean print) throws IOException{
sendNoReplyCommand(cmd, print);
return readCommand(print);
}
/**
* Sends a given command and don't cares about the reply
*
* @param cmd The command
* @param print If it should print to System.out
* @throws IOException
*/
private void sendNoReplyCommand(String cmd, boolean print) throws IOException{
out.println(cmd);
if(print)System.out.println(cmd);
}
/**
* Reads on line from the command channel
*
* @param print If the method should print the input line
* @return The input line
* @throws IOException if the server returns a error code
*/
private String readCommand(boolean print) throws IOException{
String tmp = in.readLine();
if(print)System.out.println(tmp);
if( !parseReturnCode(tmp) ) throw new IOException(tmp);
return tmp;
}
/**
* Reads from the server until there are a line with
* only one '.'
*
* @param print To print out the received lines
* @return String with the text
* @throws IOException
*/
private String readMultipleLines(boolean print) throws IOException{
StringBuffer msg = new StringBuffer();
String tmp = in.readLine();
while(!tmp.equals(".")){
msg.append(tmp);
msg.append('\n');
tmp = in.readLine();
if(print)System.out.println(tmp);
}
return msg.toString();
}
/**
* Parses the return line from the server and returns the status code
*
* @param msg The message from the server
* @return Returns true if return code is OK false if it is ERR
* @throws IOException
*/
private boolean parseReturnCode(String msg){
int endpos = (msg.indexOf(' ')<0 ? msg.length() : msg.indexOf(' '));
return msg.substring(0, endpos).equals("+OK");
}
//*********************************************************************************
/**
* All the delete marked messages are unmarkt
* @throws IOException
*/
public void reset() throws IOException{
sendCommand("RSET", DEBUG);
}
/**
* All the changes(DELETE) are performed and then the connection is closed
*
* @throws IOException
*/
public void close() throws IOException{
sendCommand("QUIT", DEBUG);
in.close();
out.close();
socket.close();
}
}

View file

@ -17,6 +17,7 @@ import java.util.Date;
* *
*/ */
public class SMTPClient { public class SMTPClient {
public static boolean DEBUG = false;
private BufferedReader in; private BufferedReader in;
private PrintStream out; private PrintStream out;
@ -52,13 +53,13 @@ public class SMTPClient {
sendCommand("RCPT TO:"+to); sendCommand("RCPT TO:"+to);
sendCommand("DATA"); sendCommand("DATA");
// The Message // The Message
sendNoReplyCommand("Date: "+(new Date()), true); sendNoReplyCommand("Date: "+(new Date()), DEBUG);
sendNoReplyCommand("From: "+from, true); sendNoReplyCommand("From: "+from, DEBUG);
sendNoReplyCommand("To: "+to, true); sendNoReplyCommand("To: "+to, DEBUG);
sendNoReplyCommand("Subject: "+subj, true); sendNoReplyCommand("Subject: "+subj, DEBUG);
sendNoReplyCommand(" ", true); sendNoReplyCommand(" ", DEBUG);
sendNoReplyCommand(msg, true); sendNoReplyCommand(msg, DEBUG);
sendCommand(".", true); sendCommand(".", DEBUG);
close(); close();
}catch(IOException e){ }catch(IOException e){
@ -78,7 +79,7 @@ public class SMTPClient {
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream()); out = new PrintStream(socket.getOutputStream());
readCommand(true); readCommand(DEBUG);
sendCommand("HELO "+url); sendCommand("HELO "+url);
} }
@ -90,7 +91,7 @@ public class SMTPClient {
* @throws IOException if the cmd fails * @throws IOException if the cmd fails
*/ */
private int sendCommand(String cmd) throws IOException{ private int sendCommand(String cmd) throws IOException{
return parseReturnCode(sendCommand(cmd, true)); return parseReturnCode(sendCommand(cmd, DEBUG));
} }
/** /**
@ -145,7 +146,7 @@ public class SMTPClient {
} }
public void close() throws IOException{ public void close() throws IOException{
sendCommand("QUIT", true); sendCommand("QUIT", DEBUG);
in.close(); in.close();
out.close(); out.close();
socket.close(); socket.close();

View file

@ -10,10 +10,8 @@ import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException; import java.security.NoSuchProviderException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap; import java.util.HashMap;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory; import javax.net.ssl.SSLServerSocketFactory;
import zutil.MultiPrintStream; import zutil.MultiPrintStream;
@ -131,7 +129,7 @@ public class HttpServer extends Thread{
* *
* @param certFile The cert file * @param certFile The cert file
*/ */
private void registerCertificate(File keyStore, String keyStorePass) throws CertificateException, IOException, KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException{ protected void registerCertificate(File keyStore, String keyStorePass) throws CertificateException, IOException, KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException{
System.setProperty("javax.net.ssl.keyStore", keyStore.getAbsolutePath()); System.setProperty("javax.net.ssl.keyStore", keyStore.getAbsolutePath());
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePass); System.setProperty("javax.net.ssl.keyStorePassword", keyStorePass);
} }