List of usage examples for org.springframework.integration.file.remote.session Session remove
boolean remove(String path) throws IOException;
From source file:com.aeg.ims.ftp.SftpTestUtils.java
public static void cleanUp(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { template.execute(new SessionCallback<LsEntry, Void>() { @Override//from w ww . j a v a 2 s . c o m public Void doInSession(Session<LsEntry> session) throws IOException { // TODO: avoid DFAs with Spring 4.1 (INT-3412) ChannelSftp channel = (ChannelSftp) new DirectFieldAccessor( new DirectFieldAccessor(session).getPropertyValue("targetSession")) .getPropertyValue("channel"); for (int i = 0; i < fileNames.length; i++) { try { session.remove("Test" + fileNames[i]); } catch (IOException e) { } } try { // should be empty channel.rmdir("Test"); } catch (SftpException e) { fail("Expected remote directory to be empty " + e.getMessage()); } return null; } }); } }
From source file:org.cruk.genologics.api.impl.GenologicsAPIImpl.java
@Override public void deleteAndRemoveFile(Linkable<GenologicsFile> file) throws IOException { if (file == null) { throw new IllegalArgumentException("file cannot be null"); }/* w w w .java 2s . com*/ GenologicsFile realFile; if (file instanceof GenologicsFile) { realFile = (GenologicsFile) file; if (realFile.getContentLocation() == null) { // Don't know where the actual file is, so fetch to get the full info. realFile = retrieve(file.getUri(), GenologicsFile.class); } } else { realFile = retrieve(file.getUri(), GenologicsFile.class); } URL targetURL = new URL(null, realFile.getContentLocation().toString(), NullURLStreamHandler.INSTANCE); if ("sftp".equalsIgnoreCase(targetURL.getProtocol())) { logger.info("Deleting file {} from file store on {}", targetURL.getPath(), targetURL.getHost()); checkFilestoreSet(); Session<LsEntry> session = filestoreSessionFactory.getSession(); try { session.remove(targetURL.getPath()); } catch (NestedIOException e) { // Don't want things to fail if the file doesn't exist on the file store, // just a warning. This handling code deals with this. try { if (e.getCause() != null) { throw e.getCause(); } else { // There is an error in line 71 of SftpSession, where instead of the // SftpException being the cause, its own message is appended to the // detail message for the outer exception with a +. // Bug raised with Spring Integrations as issue INT-3954. if ("Failed to remove file: 2: No such file".equals(e.getMessage())) { throw new SftpException(2, e.getMessage()); } throw e; } } catch (SftpException se) { // See if it's just a "file not found". if (se.id == 2) { logger.warn("File {} does not exist on {}", targetURL.getPath(), targetURL.getHost()); } else { throw e; } } catch (Throwable t) { throw e; } } finally { session.close(); } } else { logger.debug("File {} is not in the file store, so just removing its record.", targetURL.getPath()); } delete(realFile); }
From source file:org.opentestsystem.authoring.testitembank.service.impl.SftpFileTransferServiceImpl.java
private final int cleanDirectory(final String baseDirectory, final int maxAge, final Session<ChannelSftp.LsEntry> sftpSession) { final long maxAgeInMillis = Long.valueOf(maxAge) * MILLIS_IN_A_DAY; final long currentTimeInMillis = new Date().getTime(); int remainingFileCount = 0; try {// w ww . ja v a 2s . co m final LsEntry[] remoteFiles = sftpSession.list(baseDirectory); for (final LsEntry remoteFile : remoteFiles) { if (isSubDirectory(remoteFile)) { final int directoryFileCount = cleanDirectory(baseDirectory + "/" + remoteFile.getFilename(), maxAge, sftpSession); remainingFileCount += directoryFileCount; // if (!remoteFile.getFilename().startsWith(ImportSetServiceImpl.SFTP_TENANT_FOLDER_PREFIX) && (directoryFileCount == 0)) { // TODO - do we need to delete empty sub-directories? // TODO - we'll need a different SFTP library for that, or wait for Spring integration 4.1.0 which has rmdir() method // } } else if (!isDirectory(remoteFile)) { final long lastModifiedTimeInMillis = Long.valueOf(remoteFile.getAttrs().getMTime()) * 1000L; final long fileAgeInMillis = currentTimeInMillis - lastModifiedTimeInMillis; if (fileAgeInMillis > maxAgeInMillis) { sftpSession.remove(baseDirectory + "/" + remoteFile.getFilename()); } else { remainingFileCount++; } } } } catch (final IOException e) { LOGGER.error("unable to clean SFTP directory: " + baseDirectory, e); throw new TestItemBankException("sftp.clean.error", new String[] { baseDirectory }); } return remainingFileCount; }
From source file:org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.java
private void copyFileToLocalDirectory(String remoteDirectoryPath, F remoteFile, File localDirectory, Session<F> session) throws IOException { String remoteFileName = this.getFilename(remoteFile); String localFileName = this.generateLocalFileName(remoteFileName); String remoteFilePath = remoteDirectoryPath + remoteFileSeparator + remoteFileName; if (!this.isFile(remoteFile)) { if (logger.isDebugEnabled()) { logger.debug("cannot copy, not a file: " + remoteFilePath); }/*from w w w . ja v a 2 s . c o m*/ return; } File localFile = new File(localDirectory, localFileName); if (!localFile.exists()) { String tempFileName = localFile.getAbsolutePath() + this.temporaryFileSuffix; File tempFile = new File(tempFileName); InputStream inputStream = null; FileOutputStream fileOutputStream = new FileOutputStream(tempFile); try { session.read(remoteFilePath, fileOutputStream); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } else { throw new MessagingException("Failure occurred while copying from remote to local directory", e); } } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception ignored1) { } try { fileOutputStream.close(); } catch (Exception ignored2) { } } if (tempFile.renameTo(localFile)) { if (this.deleteRemoteFiles) { session.remove(remoteFilePath); if (logger.isDebugEnabled()) { logger.debug("deleted " + remoteFilePath); } } } } }
From source file:org.springframework.integration.sftp.outbound.SftpServerOutboundTests.java
@Test public void testInt3047ConcurrentSharedSession() throws Exception { final Session<?> session1 = this.sessionFactory.getSession(); final Session<?> session2 = this.sessionFactory.getSession(); final PipedInputStream pipe1 = new PipedInputStream(); PipedOutputStream out1 = new PipedOutputStream(pipe1); final PipedInputStream pipe2 = new PipedInputStream(); PipedOutputStream out2 = new PipedOutputStream(pipe2); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); Executors.newSingleThreadExecutor().execute(() -> { try {/* ww w.j a v a 2 s . co m*/ session1.write(pipe1, "foo.txt"); } catch (IOException e) { e.printStackTrace(); } latch1.countDown(); }); Executors.newSingleThreadExecutor().execute(() -> { try { session2.write(pipe2, "bar.txt"); } catch (IOException e) { e.printStackTrace(); } latch2.countDown(); }); out1.write('a'); out2.write('b'); out1.write('c'); out2.write('d'); out1.write('e'); out2.write('f'); out1.close(); out2.close(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); assertTrue(latch2.await(10, TimeUnit.SECONDS)); ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); session1.read("foo.txt", bos1); session2.read("bar.txt", bos2); assertEquals("ace", new String(bos1.toByteArray())); assertEquals("bdf", new String(bos2.toByteArray())); session1.remove("foo.txt"); session2.remove("bar.txt"); session1.close(); session2.close(); }