getting a file from an smb share

0
Hi,  I'm trying to make a user specified scheduled event that connects to a smb share, navigates to a user specified directory within the share, and imports the found excel file. I found documentation for some java actions here: https://docs.mendix.com/howto/extensibility/access-a-samba-share-from-the-mxcloud. However, I am confused as to which parts of jcifs to import into my java action. I tried the following code. In addition the server needs to be entered as the servername because the ip address changes when I ping using the command prompt.  package importutility.actions; import java.io.IOException; import java.io.OutputStream; import org.apache.poi.EncryptedDocumentException; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.systemwideinterfaces.core.IMendixObject; import com.mendix.webui.CustomJavaAction; import jcifs.smb.*; import jcifs.netbios.*; import jcifs.resolveOrder; public class JA_GetSharedFile extends CustomJavaAction<IMendixObject> { private IMendixObject __XLSFileParameter1; private importutility.proxies.XLSFile XLSFileParameter1; private java.lang.String ImportDirectory; private java.lang.String FileExtension; public JA_GetSharedFile(IContext context, IMendixObject XLSFileParameter1, java.lang.String ImportDirectory, java.lang.String FileExtension) { super(context); this.__XLSFileParameter1 = XLSFileParameter1; this.ImportDirectory = ImportDirectory; this.FileExtension = FileExtension; } @Override public IMendixObject executeAction() throws Exception { this.XLSFileParameter1 = __XLSFileParameter1 == null ? null : importutility.proxies.XLSFile.initialize(getContext(), __XLSFileParameter1); // BEGIN USER CODE String server = "String"; String user = "String"; String pass = "String"; NTLMFileHandler fileHandler = new NTLMFileHandler(getContext(), server, user, pass); fileHandler.getSingleFile(this.SMBConfig.getImportFolder(), this.XLSFileParameter1.getName(getContext()), this.XLSFileParameter1.getMendixObject(), this.XLSFileParameter1.getDeleteAfterDownload());; return fileHandler; // END USER CODE } /** * Returns a string representation of this action */ @Override public java.lang.String toString() { return "JA_GetSharedFile"; } // BEGIN EXTRA CODE // END EXTRA CODE } I've already gone to Project/Properties/Java Build Path and added the jcifs.jar but it says that the 'import jcifs.resolveOrder;' cannot be resolved. the same goes for the creation of the NTLMFileHandler object. I have little experience with java and object oriented coding.  Thanks, Benjamin Griggs
asked
2 answers
0

You should not have to add the .jar to your build path manually.

If you place it in the userlib folder of your project, that should be enough.

Also, you import statements look suspicious. Are you using any Java ide at all?

answered
0

This is what I've got now and it stops at           

 Core.storeFileDocumentContent(context, XLSFile,"filename", in);

no exception it just takes either 10 minutes to finish it or never does.

I really dont understand what input streams are and how they work, if I'm doing something redundant or stupid let me know.

public class JA_GetSharedFile extends CustomJavaAction<IMendixObject>
{
	private IMendixObject __XLSFileParameter1;
	private importutility.proxies.XLSFile XLSFileParameter1;
	private java.lang.String ImportDirectory;
	private java.lang.String FileExtension;

	public JA_GetSharedFile(IContext context, IMendixObject XLSFileParameter1, java.lang.String ImportDirectory, java.lang.String FileExtension)
	{
		super(context);
		this.__XLSFileParameter1 = XLSFileParameter1;
		this.ImportDirectory = ImportDirectory;
		this.FileExtension = FileExtension;
	}

	@Override
	public IMendixObject executeAction() throws Exception
	{
		this.XLSFileParameter1 = __XLSFileParameter1 == null ? null : importutility.proxies.XLSFile.initialize(getContext(), __XLSFileParameter1);

		// BEGIN USER CODE
		SharedFileGetter GetHer = new SharedFileGetter();
		IContext context = this.getContext();
		IMendixObject ScheduledFile = null;
		
		if (ImportDirectory == null)
		{
			ScheduledFile = GetHer.GetFileUnspecified(context, __XLSFileParameter1);
		}
		else
		{
			ScheduledFile = GetHer.GetFileFromDirectory(context, __XLSFileParameter1, ImportDirectory, FileExtension);
		}
		
		return ScheduledFile;
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@Override
	public java.lang.String toString()
	{
		return "JA_GetSharedFile";
	}

	// BEGIN EXTRA CODE
	private class SharedFileGetter
	{
		public IMendixObject GetFileFromDirectory(IContext context, IMendixObject XLSFile, String ImportDirectory, String FileExtension) throws IOException 
		{
			String user = "user";
			String pass = "pass";
			String server = "smb://server/0412_Project_Request_Form_Master_Data_export-en-us.xlsx";
			// Authenticate
			NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, user, pass);
			SmbFile dir = new SmbFile(server, auth);
			SmbFileInputStream in = new SmbFileInputStream(dir);
			FileOutputStream out = new FileOutputStream(dir.getName() );
			        
			Core.storeFileDocumentContent(context, XLSFile, in);
			out.close();
			in.close(); 
			return XLSFile;
		}
		public IMendixObject GetFileUnspecified(IContext context, IMendixObject XLSFile) throws IOException 
		{
			String user = "pass";
			String pass = "user";
			String server = "smb://server/0412_Project_Request_Form_Master_Data_export-en-us.xlsx";
			// Authenticate
            SmbFile scheduledFile = null;
			NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", user, pass);
			scheduledFile = new SmbFile(server, auth);
			InputStream in = scheduledFile.getInputStream();
			Core.storeFileDocumentContent(context, XLSFile,"0412_Project_Request_Form_Master_Data_export-en-us", in);
			in.close(); 
			return XLSFile;
		}
	}
	// END EXTRA CODE
}

 

answered