List of usage examples for org.apache.commons.net.ftp FTPClient getReplyCode
public int getReplyCode()
From source file:org.moxie.ftp.FTPTaskMirrorImpl.java
/** * Create the specified directory on the remote host. * * @param ftp The FTP client connection//from w w w . j a va 2 s.com * @param dir The directory to create (format must be correct for host * type) * @throws IOException in unknown circumstances * @throws BuildException if ignoreNoncriticalErrors has not been set to true * and a directory could not be created, for instance because it was * already existing. Precisely, the codes 521, 550 and 553 will trigger * a BuildException */ protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException { String workingDirectory = ftp.printWorkingDirectory(); if (task.isVerbose()) { if (dir.indexOf("/") == 0 || workingDirectory == null) { task.log("Creating directory: " + dir + " in /"); } else { task.log("Creating directory: " + dir + " in " + workingDirectory); } } if (dir.indexOf("/") == 0) { ftp.changeWorkingDirectory("/"); } String subdir = ""; StringTokenizer st = new StringTokenizer(dir, "/"); while (st.hasMoreTokens()) { subdir = st.nextToken(); task.log("Checking " + subdir, Project.MSG_DEBUG); if (!ftp.changeWorkingDirectory(subdir)) { if (!ftp.makeDirectory(subdir)) { // codes 521, 550 and 553 can be produced by FTP Servers // to indicate that an attempt to create a directory has // failed because the directory already exists. int rc = ftp.getReplyCode(); if (!(task.isIgnoreNoncriticalErrors() && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) { throw new BuildException("could not create directory: " + ftp.getReplyString()); } if (task.isVerbose()) { task.log("Directory already exists"); } } else { if (task.isVerbose()) { task.log("Directory created OK"); } ftp.changeWorkingDirectory(subdir); } } } if (workingDirectory != null) { ftp.changeWorkingDirectory(workingDirectory); } }
From source file:org.mule.transport.ftp.FtpConnectionFactory.java
public Object makeObject() throws Exception { FTPClient client = new FTPClient(); if (uri.getPort() > 0) { client.connect(uri.getHost(), uri.getPort()); } else {//from w w w. j a va2 s . c o m client.connect(uri.getHost()); } if (!FTPReply.isPositiveCompletion(client.getReplyCode())) { throw new IOException("Ftp connect failed: " + client.getReplyCode()); } if (!client.login(uri.getUser(), uri.getPassword())) { throw new IOException("Ftp login failed: " + client.getReplyCode()); } if (!client.setFileType(FTP.BINARY_FILE_TYPE)) { throw new IOException("Ftp error. Couldn't set BINARY transfer type: " + client.getReplyCode()); } return client; }
From source file:org.mule.transport.ftp.FtpConnector.java
/** * Creates a new FTPClient that logs in and changes the working directory using the data * provided in <code>endpoint</code>. *///from w w w. j a v a 2s. c o m protected FTPClient createFtpClient(ImmutableEndpoint endpoint) throws Exception { EndpointURI uri = endpoint.getEndpointURI(); FTPClient client = this.getFtp(uri); this.enterActiveOrPassiveMode(client, endpoint); this.setupFileType(client, endpoint); String path = uri.getPath(); // only change directory if one was configured if (StringUtils.isNotBlank(path)) { // MULE-2400: if the path begins with '~' we must strip the first '/' to make things // work with FTPClient if ((path.length() >= 2) && (path.charAt(1) == '~')) { path = path.substring(1); } if (!client.changeWorkingDirectory(path)) { throw new IOException(MessageFormat.format( "Failed to change working directory to {0}. Ftp error: {1}", path, client.getReplyCode())); } } return client; }
From source file:org.mule.transport.ftp.FtpMessageReceiver.java
protected FTPFile[] listFiles() throws Exception { FTPClient client = null; try {//from ww w . ja v a 2 s.co m client = connector.createFtpClient(endpoint); FTPListParseEngine engine = client.initiateListParsing(); FTPFile[] files = null; List<FTPFile> v = new ArrayList<FTPFile>(); while (engine.hasNext()) { if (getLifecycleState().isStopping()) { break; } files = engine.getNext(FTP_LIST_PAGE_SIZE); if (files == null || files.length == 0) { return files; } for (FTPFile file : files) { if (file.isFile()) { if (filenameFilter == null || filenameFilter.accept(null, file.getName())) { v.add(file); } } } } if (!FTPReply.isPositiveCompletion(client.getReplyCode())) { throw new IOException("Failed to list files. Ftp error: " + client.getReplyCode()); } return v.toArray(new FTPFile[v.size()]); } finally { if (client != null) { connector.releaseFtp(endpoint.getEndpointURI(), client); } } }
From source file:org.mule.transport.ftp.FtpMessageReceiver.java
protected void postProcess(FTPClient client, FTPFile file, MuleMessage message) throws Exception { if (!client.deleteFile(file.getName())) { throw new IOException(MessageFormat.format("Failed to delete file {0}. Ftp error: {1}", file.getName(), client.getReplyCode())); }// www . j a v a2 s.c om if (logger.isDebugEnabled()) { logger.debug("Deleted processed file " + file.getName()); } if (connector.isStreaming()) { if (!client.completePendingCommand()) { throw new IOException(MessageFormat.format( "Failed to complete a pending command. Retrieveing file {0}. Ftp error: {1}", file.getName(), client.getReplyCode())); } } }
From source file:org.mule.transport.ftp.FtpMessageRequester.java
protected void postProcess(FTPClient client, FTPFile file, MuleMessage message) throws Exception { if (!connector.isStreaming()) { if (!client.deleteFile(file.getName())) { throw new IOException(MessageFormat.format("Failed to delete file {0}. Ftp error: {1}", file.getName(), client.getReplyCode())); }/*from w w w . ja v a2s . co m*/ if (logger.isDebugEnabled()) { logger.debug("Deleted file " + file.getName()); } } }
From source file:org.mule.transport.ftp.FtpMessageRequester.java
protected FTPFile findFileToProcess(FTPClient client) throws Exception { FTPListParseEngine engine = client.initiateListParsing(); FTPFile[] files = null;/* w ww . j a v a 2 s . com*/ while (engine.hasNext()) { files = engine.getNext(FTP_LIST_PAGE_SIZE); if (files == null) { break; } FilenameFilter filenameFilter = getFilenameFilter(); for (int i = 0; i < files.length; i++) { FTPFile file = files[i]; if (file.isFile()) { if (filenameFilter.accept(null, file.getName())) { if (connector.validateFile(file)) { // only read the first one return file; } } } } } if (!FTPReply.isPositiveCompletion(client.getReplyCode())) { throw new IOException("Ftp error: " + client.getReplyCode()); } return null; }
From source file:org.opennms.systemreport.formatters.FtpSystemReportFormatter.java
@Override public void end() { m_zipFormatter.end();//from w w w . j a v a 2s . co m IOUtils.closeQuietly(m_outputStream); final FTPClient ftp = new FTPClient(); FileInputStream fis = null; try { if (m_url.getPort() == -1 || m_url.getPort() == 0 || m_url.getPort() == m_url.getDefaultPort()) { ftp.connect(m_url.getHost()); } else { ftp.connect(m_url.getHost(), m_url.getPort()); } if (m_url.getUserInfo() != null && m_url.getUserInfo().length() > 0) { final String[] userInfo = m_url.getUserInfo().split(":", 2); ftp.login(userInfo[0], userInfo[1]); } else { ftp.login("anonymous", "opennmsftp@"); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); LOG.error("FTP server refused connection."); return; } String path = m_url.getPath(); if (path.endsWith("/")) { LOG.error("Your FTP URL must specify a filename."); return; } File f = new File(path); path = f.getParent(); if (!ftp.changeWorkingDirectory(path)) { LOG.info("unable to change working directory to {}", path); return; } LOG.info("uploading {} to {}", f.getName(), path); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); fis = new FileInputStream(m_zipFile); if (!ftp.storeFile(f.getName(), fis)) { LOG.info("unable to store file"); return; } LOG.info("finished uploading"); } catch (final Exception e) { LOG.error("Unable to FTP file to {}", m_url, e); } finally { IOUtils.closeQuietly(fis); if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } }
From source file:org.openspice.vfs.ftp.FtpVFile.java
public InputStream inputStreamContents() { if (Print.wouldPrint(Print.VFS)) { Print.println("Trying to read contents of file " + this.path); Print.println("path = " + this.path); }/*from w w w . j a v a 2s . co m*/ final FTPClient ftpc = this.fvol.getConnectedFTPClient(); Print.println(Print.VFS, "Connected .... "); try { final ByteArrayOutputStream output = new ByteArrayOutputStream(); if (ftpc.retrieveFile(this.path, output)) { final String s = new String(output.toByteArray()); if (Print.wouldPrint(Print.VFS)) { Print.println("Got file: " + s.length()); } if (s.length() < 100) { if (Print.wouldPrint(Print.VFS)) Print.println("s = " + s); } return new ByteArrayInputStream(output.toByteArray()); } else { if (Print.wouldPrint(Print.VFS)) Print.println("reply code = " + ftpc.getReplyCode()); throw new Alert("Cannot retrieve file from FTP server").culprit("file", this.path).mishap(); } } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.oss.digitalforms.MobileFormsImpl.java
private void ftpValidations(String ftpHost, String user, String password) throws Exception { FTPClient ftp = new FTPClient(); ftp.connect(ftpHost);//w w w . j ava 2 s. co m int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); throw new Exception("Exception in connecting to FTP Server"); } ftp.login(user, password); ftp.listFiles(); ftp.disconnect(); }