Wednesday, November 20, 2019

Working with shared files in remote server using SMB in Java

Introduction

We are used to working with shared files where sharing is limited to users either in some domain to certain users. The remote application needs to access the shared resource and take actions such as add, delete resources in the shared location.

A server has shared resources that typically uses the Server Message Block (SMB) protocol (Server Message Block)in the Windows machine. The SMB protocol enables an application to access files on the remote servers, as well as other resources allowing a client application to open, read, move, update files on the remote server. The resource is limited to certain users. The user needs to pass credentials to access the shared resource. The activities that the user can do on the shared resource is defined in the sharing permission. Let's assume that the user has both read and write permission.

In this article, I am demonstrating a use case where an application needs to interact with shared files in Windows server machines using a simple java application. The use case makes the following assumption:

  • The server is a windows machine
  • There exists a local user named 'Test' and the password for the user is 'Password'. 
  • The shared location is '127.0.0.0.1\temp' 
  • The user 'Test' has access to the shared location 'temp' which can be anywhere in the machine that has IP of '127.0.0.1'.
  • The client application has access to the network

CodeBase:

The application that we are designing is in Java using Maven for build and dependency management. I am using JCIFS, an open-source client library that implements the CIFS/SMB networking protocol (https://www.jcifs.org/). The library is available from Maven Repository (https://mvnrepository.com/artifact/jcifs/jcifs/1.3.17).

The dependencies are shown in the following pom.xml file:
Currently, the application has one file Main.java with the following content:

Here, the main method uses a url pattern of for smb protocol and points to shared location at line 8.
Local variables for userName, password and domains are initialized in lines 9 through 11 and authenticates the user in line 13 at which time, the application has successfully connected to the shared folder.

The application creates a SmbFile object in line 15 and passes that folder to a method called doRecursiveLookup(..) in line 24. The method simply iterates through all the files and folders within the shared location and prints the names in the console in a recursive fashion. Note that the SmbFile object is the same for file or folders and .isDirectory() method was used to test if the object in scope is file or folder.

In the demo, I showed reading the resources in the remote server using SMB protocol.

Update:

Some institutions tend to disable SMB 1.0 in which case we need to use library from https://repo1.maven.org/maven2/org/codelibs/jcifs/ for newer version of the protocol. 

Additional Resources