Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / core / ftp / FTPDefaultListService.java
blob3e30cb02c35cb0b20c8276ce3a3ea3fe11721be0
1 package ch.cyberduck.core.ftp;
3 /*
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
20 import ch.cyberduck.core.AttributedList;
21 import ch.cyberduck.core.HostPasswordStore;
22 import ch.cyberduck.core.ListProgressListener;
23 import ch.cyberduck.core.ListService;
24 import ch.cyberduck.core.LoginCallback;
25 import ch.cyberduck.core.Path;
26 import ch.cyberduck.core.exception.BackgroundException;
27 import ch.cyberduck.core.ftp.parser.CompositeFileEntryParser;
29 import org.apache.commons.net.ftp.FTPReply;
31 import java.io.IOException;
32 import java.util.List;
34 /**
35 * @version $Id$
37 public class FTPDefaultListService implements ListService {
39 private final FTPSession session;
41 private final FTPListService.Command command;
43 private final FTPDataResponseReader reader;
45 private final HostPasswordStore keychain;
47 private final LoginCallback prompt;
49 public FTPDefaultListService(final FTPSession session, final HostPasswordStore keychain, final LoginCallback prompt,
50 final CompositeFileEntryParser parser, final FTPListService.Command command) {
51 this.session = session;
52 this.keychain = keychain;
53 this.prompt = prompt;
54 this.command = command;
55 this.reader = new FTPListResponseReader(parser, false);
58 @Override
59 public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
60 try {
61 if(!(FTPReply.isPositiveCompletion(session.getClient().cwd(directory.getAbsolute())))) {
62 throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString());
64 if(!session.getClient().setFileType(FTPClient.ASCII_FILE_TYPE)) {
65 // Set transfer type for traditional data socket file listings. The data transfer is over the
66 // data connection in type ASCII or type EBCDIC.
67 throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString());
69 final List<String> list = new FTPDataFallback(session, keychain, prompt).data(new DataConnectionAction<List<String>>() {
70 @Override
71 public List<String> execute() throws BackgroundException {
72 try {
73 return session.getClient().list(command.getCommand(), command.getArg());
75 catch(IOException e) {
76 throw new FTPExceptionMappingService().map(e);
79 }, listener);
80 return reader.read(directory, list, listener);
82 catch(IOException e) {
83 throw new FTPExceptionMappingService().map("Listing directory {0} failed", e, directory);