1 17 package org.columba.core.main; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.io.PrintWriter ; 23 import java.io.Writer ; 24 import java.net.Socket ; 25 26 import org.columba.api.exception.AuthenticationException; 27 import org.columba.core.versioninfo.VersionInfo; 28 29 37 public class ColumbaClient { 38 protected static final String NEWLINE = "\r\n"; 39 40 protected Socket socket; 41 42 protected Writer writer; 43 44 protected BufferedReader reader; 45 46 public ColumbaClient() { 47 } 48 49 54 public void connect() throws IOException , AuthenticationException { 55 socket = new Socket ("127.0.0.1", SessionController 56 .deserializePortNumber()); 57 writer = new PrintWriter (socket.getOutputStream()); 58 writer.write("Columba " + VersionInfo.getVersion()); 59 writer.write(NEWLINE); 60 writer.flush(); 61 62 writer 63 .write("User " 64 + System.getProperty("user.name", 65 ColumbaServer.ANONYMOUS_USER)); 66 writer.write(NEWLINE); 67 writer.flush(); 68 reader = new BufferedReader (new InputStreamReader (socket 69 .getInputStream())); 70 String response = reader.readLine(); 71 if (response.equals("WRONG USER")) { 72 throw new AuthenticationException(); 73 } 74 } 75 76 81 public void sendCommandLine(String [] args) throws IOException { 82 StringBuffer buf = new StringBuffer (); 83 84 for (int i = 0; i < args.length; i++) { 85 buf.append(args[i]); 86 buf.append('%'); 87 } 88 89 writer.write(buf.toString()); 90 writer.write(NEWLINE); 91 writer.flush(); 92 } 93 94 97 public void close() { 98 try { 99 if (writer != null) { 100 writer.close(); 101 } 102 if (reader != null) { 103 reader.close(); 104 } 105 if (socket != null) { 106 socket.close(); 107 } 108 } catch (IOException ioe) { 109 } 110 } 111 } 112 | Popular Tags |