1 14 15 package org.quickserver.net.qsadmin; 16 17 import java.io.*; 18 import java.net.*; 19 import java.util.logging.*; 20 21 41 public class QSAdminAPI { 42 private static Logger logger = Logger.getLogger(QSAdminAPI.class.getName()); 43 44 private String username = "Admin"; 45 private String password = "QsAdm1n"; 46 47 private String host = "localhost"; 48 private int port = 9877; 49 50 private Socket socket; 51 private InputStream in; 52 private OutputStream out; 53 private BufferedReader br; 54 private BufferedWriter bw; 55 56 60 public QSAdminAPI(String host, int port) { 61 this.host = host; 62 this.port = port; 63 } 64 65 68 public boolean logon() throws IOException { 69 return logon(username, password); 70 } 71 72 75 public boolean logon(String username, String password) 76 throws IOException { 77 this.username = username; 78 this.password = password; 79 80 logger.fine("Connecting to "+host+":"+port); 81 socket = new Socket(host, port); 82 in = socket.getInputStream(); 83 out = socket.getOutputStream(); 84 br = new BufferedReader(new InputStreamReader(in)); 85 bw = new BufferedWriter(new OutputStreamWriter(out)); 86 logger.fine("Got : "+br.readLine()); 87 logger.fine("Got : "+br.readLine()); 88 logger.fine("Got : "+br.readLine()); 89 90 logger.fine("Got : "+br.readLine()); 91 logger.fine("Sending username"); 92 bw.write(username+"\r\n"); 93 bw.flush(); 94 logger.fine("Got : "+br.readLine()); 95 96 logger.fine("Sending password"); 97 bw.write(password+"\r\n"); 98 bw.flush(); 99 100 String temp = br.readLine(); 101 logger.fine("Got : "+temp); 102 return temp.startsWith("+OK "); 103 } 104 105 108 public String sendCommand(String data) throws IOException { 109 logger.fine("Sending command : "+data); 110 bw.write(data+"\r\n"); 111 bw.flush(); 112 String temp = readResponse(); 113 logger.fine("Got : "+temp); 114 return temp; 115 } 116 117 private String readResponse() throws IOException { 118 StringBuffer command = new StringBuffer (); 119 String res = br.readLine(); 120 if(res!=null && res.equals("+OK info follows")==false) 121 return res; 122 while(res!=null && res.equals(".")==false) { 123 command.append(res + "\r\n"); 124 res = br.readLine(); 125 } 126 return command.toString(); 127 } 128 129 132 public void logoff() throws IOException { 133 logger.fine("Logging off"); 134 logger.fine("Sending command : quit"); 135 bw.write("quit"+"\r\n"); 136 bw.flush(); 137 logger.fine("Got : "+br.readLine()); 138 close(); 139 } 140 141 144 public void close() throws IOException { 145 logger.fine("Closing"); 146 socket.close(); 147 socket = null; 148 } 149 150 public static void main(String [] args) throws Exception { 151 QSAdminAPI qsAdminApi = new QSAdminAPI("127.0.0.1", 9080); 152 if(qsAdminApi.logon()) { 153 logger.info("Logged in"); 154 String info = qsAdminApi.sendCommand("info server"); 155 logger.info("Info on Server :\n"+info); 156 qsAdminApi.logoff(); 157 } else { 158 logger.warning("Bad Login!"); 159 qsAdminApi.close(); 160 } 161 } 162 } 163 | Popular Tags |