List of usage examples for org.apache.commons.vfs2 FileObject getContent
FileContent getContent() throws FileSystemException;
From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProviderTest.java
private FileObject createFile(FileObject parent, String name, String resource, long timestamp) throws IOException { FileObject file = parent.resolveFile(name); if (!file.exists()) { file.createFile();/* w ww.j a v a 2s .com*/ } InputStream input = ClassLoader.getSystemResourceAsStream(resource); OutputStream output = file.getContent().getOutputStream(); IOUtils.copy(input, output); output.flush(); input.close(); output.close(); file.getContent().setLastModifiedTime(timestamp); assertTrue("Failed to create test file " + file.getName().getFriendlyURI(), file.exists()); assertTrue("Failed to populate test file " + file.getName().getFriendlyURI(), file.getContent().getSize() > 0); // ByteArrayOutputStream buffer = new ByteArrayOutputStream(); // IOUtils.copy( file.getContent().getInputStream(), buffer ); // System.out.println( new String( buffer.toString( "UTF-8" ) ) ); return file; }
From source file:org.apache.nifi.processors.pcap.SplitPcap.java
@Override public void onTrigger(ProcessContext processContext, ProcessSession processSession) throws ProcessException { final FlowFile flowFile = processSession.get(); final ComponentLog logger = getLogger(); if (flowFile != null) { final String vfsFilename = String.format("ram:/%s", flowFile.getAttribute(CoreAttributes.FILENAME.key())); final FileObject fileObject; try {/*from w w w .ja va2 s . c o m*/ fileObject = vfsManager.resolveFile(vfsFilename); } catch (FileSystemException e) { throw new ProcessException(String.format("Unable to resolve file in VFS: %s", vfsFilename)); } processSession.read(flowFile, inputStream -> { final OutputStream vfsOutputStream = fileObject.getContent().getOutputStream(); ByteStreams.copy(inputStream, vfsOutputStream); }); final StringBuilder errorStringBuilder = new StringBuilder(); final Pcap pcap = Pcap.openOffline(vfsFilename, errorStringBuilder); if (pcap == null) { throw new ProcessException(String.format("Unable to open pcap %s, error: %s", vfsFilename, errorStringBuilder.toString())); } try { pcap.loop(-1, (PcapPacketHandler<String>) (pcapPacket, s) -> logger.info("got packet {}", new Object[] { pcapPacket.getTotalSize() }), "offline capture"); } finally { pcap.close(); } } }
From source file:org.apache.olingo.fit.utils.AbstractUtilities.java
public InputStream saveSingleEntity(final String key, final String entitySetName, final InputStream is, final NavigationLinks links) throws Exception { // ----------------------------------------- // 0. Get the path // ----------------------------------------- final String path = entitySetName + File.separatorChar + Commons.getEntityKey(key) + File.separatorChar + Constants.get(ConstantKey.ENTITY); // ----------------------------------------- // ----------------------------------------- // 1. Normalize navigation info; edit link; ... // ----------------------------------------- final InputStream normalized = normalizeLinks(entitySetName, key, is, links); // ----------------------------------------- // ----------------------------------------- // 2. save the entity // ----------------------------------------- final FileObject fo = fsManager.putInMemory(normalized, fsManager.getAbsolutePath(path, getDefaultFormat())); // ----------------------------------------- return fo.getContent().getInputStream(); }
From source file:org.apache.olingo.fit.utils.AbstractUtilities.java
public InputStream addOrReplaceEntity(final String key, final String entitySetName, final InputStream is, final Entity entry) throws Exception { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(is, bos);//from ww w. j a v a2s . c o m IOUtils.closeQuietly(is); final Map<String, NavigationProperty> navigationProperties = metadata .getNavigationProperties(entitySetName); // ----------------------------------------- // 0. Retrieve navigation links to be kept // ----------------------------------------- Set<String> linksToBeKept; try { linksToBeKept = new HashSet<String>(navigationProperties.keySet()); } catch (NullPointerException e) { linksToBeKept = Collections.<String>emptySet(); } for (String availableLink : new HashSet<String>(linksToBeKept)) { try { fsManager.resolve(Commons.getLinksPath(entitySetName, key, availableLink, Accept.JSON_FULLMETA)); } catch (Exception e) { linksToBeKept.remove(availableLink); } } for (String linkName : retrieveAllLinkNames(new ByteArrayInputStream(bos.toByteArray()))) { linksToBeKept.remove(linkName); } // ----------------------------------------- // ----------------------------------------- // 1. Get default entry key and path (N.B. operation will consume/close the stream; use a copy instead) // ----------------------------------------- final String entityKey = key == null ? getDefaultEntryKey(entitySetName, entry) : key; final String path = Commons.getEntityBasePath(entitySetName, entityKey); // ----------------------------------------- // ----------------------------------------- // 2. Retrieve navigation info // ----------------------------------------- final NavigationLinks links = retrieveNavigationInfo(entitySetName, new ByteArrayInputStream(bos.toByteArray())); // ----------------------------------------- // ----------------------------------------- // 3. Normalize navigation info; add edit link; ... and save entity .... // ----------------------------------------- final InputStream createdEntity = saveSingleEntity(entityKey, entitySetName, new ByteArrayInputStream(bos.toByteArray()), links); // ----------------------------------------- bos.reset(); IOUtils.copy(createdEntity, bos); // ----------------------------------------- // 4. Add navigation links to be kept // ----------------------------------------- final InputStream normalizedEntity = addLinks(entitySetName, entityKey, new ByteArrayInputStream(bos.toByteArray()), linksToBeKept); // ----------------------------------------- IOUtils.closeQuietly(bos); // ----------------------------------------- // 5. save the entity // ----------------------------------------- final FileObject fo = fsManager.putInMemory(normalizedEntity, fsManager.getAbsolutePath(path + Constants.get(ConstantKey.ENTITY), getDefaultFormat())); // ----------------------------------------- // ----------------------------------------- // 4. Create links file and provided inlines // ----------------------------------------- for (Map.Entry<String, List<String>> link : links.getLinks()) { putLinksInMemory(path, entitySetName, entityKey, link.getKey(), link.getValue()); } final List<String> hrefs = new ArrayList<String>(); for (final Link link : entry.getNavigationLinks()) { final NavigationProperty navProp = navigationProperties == null ? null : navigationProperties.get(link.getTitle()); if (navProp != null) { final String inlineEntitySetName = navProp.getTarget(); if (link.getInlineEntity() != null) { final String inlineEntryKey = getDefaultEntryKey(inlineEntitySetName, link.getInlineEntity()); addOrReplaceEntity(inlineEntryKey, inlineEntitySetName, toInputStream(link.getInlineEntity()), link.getInlineEntity()); hrefs.add(inlineEntitySetName + "(" + inlineEntryKey + ")"); } else if (link.getInlineEntitySet() != null) { for (Entity subentry : link.getInlineEntitySet().getEntities()) { final String inlineEntryKey = getDefaultEntryKey(inlineEntitySetName, subentry); addOrReplaceEntity(inlineEntryKey, inlineEntitySetName, toInputStream(subentry), subentry); hrefs.add(inlineEntitySetName + "(" + inlineEntryKey + ")"); } } if (!hrefs.isEmpty()) { putLinksInMemory(path, entitySetName, entityKey, link.getTitle(), hrefs); } } } // ----------------------------------------- return fo.getContent().getInputStream(); }
From source file:org.apache.olingo.fit.utils.AbstractUtilities.java
public InputStream putMediaInMemory(final String entitySetName, final String entityId, final String name, final InputStream value) throws IOException { final FileObject fo = fsManager .putInMemory(value,/*w w w . java 2 s . c om*/ fsManager.getAbsolutePath( Commons.getEntityBasePath(entitySetName, entityId) + (name == null ? Constants.get(ConstantKey.MEDIA_CONTENT_FILENAME) : name), null)); return fo.getContent().getInputStream(); }
From source file:org.apache.olingo.fit.utils.FSManager.java
private FSManager() throws IOException { fsManager = VFS.getManager();//from w w w . j a v a 2 s . c o m final FileObject basePath = fsManager .resolveFile(RES_PREFIX + File.separatorChar + ODataServiceVersion.V40.name()); final String absoluteBaseFolder = basePath.getURL().getPath(); for (FileObject fo : find(basePath, null)) { if (fo.getType() == FileType.FILE && !fo.getName().getBaseName().contains("Metadata") && !fo.getName().getBaseName().contains("metadata")) { final String path = fo.getURL().getPath().replace(absoluteBaseFolder, "//" + ODataServiceVersion.V40.name()); putInMemory(fo.getContent().getInputStream(), path); } } }
From source file:org.apache.olingo.fit.utils.FSManager.java
public final FileObject putInMemory(final InputStream is, final String path) throws IOException { LOG.info("Write in memory {}", path); final FileObject memObject = fsManager.resolveFile(MEM_PREFIX + path); if (memObject.exists()) { memObject.delete();/*from w ww . ja v a 2 s . c om*/ } // create in-memory file memObject.createFile(); // read in-memory content final OutputStream os = memObject.getContent().getOutputStream(); IOUtils.copy(is, os); IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); return memObject; }
From source file:org.apache.olingo.fit.utils.FSManager.java
private InputStream readFile(final String relativePath, final Accept accept, final String fs) { final String path = getAbsolutePath(relativePath, accept); LOG.info("Read {}{}", fs, path); try {/* w ww. j a v a 2 s . c om*/ final FileObject fileObject = fsManager.resolveFile(fs + path); if (fileObject.exists()) { // return new in-memory content return fileObject.getContent().getInputStream(); } else { LOG.warn("In-memory path '{}' not found", path); throw new NotFoundException(); } } catch (FileSystemException e) { throw new NotFoundException(); } }
From source file:org.apache.river.container.deployer.FolderBasedAppRunner.java
private Map<String, DeploymentRecord> scanDeploymentArchives() throws FileSystemException { /*/*from w w w.j a v a2s .c o m*/ Go through the deployment directory looking for services to deploy. */ Map<String, DeploymentRecord> deployDirListing = new HashMap<String, DeploymentRecord>(); deploymentDirectoryFile.refresh(); List<FileObject> serviceArchives = Utils.findChildrenWithSuffix(deploymentDirectoryFile, org.apache.river.container.Strings.JAR); if (serviceArchives != null) { log.log(Level.FINER, MessageNames.FOUND_SERVICE_ARCHIVES, new Object[] { serviceArchives.size(), deployDirectory }); for (FileObject serviceArchive : serviceArchives) { DeploymentRecord rec = new DeploymentRecord(); rec.fileObject = serviceArchive; rec.name = serviceArchive.getName().getBaseName(); rec.updateTime = serviceArchive.getContent().getLastModifiedTime(); deployDirListing.put(rec.name, rec); } } return deployDirListing; }
From source file:org.apache.synapse.commons.vfs.VFSUtils.java
/** * Acquires a file item lock before processing the item, guaranteing that * the file is not processed while it is being uploaded and/or the item is * not processed by two listeners/*from w w w . j a va 2 s .c o m*/ * * @param fsManager * used to resolve the processing file * @param fo * representing the processing file item * @param fso * represents file system options used when resolving file from file system manager. * @return boolean true if the lock has been acquired or false if not */ public synchronized static boolean acquireLock(FileSystemManager fsManager, FileObject fo, VFSParamDTO paramDTO, FileSystemOptions fso) { // generate a random lock value to ensure that there are no two parties // processing the same file Random random = new Random(); // Lock format random:hostname:hostip:time String strLockValue = String.valueOf(random.nextLong()); try { strLockValue += STR_SPLITER + InetAddress.getLocalHost().getHostName(); strLockValue += STR_SPLITER + InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException ue) { if (log.isDebugEnabled()) { log.debug("Unable to get the Hostname or IP."); } } strLockValue += STR_SPLITER + (new Date()).getTime(); byte[] lockValue = strLockValue.getBytes(); try { // check whether there is an existing lock for this item, if so it is assumed // to be processed by an another listener (downloading) or a sender (uploading) // lock file is derived by attaching the ".lock" second extension to the file name String fullPath = fo.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos != -1) { fullPath = fullPath.substring(0, pos); } FileObject lockObject = fsManager.resolveFile(fullPath + ".lock", fso); if (lockObject.exists()) { log.debug("There seems to be an external lock, aborting the processing of the file " + maskURLPassword(fo.getName().getURI()) + ". This could possibly be due to some other party already " + "processing this file or the file is still being uploaded"); if (paramDTO != null && paramDTO.isAutoLockRelease()) { releaseLock(lockValue, strLockValue, lockObject, paramDTO.isAutoLockReleaseSameNode(), paramDTO.getAutoLockReleaseInterval()); } } else { // write a lock file before starting of the processing, to ensure that the // item is not processed by any other parties lockObject.createFile(); OutputStream stream = lockObject.getContent().getOutputStream(); try { stream.write(lockValue); stream.flush(); stream.close(); } catch (IOException e) { lockObject.delete(); log.error( "Couldn't create the lock file before processing the file " + maskURLPassword(fullPath), e); return false; } finally { lockObject.close(); } // check whether the lock is in place and is it me who holds the lock. This is // required because it is possible to write the lock file simultaneously by // two processing parties. It checks whether the lock file content is the same // as the written random lock value. // NOTE: this may not be optimal but is sub optimal FileObject verifyingLockObject = fsManager.resolveFile(fullPath + ".lock", fso); if (verifyingLockObject.exists() && verifyLock(lockValue, verifyingLockObject)) { return true; } } } catch (FileSystemException fse) { log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI()) + " before processing"); } return false; }