List of usage examples for org.apache.commons.vfs2 FileObject getURL
URL getURL() throws FileSystemException;
From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FilePollingConsumer.java
/** * // w w w .j ava 2s . c om * Do the file processing operation for the given set of properties. Do the * checks and pass the control to processFile method * * */ public FileObject poll() { if (fileURI == null || fileURI.trim().equals("")) { log.error("Invalid file url. Check the inbound endpoint configuration. Endpoint Name : " + name + ", File URL : " + VFSUtils.maskURLPassword(fileURI)); return null; } if (log.isDebugEnabled()) { log.debug("Start : Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI)); } if (!initFileCheck()) { // Unable to read from the source location provided. return null; } // If file/folder found proceed to the processing stage try { lastCycle = 0; if (fileObject.exists() && fileObject.isReadable()) { FileObject[] children = null; try { children = fileObject.getChildren(); } catch (FileNotFolderException ignored) { if (log.isDebugEnabled()) { log.debug("No Folder found. Only file found on : " + VFSUtils.maskURLPassword(fileURI)); } } catch (FileSystemException ex) { log.error(ex.getMessage(), ex); } // if this is a file that would translate to a single message if (children == null || children.length == 0) { // Fail record is a one that is processed but was not moved // or deleted due to an error. boolean isFailedRecord = VFSUtils.isFailRecord(fsManager, fileObject); if (!isFailedRecord) { fileHandler(); if (injectHandler == null) { return fileObject; } } else { try { lastCycle = 2; moveOrDeleteAfterProcessing(fileObject); } catch (SynapseException synapseException) { log.error("File object '" + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + "' " + "cloud not be moved after first attempt", synapseException); } if (fileLock) { // TODO: passing null to avoid build break. Fix properly VFSUtils.releaseLock(fsManager, fileObject, fso); } if (log.isDebugEnabled()) { log.debug("File '" + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + "' has been marked as a failed" + " record, it will not process"); } } } else { FileObject fileObject = directoryHandler(children); if (fileObject != null) { return fileObject; } } } else { log.warn("Unable to access or read file or directory : " + VFSUtils.maskURLPassword(fileURI) + "." + " Reason: " + (fileObject.exists() ? (fileObject.isReadable() ? "Unknown reason" : "The file can not be read!") : "The file does not exists!")); return null; } } catch (FileSystemException e) { log.error("Error checking for existence and readability : " + VFSUtils.maskURLPassword(fileURI), e); return null; } catch (Exception e) { log.error("Error while processing the file/folder in URL : " + VFSUtils.maskURLPassword(fileURI), e); return null; } finally { try { if (fsManager != null) { fsManager.closeFileSystem(fileObject.getParent().getFileSystem()); } fileObject.close(); } catch (Exception e) { log.error("Unable to close the file system. " + e.getMessage()); log.error(e); } } if (log.isDebugEnabled()) { log.debug("End : Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI)); } return null; }
From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FilePollingConsumer.java
/** * //from ww w .j a v a 2 s .c o m * Handle directory with chile elements * * @param children * @return * @throws FileSystemException */ private FileObject directoryHandler(FileObject[] children) throws FileSystemException { // Process Directory lastCycle = 0; int failCount = 0; int successCount = 0; int processCount = 0; if (log.isDebugEnabled()) { log.debug("File name pattern : " + vfsProperties.getProperty(VFSConstants.TRANSPORT_FILE_FILE_NAME_PATTERN)); } // Sort the files String strSortParam = vfsProperties.getProperty(VFSConstants.FILE_SORT_PARAM); if (strSortParam != null && !"NONE".equals(strSortParam)) { log.debug("Start Sorting the files."); String strSortOrder = vfsProperties.getProperty(VFSConstants.FILE_SORT_ORDER); boolean bSortOrderAsscending = true; if (strSortOrder != null && strSortOrder.toLowerCase().equals("false")) { bSortOrderAsscending = false; } if (log.isDebugEnabled()) { log.debug("Sorting the files by : " + strSortOrder + ". (" + bSortOrderAsscending + ")"); } if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_NAME) && bSortOrderAsscending) { Arrays.sort(children, new FileNameAscComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_NAME) && !bSortOrderAsscending) { Arrays.sort(children, new FileNameDesComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_SIZE) && bSortOrderAsscending) { Arrays.sort(children, new FileSizeAscComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_SIZE) && !bSortOrderAsscending) { Arrays.sort(children, new FileSizeDesComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP) && bSortOrderAsscending) { Arrays.sort(children, new FileLastmodifiedtimestampAscComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP) && !bSortOrderAsscending) { Arrays.sort(children, new FileLastmodifiedtimestampDesComparator()); } log.debug("End Sorting the files."); } for (FileObject child : children) { // skipping *.lock / *.fail file if (child.getName().getBaseName().endsWith(".lock") || child.getName().getBaseName().endsWith(".fail")) { continue; } boolean isFailedRecord = VFSUtils.isFailRecord(fsManager, child); // child's file name matches the file name pattern or process all // files now we try to get the lock and process if ((strFilePattern == null || child.getName().getBaseName().matches(strFilePattern)) && !isFailedRecord) { if (log.isDebugEnabled()) { log.debug("Matching file : " + child.getName().getBaseName()); } if ((!fileLock || (fileLock && acquireLock(fsManager, child)))) { // process the file boolean runPostProcess = true; try { if (log.isDebugEnabled()) { log.debug("Processing file :" + VFSUtils.maskURLPassword(child.toString())); } processCount++; if (processFile(child) == null) { runPostProcess = false; } else { successCount++; } // tell moveOrDeleteAfterProcessing() file was success lastCycle = 1; } catch (Exception e) { if (e.getCause() instanceof FileNotFoundException) { log.warn("Error processing File URI : " + VFSUtils.maskURLPassword(child.getName().toString()) + ". This can be due to file moved from another process."); runPostProcess = false; } else { log.error("Error processing File URI : " + VFSUtils.maskURLPassword(child.getName().toString()), e); failCount++; // tell moveOrDeleteAfterProcessing() file failed lastCycle = 2; } } // skipping un-locking file if failed to do delete/move // after process boolean skipUnlock = false; if (runPostProcess) { try { moveOrDeleteAfterProcessing(child); } catch (SynapseException synapseException) { log.error( "File object '" + VFSUtils.maskURLPassword(child.getURL().toString()) + "'cloud not be moved, will remain in \"locked\" state", synapseException); skipUnlock = true; failCount++; lastCycle = 3; VFSUtils.markFailRecord(fsManager, child); } } // if there is a failure or not we'll try to release the // lock if (fileLock && !skipUnlock) { // TODO: passing null to avoid build break. Fix properly VFSUtils.releaseLock(fsManager, child, fso); } if (injectHandler == null) { return child; } } } else if (log.isDebugEnabled() && strFilePattern != null && !child.getName().getBaseName().matches(strFilePattern) && !isFailedRecord) { // child's file name does not match the file name pattern log.debug("Non-Matching file : " + child.getName().getBaseName()); } else if (isFailedRecord) { // it is a failed record try { lastCycle = 1; moveOrDeleteAfterProcessing(child); } catch (SynapseException synapseException) { log.error("File object '" + VFSUtils.maskURLPassword(child.getURL().toString()) + "'cloud not be moved, will remain in \"fail\" state", synapseException); } if (fileLock) { // TODO: passing null to avoid build break. Fix properly VFSUtils.releaseLock(fsManager, child, fso); VFSUtils.releaseLock(fsManager, fileObject, fso); } if (log.isDebugEnabled()) { log.debug("File '" + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + "' has been marked as a failed record, it will not " + "process"); } } //close the file system after processing try { child.close(); } catch (Exception e) { } // Manage throttling of file processing if (iFileProcessingInterval != null && iFileProcessingInterval > 0) { try { if (log.isDebugEnabled()) { log.debug("Put the VFS processor to sleep for : " + iFileProcessingInterval); } Thread.sleep(iFileProcessingInterval); } catch (InterruptedException ie) { log.error("Unable to set the interval between file processors." + ie); } } else if (iFileProcessingCount != null && iFileProcessingCount <= processCount) { break; } } if (failCount == 0 && successCount > 0) { lastCycle = 1; } else if (successCount == 0 && failCount > 0) { lastCycle = 4; } else { lastCycle = 5; } return null; }
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;// w ww . j a va 2 s . c o m 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;/*from w w w .ja v 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:pl.otros.vfs.browser.actions.AddCurrentLocationToFavoriteAction.java
@Override public void actionPerformed(ActionEvent e) { FileObject currentLocation = vfsBrowser.getCurrentLocation(); if (currentLocation != null) { String name = currentLocation.getName().getBaseName(); if (remoteSchemas.contains(currentLocation.getName().getScheme())) { try { URI uri = new URI(currentLocation.getName().getURI()); name = uri.getHost() + "/" + name; } catch (URISyntaxException e1) { e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. }//from w ww. j a v a 2s . c o m } Favorite favorite; try { favorite = new Favorite(name, currentLocation.getURL().toExternalForm(), Favorite.Type.USER); vfsBrowser.getFavoritesUserListModel().add(favorite); } catch (FileSystemException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
From source file:pl.otros.vfs.browser.JOtrosVfsBrowserDialog.java
public static void main(String[] args) throws FileSystemException { if (args.length > 1) throw new IllegalArgumentException( "SYNTAX: java... " + JOtrosVfsBrowserDialog.class.getName() + " [initialPath]"); JOtrosVfsBrowserDialog jOtrosVfsBrowserDialog = new JOtrosVfsBrowserDialog( (args.length < 1) ? null : args[0]); jOtrosVfsBrowserDialog.setMultiSelectionEnabled(true); jOtrosVfsBrowserDialog.vfsBrowser.setSelectionMode(SelectionMode.DIRS_AND_FILES); ReturnValue rv = jOtrosVfsBrowserDialog.showOpenDialog(null, "title"); System.out.println(rv);/* w ww . j av a 2 s . co m*/ FileObject[] selectedFiles = jOtrosVfsBrowserDialog.getSelectedFiles(); System.out.println("Selected files count " + selectedFiles.length); for (FileObject selectedFile : selectedFiles) { System.out.println(selectedFile.getType().toString() + ": " + selectedFile.getURL()); } System.exit(0); }
From source file:pl.otros.vfs.browser.util.VFSUtils.java
/** * Returns whether a file object is a local file * * @param fileObject/*from w w w .j a va2s. co m*/ * @return true of {@link FileObject} is a local file */ public static boolean isLocalFile(FileObject fileObject) { try { return fileObject.getURL().getProtocol().equalsIgnoreCase("file") && FileType.FILE.equals(fileObject.getType()); } catch (FileSystemException e) { LOGGER.info("Exception when checking if fileobject is local file", e); return false; } }
From source file:pl.otros.vfs.browser.util.VFSUtils.java
public static boolean pointToItself(FileObject fileObject) throws FileSystemException { if (!fileObject.getURL().getProtocol().equalsIgnoreCase("file") && FileType.FILE.equals(fileObject.getType())) { LOGGER.debug("Checking if {} is pointing to itself", fileObject.getName().getFriendlyURI()); FileObject[] children = VFSUtils.getChildren(fileObject); LOGGER.debug("Children number of {} is {}", fileObject.getName().getFriendlyURI(), children.length); if (children.length == 1) { FileObject child = children[0]; if (child.getContent().getSize() != child.getContent().getSize()) { return false; }/* w w w . j av a2 s . co m*/ if (child.getName().getBaseName().equals(fileObject.getName().getBaseName())) { return true; } } } return false; }
From source file:pl.otros.vfs.browser.util.VFSUtils.java
public static FileObject[] getChildren(FileObject fileObject) throws FileSystemException { FileObject[] result;//from w w w. j av a 2 s . c o m if (isHttpProtocol(fileObject)) { result = extractHttpFileObjectChildren(fileObject); } else if (isLocalFileSystem(fileObject) && isArchive(fileObject)) { String extension = fileObject.getName().getExtension(); result = VFSUtils.resolveFileObject(extension + ":" + fileObject.getURL().toString() + "!/") .getChildren(); } else { result = fileObject.getChildren(); } return result; }
From source file:pl.otros.vfs.browser.util.VFSUtils.java
private static FileObject[] extractHttpFileObjectChildren(FileObject fileObject) throws FileSystemException { FileObject[] result;//ww w .j a v a2 s.c o m HttpFileObject fo = (HttpFileObject) fileObject; FileContent content = fo.getContent(); String contentType = content.getContentInfo().getContentType(); result = new FileObject[] { fileObject }; if (contentType.equalsIgnoreCase("text/html")) { try { String html = IOUtils.toString(content.getInputStream()); if (html.toLowerCase().contains("index of")) { LOGGER.info("Page contains \"index of\", resolving children"); //a href="DSC_0410.JPG"> Pattern p = Pattern.compile("<A .*?href=\"(.*?)\"", Pattern.CASE_INSENSITIVE); Matcher matcher = p.matcher(html); ArrayList<FileObject> list = new ArrayList<FileObject>(); while (matcher.find()) { String link = matcher.group(1); LOGGER.info("Getting children from link {}", link); if (StringUtils.isBlank(link)) { LOGGER.debug("URL link is blank"); continue; } FileObject child; URL url = fileObject.getURL(); child = extractHttpFileObject(link, url); list.add(child); } result = list.toArray(result); } //TODO extract links } catch (Exception e) { throw new FileSystemException(e); } } return result; }