List of usage examples for org.apache.commons.vfs2 FileObject getChildren
FileObject[] getChildren() throws FileSystemException;
From source file:org.wso2.carbon.connector.FileArchiveConnector.java
/** * Add the all files into List.//from w ww . java 2 s .c o m * * @param dir The source file/folder directory. * @param fileList List for adding the files. */ private void addAllFilesToList(FileObject dir, List<FileObject> fileList) { try { FileObject[] children = dir.getChildren(); for (FileObject child : children) { fileList.add(child); if (FileType.FOLDER.equals(child.getType())) { addAllFilesToList(child, fileList); } } } catch (FileSystemException e) { throw new SynapseException("Unable to add all files into List", e); } finally { try { dir.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } }
From source file:org.wso2.carbon.connector.FileArchives.java
/** * @param dir source file directory * @param fileList list of file inside directory * @param messageContext The message context that is generated for processing the file *///from w w w . j a va2 s.c o m private void getAllFiles(FileObject dir, List<FileObject> fileList, MessageContext messageContext) { try { FileObject[] children = dir.getChildren(); for (FileObject child : children) { fileList.add(child); if (child.getType() == FileType.FOLDER) { getAllFiles(child, fileList, messageContext); } } } catch (IOException e) { handleException("Unable to get all files", e, messageContext); } }
From source file:org.wso2.carbon.connector.FileCopy.java
/** * Copy files/*from www .j av a2 s . c om*/ * * @param source Location of the file * @param destination new file location * @param filePattern pattern of the file * @param messageContext The message context that is generated for processing the file * @param opts FileSystemOptions * @return return a resultStatus */ private boolean copyFile(String source, String destination, String filePattern, MessageContext messageContext, FileSystemOptions opts) { boolean resultStatus = false; StandardFileSystemManager manager = null; try { manager = FileConnectorUtils.getManager(); FileObject souFile = manager.resolveFile(source, opts); FileObject destFile = manager.resolveFile(destination, opts); if (StringUtils.isNotEmpty(filePattern)) { FileObject[] children = souFile.getChildren(); for (FileObject child : children) { if (child.getType() == FileType.FILE) { copy(source, destination, filePattern, opts); } else if (child.getType() == FileType.FOLDER) { String newSource = source + File.separator + child.getName().getBaseName(); copyFile(newSource, destination, filePattern, messageContext, opts); } } resultStatus = true; } else { if (souFile.exists()) { if (souFile.getType() == FileType.FILE) { InputStream fileIn = null; OutputStream fileOut = null; try { String name = souFile.getName().getBaseName(); FileObject outFile = manager.resolveFile(destination + File.separator + name, opts); //TODO make parameter sense fileIn = souFile.getContent().getInputStream(); fileOut = outFile.getContent().getOutputStream(); IOUtils.copyLarge(fileIn, fileOut); resultStatus = true; } catch (FileSystemException e) { log.error("Error while copying a file " + e.getMessage()); } finally { try { if (fileOut != null) { fileOut.close(); } } catch (Exception e) { log.error("Error while closing OutputStream: " + e.getMessage(), e); } try { if (fileIn != null) { fileIn.close(); } } catch (Exception e) { log.error("Error while closing InputStream: " + e.getMessage(), e); } } } else if (souFile.getType() == FileType.FOLDER) { destFile.copyFrom(souFile, Selectors.SELECT_ALL); resultStatus = true; } if (log.isDebugEnabled()) { log.debug("File copying completed from " + source + "to" + destination); } } else { log.error("The File Location does not exist."); resultStatus = false; } } return resultStatus; } catch (IOException e) { handleException("Unable to copy a file/folder", e, messageContext); } finally { if (manager != null) { manager.close(); } } return resultStatus; }
From source file:org.wso2.carbon.connector.FileCopy.java
/** * @param source file location//from ww w . j av a 2 s . c o m * @param destination target file location * @param filePattern pattern of the file * @param opts FileSystemOptions * @throws IOException */ private void copy(String source, String destination, String filePattern, FileSystemOptions opts) throws IOException { StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject souFile = manager.resolveFile(source, opts); FileObject[] children = souFile.getChildren(); FilePattenMatcher patternMatcher = new FilePattenMatcher(filePattern); for (FileObject child : children) { try { if (patternMatcher.validate(child.getName().getBaseName())) { String name = child.getName().getBaseName(); FileObject outFile = manager.resolveFile(destination + File.separator + name, opts); outFile.copyFrom(child, Selectors.SELECT_FILES); } } catch (IOException e) { log.error("Error occurred while copying a file. " + e.getMessage(), e); } } manager.close(); }
From source file:org.wso2.carbon.connector.FileCopyConnector.java
/** * Copy the file or folder from source to destination. * * @param messageContext The message context that is generated for processing the file. * @param opts FileSystemOptions. * @return return true, if file/folder is successfully copied. *///w w w . ja va 2 s .c o m private boolean copyFile(String source, MessageContext messageContext, FileSystemOptions opts) { String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.NEW_FILE_LOCATION); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); String includeParentDir = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.INCLUDE_PARENT_DIRECTORY); boolean includeParentDirectory = FileConstants.DEFAULT_INCLUDE_PARENT_DIRECTORY; if (StringUtils.isNotEmpty(includeParentDir)) { includeParentDirectory = Boolean.parseBoolean(includeParentDir); } boolean resultStatus = false; StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject souFile = null; FileObject destFile = null; try { souFile = manager.resolveFile(source, opts); destFile = manager.resolveFile(destination, opts); if (!souFile.exists()) { log.error("The File Location does not exist."); return false; } if (StringUtils.isNotEmpty(filePattern)) { FileObject[] children = souFile.getChildren(); for (FileObject child : children) { if (FileType.FILE.equals(child.getType())) { copy(child, destination, filePattern, opts, manager); } else if (FileType.FOLDER.equals(child.getType())) { String newSource = source + File.separator + child.getName().getBaseName(); copyFile(newSource, messageContext, opts); } } } else { if (FileType.FILE.equals(souFile.getType())) { String name = souFile.getName().getBaseName(); FileObject outFile = manager.resolveFile(destination + File.separator + name, opts); outFile.copyFrom(souFile, Selectors.SELECT_ALL); } else if (FileType.FOLDER.equals(souFile.getType())) { if (includeParentDirectory) { destFile = manager .resolveFile(destination + File.separator + souFile.getName().getBaseName(), opts); destFile.createFolder(); } destFile.copyFrom(souFile, Selectors.SELECT_ALL); } if (log.isDebugEnabled()) { log.debug("File copying completed from " + source + "to" + destination); } } } catch (FileSystemException e) { throw new SynapseException("Unable to copy a file/folder", e); } finally { manager.close(); } try { souFile.close(); destFile.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } return true; }
From source file:org.wso2.carbon.connector.FileDeleteConnector.java
/** * Delete an existing file/folder.// w w w . j av a 2s. c om * * @param messageContext The message context that is generated for processing the file. * @return true, if the file/folder is successfully deleted. */ private boolean deleteFile(MessageContext messageContext) { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject remoteFile = null; try { // create remote fileObject remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (!remoteFile.exists()) { log.error("The file does not exist."); return false; } if (FileType.FILE.equals(remoteFile.getType())) { // delete a file remoteFile.delete(); } else if (FileType.FOLDER.equals(remoteFile.getType())) { String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); if (StringUtils.isNotEmpty(filePattern) && !"*".equals(filePattern)) { FileObject[] children = remoteFile.getChildren(); for (FileObject child : children) { if (child.getName().getBaseName().matches(filePattern)) { child.delete(); } } } else { // delete folder remoteFile.delete(Selectors.SELECT_ALL); } } if (log.isDebugEnabled()) { log.debug("File delete completed with " + source); } } catch (FileSystemException e) { throw new SynapseException("Error while deleting file/folder", e); } finally { // close the StandardFileSystemManager manager.close(); try { if (remoteFile != null) { // close the file object remoteFile.close(); } } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } return true; }
From source file:org.wso2.carbon.connector.FileMoveConnector.java
/** * Move the folder to the target directory. * * @param destination The target location of the folder to move folder. * @param source Location of the source folder. * @param includeParentDirectory Boolean type to include the parent directory. * @param manager Standard file system manager. * @param opts Configured file system options. *///w w w . j a v a 2 s .c om private void moveFolder(String source, String destination, String filePattern, boolean includeParentDirectory, StandardFileSystemManager manager, FileSystemOptions opts) throws FileSystemException { FileObject remoteFile = manager.resolveFile(source, opts); FileObject file = manager.resolveFile(destination, opts); if (StringUtils.isNotEmpty(filePattern)) { FileObject[] children = remoteFile.getChildren(); for (FileObject child : children) { if (FileType.FILE.equals(child.getType())) { moveFileWithPattern(child, destination, filePattern, manager, opts); } else if (FileType.FOLDER.equals(child.getType())) { String newSource = source + File.separator + child.getName().getBaseName(); moveFolder(newSource, destination, filePattern, includeParentDirectory, manager, opts); } } } else if (includeParentDirectory) { FileObject destFile = manager .resolveFile(destination + File.separator + remoteFile.getName().getBaseName(), opts); destFile.createFolder(); remoteFile.moveTo(destFile); } else { if (!file.exists()) { file.createFolder(); } remoteFile.moveTo(file); remoteFile.createFolder(); } }
From source file:org.wso2.carbon.connector.FileRead.java
public void connect(MessageContext messageContext) { String fileLocation = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String contentType = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT_TYPE); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); FileObject fileObj = null; StandardFileSystemManager manager = null; try {//w w w .j ava 2 s . c o m manager = FileConnectorUtils.getManager(); fileObj = manager.resolveFile(fileLocation, FileConnectorUtils.init(messageContext)); if (fileObj.exists()) { if (fileObj.getType() == FileType.FOLDER) { FileObject[] children = fileObj.getChildren(); if (children == null || children.length == 0) { log.warn("Empty folder."); handleException("Empty folder.", messageContext); } else if (filePattern != null && !filePattern.trim().equals("")) { boolean bFound = false; for (FileObject child : children) { if (child.getName().getBaseName().matches(filePattern)) { fileObj = child; bFound = true; break; } } if (!bFound) { log.warn("File does not exists for the mentioned pattern."); handleException("File does not exists for the mentioned pattern.", messageContext); } } else { fileObj = children[0]; } } else if (fileObj.getType() != FileType.FILE) { log.warn("File does not exists, or an empty folder."); handleException("File does not exists, or an empty folder.", messageContext); } } else { log.warn("File/Folder does not exists"); handleException("File/Folder does not exists", messageContext); } ResultPayloadCreate.buildFile(fileObj, messageContext, contentType); if (log.isDebugEnabled()) { log.debug("File read completed." + fileLocation); } } catch (Exception e) { handleException(e.getMessage(), messageContext); } finally { try { // Close the File system if it is not already closed by the finally block of // processFile method if (fileObj != null && fileObj.getParent() != null && fileObj.getParent().getFileSystem() != null) { manager.closeFileSystem(fileObj.getParent().getFileSystem()); } } catch (FileSystemException warn) { // ignore the warning, since we handed over the stream close job to // AutoCloseInputStream.. } try { if (fileObj != null) { fileObj.close(); } } catch (Exception e) { // ignore the warning, since we handed over the stream close job to // AutoCloseInputStream.. } } }
From source file:org.wso2.carbon.connector.FileReadConnector.java
/** * Read the file content./*from w w w . ja va2s . c o m*/ * * @param messageContext The message context that is generated for processing the read operation. */ private void readFile(MessageContext messageContext) { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String contentType = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT_TYPE); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); boolean streaming = false; String enableStreamingParameter = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.ENABLE_STREAMING); if (StringUtils.isNotEmpty(enableStreamingParameter)) { streaming = Boolean.parseBoolean(enableStreamingParameter); } FileObject fileObjectToRead = null; StandardFileSystemManager manager = FileConnectorUtils.getManager(); try { FileObject rootFileObject = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (!rootFileObject.exists()) { log.error("File/Folder does not exists."); } if (FileType.FOLDER.equals(rootFileObject.getType())) { FileObject[] children = rootFileObject.getChildren(); if (children == null || children.length == 0) { log.error("Empty folder."); } else if (StringUtils.isNotEmpty(filePattern)) { for (FileObject child : children) { if (child.getName().getBaseName().matches(filePattern)) { fileObjectToRead = child; break; } } if (fileObjectToRead == null) { log.error("File does not exists for the mentioned pattern."); } } else { fileObjectToRead = children[0]; } } else if (FileType.FILE.equals(rootFileObject.getType())) { fileObjectToRead = rootFileObject; } else { log.error("File does not exists, or an empty folder"); } ResultPayloadCreator.buildFile(fileObjectToRead, messageContext, contentType, streaming); if (log.isDebugEnabled()) { log.debug("File read completed." + source); } } catch (FileSystemException e) { throw new SynapseException("Error while reading a file", e); } finally { try { // Close the File system if it is not already closed by the finally block of processFile method if (fileObjectToRead != null && fileObjectToRead.getParent() != null && fileObjectToRead.getParent().getFileSystem() != null) { manager.closeFileSystem(fileObjectToRead.getParent().getFileSystem()); } if (fileObjectToRead != null) { fileObjectToRead.close(); } } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } }
From source file:org.wso2.carbon.connector.FileSearch.java
/** * Generate the file search/*from w ww . java 2s . c om*/ * * @param source Location fo the file * @param filePattern Pattern of the file * @param recursiveSearch check whether recursively search or not * @param messageContext The message context that is processed by a handler in the handle method */ private void search(String source, String filePattern, String recursiveSearch, MessageContext messageContext) { ResultPayloadCreate resultPayload = new ResultPayloadCreate(); StandardFileSystemManager manager = null; if (StringUtils.isEmpty(filePattern)) { log.error("FilePattern should not be null"); } else { try { manager = FileConnectorUtils.getManager(); FileSystemOptions opt = FileConnectorUtils.init(messageContext); FileObject remoteFile = manager.resolveFile(source, opt); if (remoteFile.exists()) { FileObject[] children = remoteFile.getChildren(); OMFactory factory = OMAbstractFactory.getOMFactory(); String outputResult; OMNamespace ns = factory.createOMNamespace(FileConstants.FILECON, FileConstants.NAMESPACE); OMElement result = factory.createOMElement(FileConstants.RESULT, ns); resultPayload.preparePayload(messageContext, result); FilePattenMatcher fpm = new FilePattenMatcher(filePattern); recursiveSearch = recursiveSearch.trim(); for (FileObject child : children) { try { if (child.getType() == FileType.FILE && fpm.validate(child.getName().getBaseName().toLowerCase())) { outputResult = child.getName().getPath(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } else if (child.getType() == FileType.FOLDER && "true".equals(recursiveSearch)) { searchSubFolders(child, filePattern, messageContext, factory, result, ns); } } catch (IOException e) { handleException("Unable to search a file.", e, messageContext); } finally { try { if (child != null) { child.close(); } } catch (IOException e) { log.error("Error while closing Directory: " + e.getMessage(), e); } } } messageContext.getEnvelope().getBody().addChild(result); } else { log.error("File location does not exist."); } } catch (IOException e) { handleException("Unable to search a file.", e, messageContext); } finally { if (manager != null) { manager.close(); } } } }