List of usage examples for org.apache.commons.vfs2 FileObject getContent
FileContent getContent() throws FileSystemException;
From source file:org.wso2.carbon.connector.util.ResultPayloadCreator.java
/** * Read the file content and set those content as the current SOAPEnvelope. * * @param file File which needs to be read. * @param msgCtx Message Context that is used in the file read mediation flow. * @param contentType content type.//from w ww . j av a 2s . c om * @param streaming streaming mode (true/false). * @return true, if file content is read successfully. */ public static boolean buildFile(FileObject file, MessageContext msgCtx, String contentType, boolean streaming) { ManagedDataSource dataSource = null; InputStream in = null; try { if (StringUtils.isEmpty(contentType)) { if (file.getName().getExtension().toLowerCase().endsWith("xml")) { contentType = "application/xml"; } else if (file.getName().getExtension().toLowerCase().endsWith("txt")) { contentType = "text/plain"; } } else { // Extract the charset encoding from the configured content type and // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this. try { String charSetEnc = new ContentType(contentType).getParameter("charset"); msgCtx.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); } catch (ParseException ex) { throw new SynapseException("Invalid encoding type.", ex); } } if (log.isDebugEnabled()) { log.debug("Processed file : " + file + " of Content-type : " + contentType); } org.apache.axis2.context.MessageContext axis2MsgCtx = ((org.apache.synapse.core.axis2.Axis2MessageContext) msgCtx) .getAxis2MessageContext(); // Determine the message builder to use Builder builder; if (StringUtils.isEmpty(contentType)) { log.debug("No content type specified. Using RELAY builder."); builder = new BinaryRelayBuilder(); } else { int index = contentType.indexOf(';'); String type = index > 0 ? contentType.substring(0, index) : contentType; builder = BuilderUtil.getBuilderFromSelector(type, axis2MsgCtx); if (builder == null) { if (log.isDebugEnabled()) { log.debug( "No message builder found for type '" + type + "'. Falling back to RELAY builder."); } builder = new BinaryRelayBuilder(); } } // set the message payload to the message context OMElement documentElement; if (builder instanceof DataSourceMessageBuilder && streaming) { dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType)); documentElement = ((DataSourceMessageBuilder) builder).processDocument(dataSource, contentType, axis2MsgCtx); } else { in = new AutoCloseInputStream(file.getContent().getInputStream()); documentElement = builder.processDocument(in, contentType, axis2MsgCtx); } // We need this to build the complete message before closing the stream if (!streaming && documentElement != null) { //msgCtx.getEnvelope().build(); documentElement.toString(); } msgCtx.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement)); } catch (Exception e) { throw new SynapseException("Error while processing the file/folder", e); } finally { if (dataSource != null) { dataSource.destroy(); } if (in != null) { try { in.close(); } catch (IOException e) { log.error("Error while closing the InputStream"); } } try { file.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } return true; }
From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FileInjectHandler.java
/** * Inject the message to the sequence//from w ww. jav a 2 s . com * */ public boolean invoke(Object object, String name) throws SynapseException { ManagedDataSource dataSource = null; ; FileObject file = (FileObject) object; try { org.apache.synapse.MessageContext msgCtx = createMessageContext(); msgCtx.setProperty("inbound.endpoint.name", name); InboundEndpoint inboundEndpoint = msgCtx.getConfiguration().getInboundEndpoint(name); CustomLogSetter.getInstance().setLogAppender(inboundEndpoint.getArtifactContainerName()); String contentType = vfsProperties.getProperty(VFSConstants.TRANSPORT_FILE_CONTENT_TYPE); if (contentType == null || contentType.trim().equals("")) { if (file.getName().getExtension().toLowerCase().endsWith("xml")) { contentType = "text/xml"; } else if (file.getName().getExtension().toLowerCase().endsWith("txt")) { contentType = "text/plain"; } } else { // Extract the charset encoding from the configured content type and // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this. String charSetEnc = null; try { if (contentType != null) { charSetEnc = new ContentType(contentType).getParameter("charset"); } } catch (ParseException ex) { // ignore } msgCtx.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); } if (log.isDebugEnabled()) { log.debug("Processed file : " + file + " of Content-type : " + contentType); } MessageContext axis2MsgCtx = ((org.apache.synapse.core.axis2.Axis2MessageContext) msgCtx) .getAxis2MessageContext(); // Determine the message builder to use Builder builder; if (contentType == null) { log.debug("No content type specified. Using SOAP builder."); builder = new SOAPBuilder(); } else { int index = contentType.indexOf(';'); String type = index > 0 ? contentType.substring(0, index) : contentType; builder = BuilderUtil.getBuilderFromSelector(type, axis2MsgCtx); if (builder == null) { if (log.isDebugEnabled()) { log.debug("No message builder found for type '" + type + "'. Falling back to SOAP."); } builder = new SOAPBuilder(); } } // set the message payload to the message context InputStream in; String streaming = vfsProperties.getProperty(VFSConstants.STREAMING); if (builder instanceof DataSourceMessageBuilder && "true".equals(streaming)) { dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType)); in = null; } else { in = new AutoCloseInputStream(file.getContent().getInputStream()); dataSource = null; } //Inject the message to the sequence. OMElement documentElement; if (in != null) { documentElement = builder.processDocument(in, contentType, axis2MsgCtx); } else { documentElement = ((DataSourceMessageBuilder) builder).processDocument(dataSource, contentType, axis2MsgCtx); } if ("true".equals(vfsProperties.getProperty(VFSConstants.TRANSPORT_BUILD))) { documentElement.build(); } msgCtx.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement)); if (injectingSeq == null || injectingSeq.equals("")) { log.error("Sequence name not specified. Sequence : " + injectingSeq); } SequenceMediator seq = (SequenceMediator) synapseEnvironment.getSynapseConfiguration() .getSequence(injectingSeq); if (seq != null) { if (log.isDebugEnabled()) { log.debug("injecting message to sequence : " + injectingSeq); } if (!seq.isInitialized()) { seq.init(synapseEnvironment); } seq.setErrorHandler(onErrorSeq); if (!synapseEnvironment.injectInbound(msgCtx, seq, sequential)) { return false; } } else { log.error("Sequence: " + injectingSeq + " not found"); } } catch (SynapseException se) { throw se; } catch (Exception e) { log.error("Error while processing the file/folder", e); throw new SynapseException("Error while processing the file/folder", e); } finally { if (dataSource != null) { dataSource.destroy(); } } return true; }
From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FilePollingConsumer.java
/** * Actual processing of the file/folder// ww w. ja va2 s . c o m * * @param file * @return * @throws synapseException */ private FileObject processFile(FileObject file) throws SynapseException { try { FileContent content = file.getContent(); String fileName = file.getName().getBaseName(); String filePath = file.getName().getPath(); String fileURI = file.getName().getURI(); if (injectHandler != null) { Map<String, Object> transportHeaders = new HashMap<String, Object>(); transportHeaders.put(VFSConstants.FILE_PATH, filePath); transportHeaders.put(VFSConstants.FILE_NAME, fileName); transportHeaders.put(VFSConstants.FILE_URI, fileURI); try { transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize()); transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime()); } catch (FileSystemException e) { log.warn("Unable to set file length or last modified date header.", e); } injectHandler.setTransportHeaders(transportHeaders); // injectHandler if (!injectHandler.invoke(file, name)) { return null; } } } catch (FileSystemException e) { log.error("Error reading file content or attributes : " + VFSUtils.maskURLPassword(file.toString()), e); } return file; }
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;//from www . j a v a2 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.file.connector.server.FileConsumer.java
/** * Actual processing of the file/folder//from www . jav a 2 s. c om * * @param file * @return */ private FileObject processFile(FileObject file) throws FileServerConnectorException { FileContent content; String fileURI; String fileName = file.getName().getBaseName(); String filePath = file.getName().getPath(); fileURI = file.getName().getURI(); try { content = file.getContent(); } catch (FileSystemException e) { throw new FileServerConnectorException( "Could not read content of file at URI: " + FileTransportUtils.maskURLPassword(fileURI) + ". ", e); } InputStream inputStream; try { inputStream = content.getInputStream(); } catch (FileSystemException e) { throw new FileServerConnectorException("Error occurred when trying to get " + "input stream from file at URI :" + FileTransportUtils.maskURLPassword(fileURI), e); } CarbonMessage cMessage = new StreamingCarbonMessage(inputStream); cMessage.setProperty(org.wso2.carbon.messaging.Constants.PROTOCOL, Constants.PROTOCOL_NAME); cMessage.setProperty(Constants.FILE_TRANSPORT_PROPERTY_SERVICE_NAME, serviceName); cMessage.setHeader(Constants.FILE_PATH, filePath); cMessage.setHeader(Constants.FILE_NAME, fileName); cMessage.setHeader(Constants.FILE_URI, fileURI); try { cMessage.setHeader(Constants.FILE_LENGTH, Long.toString(content.getSize())); cMessage.setHeader(Constants.LAST_MODIFIED, Long.toString(content.getLastModifiedTime())); } catch (FileSystemException e) { log.warn("Unable to set file length or last modified date header.", e); } FileServerConnectorCallback callback = new FileServerConnectorCallback(); try { messageProcessor.receive(cMessage, callback); } catch (Exception e) { throw new FileServerConnectorException("Failed to send stream from file: " + FileTransportUtils.maskURLPassword(fileURI) + " to message processor. ", e); } try { callback.waitTillDone(timeOutInterval, deleteIfNotAck, fileURI); } catch (InterruptedException e) { throw new FileServerConnectorException("Interrupted while waiting for message " + "processor to consume the file input stream. Aborting processing of file: " + FileTransportUtils.maskURLPassword(fileURI), e); } return file; }
From source file:org.wso2.carbon.transport.filesystem.test.util.TestMessageProcessor.java
@Override public boolean receive(CarbonMessage carbonMessage, CarbonCallback carbonCallback) throws Exception { FileSystemManager fileSystemManager = VFS.getManager(); FileObject fileObject = fileSystemManager.resolveFile(((TextCarbonMessage) carbonMessage).getText()); fileContent = getStringFromInputStream(fileObject.getContent().getInputStream()); carbonCallback.done(carbonMessage);//from w w w. j ava 2 s . co m done(); return false; }
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 . jav a2s. c o m 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.logview.api.io.Utils.java
public static boolean checkIfIsGzipped(FileObject fileObject) throws IOException { boolean gziped = false; if (fileObject.getContent().getSize() == 0) { LOGGER.debug("File object " + fileObject.getName() + " is empty, can't detect gzip compression"); return false; }// ww w. j a v a 2 s . co m InputStream inputStream = fileObject.getContent().getInputStream(); byte[] loadProbe = loadProbe(inputStream, GZIP_CHECK_BUFFER_SIZE); // IOUtils.closeQuietly(inputStream); if (loadProbe.length < GZIP_MIN_SIZE) { LOGGER.info("Loaded probe is too small to check if it is gziped"); return false; } try { ByteArrayInputStream bin = new ByteArrayInputStream(loadProbe); int available = bin.available(); byte[] b = new byte[available < GZIP_CHECK_BUFFER_SIZE ? available : GZIP_CHECK_BUFFER_SIZE]; int read = bin.read(b); gziped = checkIfIsGzipped(b, read); } catch (IOException e) { // Not gziped LOGGER.debug(fileObject.getName() + " is not gzip"); } return gziped; }
From source file:pl.otros.logview.api.io.Utils.java
public static LoadingInfo openFileObject(FileObject fileObject, boolean tailing) throws Exception { LoadingInfo loadingInfo = new LoadingInfo(); loadingInfo.setFileObject(fileObject); loadingInfo.setFriendlyUrl(fileObject.getName().getFriendlyURI()); final FileContent content = fileObject.getContent(); InputStream httpInputStream = content.getInputStream(); byte[] buff = Utils.loadProbe(httpInputStream, 10000); loadingInfo.setGziped(checkIfIsGzipped(buff, buff.length)); ByteArrayInputStream bin = new ByteArrayInputStream(buff); SequenceInputStream sequenceInputStream = new SequenceInputStream(bin, httpInputStream); ObservableInputStreamImpl observableInputStreamImpl = new ObservableInputStreamImpl(sequenceInputStream); if (loadingInfo.isGziped()) { loadingInfo.setContentInputStream(new GZIPInputStream(observableInputStreamImpl)); loadingInfo.setInputStreamBufferedStart(ungzip(buff)); } else {/*from w w w. ja v a2 s . co m*/ loadingInfo.setContentInputStream(observableInputStreamImpl); loadingInfo.setInputStreamBufferedStart(buff); } loadingInfo.setObserableInputStreamImpl(observableInputStreamImpl); loadingInfo.setTailing(tailing); if (fileObject.getType().hasContent()) { loadingInfo.setLastFileSize(content.getSize()); } return loadingInfo; }
From source file:pl.otros.logview.api.io.UtilsTest.java
@Test public void testEmptyFile() throws IOException { FileObject resolveFile = resolveFileObject("/empty.log"); AssertJUnit.assertEquals(0, resolveFile.getContent().getSize()); boolean checkIfIsGzipped = Utils.checkIfIsGzipped(resolveFile); AssertJUnit.assertFalse(checkIfIsGzipped); }