1 18 19 package org.apache.jmeter.protocol.ftp.sampler; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.OutputStreamWriter ; 28 import java.net.InetAddress ; 29 import java.net.ServerSocket ; 30 import java.net.Socket ; 31 32 import org.apache.jorphan.logging.LoggingManager; 33 import org.apache.log.Logger; 34 35 43 public class FtpClient 44 { 45 transient private static Logger log = LoggingManager.getLoggerForClass(); 46 BufferedWriter out; 48 BufferedReader in; 49 Socket s; 50 boolean passive = false; 51 static int port = 21; 52 static int dataPort = 4096; 53 54 57 public FtpClient() 58 { 59 } 60 61 66 public void setPassive(boolean flag) 67 { 68 passive = flag; 69 } 70 71 76 public String getResponse() throws IOException  77 { 78 StringBuffer response = new StringBuffer (); 79 String line = in.readLine(); 80 response.append(line); 81 log.info("FtpClient.getResponse(): #" + line + "#"); 82 while (line.charAt(3) == '-') 83 { 84 line = in.readLine(); 85 response.append("\n"); 86 response.append(line); 87 log.info("FtpClient.getResponse(): #" + line + "#"); 88 } 89 log.info("return response"); 90 return response.toString(); 91 } 92 93 96 public String get(String file) throws Exception  97 { 98 send("SYST"); 99 getResponse(); 100 send("PWD"); 101 getResponse(); 102 send("TYPE I"); 103 getResponse(); 104 String data = ""; 105 if (!passive) 106 { 107 dataPort++; 108 int upper = getUpper(dataPort); 109 int lower = getLower(dataPort); 110 String ip = 111 InetAddress.getLocalHost().getHostAddress().replace('.', ','); 112 String port = ip + "," + upper + "," + lower; 113 log.info("port:" + port); 114 send("PORT " + port); 115 getResponse(); 116 dataGrabber grab = new dataGrabber(ip, dataPort); 117 while (!grab.isPortCreated()) 118 { 119 } 120 send("RETR " + file); 121 String response = in.readLine(); 122 log.info(response); 123 log.info("" + dataPort); 124 data = "FTP client - File Not Found"; 125 if (!response.startsWith("5")) 126 { 127 while (!grab.isDone()) 128 { 129 } 130 data = grab.getData(); 131 } 132 } 133 else 134 { 135 send("PASV"); 136 String port = getResponse(); 137 while (!port.startsWith("227")) 138 { 139 port = getResponse(); 140 } 141 int start = port.indexOf('('); 142 int end = port.indexOf(')'); 143 port = port.substring(start + 1, end); 144 int a = port.indexOf(','); 145 int b = port.indexOf(',', a + 1); 146 int c = port.indexOf(',', b + 1); 147 int d = port.indexOf(',', c + 1); 148 int e = port.indexOf(',', d + 1); 149 String ip = 150 port.substring(0, a) 151 + "." 152 + port.substring(a + 1, b) 153 + "." 154 + port.substring(b + 1, c) 155 + "." 156 + port.substring(c + 1, d); 157 int upper = Integer.parseInt(port.substring(d + 1, e)); 158 int lower = Integer.parseInt(port.substring(e + 1)); 159 int dataPort = getPort(upper, lower); 160 send("RETR " + file); 161 dataGrabber grab = new dataGrabber(ip, dataPort); 162 getResponse(); 163 while (!grab.isDone()) 164 { 165 } 166 data = grab.getData(); 167 } 168 return data; 169 } 170 171 174 public void connect(String host, String username, String password) 175 throws Exception  176 { 177 InetAddress addr = InetAddress.getByName(host); 178 s = new Socket (addr, port); 179 out = new BufferedWriter (new OutputStreamWriter (s.getOutputStream())); 180 181 InputStreamReader isr = new InputStreamReader (s.getInputStream()); 182 in = new BufferedReader (isr); 183 send("USER " + username); 184 send("PASS " + password); 185 } 186 187 190 public void disconnect() 191 { 192 try 193 { 194 send("QUIT"); 195 getResponse(); 196 } 197 catch (Exception e) 198 { 199 log.error("FTP client - ", e); 200 } 201 try 202 { 203 in.close(); 204 out.close(); 205 s.close(); 206 } 207 catch (Exception e) 208 { 209 log.error("FTP client - ", e); 210 } 211 } 212 213 216 public void send(String command) throws IOException  217 { 218 for (int i = 0; i < command.length(); i++) 219 { 220 out.write(command.charAt(i)); 221 } 222 out.write('\r'); 223 out.write('\n'); 224 out.flush(); 225 } 226 227 231 public static int getPort(int upper, int lower) 232 { 233 return upper * 256 + lower; 234 } 235 236 240 public static int getUpper(int port) 241 { 242 return port / 256; 243 } 244 245 250 public static int getLower(int port) 251 { 252 return port % 256; 253 } 254 255 262 public class dataGrabber implements Runnable  263 { 264 StringBuffer buffer = new StringBuffer (); 265 Socket s; 266 boolean done = false; 267 boolean portCreated = false; 268 String host = ""; 269 int port = 22; 270 271 274 public dataGrabber(String host, int port) throws Exception  275 { 276 this.host = host; 277 this.port = port; 278 new Thread ((Runnable ) this).start(); 279 } 280 281 286 public boolean isDone() 287 { 288 return done; 289 } 290 291 296 public String getData() 297 { 298 return buffer.toString(); 299 } 300 301 306 public boolean isPortCreated() 307 { 308 return portCreated; 309 } 310 311 314 public void run() 315 { 316 try 317 { 318 if (passive) 319 { 320 s = new Socket (host, port); 321 } 322 else 323 { 324 log.info("creating socket on " + port); 325 ServerSocket server = new ServerSocket (port); 326 log.info("accepting..."); 327 portCreated = true; 328 s = server.accept(); 329 log.info("accepted"); 330 } 331 } 332 catch (Exception e) 333 { 334 } 335 try 336 { 337 InputStream in = s.getInputStream(); 338 BufferedInputStream dataIn = new BufferedInputStream (in); 339 int bufferSize = 4096; 340 byte[] inputBuffer = new byte[bufferSize]; 341 int i = 0; 342 while ((i = dataIn.read(inputBuffer, 0, bufferSize)) != -1) 343 { 344 buffer.append((char) i); 345 } 346 dataIn.close(); 347 s.close(); 348 } 349 catch (Exception e) 350 { 351 log.error("FTP client: dataGrabber", e); 352 } 353 done = true; 354 } 355 } 356 } 357
| Popular Tags
|