import javax.net.ssl.*;
import java.security.KeyStore;
import javax.security.cert.X509Certificate;
import java.io.*;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
/**
* Rudimentary command-line epp-client.
*/
public class EppClient {
private static final String SERVER = "epp.registry.tryout.be";
private static final int PORT = 33123;
private static final int TIME_OUT = 3000;
private static final String KEYSTORE_FILE = "epp-tryout.ks";
private static final String KEYSTORE_PWD = "test123";
private static final String logout =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<epp xmlns=\"urn:ietf:params:xml:ns:epp-1.0\">\n" +
" <command> <logout/> </command>\n" +
"</epp>\n";
private BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
private SSLSocket socket;
private DataInputStream dis;
private DataOutputStream dos;
public static void main(String[] args) throws Exception {
EppClient client = new EppClient();
client.run();
}
private String readOption (String message, String defaultvalue) throws Exception {
System.out.print(message + " [" + defaultvalue + "] : ");
String value = stdin.readLine();
if (value == null || value.length() == 0) {
return defaultvalue;
return value;
private int readOption (String message, int defaultvalue) throws Exception {
return Integer.parseInt(value);
private void run() throws Exception {
String server = readOption("Enter name of the server",SERVER);
int port = readOption("Enter remote port", PORT);
SSLSocketFactory factory = null;
SSLContext ctx = SSLContext.getInstance("TLS");
// add KeyManagers if server checks client certificates
ctx.init(null, getTrustManagers(), null);
factory = ctx.getSocketFactory();
socket = (SSLSocket)factory.createSocket();
int timeout = readOption ("Enter socket time-out", TIME_OUT);
socket.setSoTimeout(timeout);
SocketAddress addr = new InetSocketAddress(server,port);
socket.connect(addr,timeout);
System.out.println("Connected to " + socket.getRemoteSocketAddress());
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
X509Certificate chain[] = socket.getSession().getPeerCertificateChain();
for (int i = 0; i < chain.length; i++) {
System.out.println("peer-certificate " + i);
System.out.println(" Subject : " + chain[i].getSubjectDN().getName());
System.out.println(" Issuer : " + chain[i].getIssuerDN().getName());
pause("press <Enter> to read the initial greeting");
readEppString();
sendCommands();
System.out.println("closing the connection");
socket.close();
TrustManager[] getTrustManagers() throws Exception {
String filename = readOption("Enter path of the keystore file", KEYSTORE_FILE);
String password = readOption("Enter path of the keystore file", KEYSTORE_PWD);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load (
new FileInputStream(filename),
password.toCharArray()
);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init (keyStore);
return tmf.getTrustManagers();
private void sendCommands() throws Exception {
while (true) {
System.out.println("Please, enter one of the following:");
System.out.println(" STOP : to quit immediately");
System.out.println(" LOGOUT : to log out and quit");
System.out.println(" the name of an UTF-encoded file to send to the server");
String input = stdin.readLine();
if ("STOP".equals(input)) {
break;
} else
if ("LOGOUT".equals(input)) {
writeEppString(logout);
pause("press <Enter> to see the response");
else {
try {
String eppCommand = readFile(input);
writeEppString(eppCommand);
} catch (FileNotFoundException e) {
System.out.println(e);
System.out.println("============================");
private String readFile (String filename) throws Exception {
StringBuffer buffer = new StringBuffer();
BufferedReader file = new BufferedReader(new FileReader(filename));
String line;
while ((line = file.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
return buffer.toString();
} finally {
file.close();
private String readEppString () throws IOException {
int len = dis.readInt();
if (len > 4000) {
throw new IOException ("Indicated length is unlikely long: " + len);
System.out.println("length of input: " + len + " bytes");
len = len - 4;
byte bytes[] = new byte[len];
dis.readFully(bytes,0,len);
String input = new String(bytes,"UTF-8");
System.out.println("=========================================");
System.out.print (input);
return input;
private void writeEppString (String output) throws IOException {
byte[] bytes = output.getBytes("UTF-8");
int len = bytes.length + 4;
System.out.println("sending " + len + " bytes");
System.out.println("===================================");
System.out.print (output);
dos.writeInt(len);
dos.write(bytes);
private void pause(String msg) throws Exception {
System.out.print(msg);
System.in.read();
Please help.....