Skip to content

Commit 0418f37

Browse files
Merge pull request #6 from marlonrcfranco/develop
v1.0
2 parents b4d0962 + e628771 commit 0418f37

25 files changed

Lines changed: 116 additions & 37 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>groupId</groupId>
88
<artifactId>jcon</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.0</version>
1010
<dependencies>
1111
<dependency>
1212
<groupId>org.slf4j</groupId>

src/main/java/com/marlonrcfranco/IJcon.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marlonrcfranco;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
45

56
public interface IJcon {
67

@@ -85,7 +86,7 @@ enum types {
8586
* @throws IOException
8687
*/
8788
public String listFiles(String IP, String filePath, String user, String pass) throws IOException;
88-
89+
public ArrayList listFilesAsList(String IP, String filePath, String user, String pass) throws Exception;
8990
/**
9091
* public String copyFileTo(String sourceIP, String sourceFilePath, String destIP, String destFilePath, String user, String pass)
9192
*

src/main/java/com/marlonrcfranco/Jcon.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marlonrcfranco;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
45

56
public class Jcon implements IJcon{
67
private IJcon jcon;
@@ -51,6 +52,11 @@ public String listFiles(String IP, String filePath, String user, String pass) th
5152
return this.jcon.listFiles(IP,filePath,user,pass);
5253
}
5354

55+
@Override
56+
public ArrayList<String> listFilesAsList(String IP, String filePath, String user, String pass) throws Exception {
57+
return null;
58+
}
59+
5460
@Override
5561
public String copyFileTo(String sourceIP, String sourceFilePath, String destIP, String destFilePath, String user, String pass) throws IOException {
5662
return this.jcon.copyFileTo(sourceIP,sourceFilePath,destIP,destFilePath,user,pass);

src/main/java/com/marlonrcfranco/JconFileSystem.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marlonrcfranco;
22

33
import java.io.*;
4+
import java.util.ArrayList;
45

56
public class JconFileSystem implements IJcon{
67

@@ -84,7 +85,6 @@ public byte[] writeBytes(String IP, String filePath, String user, String pass, b
8485
} catch (IOException e) {
8586
output=("Erro: Não foi possível escrever no arquivo \""+filePath+"\""
8687
+((e.getMessage() != null)?" ("+e.getMessage()+")":"")).getBytes();
87-
8888
}
8989
finally {
9090
if (file != null) file.close();
@@ -139,22 +139,31 @@ public String listFiles(String filePath) throws IOException {
139139
@Override
140140
public String listFiles(String IP, String filePath, String user, String pass) throws IOException {
141141
String output="";
142-
filePath = filePath.replace("\\", "/");
143-
if (!filePath.endsWith("/")) filePath+="/";
144-
File curDir = null;
145142
try {
146-
curDir = new File(filePath);
147-
File[] filesList = curDir.listFiles();
148-
for(File f : filesList){
149-
output += f.getName()+(f.isDirectory()? "/":"")+"\n";
143+
ArrayList<File> listFiles = listFilesAsList(IP, filePath, user, pass);
144+
for (File file : listFiles) {
145+
output += file + "\n";
150146
}
151147
} catch (Exception e) {
152-
output = "Error: Could not list the files in \""+filePath+"\".";
148+
output += "Error: Could not list the files in \""+filePath+"\".";
153149
if (e.getMessage() != null) output+=" ("+e.getMessage()+")";
154150
}
155151
return output;
156152
}
157153

154+
@Override
155+
public ArrayList<File> listFilesAsList(String IP, String filePath, String user, String pass) throws Exception {
156+
ArrayList<File> output = new ArrayList<>();
157+
filePath = filePath.replace("\\", "/");
158+
if (!filePath.endsWith("/")) filePath+="/";
159+
File curDir = new File(filePath);
160+
File[] filesList = curDir.listFiles();
161+
for(File f : filesList){
162+
output.add(f);
163+
}
164+
return output;
165+
}
166+
158167
public String copyFileTo(String sourceFilePath, String destFilePath) throws IOException{
159168
return copyFileTo("",sourceFilePath,"",destFilePath,"","");
160169
}

src/main/java/com/marlonrcfranco/JconNFS.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.emc.ecs.nfsclient.rpc.CredentialUnix;
88

99
import java.io.*;
10+
import java.util.ArrayList;
1011
import java.util.List;
1112

1213
public class JconNFS implements IJcon{
@@ -136,6 +137,11 @@ public String listFiles(String IP, String filePath, String user, String pass) th
136137
return listFiles(IP, sharedFolder, sFilePath, user, pass);
137138
}
138139

140+
@Override
141+
public ArrayList<String> listFilesAsList(String IP, String filePath, String user, String pass) throws IOException {
142+
return null;
143+
}
144+
139145
public String listFiles(String IP, String sharedFolder, String path, String user, String pass) {
140146
String output="";
141147
path = path.replace("\\", "/");

src/main/java/com/marlonrcfranco/JconSMB1.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.net.MalformedURLException;
77
import java.net.UnknownHostException;
8+
import java.util.ArrayList;
89

910
public class JconSMB1 implements IJcon {
1011

@@ -133,25 +134,37 @@ public String listFiles(String IP, String filePath, String user, String pass) th
133134
String output="";
134135
filePath=filePath.replace("\\", "/");
135136
if (!filePath.endsWith("/") && !"".equalsIgnoreCase(filePath.trim())) filePath+="/";
136-
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
137137
String path="smb://"+IP+"/"+filePath;
138-
SmbFile smbFile=null;
139138
try {
140-
smbFile = new SmbFile(path,auth);
141-
SmbFile[] aSmbFiles = smbFile.listFiles();
139+
ArrayList<SmbFile> aSmbFiles = listFilesAsList(IP, filePath, user, pass);
142140
for (SmbFile smbF : aSmbFiles) {
143141
output+=smbF.getName()+"\n";
144142
}
145143
} catch (MalformedURLException e) {
146144
output="Erro: Nao foi possivel localizar o caminho \"" + path + "\"";
147145
if (e.getMessage() != null) output+=" ("+e.getMessage()+")";
148-
}catch (SmbException e) {
146+
}catch (Exception e) {
149147
output="Erro: Verifique se o usuário e senha estão corretos, e se possui permissão para acessar o caminho \"" + path + "\"";
150148
if (e.getMessage() != null) output+=" ("+e.getMessage()+")";
151149
}
152150
return output;
153151
}
154152

153+
@Override
154+
public ArrayList<SmbFile> listFilesAsList(String IP, String filePath, String user, String pass) throws Exception {
155+
ArrayList<SmbFile> output = new ArrayList<>();
156+
filePath=filePath.replace("\\", "/");
157+
if (!filePath.endsWith("/") && !"".equalsIgnoreCase(filePath.trim())) filePath+="/";
158+
String path="smb://"+IP+"/"+filePath;
159+
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
160+
SmbFile smbFile = new SmbFile(path,auth);
161+
SmbFile[] aSmbFiles = smbFile.listFiles();
162+
for (SmbFile smbF : aSmbFiles) {
163+
output.add(smbF);
164+
}
165+
return output;
166+
}
167+
155168
@Override
156169
public String copyFileTo(String sourceIP, String sourceFilePath, String destIP, String destFilePath, String user, String pass) throws IOException {
157170
String output="";

src/main/java/com/marlonrcfranco/JconSMB23.java

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.IOException;
1818
import java.io.OutputStream;
19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.EnumSet;
2122
import java.util.HashSet;
@@ -202,36 +203,53 @@ public String copyFileTo(String sourceIP, String sourceFilePath, String destIP,
202203
@Override
203204
public String listFiles(String IP, String filePath, String user, String pass) throws IOException {
204205
extractSharedPathFromPath(filePath.replace("\\", "/"));
205-
return listFiles(IP, sharedFolder, sFilePath, user, pass, null);
206-
}
207-
208-
public String listFiles(String IP, String sharedFolder, String path, String user, String pass, String domain) {
209206
String output="";
207+
boolean isDirectory = false;
210208
sharedFolder = parsePath(sharedFolder);
211-
path = parsePath(path);
209+
String path = parsePath(sFilePath);
212210
if (!path.trim().endsWith("/") && !"".equalsIgnoreCase(path.trim())) path+="/";
213-
boolean isDirectory = false;
214-
SMBClient client = new SMBClient();
215-
try (Connection connection = client.connect(IP)) {
216-
AuthenticationContext ac = new AuthenticationContext(user, pass.toCharArray(),domain);
217-
Session session = connection.authenticate(ac);
218-
// Connect to Share
219-
try (DiskShare share = (DiskShare) session.connectShare(sharedFolder)) {
220-
for (FileIdBothDirectoryInformation f : share.list(path, "*")) {
221-
isDirectory = f.getFileAttributes()==FileAttributes.FILE_ATTRIBUTE_DIRECTORY.getValue();
222-
output+= f.getFileName() + (isDirectory? "/" : "") + "\n";
223-
}
224-
} catch (SMBApiException e) {
225-
output="Erro: Nao foi possivel localizar o diretorio "+sharedFolder+(!"".equalsIgnoreCase((sharedFolder+path).trim())? "/" : "")+path;
226-
if (e.getMessage() != null) output+=" ("+e.getMessage()+")";
211+
try {
212+
ArrayList<FileIdBothDirectoryInformation> list = listFilesAsList(IP, sharedFolder, sFilePath, user, pass, null);
213+
for (FileIdBothDirectoryInformation f : list) {
214+
isDirectory = f.getFileAttributes()==FileAttributes.FILE_ATTRIBUTE_DIRECTORY.getValue();
215+
output+= f.getFileName() + (isDirectory? "/" : "") + "\n";
227216
}
228217
} catch (IOException e) {
229-
output="Erro: Nao foi possivel listar os arquivos do diretorio: "+sharedFolder;
218+
output="Erro: Nao foi possivel listar os arquivos do diretorio: "+sharedFolder;
219+
if (e.getMessage() != null) output+=" ("+e.getMessage()+")";
220+
} catch (Exception e) {
221+
output="Erro: Nao foi possivel localizar o diretorio "+sharedFolder+(!"".equalsIgnoreCase((sharedFolder+path).trim())? "/" : "")+path;
230222
if (e.getMessage() != null) output+=" ("+e.getMessage()+")";
231223
}
232224
return output;
233225
}
234226

227+
@Override
228+
public ArrayList<FileIdBothDirectoryInformation> listFilesAsList(String IP, String filePath, String user, String pass) throws Exception {
229+
extractSharedPathFromPath(filePath.replace("\\", "/"));
230+
return listFilesAsList(IP, sharedFolder, sFilePath, user, pass, null);
231+
}
232+
233+
public ArrayList<FileIdBothDirectoryInformation> listFilesAsList(String IP, String sharedFolder, String path, String user, String pass, String domain) throws Exception {
234+
ArrayList<FileIdBothDirectoryInformation> output = new ArrayList<>();
235+
sharedFolder = parsePath(sharedFolder);
236+
path = parsePath(path);
237+
if (!path.trim().endsWith("/") && !"".equalsIgnoreCase(path.trim())) path+="/";
238+
SMBClient client = new SMBClient();
239+
Connection connection = client.connect(IP);
240+
AuthenticationContext ac = new AuthenticationContext(user, pass.toCharArray(),domain);
241+
Session session = connection.authenticate(ac);
242+
// Connect to Share
243+
DiskShare share = (DiskShare) session.connectShare(sharedFolder);
244+
for (FileIdBothDirectoryInformation f : share.list(path, "*")) {
245+
output.add(f);
246+
}
247+
if (share!=null) share.close();
248+
if (session!=null) session.close();
249+
if (connection!=null) connection.close();
250+
return output;
251+
}
252+
235253
private String parsePath(String path) {
236254
while (path.startsWith("\\") || path.startsWith("/")) {
237255
path = path.substring(1);

src/main/java/com/marlonrcfranco/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void main(String[] args) {
2929
"\n║ ║ ╠═══╦═══╣ ╚╗ ║║ ║" +
3030
"\n║ ╔═╗ ║ ║ ╔═╣╔═╗║╔╗╚╗║║ ║" +
3131
"\n║ ║ ╚═╝ ║ ╚═╣╚═╝║║╚╗╚╝║ ║" +
32-
"\n║ ╚═════╩═══╩═══╩╝ ╚══╝ v0.1 ║" +
32+
"\n║ ╚═════╩═══╩═══╩╝ ╚══╝ v"+Util.getVersion()+" ║" +
3333
"\n║ @marlonrcfranco ║" +
3434
"\n║ https://github.com/marlonrcfranco/jcon ║" +
3535
"\n║ ║" +

src/main/java/com/marlonrcfranco/Util.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ public static byte[] toByteArray(InputStream in) throws IOException {
1919
}
2020
return os.toByteArray();
2121
}
22+
23+
public static String getVersion() {
24+
return "1.0";
25+
}
2226
}

src/main/java/tests/JconFileSystemTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.junit.jupiter.api.BeforeEach;
55
import org.junit.jupiter.api.Test;
66

7+
import java.io.File;
78
import java.io.IOException;
9+
import java.util.ArrayList;
810

911
class JconFileSystemTest {
1012

@@ -173,11 +175,16 @@ public void listFiles() {
173175
System.out.println(response);
174176
assert response.contains("teste10.xml");
175177

178+
ArrayList<File> list = jconFS.listFilesAsList("","C:\\Users\\marlon.franco\\Documents","","");
179+
assert list.size()==response.split("\\n").length;
180+
176181
response = jconFS.delete( "C:\\Users\\marlon.franco\\Documents\\teste10.xml");
177182
assert !response.contains("Erro");
178183
System.out.println(response);
179184
} catch (IOException e) {
180185
assert false;
186+
} catch (Exception e) {
187+
assert false;
181188
}
182189
}
183190

0 commit comments

Comments
 (0)