Skip to content

Commit

Permalink
Merge pull request #212 from malakaganga/add_smb2
Browse files Browse the repository at this point in the history
Add SMB2 support to file connector
  • Loading branch information
malakaganga authored Apr 18, 2024
2 parents e8e3c0b + a166894 commit e026d65
Show file tree
Hide file tree
Showing 27 changed files with 366 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.wso2.carbon.esb.connector</groupId>
<artifactId>org.wso2.carbon.esb.connector.file</artifactId>
<version>4.0.25</version>
<version>4.0.26</version>
<packaging>jar</packaging>
<name>WSO2 Carbon - Connector For esb-connector-file</name>
<url>http://wso2.org</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public enum FileServerProtocol {
LOCAL("LOCAL", false),
FTP("FTP", false),
FTPS("FTPS", true),
SFTP("SFTP", true);
SFTP("SFTP", true),
SMB2("SMB2", true),
SMB("SMB", true);


private final String name;
private final boolean secure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ private void setupFSO(FileSystemOptions fso, ConnectionConfiguration fsConfig)
case SFTP:
fileSystemSetup = new SFTPFileSystemSetup();
break;
case SMB:
fileSystemSetup = new SMBFileSystemSetup();
break;
case SMB2:
fileSystemSetup = new SMB2FileSystemSetup();
break;
default:
throw new IllegalStateException("Unexpected protocol value: " + fsConfig.getProtocol());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.connector.connection;

import org.apache.commons.vfs2.FileSystemOptions;
import org.wso2.carbon.connector.pojo.ConnectionConfiguration;
import org.wso2.carbon.connector.utils.Const;

/**
* Sets up local file system.
*/
public class SMB2FileSystemSetup implements ProtocolBasedFileSystemSetup {

@Override
public String setupFileSystemHandler(FileSystemOptions fso, ConnectionConfiguration fsConfig) {

return Const.SMB2_PROTOCOL_PREFIX + constructVfsUrl(fsConfig);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.connector.connection;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.wso2.carbon.connector.core.pool.ConnectionFactory;
import org.wso2.carbon.connector.pojo.ConnectionConfiguration;

public class SMBConnectionFactory implements ConnectionFactory {
private ConnectionConfiguration connectionConfiguration;
public SMBConnectionFactory(ConnectionConfiguration connectionConfiguration) {
this.connectionConfiguration = connectionConfiguration;
}
@Override
public Object makeObject() throws Exception {
FileSystemHandler fileSystemConnection = new FileSystemHandler(connectionConfiguration);
return fileSystemConnection;
}

@Override
public void destroyObject(Object connection) throws Exception {
((FileSystemHandler) connection).close();
}

@Override
public boolean validateObject(Object connection) {
try {
FileSystemHandler fileSystemHandlerConnection = (FileSystemHandler) connection;
String filePath = fileSystemHandlerConnection.getBaseDirectoryPath();
FileSystemManager fsManager = fileSystemHandlerConnection.getFsManager();
FileSystemOptions fso = fileSystemHandlerConnection.getFsOptions();

// Use try-with-resources statement for the FileObject to ensure it's properly closed
try (FileObject fileObject = fsManager.resolveFile(filePath, fso)) {
// Attempt to perform a simple operation on the root directory.
// Checking for existence is a minimal, non-intrusive operation.
return fileObject.exists(); // This throws an exception if the connection is not valid
}
} catch (Throwable e) {
return false;
}
}

@Override
public void activateObject(Object o) throws Exception {
// Nothing to do here
}

@Override
public void passivateObject(Object o) throws Exception {
// Nothing to do here
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.connector.connection;

import org.apache.commons.vfs2.FileSystemOptions;
import org.wso2.carbon.connector.pojo.ConnectionConfiguration;
import org.wso2.carbon.connector.utils.Const;

/**
* Sets up local file system.
*/
public class SMBFileSystemSetup implements ProtocolBasedFileSystemSetup {

@Override
public String setupFileSystemHandler(FileSystemOptions fso, ConnectionConfiguration fsConfig) {

return Const.SMB_PROTOCOL_PREFIX + constructVfsUrl(fsConfig);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.synapse.core.SynapseEnvironment;
import org.wso2.carbon.connector.connection.FileSystemHandler;
import org.wso2.carbon.connector.connection.SFTPConnectionFactory;
import org.wso2.carbon.connector.connection.SMBConnectionFactory;
import org.wso2.carbon.connector.core.AbstractConnector;
import org.wso2.carbon.connector.core.ConnectException;
import org.wso2.carbon.connector.core.connection.ConnectionHandler;
Expand Down Expand Up @@ -79,6 +80,15 @@ public void connect(MessageContext messageContext) throws ConnectException {
handler.createConnection(Const.CONNECTOR_NAME, tenantSpecificConnectionName,
new SFTPConnectionFactory(configuration), configuration.getConfiguration());
}
} else if ("SMB".equalsIgnoreCase(configuration.getProtocol().getName())
|| "SMB2".equalsIgnoreCase(configuration.getProtocol().getName())) {
try {
handler.createConnection(Const.CONNECTOR_NAME, tenantSpecificConnectionName,
new SMBConnectionFactory(configuration), configuration.getConfiguration(), messageContext);
} catch (NoSuchMethodError e) {
handler.createConnection(Const.CONNECTOR_NAME, tenantSpecificConnectionName,
new SMBConnectionFactory(configuration), configuration.getConfiguration());
}
} else if (!handler.checkIfConnectionExists(connectorName, tenantSpecificConnectionName)) {
FileSystemHandler fileSystemHandler = new FileSystemHandler(configuration);
try {
Expand Down Expand Up @@ -158,7 +168,7 @@ private ConnectionConfiguration getConnectionConfigFromContext(MessageContext ms
private RemoteServerConfig getRemoteServerConfig(MessageContext msgContext, ConnectionConfiguration configuration)
throws InvalidConfigurationException {

RemoteServerConfig remoteServerConfig;
RemoteServerConfig remoteServerConfig = null;

switch (configuration.getProtocol()) {
case FTP:
Expand All @@ -179,6 +189,12 @@ private RemoteServerConfig getRemoteServerConfig(MessageContext msgContext, Conn
setSFTPConnectionConfigsFromContext(msgContext, (SFTPConnectionConfig) remoteServerConfig);
configuration.setRemoteServerConfig(remoteServerConfig);
break;
case SMB:
case SMB2:
remoteServerConfig = new RemoteServerConfig();
setCommonRemoteServerConfigs(msgContext, remoteServerConfig);
configuration.setRemoteServerConfig(remoteServerConfig);
break;
default:
throw new InvalidConfigurationException("Unknown File Connector protocol");
}
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/org/wso2/carbon/connector/operations/WriteFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,17 +369,23 @@ private long writeToFile(FileObject targetFile, MessageContext msgCtx, Config co
throw new FileOperationException("Target file already exists. Path = "
+ targetFile.getURL());
} else {
// Create a temporary file with .tmp extension
FileObject tempFile = targetFile.getFileSystem().getFileSystemManager().resolveFile(targetFile.getParent(),
targetFile.getName().getBaseName() + ".tmp");
tempFile.createFile();
if (contentToWriteIsProvided) {
writtenBytesCount = performContentWrite(tempFile, config);
} else {
writtenBytesCount = performBodyWrite(tempFile, msgCtx, false, config);
try (FileObject tempFile = targetFile.getFileSystem().getFileSystemManager().resolveFile(
targetFile.getParent(), targetFile.getName().getBaseName() + ".tmp")) {

// Create a temporary file with .tmp extension
tempFile.createFile();

// Write content to the temporary file based on a condition
if (contentToWriteIsProvided) {
writtenBytesCount = performContentWrite(tempFile, config);
} else {
writtenBytesCount = performBodyWrite(tempFile, msgCtx, false, config);
}

// Rename temporary file to original file
tempFile.moveTo(targetFile);

}
// Rename temporary file to original file
tempFile.moveTo(targetFile);
}
break;
case OVERWRITE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Configurations common to any remote file server.
*/
public abstract class RemoteServerConfig {
public class RemoteServerConfig {

//Protocol associated with this connection
private String protocol;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/wso2/carbon/connector/utils/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public final class Const {
public static final String FTP_PROTOCOL_PREFIX = "ftp://";
public static final String FTPS_PROTOCOL_PREFIX = "ftps://";
public static final String SFTP_PROTOCOL_PREFIX = "sftp://";
public static final String SMB_PROTOCOL_PREFIX = "smb://";
public static final String SMB2_PROTOCOL_PREFIX = "smb2://";
//FILE.Separator is not needed for VFS. Windows also support /
public static final String FILE_SEPARATOR = "/";

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config/init.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<template name="init" onError="fault" xmlns="http://ws.apache.org/ns/synapse">
<!--common params-->
<parameter name="name" description="Unique name the connection is identified by"/>
<parameter name="connectionType" description="Protocol to communicate with file system. Possible values 'LOCAL', 'FTP', 'FTPS', 'SFTP'"/>
<parameter name="connectionType" description="Protocol to communicate with file system. Possible values 'LOCAL', 'SMB2', 'FTP', 'FTPS', 'SFTP'"/>
<parameter name="workingDir" description="Working directory. File paths in operations should be given w.r.t this folder"/>
<parameter name="fileLockScheme" description="File locking behaviour to use. Local or Cluster" />
<parameter name="maxFailureRetryCount" description="Max retry count upon file operation failure"/>
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/checkExists.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/compress.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/copy.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/createDirectory.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/delete.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/exploreZipFile.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/fetchDirectoryContent.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/listFiles.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/mergeFiles.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/move.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/uischema/read.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"LOCAL",
"FTP",
"FTPS",
"SFTP"
"SFTP",
"SMB2"
],
"defaultType": "connection.local",
"defaultValue": "",
Expand Down
Loading

0 comments on commit e026d65

Please sign in to comment.