| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.mockftpserver.core.session; |
| 17 | |
|
| 18 | |
import org.slf4j.Logger; |
| 19 | |
import org.slf4j.LoggerFactory; |
| 20 | |
import org.mockftpserver.core.MockFtpServerException; |
| 21 | |
import org.mockftpserver.core.command.Command; |
| 22 | |
import org.mockftpserver.core.command.CommandHandler; |
| 23 | |
import org.mockftpserver.core.command.CommandNames; |
| 24 | |
import org.mockftpserver.core.socket.DefaultServerSocketFactory; |
| 25 | |
import org.mockftpserver.core.socket.DefaultSocketFactory; |
| 26 | |
import org.mockftpserver.core.socket.ServerSocketFactory; |
| 27 | |
import org.mockftpserver.core.socket.SocketFactory; |
| 28 | |
import org.mockftpserver.core.util.Assert; |
| 29 | |
import org.mockftpserver.core.util.AssertFailedException; |
| 30 | |
|
| 31 | |
import java.io.BufferedReader; |
| 32 | |
import java.io.ByteArrayOutputStream; |
| 33 | |
import java.io.IOException; |
| 34 | |
import java.io.InputStream; |
| 35 | |
import java.io.InputStreamReader; |
| 36 | |
import java.io.OutputStream; |
| 37 | |
import java.io.PrintWriter; |
| 38 | |
import java.io.Writer; |
| 39 | |
import java.net.InetAddress; |
| 40 | |
import java.net.ServerSocket; |
| 41 | |
import java.net.Socket; |
| 42 | |
import java.net.SocketTimeoutException; |
| 43 | |
import java.util.ArrayList; |
| 44 | |
import java.util.HashMap; |
| 45 | |
import java.util.List; |
| 46 | |
import java.util.Map; |
| 47 | |
import java.util.Set; |
| 48 | |
import java.util.StringTokenizer; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public class DefaultSession implements Session { |
| 57 | |
|
| 58 | 2 | private static final Logger LOG = LoggerFactory.getLogger(DefaultSession.class); |
| 59 | |
private static final String END_OF_LINE = "\r\n"; |
| 60 | |
protected static final int DEFAULT_CLIENT_DATA_PORT = 21; |
| 61 | |
|
| 62 | 130 | protected SocketFactory socketFactory = new DefaultSocketFactory(); |
| 63 | 130 | protected ServerSocketFactory serverSocketFactory = new DefaultServerSocketFactory(); |
| 64 | |
|
| 65 | |
private BufferedReader controlConnectionReader; |
| 66 | |
private Writer controlConnectionWriter; |
| 67 | |
private Socket controlSocket; |
| 68 | |
private Socket dataSocket; |
| 69 | |
ServerSocket passiveModeDataSocket; |
| 70 | |
private InputStream dataInputStream; |
| 71 | |
private OutputStream dataOutputStream; |
| 72 | |
private Map commandHandlers; |
| 73 | 130 | private int clientDataPort = DEFAULT_CLIENT_DATA_PORT; |
| 74 | |
private InetAddress clientHost; |
| 75 | |
private InetAddress serverHost; |
| 76 | 130 | private Map attributes = new HashMap(); |
| 77 | 130 | private volatile boolean terminate = false; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | 130 | public DefaultSession(Socket controlSocket, Map commandHandlers) { |
| 87 | 130 | Assert.notNull(controlSocket, "controlSocket"); |
| 88 | 129 | Assert.notNull(commandHandlers, "commandHandlers"); |
| 89 | |
|
| 90 | 128 | this.controlSocket = controlSocket; |
| 91 | 128 | this.commandHandlers = commandHandlers; |
| 92 | 128 | this.serverHost = controlSocket.getLocalAddress(); |
| 93 | 128 | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public InetAddress getClientHost() { |
| 102 | 195 | return controlSocket.getInetAddress(); |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public InetAddress getServerHost() { |
| 112 | 5 | return serverHost; |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public void sendReply(int code, String text) { |
| 123 | 368 | assertValidReplyCode(code); |
| 124 | |
|
| 125 | 367 | StringBuffer buffer = new StringBuffer(Integer.toString(code)); |
| 126 | |
|
| 127 | 367 | if (text != null && text.length() > 0) { |
| 128 | 366 | String replyText = text.trim(); |
| 129 | 366 | if (replyText.indexOf("\n") != -1) { |
| 130 | 3 | int lastIndex = replyText.lastIndexOf("\n"); |
| 131 | 3 | buffer.append("-"); |
| 132 | 164 | for (int i = 0; i < replyText.length(); i++) { |
| 133 | 161 | char c = replyText.charAt(i); |
| 134 | 161 | buffer.append(c); |
| 135 | 161 | if (i == lastIndex) { |
| 136 | 3 | buffer.append(Integer.toString(code)); |
| 137 | 3 | buffer.append(" "); |
| 138 | |
} |
| 139 | |
} |
| 140 | 3 | } else { |
| 141 | 363 | buffer.append(" "); |
| 142 | 363 | buffer.append(replyText); |
| 143 | |
} |
| 144 | |
} |
| 145 | 367 | LOG.debug("Sending Reply [" + buffer.toString() + "]"); |
| 146 | 367 | writeLineToControlConnection(buffer.toString()); |
| 147 | 367 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public void openDataConnection() { |
| 153 | |
try { |
| 154 | 38 | if (passiveModeDataSocket != null) { |
| 155 | 4 | LOG.debug("Waiting for (passive mode) client connection from client host [" + clientHost |
| 156 | |
+ "] on port " + passiveModeDataSocket.getLocalPort()); |
| 157 | |
|
| 158 | |
try { |
| 159 | 4 | dataSocket = passiveModeDataSocket.accept(); |
| 160 | 3 | LOG.debug("Successful (passive mode) client connection to port " |
| 161 | |
+ passiveModeDataSocket.getLocalPort()); |
| 162 | |
} |
| 163 | 1 | catch (SocketTimeoutException e) { |
| 164 | 1 | throw new MockFtpServerException(e); |
| 165 | 3 | } |
| 166 | |
} else { |
| 167 | 34 | Assert.notNull(clientHost, "clientHost"); |
| 168 | 33 | LOG.debug("Connecting to client host [" + clientHost + "] on data port [" + clientDataPort |
| 169 | |
+ "]"); |
| 170 | 33 | dataSocket = socketFactory.createSocket(clientHost, clientDataPort); |
| 171 | |
} |
| 172 | 36 | dataOutputStream = dataSocket.getOutputStream(); |
| 173 | 36 | dataInputStream = dataSocket.getInputStream(); |
| 174 | |
} |
| 175 | 0 | catch (IOException e) { |
| 176 | 0 | throw new MockFtpServerException(e); |
| 177 | 36 | } |
| 178 | 36 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public int switchToPassiveMode() { |
| 187 | |
try { |
| 188 | 8 | passiveModeDataSocket = serverSocketFactory.createServerSocket(0); |
| 189 | 8 | return passiveModeDataSocket.getLocalPort(); |
| 190 | |
} |
| 191 | 0 | catch (IOException e) { |
| 192 | 0 | throw new MockFtpServerException("Error opening passive mode server data socket", e); |
| 193 | |
} |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public void closeDataConnection() { |
| 200 | |
try { |
| 201 | 27 | LOG.debug("Flushing and closing client data socket"); |
| 202 | 27 | dataOutputStream.flush(); |
| 203 | 27 | dataOutputStream.close(); |
| 204 | 27 | dataInputStream.close(); |
| 205 | 27 | dataSocket.close(); |
| 206 | |
} |
| 207 | 0 | catch (IOException e) { |
| 208 | 0 | LOG.error("Error closing client data socket", e); |
| 209 | 27 | } |
| 210 | 27 | } |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
private void writeLineToControlConnection(String line) { |
| 218 | |
try { |
| 219 | 367 | controlConnectionWriter.write(line + END_OF_LINE); |
| 220 | 367 | controlConnectionWriter.flush(); |
| 221 | |
} |
| 222 | 0 | catch (IOException e) { |
| 223 | 0 | LOG.error("Error writing to control connection", e); |
| 224 | 0 | throw new MockFtpServerException("Error writing to control connection", e); |
| 225 | 367 | } |
| 226 | 367 | } |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public void close() { |
| 232 | 103 | LOG.trace("close()"); |
| 233 | 103 | terminate = true; |
| 234 | 103 | } |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
public void sendData(byte[] data, int numBytes) { |
| 240 | 21 | Assert.notNull(data, "data"); |
| 241 | |
try { |
| 242 | 20 | dataOutputStream.write(data, 0, numBytes); |
| 243 | |
} |
| 244 | 0 | catch (IOException e) { |
| 245 | 0 | throw new MockFtpServerException(e); |
| 246 | 20 | } |
| 247 | 20 | } |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public byte[] readData() { |
| 253 | 9 | return readData(Integer.MAX_VALUE); |
| 254 | |
} |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
public byte[] readData(int numBytes) { |
| 260 | 11 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 261 | 11 | int numBytesRead = 0; |
| 262 | |
try { |
| 263 | 431 | while (numBytesRead < numBytes) { |
| 264 | 430 | int b = dataInputStream.read(); |
| 265 | 430 | if (b == -1) { |
| 266 | 10 | break; |
| 267 | |
} |
| 268 | 420 | bytes.write(b); |
| 269 | 420 | numBytesRead++; |
| 270 | 420 | } |
| 271 | 11 | return bytes.toByteArray(); |
| 272 | |
} |
| 273 | 0 | catch (IOException e) { |
| 274 | 0 | throw new MockFtpServerException(e); |
| 275 | |
} |
| 276 | |
} |
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
Command readCommand() { |
| 286 | |
|
| 287 | 334 | final long socketReadIntervalMilliseconds = 20L; |
| 288 | |
|
| 289 | |
try { |
| 290 | |
while (true) { |
| 291 | 712 | if (terminate) { |
| 292 | 89 | return null; |
| 293 | |
} |
| 294 | |
|
| 295 | 623 | if (controlConnectionReader.ready()) { |
| 296 | 245 | String command = controlConnectionReader.readLine(); |
| 297 | 245 | LOG.info("Received command: [" + command + "]"); |
| 298 | 245 | return parseCommand(command); |
| 299 | |
} |
| 300 | |
try { |
| 301 | 378 | Thread.sleep(socketReadIntervalMilliseconds); |
| 302 | |
} |
| 303 | 0 | catch (InterruptedException e) { |
| 304 | 0 | throw new MockFtpServerException(e); |
| 305 | 378 | } |
| 306 | |
} |
| 307 | |
} |
| 308 | 0 | catch (IOException e) { |
| 309 | 0 | LOG.error("Read failed", e); |
| 310 | 0 | throw new MockFtpServerException(e); |
| 311 | |
} |
| 312 | |
} |
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
Command parseCommand(String commandString) { |
| 321 | 249 | Assert.notNullOrEmpty(commandString, "commandString"); |
| 322 | |
|
| 323 | 248 | List parameters = new ArrayList(); |
| 324 | |
String name; |
| 325 | |
|
| 326 | 248 | int indexOfFirstSpace = commandString.indexOf(" "); |
| 327 | 248 | if (indexOfFirstSpace != -1) { |
| 328 | 181 | name = commandString.substring(0, indexOfFirstSpace); |
| 329 | 181 | StringTokenizer tokenizer = new StringTokenizer(commandString.substring(indexOfFirstSpace + 1), |
| 330 | |
","); |
| 331 | 503 | while (tokenizer.hasMoreTokens()) { |
| 332 | 322 | parameters.add(tokenizer.nextToken()); |
| 333 | |
} |
| 334 | 181 | } else { |
| 335 | 67 | name = commandString; |
| 336 | |
} |
| 337 | |
|
| 338 | 248 | String[] parametersArray = new String[parameters.size()]; |
| 339 | 248 | return new Command(name, (String[]) parameters.toArray(parametersArray)); |
| 340 | |
} |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
public void setClientDataHost(InetAddress clientHost) { |
| 346 | 35 | this.clientHost = clientHost; |
| 347 | 35 | } |
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
public void setClientDataPort(int dataPort) { |
| 353 | 29 | this.clientDataPort = dataPort; |
| 354 | |
|
| 355 | |
|
| 356 | 29 | if (passiveModeDataSocket != null) { |
| 357 | |
try { |
| 358 | 1 | this.passiveModeDataSocket.close(); |
| 359 | |
} |
| 360 | 0 | catch (IOException e) { |
| 361 | 0 | throw new MockFtpServerException(e); |
| 362 | 1 | } |
| 363 | 1 | passiveModeDataSocket = null; |
| 364 | |
} |
| 365 | 29 | } |
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
public void run() { |
| 371 | |
try { |
| 372 | |
|
| 373 | 99 | InputStream inputStream = controlSocket.getInputStream(); |
| 374 | 99 | OutputStream outputStream = controlSocket.getOutputStream(); |
| 375 | 99 | controlConnectionReader = new BufferedReader(new InputStreamReader(inputStream)); |
| 376 | 99 | controlConnectionWriter = new PrintWriter(outputStream, true); |
| 377 | |
|
| 378 | 99 | LOG.debug("Starting the session..."); |
| 379 | |
|
| 380 | 99 | CommandHandler connectCommandHandler = (CommandHandler) commandHandlers.get(CommandNames.CONNECT); |
| 381 | 99 | connectCommandHandler.handleCommand(new Command(CommandNames.CONNECT, new String[0]), this); |
| 382 | |
|
| 383 | 433 | while (!terminate) { |
| 384 | 334 | readAndProcessCommand(); |
| 385 | |
} |
| 386 | |
} |
| 387 | 0 | catch (Exception e) { |
| 388 | 0 | LOG.error("Error:", e); |
| 389 | 0 | throw new MockFtpServerException(e); |
| 390 | |
} |
| 391 | |
finally { |
| 392 | 99 | LOG.debug("Cleaning up the session"); |
| 393 | |
try { |
| 394 | 99 | controlConnectionReader.close(); |
| 395 | 99 | controlConnectionWriter.close(); |
| 396 | |
} |
| 397 | 0 | catch (IOException e) { |
| 398 | 0 | LOG.error("Error:", e); |
| 399 | 99 | } |
| 400 | 99 | LOG.debug("Session stopped."); |
| 401 | 99 | } |
| 402 | 99 | } |
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
private void readAndProcessCommand() throws Exception { |
| 410 | |
|
| 411 | 334 | Command command = readCommand(); |
| 412 | 334 | if (command != null) { |
| 413 | 245 | String normalizedCommandName = Command.normalizeName(command.getName()); |
| 414 | 245 | CommandHandler commandHandler = (CommandHandler) commandHandlers.get(normalizedCommandName); |
| 415 | |
|
| 416 | 245 | if (commandHandler == null) { |
| 417 | 3 | commandHandler = (CommandHandler) commandHandlers.get(CommandNames.UNSUPPORTED); |
| 418 | |
} |
| 419 | |
|
| 420 | 245 | Assert.notNull(commandHandler, "CommandHandler for command [" + normalizedCommandName + "]"); |
| 421 | 245 | commandHandler.handleCommand(command, this); |
| 422 | |
} |
| 423 | 334 | } |
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
private void assertValidReplyCode(int replyCode) { |
| 431 | 368 | Assert.isTrue(replyCode > 0, "The number [" + replyCode + "] is not a valid reply code"); |
| 432 | 367 | } |
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
public Object getAttribute(String name) { |
| 443 | 157 | Assert.notNull(name, "name"); |
| 444 | 156 | return attributes.get(name); |
| 445 | |
} |
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
public void setAttribute(String name, Object value) { |
| 455 | 138 | Assert.notNull(name, "name"); |
| 456 | 137 | attributes.put(name, value); |
| 457 | 137 | } |
| 458 | |
|
| 459 | |
|
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
public Set getAttributeNames() { |
| 467 | 3 | return attributes.keySet(); |
| 468 | |
} |
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
public void removeAttribute(String name) { |
| 479 | 5 | Assert.notNull(name, "name"); |
| 480 | 4 | attributes.remove(name); |
| 481 | 4 | } |
| 482 | |
|
| 483 | |
} |