List of usage examples for org.apache.commons.vfs2 FileObject exists
boolean exists() throws FileSystemException;
From source file:org.wso2.carbon.connector.FileCopy.java
/** * Copy files/*from w ww .ja v a 2 s . co m*/ * * @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.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. *///from ww w . j av a2 s . c om 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.FileDelete.java
/** * Delete the file//from w w w .j a v a2s. co m * * @param source Location of the file * @param messageContext The message context that is generated for processing the file * @return Return the status */ private boolean deleteFile(String source, MessageContext messageContext) { boolean resultStatus = false; StandardFileSystemManager manager = null; try { manager = FileConnectorUtils.getManager(); // Create remote object FileObject remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (remoteFile.exists()) { if (remoteFile.getType() == FileType.FILE) { //delete a file remoteFile.delete(); } else if (remoteFile.getType() == FileType.FOLDER) { //delete folder remoteFile.delete(Selectors.SELECT_ALL); } resultStatus = true; } else { log.error("The file does not exist."); resultStatus = false; } if (log.isDebugEnabled()) { log.debug("File delete completed with. " + source); } } catch (IOException e) { handleException("Error occurs while deleting a file.", e, messageContext); } finally { if (manager != null) { manager.close(); } } return resultStatus; }
From source file:org.wso2.carbon.connector.FileDeleteConnector.java
/** * Delete an existing file/folder.// ww w .jav a 2s. c o m * * @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.FileExist.java
/** * Check is that file exists//from w w w . j a v a 2s .c om * * @param source Location of the file * @param messageContext The message context that is generated for processing the file * @return return a resultStatus */ private boolean isFileExist(String source, MessageContext messageContext) { boolean isFileExist = false; StandardFileSystemManager manager = null; FileSystemOptions opt = FileConnectorUtils.init(messageContext); try { manager = FileConnectorUtils.getManager(); // Create remote object FileObject remoteFile = manager.resolveFile(source, opt); if (remoteFile.exists()) { isFileExist = true; } if (log.isDebugEnabled()) { log.debug("File exist completed with. " + source); } } catch (IOException e) { isFileExist = false; handleException("Error while processing a file.", e, messageContext); } finally { if (manager != null) { manager.close(); } } return isFileExist; }
From source file:org.wso2.carbon.connector.FileExistConnector.java
/** * Determine if file/folder exists./* ww w .j ava 2 s. co m*/ * * @param messageContext The message context that is generated for processing the file. * @return true, if the file/folder exists. */ private boolean isFileExist(MessageContext messageContext) { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject remoteFile = null; try { FileSystemOptions opt = FileConnectorUtils.init(messageContext); // create remote fileObject remoteFile = manager.resolveFile(source, opt); if (!remoteFile.exists()) { return false; } if (log.isDebugEnabled()) { log.debug("File exist completed with. " + source); } } catch (FileSystemException e) { throw new SynapseException("Error while processing a file.", 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.FileList.java
public void list(MessageContext messageContext, String fileLocation) throws SynapseException { try {//from www .j a v a 2s .c o m FileSystemOptions opts = FTPSiteUtils.createDefaultOptions(); FileSystemManager manager = VFS.getManager(); // Create remote object FileObject remoteFile = manager.resolveFile(fileLocation, opts); if (remoteFile.exists()) { log.info("Reading a zip File."); // open the zip file InputStream input = remoteFile.getContent().getInputStream(); ZipInputStream zip = new ZipInputStream(input); OMFactory factory = OMAbstractFactory.getOMFactory(); String outputResult; OMNamespace ns = factory.createOMNamespace(FileConnectorConstants.FILECON, FileConnectorConstants.NAMESPACE); OMElement result = factory.createOMElement(FileConnectorConstants.RESULT, ns); ZipEntry zipEntry; // iterates over entries in the zip file while ((zipEntry = zip.getNextEntry()) != null) { if (!zipEntry.isDirectory()) { //add the entries outputResult = zipEntry.getName(); OMElement messageElement = factory.createOMElement(FileConnectorConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } } messageContext.getEnvelope().getBody().addChild(result); if (log.isDebugEnabled()) { log.info("The envelop body with the read files path is " + messageContext.getEnvelope().getBody().toString()); } //we must always close the zip file zip.close(); } else { log.error("Zip file does not exist."); } } catch (IOException e) { handleException("Unable to process the zip file", e, messageContext); } }
From source file:org.wso2.carbon.connector.FileListZip.java
/** * @param messageContext The message context that is generated for processing the file * @param source Location of the zip file *///from www . java2s .co m private void list(MessageContext messageContext, String source) { StandardFileSystemManager manager = null; try { manager = FileConnectorUtils.getManager(); ResultPayloadCreate resultPayload = new ResultPayloadCreate(); // Create remote object FileObject remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (remoteFile != null && remoteFile.exists()) { // open the zip file InputStream input = remoteFile.getContent().getInputStream(); ZipInputStream zip = new ZipInputStream(input); 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); ZipEntry zipEntry; // iterates over entries in the zip file while ((zipEntry = zip.getNextEntry()) != null) { if (!zipEntry.isDirectory()) { //add the entries outputResult = zipEntry.getName(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } } messageContext.getEnvelope().getBody().addChild(result); if (log.isDebugEnabled()) { log.debug("The envelop body with the read files path is " + messageContext.getEnvelope().getBody().toString()); } //we must always close the zip file zip.close(); if (log.isDebugEnabled()) { log.debug("File listZip completed with. " + source); } } else { log.error("Zip file does not exist."); } } catch (IOException e) { handleException("Error while processing a file.", e, messageContext); } finally { if (manager != null) { manager.close(); } } }
From source file:org.wso2.carbon.connector.FileListZipConnector.java
/** * List all the files inside zip file./* w w w .ja v a2 s. c o m*/ * * @param messageContext The message context that is generated for processing the file. */ private void listAllFiles(MessageContext messageContext) throws FileSystemException { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (!remoteFile.exists()) { log.error("Zip file location does not exist."); } // open the zip file InputStream input = remoteFile.getContent().getInputStream(); ZipInputStream zip = new ZipInputStream(input); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace(FileConstants.FILECON, FileConstants.NAMESPACE); OMElement result = factory.createOMElement(FileConstants.RESULT, ns); ZipEntry zipEntry; try { while ((zipEntry = zip.getNextEntry()) != null && !zipEntry.isDirectory()) { // add the entries String outputResult = zipEntry.getName(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } } catch (IOException e) { throw new SynapseException("Error while reading the next ZIP file entry", e); } finally { try { zip.close(); } catch (IOException e) { log.error("Error while closing ZipInputStream"); } try { remoteFile.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } // close the StandardFileSystemManager manager.close(); } ResultPayloadCreator.preparePayload(messageContext, result); if (log.isDebugEnabled()) { log.debug("The envelop body with the read files path is " + messageContext.getEnvelope().getBody().toString()); } }
From source file:org.wso2.carbon.connector.FileMove.java
/** * Move the files// w w w . j av a 2 s. c o m * * @param source Location of the file * @param destination Destination of the file * @return return a resultStatus */ private boolean moveFile(String source, String destination, MessageContext messageContext) { boolean resultStatus = false; StandardFileSystemManager manager = null; try { manager = FileConnectorUtils.getManager(); // Create remote object FileObject remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (remoteFile.exists()) { FileObject file = manager.resolveFile(destination, FileConnectorUtils.init(messageContext)); if (!file.exists()) { file.createFolder(); } if (remoteFile.getType() == FileType.FOLDER) { remoteFile.moveTo(file); } else if (remoteFile.getType() == FileType.FILE) { FileObject newFile = manager.resolveFile( destination + File.separator + remoteFile.getName().getBaseName(), FileConnectorUtils.init(messageContext)); remoteFile.moveTo(newFile); } resultStatus = true; if (log.isDebugEnabled()) { log.debug("File move completed from " + source + " to " + destination); } } else { log.error("The file/folder location does not exist."); resultStatus = false; } } catch (IOException e) { handleException("Unable to move a file/folder.", e, messageContext); } finally { if (manager != null) { manager.close(); } } return resultStatus; }