001package org.hl7.fhir.utilities; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.IOException; 006 007import org.apache.commons.net.ftp.FTP; 008import org.apache.commons.net.ftp.FTPReply; 009import org.hl7.fhir.exceptions.FHIRException; 010import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 011import org.hl7.fhir.utilities.settings.FhirSettings; 012import org.slf4j.Logger; 013import org.slf4j.LoggerFactory; 014 015import lombok.AccessLevel; 016import lombok.AllArgsConstructor; 017import lombok.Getter; 018 019public class FTPClient { 020 021 private static final Logger logger = LoggerFactory.getLogger(FTPClient.class); 022 023 private final org.apache.commons.net.ftp.FTPClient clientImpl; 024 025 @Getter 026 private long createRemotePathIfNotExistsNanos; 027 028 @Getter 029 private long storeFileTimeNanos; 030 031 @Getter 032 private long deleteFileTimeNanos; 033 034 @Getter 035 private final String server; 036 037 @Getter 038 private final String path; 039 040 private String resolvedPath = null; 041 042 @Getter 043 private final String user; 044 045 @Getter 046 private final String password; 047 048 @Getter 049 private final int port; 050 051 @Getter 052 private final String remoteSeparator; 053 054 @AllArgsConstructor(access = AccessLevel.PROTECTED) 055 private class FTPReplyCodeAndString { 056 @Getter 057 private final int replyCode; 058 059 @Getter 060 private final String replyString; 061 062 public String toString() 063 { 064 return "Reply code: " + replyCode + " Message: " + replyString; 065 } 066 } 067 068 /** 069 * Connect to an FTP server 070 * @param server - the server to connect to (usually just an IP address). It's up to the system to figure out access (VPN etc) 071 * @param path - the path on the FTP server to treat all the operations as relative to 072 * @param user - username for the FTP server 073 * @param password - password for the FTP server 074 */ 075 public FTPClient(String server, String path, String user, String password) { 076 this (server, -1, path, user, password); 077 } 078 079 protected FTPClient(String server, int port, String path, String user, String password) { 080 this.server = server; 081 this.port = port; 082 this.remoteSeparator = "/"; 083 this.path = buildPath(path); 084 085 this.user = user; 086 this.password = password; 087 088 clientImpl = new org.apache.commons.net.ftp.FTPClient(); 089 } 090 091 private String buildPath(String path) { 092 if (path.length() == 0) { 093 return ""; 094 } 095 if (path.endsWith(remoteSeparator)) 096 { 097 return path; 098 } 099 return path + remoteSeparator; 100 } 101 102 /** 103 * Connect to the server, throw an exception if it fails 104 */ 105 public void connect() throws IOException { 106 if (FhirSettings.isProhibitNetworkAccess()) { 107 throw new FHIRException("Network Access is prohibited in this context"); 108 } 109 110 if (port != -1) { 111 logger.debug("Connecting to : " + server + ":" + port); 112 clientImpl.connect(server, port); 113 logger.debug("Connected"); 114 } 115 else { 116 logger.debug("Connecting to : " + server); 117 clientImpl.connect(server); 118 logger.debug("Connected"); 119 } 120 121 clientImpl.login(user, password); 122 123 throwExceptionForNegativeCompletion("FTP server could not connect.", true); 124 125 resetTimers(); 126 127 clientImpl.setFileType(FTP.BINARY_FILE_TYPE); 128 clientImpl.enterLocalPassiveMode(); 129 130 logger.debug("Setting initial working directory: " + clientImpl.printWorkingDirectory()); 131 132 clientImpl.changeWorkingDirectory(path); 133 134 throwExceptionForNegativeCompletion("FTP server could not establish default working directory", true); 135 logger.debug("Set initial working directory."); 136 137 logger.debug("Resolving remote resolved path."); 138 resolvedPath = clientImpl.printWorkingDirectory(); 139 140 logger.debug("Resolved remote resolved path: " + resolvedPath); 141 } 142 143 public void setBufferSize(int bufferSize) { 144 clientImpl.setBufferSize(bufferSize); 145 } 146 147 public int getBufferSize() { 148 return clientImpl.getBufferSize(); 149 } 150 151 private void resetTimers() { 152 this.createRemotePathIfNotExistsNanos = 0; 153 this.storeFileTimeNanos = 0; 154 this.deleteFileTimeNanos = 0; 155 } 156 157 /** 158 * Delete a file on the FTP server 159 * 160 * @param path - relative to the path provided in the constructor 161 */ 162 public void delete(String path) throws IOException { 163 String resolvedPath = resolveRemotePath(path); 164 logger.debug("Deleting remote file: " + resolvedPath); 165 long startTime = System.nanoTime(); 166 clientImpl.deleteFile(resolvedPath); 167 this.deleteFileTimeNanos += System.nanoTime() - startTime; 168 throwExceptionForNegativeCompletion("Error deleting file.", false); 169 logger.debug("Deleted remote file: " + resolvedPath); 170 } 171 172 /** 173 * Takes a file path and creates all intermediate directories if they do not yet exist. 174 * @param filePath relative to the path provided in the constructor and including the file name 175 * @throws IOException 176 */ 177 protected void createRemotePathIfNotExists(String filePath) throws IOException { 178 long startTime = System.nanoTime(); 179 String[] subPath = filePath.split(remoteSeparator); 180 try { 181 for (int i = 0 ; i < subPath.length - 1; i++){ 182 if (subPath[i].isEmpty() ) { 183 continue; 184 } 185 boolean exists = clientImpl.changeWorkingDirectory(subPath[i]); 186 if (!exists) { 187 logger.debug("Creating non-existent directory: " + clientImpl.printWorkingDirectory() + remoteSeparator + subPath[i] + " Creating"); 188 clientImpl.makeDirectory(subPath[i]); 189 throwExceptionForNegativeCompletion("Creating directory:", true); 190 logger.debug("Created directory: " + subPath[i]); 191 192 logger.debug("Changing to created directory: " + subPath[i]); 193 clientImpl.changeWorkingDirectory(subPath[i]); 194 throwExceptionForNegativeCompletion("Changing to directory:", true); 195 logger.debug("Changed to directory: " + subPath[i]); 196 } 197 }} catch (IOException e) { 198 throw new IOException("Error creating remote path: " + filePath, e); 199 } finally { 200 logger.debug("Changing to original directory: " + this.resolvedPath); 201 clientImpl.changeWorkingDirectory(this.resolvedPath); 202 logger.debug("Changed to original directory: " + this.resolvedPath); 203 } 204 this.createRemotePathIfNotExistsNanos += System.nanoTime() - startTime; 205 } 206 207 protected boolean remotePathExists(String path) throws IOException { 208 boolean output; 209 try { 210 output = clientImpl.changeWorkingDirectory(path); 211 } finally { 212 clientImpl.changeWorkingDirectory(this.resolvedPath); 213 } 214 return output; 215 } 216 217 private String resolveRemotePath(String path) { 218 if (path.startsWith(remoteSeparator)) { 219 throw new IllegalArgumentException("Absolute remote path is not permitted. Path: " + path); 220 } 221 return String.join(remoteSeparator, path.replace(File.separator, remoteSeparator)); 222 } 223 224 /** 225 * Upload a file from the local system to the FTP Server 226 * @param source - absolute path on local system 227 * @param path - relative to the path provided in the constructor 228 */ 229 public void upload(String source, String path) throws IOException { 230 String resolvedPath = resolveRemotePath(path); 231 logger.debug("Uploading file to remote path: " + resolvedPath); 232 233 attemptUpload(source, resolvedPath); 234 235 FTPReplyCodeAndString reply = getFTPReplyCodeAndString(); 236 if (FTPReply.isPositiveCompletion(reply.replyCode)) { 237 logger.debug("Uploaded file: " + resolvedPath); 238 return; 239 } else if (possibleDirectoryNotExistsCode(reply)) { 240 logger.debug("Uploading failed with reply: " + reply); 241 createRemotePathIfNotExists(resolvedPath); 242 attemptUpload(source, resolvedPath); 243 throwExceptionForNegativeCompletion("Error uploading file (second attempt).", false); 244 logger.debug("Uploaded file after path creation: " + resolvedPath); 245 return; 246 } 247 248 throwExceptionForNegativeCompletion(reply,"Error uploading file.", false); 249 logger.debug("Remote file uploaded: " + resolvedPath); 250 } 251 252 253 private boolean possibleDirectoryNotExistsCode(FTPReplyCodeAndString reply) { 254 /* 255 Note: This code may be 550 on some servers (IIS) and 553 on others (mockftpserver). 256 */ 257 return reply.replyCode == 550 || reply.replyCode == 553; 258 } 259 260 private void attemptUpload(String source, String resolvedPath) throws IOException { 261 final long startTime = System.nanoTime(); 262 FileInputStream localStream = ManagedFileAccess.inStream(source); 263 try { 264 clientImpl.storeFile(resolvedPath, localStream); 265 } finally { 266 localStream.close(); 267 } 268 this.storeFileTimeNanos += System.nanoTime() - startTime; 269 } 270 271 private FTPReplyCodeAndString getFTPReplyCodeAndString() { 272 final int replyCode = clientImpl.getReplyCode(); 273 final String replyString = clientImpl.getReplyString(); 274 275 return new FTPReplyCodeAndString(replyCode, replyString); 276 } 277 278 private void throwExceptionForNegativeCompletion(String localErrorMessage, boolean disconnectOnError) throws IOException { 279 FTPReplyCodeAndString reply = getFTPReplyCodeAndString(); 280 throwExceptionForNegativeCompletion(reply, localErrorMessage, disconnectOnError); 281 } 282 283 private void throwExceptionForNegativeCompletion(FTPReplyCodeAndString reply, String localErrorMessage, boolean disconnectOnError) throws IOException { 284 if (FTPReply.isPositiveCompletion(reply.replyCode)) { 285 return; 286 } 287 288 if (disconnectOnError) { 289 clientImpl.disconnect(); 290 } 291 throw new IOException(localErrorMessage + " " + reply); 292 } 293 294 public void disconnect() throws IOException { 295 clientImpl.disconnect(); 296 } 297 298 299 public static void main(String[] args) throws IOException, FHIRException { 300 FTPClient ftp = new FTPClient(getNamedParam(args, "-upload-server"), getNamedParam(args, "-upload-path"), getNamedParam(args, "-upload-user"), getNamedParam(args, "-upload-password")); 301 ftp.connect(); 302 ftp.upload("/Users/grahamegrieve/temp/test.xml", "testing/test.xml"); 303 ftp.delete("testing/test.xml"); 304 ftp.disconnect(); 305 } 306 private static String getNamedParam(String[] args, String param) { 307 boolean found = false; 308 for (String a : args) { 309 if (found) 310 return a; 311 if (a.equals(param)) { 312 found = true; 313 } 314 } 315 return null; 316 } 317 318 319}