List of usage examples for org.apache.commons.vfs2 FileObject getName
FileName getName();
From source file:org.wso2.carbon.connector.FileRead.java
public void connect(MessageContext messageContext) { String fileLocation = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String contentType = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT_TYPE); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); FileObject fileObj = null;/* w ww .java 2 s . c om*/ StandardFileSystemManager manager = null; try { manager = FileConnectorUtils.getManager(); fileObj = manager.resolveFile(fileLocation, FileConnectorUtils.init(messageContext)); if (fileObj.exists()) { if (fileObj.getType() == FileType.FOLDER) { FileObject[] children = fileObj.getChildren(); if (children == null || children.length == 0) { log.warn("Empty folder."); handleException("Empty folder.", messageContext); } else if (filePattern != null && !filePattern.trim().equals("")) { boolean bFound = false; for (FileObject child : children) { if (child.getName().getBaseName().matches(filePattern)) { fileObj = child; bFound = true; break; } } if (!bFound) { log.warn("File does not exists for the mentioned pattern."); handleException("File does not exists for the mentioned pattern.", messageContext); } } else { fileObj = children[0]; } } else if (fileObj.getType() != FileType.FILE) { log.warn("File does not exists, or an empty folder."); handleException("File does not exists, or an empty folder.", messageContext); } } else { log.warn("File/Folder does not exists"); handleException("File/Folder does not exists", messageContext); } ResultPayloadCreate.buildFile(fileObj, messageContext, contentType); if (log.isDebugEnabled()) { log.debug("File read completed." + fileLocation); } } catch (Exception e) { handleException(e.getMessage(), messageContext); } finally { try { // Close the File system if it is not already closed by the finally block of // processFile method if (fileObj != null && fileObj.getParent() != null && fileObj.getParent().getFileSystem() != null) { manager.closeFileSystem(fileObj.getParent().getFileSystem()); } } catch (FileSystemException warn) { // ignore the warning, since we handed over the stream close job to // AutoCloseInputStream.. } try { if (fileObj != null) { fileObj.close(); } } catch (Exception e) { // ignore the warning, since we handed over the stream close job to // AutoCloseInputStream.. } } }
From source file:org.wso2.carbon.connector.FileReadConnector.java
/** * Read the file content.//from ww w . j a v a2 s . c o m * * @param messageContext The message context that is generated for processing the read operation. */ private void readFile(MessageContext messageContext) { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String contentType = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT_TYPE); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); boolean streaming = false; String enableStreamingParameter = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.ENABLE_STREAMING); if (StringUtils.isNotEmpty(enableStreamingParameter)) { streaming = Boolean.parseBoolean(enableStreamingParameter); } FileObject fileObjectToRead = null; StandardFileSystemManager manager = FileConnectorUtils.getManager(); try { FileObject rootFileObject = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (!rootFileObject.exists()) { log.error("File/Folder does not exists."); } if (FileType.FOLDER.equals(rootFileObject.getType())) { FileObject[] children = rootFileObject.getChildren(); if (children == null || children.length == 0) { log.error("Empty folder."); } else if (StringUtils.isNotEmpty(filePattern)) { for (FileObject child : children) { if (child.getName().getBaseName().matches(filePattern)) { fileObjectToRead = child; break; } } if (fileObjectToRead == null) { log.error("File does not exists for the mentioned pattern."); } } else { fileObjectToRead = children[0]; } } else if (FileType.FILE.equals(rootFileObject.getType())) { fileObjectToRead = rootFileObject; } else { log.error("File does not exists, or an empty folder"); } ResultPayloadCreator.buildFile(fileObjectToRead, messageContext, contentType, streaming); if (log.isDebugEnabled()) { log.debug("File read completed." + source); } } catch (FileSystemException e) { throw new SynapseException("Error while reading a file", e); } finally { try { // Close the File system if it is not already closed by the finally block of processFile method if (fileObjectToRead != null && fileObjectToRead.getParent() != null && fileObjectToRead.getParent().getFileSystem() != null) { manager.closeFileSystem(fileObjectToRead.getParent().getFileSystem()); } if (fileObjectToRead != null) { fileObjectToRead.close(); } } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } }
From source file:org.wso2.carbon.connector.FileSearch.java
/** * Generate the file search/*from w w w . j ava 2s . com*/ * * @param source Location fo the file * @param filePattern Pattern of the file * @param recursiveSearch check whether recursively search or not * @param messageContext The message context that is processed by a handler in the handle method */ private void search(String source, String filePattern, String recursiveSearch, MessageContext messageContext) { ResultPayloadCreate resultPayload = new ResultPayloadCreate(); StandardFileSystemManager manager = null; if (StringUtils.isEmpty(filePattern)) { log.error("FilePattern should not be null"); } else { try { manager = FileConnectorUtils.getManager(); FileSystemOptions opt = FileConnectorUtils.init(messageContext); FileObject remoteFile = manager.resolveFile(source, opt); if (remoteFile.exists()) { FileObject[] children = remoteFile.getChildren(); 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); FilePattenMatcher fpm = new FilePattenMatcher(filePattern); recursiveSearch = recursiveSearch.trim(); for (FileObject child : children) { try { if (child.getType() == FileType.FILE && fpm.validate(child.getName().getBaseName().toLowerCase())) { outputResult = child.getName().getPath(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } else if (child.getType() == FileType.FOLDER && "true".equals(recursiveSearch)) { searchSubFolders(child, filePattern, messageContext, factory, result, ns); } } catch (IOException e) { handleException("Unable to search a file.", e, messageContext); } finally { try { if (child != null) { child.close(); } } catch (IOException e) { log.error("Error while closing Directory: " + e.getMessage(), e); } } } messageContext.getEnvelope().getBody().addChild(result); } else { log.error("File location does not exist."); } } catch (IOException e) { handleException("Unable to search a file.", e, messageContext); } finally { if (manager != null) { manager.close(); } } } }
From source file:org.wso2.carbon.connector.FileSearch.java
/** * * @param child sub folder/*from w ww.j a v a 2s .c o m*/ * @param filePattern pattern of the file to be searched * @param messageContext the message context that is generated for processing the file * @param factory OMFactory * @param result OMElement * @param ns OMNamespace * @throws IOException */ private void searchSubFolders(FileObject child, String filePattern, MessageContext messageContext, OMFactory factory, OMElement result, OMNamespace ns) throws IOException { List<FileObject> fileList = new ArrayList<FileObject>(); getAllFiles(child, fileList, messageContext); FilePattenMatcher fpm = new FilePattenMatcher(filePattern); String outputResult; try { for (FileObject file : fileList) { if (file.getType() == FileType.FILE) { if (fpm.validate(file.getName().getBaseName().toLowerCase())) { outputResult = file.getName().getPath(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } } else if (file.getType() == FileType.FOLDER) { searchSubFolders(file, filePattern, messageContext, factory, result, ns); } } } catch (IOException e) { handleException("Unable to search a file in sub folder.", e, messageContext); } finally { try { if (child != null) { child.close(); } } catch (IOException e) { log.error("Error while closing Directory: " + e.getMessage(), e); } } }
From source file:org.wso2.carbon.connector.FileSearchConnector.java
/** * List the all files of given pattern./*from w w w. ja va 2 s.c o m*/ * * @param messageContext The message context that is generated for processing the file. */ private void searchFile(MessageContext messageContext) { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); boolean enableRecursiveSearch = false; String recursiveSearchParameter = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.RECURSIVE_SEARCH); if (StringUtils.isNotEmpty(recursiveSearchParameter)) { enableRecursiveSearch = Boolean.parseBoolean(recursiveSearchParameter); } StandardFileSystemManager manager = FileConnectorUtils.getManager(); if (StringUtils.isEmpty(filePattern)) { throw new SynapseException("FilePattern should not be null"); } try { FileSystemOptions opt = FileConnectorUtils.init(messageContext); FileObject remoteFile = manager.resolveFile(source, opt); if (!remoteFile.exists()) { throw new SynapseException("File location does not exist"); } FileObject[] children = remoteFile.getChildren(); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace(FileConstants.FILECON, FileConstants.NAMESPACE); OMElement result = factory.createOMElement(FileConstants.RESULT, ns); ResultPayloadCreator.preparePayload(messageContext, result); FilePattenMatcher fpm = new FilePattenMatcher(filePattern); for (FileObject child : children) { try { if (FileType.FILE.equals(child.getType()) && fpm.validate(child.getName().getBaseName())) { String outputResult = child.getName().getPath(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } else if (FileType.FOLDER.equals(child.getType()) && enableRecursiveSearch) { searchSubFolders(child, filePattern, factory, result, ns); } } catch (FileSystemException e) { throw new SynapseException("Unable to search a file.", e); } finally { try { if (child != null) { child.close(); } } catch (IOException e) { log.error("Error while closing Directory: " + e.getMessage(), e); } } } messageContext.getEnvelope().getBody().addChild(result); } catch (FileSystemException e) { throw new SynapseException("Unable to search a file for a given pattern.", e); } finally { manager.close(); } }
From source file:org.wso2.carbon.connector.FileSearchConnector.java
/** * Search the files of given pattern inside the sub directory. * * @param child Child folder./* w w w . ja v a 2 s. co m*/ * @param filePattern Pattern of the file to be searched. * @param factory OMFactory. * @param result OMElement. * @param ns OMNamespace. * @throws FileSystemException On error getting file type. */ private void searchSubFolders(FileObject child, String filePattern, OMFactory factory, OMElement result, OMNamespace ns) throws FileSystemException { List<FileObject> fileList = new ArrayList<>(); Collections.addAll(fileList, child.getChildren()); FilePattenMatcher fpm = new FilePattenMatcher(filePattern); String outputResult; try { for (FileObject file : fileList) { if (FileType.FILE.equals(file.getType())) { if (fpm.validate(file.getName().getBaseName().toLowerCase())) { outputResult = file.getName().getPath(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } } else if (FileType.FOLDER.equals(file.getType())) { searchSubFolders(file, filePattern, factory, result, ns); } } } catch (FileSystemException e) { throw new SynapseException("Unable to search files in sub folder", e); } finally { try { child.close(); } catch (IOException e) { log.error("Error while closing Directory: " + e.getMessage(), e); } } }
From source file:org.wso2.carbon.connector.util.FileConnectorUtils.java
/** * @param remoteFile Location of the remote file * @return true/false//from w w w . j a v a2 s . c o m */ public static boolean isFolder(FileObject remoteFile) { boolean isFolder = false; if (StringUtils.isEmpty(remoteFile.getName().getExtension())) { isFolder = true; } return isFolder; }
From source file:org.wso2.carbon.connector.util.ResultPayloadCreate.java
/** * @param file Read file/*from ww w . j av a 2 s.c om*/ * @param msgCtx Message Context * @param contentType content type * @param streaming streaming mode (true/false) * @return return the status * @throws SynapseException */ @SuppressWarnings("ResultOfMethodCallIgnored") public static boolean buildFile(FileObject file, MessageContext msgCtx, String contentType) throws SynapseException { ManagedDataSource dataSource = null; try { if (StringUtils.isEmpty(contentType) || StringUtils.isEmpty(contentType.trim())) { 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. String charSetEnc = null; try { charSetEnc = new ContentType(contentType).getParameter("charset"); } catch (ParseException ex) { log.warn("Invalid encoding type.", ex); } msgCtx.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); } 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 InputStream in = null; 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); } //We need this to build the complete message before closing the stream //noinspection ResultOfMethodCallIgnored documentElement.toString(); msgCtx.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement)); } 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.connector.util.ResultPayloadCreater.java
public static boolean buildFile(FileObject file, MessageContext msgCtx, String contentType, String streaming) throws SynapseException { ManagedDataSource dataSource = null; try {//from w w w .j a v a 2 s. com 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) { log.warn("Invalid encoding type.", ex); } msgCtx.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); } 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 (contentType == null) { 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 InputStream in; if (builder instanceof DataSourceMessageBuilder && "true".equals(streaming)) { in = null; dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType)); } 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); } //We need this to build the complete message before closing the stream documentElement.toString(); msgCtx.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement)); } 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.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 w w .j a v a2 s. c o m * @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; }