List of usage examples for org.apache.commons.net.io CopyStreamException getIOException
public IOException getIOException()
From source file:dk.netarkivet.common.distribute.ExtendedFTPRemoteFile.java
@Override public void appendTo(OutputStream out) { ArgumentNotValid.checkNotNull(out, "OutputStream out"); connectionManager.logOn();/*from w ww. j a va 2 s .c o m*/ try { if (!connectionManager.getFTPClient().retrieveFile(ftpFileName, out)) { final String msg = "Append operation from '" + ftpFileName + "' failed: " + connectionManager.getFtpErrorMessage(); log.warn(msg); throw new IOFailure(msg); } out.flush(); } catch (IOException e) { String msg = "Append operation from '" + ftpFileName + "' failed "; if (e instanceof CopyStreamException) { CopyStreamException realException = (CopyStreamException) e; msg += "(real cause = " + realException.getIOException() + ")"; } log.warn(msg, e); throw new IOFailure(msg, e); } finally { connectionManager.logOut(); cleanup(); } }
From source file:dk.netarkivet.common.distribute.ExtendedFTPRemoteFile.java
@Override public InputStream getInputStream() { connectionManager.logOn();/*from w ww . j a v a 2 s. c o m*/ try { InputStream in = connectionManager.getFTPClient().retrieveFileStream(ftpFileName); return new FilterInputStream(in) { public void close() throws IOException { try { super.close(); } finally { connectionManager.logOut(); cleanup(); } } }; } catch (IOException e) { String msg = "Creating inputstream from '" + ftpFileName + "' failed "; if (e instanceof CopyStreamException) { CopyStreamException realException = (CopyStreamException) e; msg += "(real cause = " + realException.getIOException() + ")"; } log.warn(msg, e); throw new IOFailure(msg, e); } }
From source file:dk.netarkivet.common.distribute.ExtendedFTPRemoteFile.java
/** * Creates a RemoteFile instance by uploading the content of the given record to a file on the ftp server. * * @param record The record to be copied. *//*from ww w . j a va 2s .c om*/ private ExtendedFTPRemoteFile(ArchiveRecord record) { this.record = record; this.name = UUID.randomUUID().toString(); this.ftpFileName = this.name; if (log.isDebugEnabled()) { log.debug("Created {} with name {}", this.getClass().getName(), toString()); } this.connectionManager = new FTPConnectionManager(Settings.get(FTP_USER_NAME), Settings.get(FTP_USER_PASSWORD), Settings.get(FTP_SERVER_NAME), Settings.getInt(FTP_SERVER_PORT), Settings.getInt(FTP_RETRIES_SETTINGS), Settings.getInt(FTP_DATATIMEOUT_SETTINGS)); connectionManager.logOn(); boolean success = false; int tried = 0; String message = null; while (!success && tried < FTP_RETRIES) { tried++; try { success = connectionManager.getFTPClient().storeFile(ftpFileName, record); if (!success) { log.debug("FTP store failed attempt '{}' of " + FTP_RETRIES + ": {}", tried, connectionManager.getFtpErrorMessage()); } } catch (IOException e) { message = "Write operation to '" + ftpFileName + "' failed on attempt " + tried + " of " + FTP_RETRIES; if (e instanceof CopyStreamException) { CopyStreamException realException = (CopyStreamException) e; message += "(real cause = " + realException.getIOException() + ")"; } log.debug(message, e); } } if (!success) { final String msg = "Failed to upload '" + name + "' after " + tried + " attempts. Reason for last failure: " + message; log.warn(msg); // Send an Notification because of this NotificationsFactory.getInstance().notify(msg, NotificationType.ERROR); throw new IOFailure(msg); } log.debug("Completed writing the file '{}'", ftpFileName); try { if (record != null) { record.close(); } } catch (IOException e) { // not a serious bug log.warn("Problem closing inputstream: ", e); } connectionManager.logOut(); log.debug("Ftp logout"); }
From source file:dk.netarkivet.common.distribute.FTPRemoteFile.java
/** * Write the contents of this ftp remote file to an output stream. Notice that while the checksum of the transferred * data is checked, no retries are performed, and in case of failure, there is no guarantee that any data have been * transferred.//from ww w .j av a 2 s . c o m * * @param out OutputStream that the data will be written to. This stream will not be closed by this operation. * @throws IOFailure If append operation fails */ @Override public void appendTo(OutputStream out) { ArgumentNotValid.checkNotNull(out, "OutputStream out"); if (filesize == 0) { return; } try { cm.logOn(); if (useChecksums) { out = new DigestOutputStream(out, ChecksumCalculator.getMessageDigest(ChecksumCalculator.MD5)); } if (!cm.getFTPClient().retrieveFile(ftpFileName, out)) { final String msg = "Append operation from '" + ftpFileName + "' failed: " + cm.getFtpErrorMessage(); log.warn(msg); throw new IOFailure(msg); } out.flush(); if (useChecksums) { String newChecksum = ChecksumCalculator .toHex(((DigestOutputStream) out).getMessageDigest().digest()); if (checksum != null && !checksum.equals(newChecksum)) { final String msg = "Checksums of '" + ftpFileName + "' do not match! Should be " + checksum + " but was " + newChecksum; log.warn(msg); throw new IOFailure(msg); } } } catch (IOException e) { String msg = "Append operation from '" + ftpFileName + "' failed "; if (e instanceof CopyStreamException) { CopyStreamException realException = (CopyStreamException) e; msg += "(real cause = " + realException.getIOException() + ")"; } log.warn(msg, e); throw new IOFailure(msg, e); } finally { cm.logOut(); if (!multipleDownloads) { cleanup(); } } }
From source file:dk.netarkivet.common.distribute.FTPRemoteFile.java
/** * An implementation of the getInputStream operation that works with FTP. Notice that most of the special work * (logging out and checking MD5) happens in the close() method of the returned InputStream, since that is the only * place where we can know we're done./* w w w. j a va 2s . co m*/ * * @return An InputStream that will deliver the data transferred by FTP. Holding on to this for long periods without * reading any data might cause a timeout. */ @Override public InputStream getInputStream() { if (filesize == 0) { return new ByteArrayInputStream(new byte[] {}); } try { cm.logOn(); InputStream in = cm.getFTPClient().retrieveFileStream(ftpFileName); if (in == null) { throw new IOFailure("Unable to retrieve input stream:" + cm.getFtpErrorMessage()); } if (useChecksums) { in = new DigestInputStream(in, ChecksumCalculator.getMessageDigest(ChecksumCalculator.MD5)); } return new FilterInputStream(in) { public void close() throws IOException { try { super.close(); if (useChecksums) { String newChecksum = ChecksumCalculator .toHex(((DigestInputStream) in).getMessageDigest().digest()); if (!newChecksum.equals(checksum)) { final String msg = "Checksums of '" + ftpFileName + "' do not match! " + "Should be " + checksum + " but was " + newChecksum; log.warn(msg); throw new IOFailure(msg); } } } finally { cm.logOut(); if (!multipleDownloads) { cleanup(); } } } }; } catch (IOException e) { String msg = "Creating inputstream from '" + ftpFileName + "' failed "; if (e instanceof CopyStreamException) { CopyStreamException realException = (CopyStreamException) e; msg += "(real cause = " + realException.getIOException() + ")"; } log.warn(msg, e); throw new IOFailure(msg, e); } }
From source file:dk.netarkivet.common.distribute.FTPRemoteFile.java
/** * Private constructor used by getInstance() static-method Tries to generate unique name on ftp-server. * * @param localFile File used to create new file on ftp-server. * @param useChecksums If true, checksums will be used to check transfers. * @param fileDeletable If true, this file will be deleted after upload to FTP. * @param multipleDownloads If true, the file will not be removed from FTP server automatically after first * download.//w ww . ja v a 2 s. c om * @param connectionParams If not null, contains connection parameters to the FTP-server desired by the user * @throws IOFailure if MD5 checksum fails, or ftp fails * @throws ArgumentNotValid if the local file cannot be read. */ private FTPRemoteFile(File localFile, boolean useChecksums, boolean fileDeletable, boolean multipleDownloads, RemoteFileSettings connectionParams) throws IOFailure { super(localFile, useChecksums, fileDeletable, multipleDownloads); if (connectionParams != null) { // use the connection parameters desired by the user. this.ftpServerName = connectionParams.getServerName(); this.ftpServerPort = connectionParams.getServerPort(); this.ftpUserName = connectionParams.getUserName(); this.ftpUserPassword = connectionParams.getUserPassword(); } else { // use the connection parameters specified by the settings. this.ftpServerName = Settings.get(CommonSettings.FTP_SERVER_NAME); this.ftpServerPort = Settings.getInt(CommonSettings.FTP_SERVER_PORT); this.ftpUserName = Settings.get(CommonSettings.FTP_USER_NAME); this.ftpUserPassword = Settings.get(CommonSettings.FTP_USER_PASSWORD); } this.cm = new FTPConnectionManager(ftpUserName, ftpUserPassword, ftpServerName, ftpServerPort, Settings.getInt(CommonSettings.FTP_RETRIES_SETTINGS), Settings.getInt(CommonSettings.FTP_DATATIMEOUT_SETTINGS)); if (filesize == 0) { if (useChecksums) { checksum = ChecksumCalculator.calculateMd5(file); } else { checksum = null; } ftpFileName = "-"; } else { // A large enough number to make it unlikely that two files are // created with the same FTP server name. Already the millisecond // datestamp reduces the likelihood, with this even if two // processes/threads try to upload the same file in the same // millisecond (very unlikely) they have only .01% chance of // clashing. final int aMagicNumber = 100000; ftpFileName = file.getName() + "-" + new Random().nextInt(aMagicNumber) + "-" + new Date().getTime(); InputStream in; try { in = new FileInputStream(localFile); } catch (FileNotFoundException e) { final String message = "Couldn't prepare file '" + localFile + "' for remote access. File not found."; log.debug(message, e); throw new IOFailure(message, e); } log.debug("Writing '{}' as '{}' on ftp-server {}", file.getName(), ftpFileName, cm.getFtpServer()); // Writing inlined in constructor to allow the checksum field to // be final (and thus must be set in constructor). try { cm.logOn(); if (useChecksums) { in = new DigestInputStream(in, ChecksumCalculator.getMessageDigest(ChecksumCalculator.MD5)); } boolean success = false; int tried = 0; String message = null; while (!success && tried < FTP_RETRIES) { tried++; try { success = cm.getFTPClient().storeFile(ftpFileName, in); if (!success) { log.debug("FTP store failed attempt '{}' of {}: {}", tried, FTP_RETRIES, cm.getFtpErrorMessage()); } } catch (IOException e) { message = "Write operation to '" + ftpFileName + "' failed on attempt " + tried + " of " + FTP_RETRIES; if (e instanceof CopyStreamException) { CopyStreamException realException = (CopyStreamException) e; message += "(real cause = " + realException.getIOException() + ")"; } log.debug(message, e); } } if (!success) { final String msg = "Failed to upload '" + localFile + "' after " + tried + " attempts. Reason for last failure: " + message; log.warn(msg); // Send an Notification because of this NotificationsFactory.getInstance().notify(msg, NotificationType.ERROR); throw new IOFailure(msg); } log.debug("Completed writing the file '{}'", ftpFileName); if (useChecksums) { checksum = ChecksumCalculator.toHex(((DigestInputStream) in).getMessageDigest().digest()); log.debug("Checksum of '{}' is:{}", ftpFileName, checksum); } else { checksum = null; } } finally { IOUtils.closeQuietly(in); cm.logOut(); log.debug("Ftp logout"); } } if (fileDeletable) { try { FileUtils.removeRecursively(localFile); } catch (IOFailure e) { // Not fatal log.warn("Couldn't remove tmp file {}", localFile, e); } } }