List of usage examples for org.apache.commons.vfs2 FileObject createFile
void createFile() throws FileSystemException;
From source file:org.wso2.carbon.connector.FileMoveConnector.java
/** * Move the file to the target directory. * * @param destination The target location of the folder to be moved. * @param remoteFile Location of the remote file. * @param manager Standard file system manager. * @param opts Configured file system options. * @throws FileSystemException On error parsing the file name, determining if the file exists and creating the * file/folder. */// www.j a v a 2s . c o m private void moveFile(String destination, FileObject remoteFile, StandardFileSystemManager manager, FileSystemOptions opts) throws FileSystemException { FileObject file = manager.resolveFile(destination, opts); if (FileConnectorUtils.isFolder(file)) { if (!file.exists()) { file.createFolder(); } file = manager.resolveFile(destination + File.separator + remoteFile.getName().getBaseName(), opts); } else if (!file.exists()) { file.createFile(); } remoteFile.moveTo(file); }
From source file:org.wso2.carbon.transport.file.connector.sender.VFSClientConnector.java
@Override public boolean send(CarbonMessage carbonMessage, CarbonCallback carbonCallback, Map<String, String> map) throws ClientConnectorException { FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true); String fileURI = map.get(Constants.FILE_URI); String action = map.get(Constants.ACTION); FileType fileType;//from w w w . ja v a 2s .c om ByteBuffer byteBuffer; InputStream inputStream = null; OutputStream outputStream = null; try { FileSystemManager fsManager = VFS.getManager(); FileObject path = fsManager.resolveFile(fileURI, opts); fileType = path.getType(); switch (action) { case Constants.CREATE: boolean isFolder = Boolean.parseBoolean(map.getOrDefault("create-folder", "false")); if (path.exists()) { throw new ClientConnectorException("File already exists: " + path.getName().getURI()); } if (isFolder) { path.createFolder(); } else { path.createFile(); } break; case Constants.WRITE: if (!path.exists()) { path.createFile(); path.refresh(); fileType = path.getType(); } if (fileType == FileType.FILE) { if (carbonMessage instanceof BinaryCarbonMessage) { BinaryCarbonMessage binaryCarbonMessage = (BinaryCarbonMessage) carbonMessage; byteBuffer = binaryCarbonMessage.readBytes(); } else { throw new ClientConnectorException("Carbon message received is not a BinaryCarbonMessage"); } byte[] bytes = byteBuffer.array(); if (map.get(Constants.APPEND) != null) { outputStream = path.getContent() .getOutputStream(Boolean.parseBoolean(map.get(Constants.APPEND))); } else { outputStream = path.getContent().getOutputStream(); } outputStream.write(bytes); outputStream.flush(); } break; case Constants.DELETE: if (path.exists()) { int filesDeleted = path.delete(Selectors.SELECT_ALL); if (logger.isDebugEnabled()) { logger.debug(filesDeleted + " files successfully deleted"); } } else { throw new ClientConnectorException( "Failed to delete file: " + path.getName().getURI() + " not found"); } break; case Constants.COPY: if (path.exists()) { String destination = map.get("destination"); FileObject dest = fsManager.resolveFile(destination, opts); dest.copyFrom(path, Selectors.SELECT_ALL); } else { throw new ClientConnectorException( "Failed to copy file: " + path.getName().getURI() + " not found"); } break; case Constants.MOVE: if (path.exists()) { //TODO: Improve this to fix issue #331 String destination = map.get("destination"); FileObject newPath = fsManager.resolveFile(destination, opts); FileObject parent = newPath.getParent(); if (parent != null && !parent.exists()) { parent.createFolder(); } if (!newPath.exists()) { path.moveTo(newPath); } else { throw new ClientConnectorException("The file at " + newPath.getURL().toString() + " already exists or it is a directory"); } } else { throw new ClientConnectorException( "Failed to move file: " + path.getName().getURI() + " not found"); } break; case Constants.READ: if (path.exists()) { //TODO: Do not assume 'path' always refers to a file inputStream = path.getContent().getInputStream(); byte[] bytes = toByteArray(inputStream); BinaryCarbonMessage message = new BinaryCarbonMessage(ByteBuffer.wrap(bytes), true); message.setProperty(org.wso2.carbon.messaging.Constants.DIRECTION, org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE); carbonMessageProcessor.receive(message, carbonCallback); } else { throw new ClientConnectorException( "Failed to read file: " + path.getName().getURI() + " not found"); } break; case Constants.EXISTS: TextCarbonMessage message = new TextCarbonMessage(Boolean.toString(path.exists())); message.setProperty(org.wso2.carbon.messaging.Constants.DIRECTION, org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE); carbonMessageProcessor.receive(message, carbonCallback); break; default: return false; } } catch (RuntimeException e) { throw new ClientConnectorException("Runtime Exception occurred : " + e.getMessage(), e); } catch (Exception e) { throw new ClientConnectorException("Exception occurred while processing file: " + e.getMessage(), e); } finally { closeQuietly(inputStream); closeQuietly(outputStream); } return true; }
From source file:org.wso2.carbon.transport.remotefilesystem.client.connector.contractimpl.VFSClientConnectorImpl.java
@Override public void send(RemoteFileSystemMessage message) { FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true); String fileURI = connectorConfig.get(Constants.URI); String action = connectorConfig.get(Constants.ACTION); FileType fileType;/* w ww .j av a 2 s.com*/ ByteBuffer byteBuffer; InputStream inputStream = null; OutputStream outputStream = null; FileObject path = null; try { FileSystemManager fsManager = VFS.getManager(); path = fsManager.resolveFile(fileURI, opts); fileType = path.getType(); switch (action) { case Constants.CREATE: boolean isFolder = Boolean .parseBoolean(connectorConfig.getOrDefault(Constants.CREATE_FOLDER, "false")); if (path.exists()) { throw new RemoteFileSystemConnectorException("File already exists: " + path.getName().getURI()); } if (isFolder) { path.createFolder(); } else { path.createFile(); } break; case Constants.WRITE: if (!path.exists()) { path.createFile(); path.refresh(); fileType = path.getType(); } if (fileType == FileType.FILE) { byteBuffer = message.getBytes(); byte[] bytes = byteBuffer.array(); if (connectorConfig.get(Constants.APPEND) != null) { outputStream = path.getContent() .getOutputStream(Boolean.parseBoolean(connectorConfig.get(Constants.APPEND))); } else { outputStream = path.getContent().getOutputStream(); } outputStream.write(bytes); outputStream.flush(); } break; case Constants.DELETE: if (path.exists()) { int filesDeleted = path.delete(Selectors.SELECT_ALL); if (logger.isDebugEnabled()) { logger.debug(filesDeleted + " files successfully deleted"); } } else { throw new RemoteFileSystemConnectorException( "Failed to delete file: " + path.getName().getURI() + " not found"); } break; case Constants.COPY: if (path.exists()) { String destination = connectorConfig.get(Constants.DESTINATION); FileObject dest = fsManager.resolveFile(destination, opts); dest.copyFrom(path, Selectors.SELECT_ALL); } else { throw new RemoteFileSystemConnectorException( "Failed to copy file: " + path.getName().getURI() + " not found"); } break; case Constants.MOVE: if (path.exists()) { //TODO: Improve this to fix issue #331 String destination = connectorConfig.get(Constants.DESTINATION); FileObject newPath = fsManager.resolveFile(destination, opts); FileObject parent = newPath.getParent(); if (parent != null && !parent.exists()) { parent.createFolder(); } if (!newPath.exists()) { path.moveTo(newPath); } else { throw new RemoteFileSystemConnectorException("The file at " + newPath.getURL().toString() + " already exists or it is a directory"); } } else { throw new RemoteFileSystemConnectorException( "Failed to move file: " + path.getName().getURI() + " not found"); } break; case Constants.READ: if (path.exists()) { //TODO: Do not assume 'path' always refers to a file inputStream = path.getContent().getInputStream(); byte[] bytes = toByteArray(inputStream); RemoteFileSystemMessage fileContent = new RemoteFileSystemMessage(ByteBuffer.wrap(bytes)); remoteFileSystemListener.onMessage(fileContent); } else { throw new RemoteFileSystemConnectorException( "Failed to read file: " + path.getName().getURI() + " not found"); } break; case Constants.EXISTS: RemoteFileSystemMessage fileContent = new RemoteFileSystemMessage(Boolean.toString(path.exists())); remoteFileSystemListener.onMessage(fileContent); break; default: break; } remoteFileSystemListener.done(); } catch (RemoteFileSystemConnectorException | IOException e) { remoteFileSystemListener.onError(e); } finally { if (path != null) { try { path.close(); } catch (FileSystemException e) { //Do nothing. } } closeQuietly(inputStream); closeQuietly(outputStream); } }
From source file:sf.net.experimaestro.connectors.SingleHostConnector.java
public FileObject getTemporaryFile(String prefix, String suffix) throws FileSystemException { FileObject tmpdir = getTemporaryDirectory(); final int MAX_ATTEMPTS = 1000; for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { FileObject child = tmpdir.resolveFile(prefix + RandomStringUtils.random(10, chars) + suffix); if (!child.exists()) { try { child.createFile(); } catch (Throwable t) { }/* w w w.j a v a2s . co m*/ return child; } } throw new FileSystemException( format("Could not find a proper temporary file name after %d attempts", MAX_ATTEMPTS)); }
From source file:tain.kr.test.vfs.v01.Shell.java
/** * Does a 'touch' command./*from w ww . j a v a 2 s. c om*/ */ private void touch(final String[] cmd) throws Exception { if (cmd.length < 2) { throw new Exception("USAGE: touch <path>"); } final FileObject file = mgr.resolveFile(cwd, cmd[1]); if (!file.exists()) { file.createFile(); } file.getContent().setLastModifiedTime(System.currentTimeMillis()); }
From source file:tain.kr.test.vfs.v01.TestVfs2FilehandleService.java
@Test public void testCreateFile() throws Exception { FileSystemManager manager = VFS.getManager(); FileObject baseDir = manager.resolveFile(this.absoluteFilePath); final FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt"); // delete a file file.delete(Selectors.SELECT_FILES); assertFalse(file.exists());//w w w. j a v a 2 s . c om // create a file file.createFile(); assertTrue(file.exists()); }
From source file:tain.kr.test.vfs.v01.TestVfs2FilehandleService.java
@Test public void testAccessFile() throws Exception { FileSystemManager manager = VFS.getManager(); FileObject baseDir = manager.resolveFile(this.absoluteFilePath); FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt"); // // w w w .j ava2 s . co m file.delete(Selectors.SELECT_FILES); assertFalse(file.exists()); // file.createFile(); assertTrue(file.exists()); FileContent fileContent = file.getContent(); assertEquals(0, fileContent.getSize()); // String string = "test."; OutputStream os = fileContent.getOutputStream(); try { os.write(string.getBytes()); os.flush(); } finally { if (os != null) { try { os.close(); } catch (Exception ignore) { // no-op } } } assertNotSame(0, fileContent.getSize()); // StringBuffer sb = new StringBuffer(); FileObject writtenFile = manager.resolveFile(baseDir, "testfolder/file1.txt"); FileContent writtenContents = writtenFile.getContent(); InputStream is = writtenContents.getInputStream(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = ""; while ((line = reader.readLine()) != null) { sb.append(line); } } finally { if (is != null) { try { is.close(); } catch (Exception ignore) { // no-op } } } // assertEquals(sb.toString(), string); }
From source file:tain.kr.test.vfs.v01.TestVfs2FilehandleService.java
@Test public void testCaching() throws Exception { String path = TestVfs2FilehandleService.class.getResource("").getPath(); if (flag)/*from w ww. ja v a2 s . c om*/ System.out.printf("[%s]\n", path); String testFolder = path + "/testfolder"; FileSystemManager manager = VFS.getManager(); FileObject scratchFolder = manager.resolveFile(testFolder); // testfolder scratchFolder.delete(Selectors.EXCLUDE_SELF); FileObject file = manager.resolveFile(path + "/testfolder/dummy.txt"); file.createFile(); // Manager DefaultFileSystemManager fs = new DefaultFileSystemManager(); fs.setFilesCache(manager.getFilesCache()); // zip, jar, tgz, tar, tbz2, file if (!fs.hasProvider("file")) { fs.addProvider("file", new DefaultLocalFileProvider()); } fs.setCacheStrategy(CacheStrategy.ON_RESOLVE); fs.init(); // FileObject foBase2 = fs.resolveFile(testFolder); if (flag) System.out.printf("## scratchFolder.getName().getPath() : %s\n", scratchFolder.getName().getPath()); FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath()); // FileObject[] fos = cachedFolder.getChildren(); assertFalse(contains(fos, "file1.txt")); // scratchFolder.resolveFile("file1.txt").createFile(); // // BUT cachedFolder fos = cachedFolder.getChildren(); assertFalse(contains(fos, "file1.txt")); // cachedFolder.refresh(); // fos = cachedFolder.getChildren(); assertTrue(contains(fos, "file1.txt")); }