List of usage examples for org.apache.commons.vfs2 FileObject getContent
FileContent getContent() throws FileSystemException;
From source file:org.wso2.carbon.connector.FileArchiveConnector.java
/** * Extract all files to add the zip directory. * * @param fileObj Source fileObject. * @param directoryToZip Destination fileObject. * @param fileList List of files to be compressed. * @throws FileSystemException When get the OutputStream, get file type. *///ww w .j a v a 2 s . c o m private void writeZipFiles(FileObject fileObj, FileObject directoryToZip, List<FileObject> fileList) throws FileSystemException { ZipOutputStream zos = null; try { zos = new ZipOutputStream(directoryToZip.getContent().getOutputStream()); for (FileObject file : fileList) { if (FileType.FILE.equals(file.getType())) { addToZip(fileObj, file, zos); } } } catch (FileSystemException e) { throw new SynapseException("Error occurs in writing files", e); } finally { try { if (zos != null) { zos.close(); } } catch (IOException e) { log.error("Error while closing the ZipOutputStream"); } try { fileObj.close(); directoryToZip.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } }
From source file:org.wso2.carbon.connector.FileArchiveConnector.java
/** * Add the file to zip directory./*from w w w . j av a 2s. c o m*/ * * @param fileObject The fileObject where zip entry data needs to be written. * @param file The fileObject which needs to be archived. * @param outputStream ZipOutputStream. */ private void addToZip(FileObject fileObject, FileObject file, ZipOutputStream outputStream) { InputStream fin = null; try { fin = file.getContent().getInputStream(); String name = file.getName().toString(); String entry = name.substring(fileObject.getName().toString().length() + 1, name.length()); ZipEntry zipEntry = new ZipEntry(entry); outputStream.putNextEntry(zipEntry); int length; while ((length = fin.read(bytes)) != -1) { outputStream.write(bytes, 0, length); } } catch (IOException e) { throw new SynapseException("Unable to write an array of bytes to the ZIP entry data", e); } finally { try { outputStream.closeEntry(); if (fin != null) { fin.close(); } } catch (IOException e) { log.error("Error while closing InputStream", e); } try { fileObject.close(); file.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } }
From source file:org.wso2.carbon.connector.FileArchives.java
/** * @param messageContext The message context that is generated for processing the file * @param source The file to be archived * @param destination Destination of the archived file * @return return status// w ww .j a v a 2 s.co m * @throws SynapseException */ private boolean fileCompress(MessageContext messageContext, String source, String destination) { boolean resultStatus = false; StandardFileSystemManager manager; FileSystemOptions opts = FileConnectorUtils.init(messageContext); try { manager = FileConnectorUtils.getManager(); FileObject fileObj = manager.resolveFile(source, opts); FileObject destObj = manager.resolveFile(destination, opts); if (fileObj.exists()) { if (fileObj.getType() == FileType.FOLDER) { List<FileObject> fileList = new ArrayList<FileObject>(); getAllFiles(fileObj, fileList, messageContext); writeZipFiles(fileObj, destObj, fileList, messageContext); } else { ZipOutputStream outputStream = null; InputStream fileIn = null; try { outputStream = new ZipOutputStream(destObj.getContent().getOutputStream()); fileIn = fileObj.getContent().getInputStream(); ZipEntry zipEntry = new ZipEntry(fileObj.getName().getBaseName()); outputStream.putNextEntry(zipEntry); int length; while ((length = fileIn.read(bytes)) != -1) { outputStream.write(bytes, 0, length); } } catch (Exception e) { log.error("Unable to compress a file." + e.getMessage()); } finally { try { if (outputStream != null) { outputStream.close(); } } catch (IOException e) { log.error("Error while closing ZipOutputStream: " + e.getMessage(), e); } try { if (fileIn != null) { fileIn.close(); } } catch (IOException e) { log.error("Error while closing InputStream: " + e.getMessage(), e); } manager.close(); } } resultStatus = true; if (log.isDebugEnabled()) { log.debug("File archiving completed." + destination); } } else { log.error("The File location does not exist."); resultStatus = false; } } catch (IOException e) { handleException("Unable to process the zip file", e, messageContext); } return resultStatus; }
From source file:org.wso2.carbon.connector.FileArchives.java
/** * @param fileObj source fileObject * @param directoryToZip destination fileObject * @param fileList list of files to be compressed * @param messageContext The message context that is generated for processing the file * @throws IOException// w w w. j a va2 s .com */ private void writeZipFiles(FileObject fileObj, FileObject directoryToZip, List<FileObject> fileList, MessageContext messageContext) throws IOException { ZipOutputStream zos = null; try { zos = new ZipOutputStream(directoryToZip.getContent().getOutputStream()); for (FileObject file : fileList) { if (file.getType() == FileType.FILE) { addToZip(fileObj, file, zos); } } } catch (IOException e) { handleException("Error occur in writing files", e, messageContext); } finally { if (zos != null) { zos.close(); } } }
From source file:org.wso2.carbon.connector.FileArchives.java
/** * @param fileObject Source fileObject * @param file The file inside source folder * @param outputStream ZipOutputStream//from ww w . j a v a 2 s . c om */ private void addToZip(FileObject fileObject, FileObject file, ZipOutputStream outputStream) { InputStream fin = null; try { fin = file.getContent().getInputStream(); String name = file.getName().toString(); String entry = name.substring(fileObject.getName().toString().length() + 1, name.length()); ZipEntry zipEntry = new ZipEntry(entry); outputStream.putNextEntry(zipEntry); int length; while ((length = fin.read(bytes)) != -1) { outputStream.write(bytes, 0, length); } } catch (IOException e) { log.error("Unable to add a file into the zip file directory." + e.getMessage()); } finally { try { outputStream.closeEntry(); if (fin != null) { fin.close(); } } catch (IOException e) { log.error("Error while closing InputStream: " + e.getMessage(), e); } } }
From source file:org.wso2.carbon.connector.FileCopy.java
/** * Copy files//from w w w . ja va 2s .c o 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.FileCopyInStream.java
/** * Copy the large files//from w ww .jav a 2 s.c om * * @param fileLocation * @param filename * @param newFileLocation * @return */ private boolean copyLargeFiles(String fileLocation, String filename, String newFileLocation) throws IOException { String sftpURL = newFileLocation + filename.toString(); FileSystemOptions opts; boolean resultStatus = false; opts = FTPSiteUtils.createDefaultOptions(); FileSystemManager manager = VFS.getManager(); FileObject localFile = manager.resolveFile(fileLocation + filename, opts); FileObject remoteFile = manager.resolveFile(sftpURL, opts); InputStream fin = null; OutputStream fout = null; try { fin = localFile.getContent().getInputStream(); fout = remoteFile.getContent().getOutputStream(); IOUtils.copyLarge(fin, fout); } finally { if (fout != null) { fout.close(); } if (fin != null) { fin.close(); } } resultStatus = true; if (log.isDebugEnabled()) { log.info("File copying completed..." + filename.toString()); } return resultStatus; }
From source file:org.wso2.carbon.connector.FileCreate.java
/** * Create a file with Apache commons/*from w w w.j a v a 2 s. com*/ * * @param source Location of the file/folder * @param content Content in a file * @param encoding Encoding type * @param messageContext The message context that is generated for processing the file * @return Return the status */ private boolean createFile(String source, String content, String encoding, MessageContext messageContext) { boolean resultStatus = false; StandardFileSystemManager manager; try { OutputStream out = null; manager = FileConnectorUtils.getManager(); if (manager != null) { FileObject sourceFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); try { if (FileConnectorUtils.isFolder(sourceFile)) { sourceFile.createFolder(); } else { if (StringUtils.isEmpty(content)) { sourceFile.createFile(); } else { FileContent fileContent = sourceFile.getContent(); out = fileContent.getOutputStream(true); if (StringUtils.isEmpty(encoding)) { IOUtils.write(content, out, DEFAULT_ENCODING); } else { IOUtils.write(content, out, encoding); } } } resultStatus = true; if (log.isDebugEnabled()) { log.debug("File creation completed with " + source); } } finally { try { if (sourceFile != null) { sourceFile.close(); } } catch (FileSystemException e) { log.error("Error while closing FileObject: " + e.getMessage(), e); } try { if (out != null) { out.close(); } } catch (IOException e) { log.error("Error while closing OutputStream: " + e.getMessage(), e); } manager.close(); } } } catch (IOException e) { handleException("Unable to create a file/folder.", e, messageContext); } return resultStatus; }
From source file:org.wso2.carbon.connector.FileCreateConnector.java
/** * Create a new file/folder./*from w w w .jav a2 s. c o m*/ * * @param messageContext The message context that is generated for processing the file. * @return true, if the file/folder is successfully created. */ private boolean createFile(MessageContext messageContext) throws FileSystemException { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String content = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT); String encoding = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.ENCODING); if (StringUtils.isEmpty(encoding)) { encoding = FileConstants.DEFAULT_ENCODING; } StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject sourceFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (FileConnectorUtils.isFolder(sourceFile)) { sourceFile.createFolder(); } else { if (StringUtils.isEmpty(content)) { sourceFile.createFile(); } else { OutputStream out = sourceFile.getContent().getOutputStream(); try { IOUtils.write(content, out, encoding); } catch (IOException e) { throw new SynapseException("Error while writing the file content", e); } finally { try { // close the file object sourceFile.close(); } catch (FileSystemException e) { log.error("Error while closing FileObject", e); } try { if (out != null) { out.close(); } } catch (IOException e) { log.error("Error while closing OutputStream", e); } // close the StandardFileSystemManager manager.close(); } } if (log.isDebugEnabled()) { log.debug("File creation completed with " + source); } } return true; }
From source file:org.wso2.carbon.connector.FileList.java
public void list(MessageContext messageContext, String fileLocation) throws SynapseException { try {//from w w w. ja v a2s . co 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); } }