diff --git a/jcon.iml b/jcon.iml index 8b76f44..4170409 100644 --- a/jcon.iml +++ b/jcon.iml @@ -1,6 +1,6 @@ - + @@ -9,53 +9,19 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 995555f..159da30 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,28 @@ groupId jcon 1.0-SNAPSHOT + + + org.slf4j + slf4j-simple + 1.7.28 + + + com.hierynomus + smbj + 0.9.1 + + + jcifs + jcifs + 1.3.17 + + + org.junit.jupiter + junit-jupiter-api + 5.5.1 + + @@ -17,15 +39,44 @@ 9 + + org.apache.maven.plugins + maven-dependency-plugin + 2.6 + + + unpack-dependencies + package + + unpack-dependencies + + + system + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + META-INF/*.* + junit,org.mockito,org.hamcrest + ${project.build.directory}/classes + + + + + + maven-assembly-plugin + + jcon + false + + + com.marlonrcfranco.Main + + + + jar-with-dependencies + + + - - - com.hierynomus - smbj - 0.9.1 - - - - \ No newline at end of file diff --git a/src/main/java/com/marlonrcfranco/IJcon.java b/src/main/java/com/marlonrcfranco/IJcon.java index 781da6a..56072af 100644 --- a/src/main/java/com/marlonrcfranco/IJcon.java +++ b/src/main/java/com/marlonrcfranco/IJcon.java @@ -4,6 +4,10 @@ public interface IJcon { + enum types { + FILESYSTEM,SMB1,SMB23,NFS + } + /** * public String read(String IP, String filePath, String user, String pass) * @@ -23,6 +27,7 @@ public interface IJcon { * @throws IOException */ public String read(String IP, String filePath, String user, String pass) throws IOException; + public byte[] readBytes(String IP, String filePath, String user, String pass) throws IOException; /** * public String write(String IP, String filePath, String user, String pass, String content) @@ -43,6 +48,7 @@ public interface IJcon { * @throws IOException */ public String write(String IP, String filePath, String user, String pass, String content) throws IOException; + public byte[] writeBytes(String IP, String filePath, String user, String pass, byte[] content) throws IOException; /** * public String copyFileTo(String sourceIP, String sourceFilePath, String destIP, String destFilePath, String user, String pass) @@ -68,5 +74,4 @@ public interface IJcon { */ public String copyFileTo(String sourceIP, String sourceFilePath, String destIP, String destFilePath, String user, String pass) throws IOException; - } diff --git a/src/main/java/com/marlonrcfranco/Jcon.java b/src/main/java/com/marlonrcfranco/Jcon.java new file mode 100644 index 0000000..e6c785e --- /dev/null +++ b/src/main/java/com/marlonrcfranco/Jcon.java @@ -0,0 +1,72 @@ +package com.marlonrcfranco; + +import java.io.IOException; + +public class Jcon { + private IJcon jcon; + private IJcon.types type; + private String response; + + public Jcon(IJcon.types type) { + this.type = type; + try { + if (type == IJcon.types.FILESYSTEM) { + this.jcon = new JconFileSystem(); + } + else if (type == IJcon.types.SMB1) { + this.jcon = new JconSMB1(); + } + else if (type == IJcon.types.SMB23) { + this.jcon = new JconSMB23(); + } + else if (type == IJcon.types.NFS) { + // Not implemented yet + } + } catch(Exception e) { + this.jcon = null; + } + } + + public String read(String IP, String filePath, String user, String pass) throws Exception{ + String path = ""; + try { + path = filePath; + this.response = this.jcon.read(IP,path,user,pass); + + String jjRows = "\""+this.response+"\""; + String jjStatus = this.response.contains("Erro")? "500": "200"; + + String json = "{\"status\":\""+jjStatus+"\",\"rows\":["+jjRows+"]}"; + return json; + + } catch (IOException e) { + return "{\"status\":\"500\",\"message\":\"Ocorreu um erro na execução.\",\"rows\":[]}"; + } + } + + public byte[] readBytes(String IP, String filePath, String user, String pass) throws IOException { + return this.jcon.readBytes(IP,filePath,user,pass); + } + + public String write(String IP, String filePath, String user, String pass, String content) throws Exception{ + String path = ""; + try { + path = filePath; + this.response = this.jcon.write(IP,path,user,pass,content); + + String jjRows = "\""+this.response+"\""; + String jjStatus = this.response.contains("Erro")? "500": "200"; + + String json = "{\"status\":\""+jjStatus+"\",\"rows\":["+jjRows+"]}"; + return json; + + } catch (IOException e) { + return "{\"status\":\"500\",\"message\":\"Ocorreu um erro na execução.\",\"rows\":[]}"; + } + } + + public byte[] writeBytes(String IP, String filePath, String user, String pass, byte[] content) throws IOException { + return this.jcon.writeBytes(IP,filePath,user,pass,content); + } + +} diff --git a/src/main/java/com/marlonrcfranco/JconFileSystem.java b/src/main/java/com/marlonrcfranco/JconFileSystem.java index c418b79..cbb74f8 100644 --- a/src/main/java/com/marlonrcfranco/JconFileSystem.java +++ b/src/main/java/com/marlonrcfranco/JconFileSystem.java @@ -1,9 +1,6 @@ package com.marlonrcfranco; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; public class JconFileSystem implements IJcon{ @@ -21,16 +18,21 @@ public String read(String filePath) throws IOException{ @Override public String read(String IP, String filePath, String user, String pass) throws IOException { - String output=""; + return new String(readBytes(IP,filePath,user,pass)); + } + + @Override + public byte[] readBytes(String IP, String filePath, String user, String pass) throws IOException { + byte[] output = "".getBytes(); //2^17 filePath = filePath.replace("\\", "/"); FileInputStream file = null; try { file = new FileInputStream(filePath); - output=new String(file.readAllBytes()); + output = Util.toByteArray(file); } catch (FileNotFoundException e) { - output= "Erro: Não foi possível localizar o arquivo \""+filePath+"\""; + output= ("Erro: Não foi possível localizar o arquivo \""+filePath+"\"").getBytes(); } catch (IOException e) { - output= "Erro: Não foi possível ler o arquivo \""+filePath+"\""; + output= ("Erro: Não foi possível ler o arquivo \""+filePath+"\"").getBytes(); } finally { if (file != null) file.close(); @@ -44,17 +46,22 @@ public String write(String filePath,String content) throws IOException{ @Override public String write(String IP, String filePath, String user, String pass, String content) throws IOException{ - String output=""; + return new String(writeBytes(IP,filePath,user,pass,content.getBytes())); + } + + @Override + public byte[] writeBytes(String IP, String filePath, String user, String pass, byte[] content) throws IOException { + byte[] output="".getBytes(); filePath = filePath.replace("\\", "/"); FileOutputStream file = null; try { file = new FileOutputStream(filePath); - file.write(content.getBytes()); - output="Escrita concluída com sucesso"; + file.write(content); + output=("Escrita concluída com sucesso").getBytes(); } catch (FileNotFoundException e) { - output= "Erro: Não foi possível localizar o caminho \""+filePath+"\""; + output=("Erro: Não foi possível localizar o caminho \""+filePath+"\"").getBytes(); } catch (IOException e) { - output= "Erro: Não foi possível ler o arquivo \""+filePath+"\""; + output=("Erro: Não foi possível ler o arquivo \""+filePath+"\"").getBytes(); } finally { if (file != null) file.close(); @@ -102,4 +109,6 @@ public String copyFileTo(String sourceIP, String sourceFilePath, String destIP, return output; } + + } diff --git a/src/main/java/com/marlonrcfranco/JconSMB1.java b/src/main/java/com/marlonrcfranco/JconSMB1.java index 205af53..5795740 100644 --- a/src/main/java/com/marlonrcfranco/JconSMB1.java +++ b/src/main/java/com/marlonrcfranco/JconSMB1.java @@ -19,9 +19,13 @@ public JconSMB1() { @Override public String read(String IP, String filePath, String user, String pass) throws IOException { - String output=""; - filePath=filePath.replace("\\", "/"); + return new String(readBytes(IP,filePath,user,pass)); + } + @Override + public byte[] readBytes(String IP, String filePath, String user, String pass) throws IOException { + byte[] output="".getBytes(); + filePath=filePath.replace("\\", "/"); NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass); // "smb://IP/filePath"; String path="smb://"+IP+"/"+filePath; @@ -30,13 +34,13 @@ public String read(String IP, String filePath, String user, String pass) throws try { smbFile = new SmbFile(path,auth); smbfin = new SmbFileInputStream(smbFile); - output = new String(smbfin.readAllBytes()); + output = Util.toByteArray(smbfin); } catch (MalformedURLException | UnknownHostException e) { - output="Erro: Nao foi possivel localizar o arquivo \""+path+"\""; + output=("Erro: Nao foi possivel localizar o arquivo \""+path+"\"").getBytes(); } catch (SmbException e) { - output="Erro: Nao foi possivel localizar o arquivo \""+path+"\". Verifique se o caminho, usuário e senha estão corretos, e se possui permissão de leitura."; + output=("Erro: Nao foi possivel localizar o arquivo \""+path+"\". Verifique se o caminho, usuário e senha estão corretos, e se possui permissão de leitura.").getBytes(); } catch (IOException e) { - output="Erro: Não foi possível ler o arquivo \""+path+"\""; + output=("Erro: Não foi possível ler o arquivo \""+path+"\"").getBytes(); } finally { if (smbfin != null) smbfin.close(); @@ -46,23 +50,28 @@ public String read(String IP, String filePath, String user, String pass) throws @Override public String write (String IP, String filePath, String user, String pass, String content) throws IOException { - String output=""; - NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass); + return new String(writeBytes(IP,filePath,user,pass,content.getBytes())); + } + @Override + public byte[] writeBytes(String IP, String filePath, String user, String pass, byte[] content) throws IOException { + byte[] output="".getBytes(); + filePath=filePath.replace("\\", "/"); + NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass); String path="smb://"+IP+"/"+filePath; SmbFile smbFile=null; SmbFileOutputStream smbfos=null; try { smbFile = new SmbFile(path,auth); smbfos = new SmbFileOutputStream(smbFile); - smbfos.write(content.getBytes()); - output="Escrita concluída com sucesso"; + smbfos.write(content); + output=("Escrita concluída com sucesso").getBytes(); } catch (MalformedURLException | UnknownHostException e) { - output = "Erro: Nao foi possivel localizar o caminho \"" + path + "\""; + output=("Erro: Nao foi possivel localizar o caminho \"" + path + "\"").getBytes(); }catch (SmbException e) { - output = "Erro: Verifique se o usuário e senha estão corretos, e se possui permissão de escrita para acessar o caminho \"" + path + "\""; + output=("Erro: Verifique se o usuário e senha estão corretos, e se possui permissão de escrita para acessar o caminho \"" + path + "\"").getBytes(); } catch (IOException e) { - output="Erro: Não foi possível ler o arquivo \""+path+"\""; + output=("Erro: Não foi possível ler o arquivo \""+path+"\"").getBytes(); } finally { if (smbfos != null) smbfos.close(); diff --git a/src/main/java/com/marlonrcfranco/JconSMB23.java b/src/main/java/com/marlonrcfranco/JconSMB23.java index b2a7153..1a07af1 100644 --- a/src/main/java/com/marlonrcfranco/JconSMB23.java +++ b/src/main/java/com/marlonrcfranco/JconSMB23.java @@ -16,6 +16,7 @@ import com.hierynomus.smbj.share.File; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.util.EnumSet; @@ -39,13 +40,18 @@ public JconSMB23() { @Override public String read(String IP, String filePath, String user, String pass) throws IOException { + return new String(readBytes(IP, filePath, user, pass)); + } + + @Override + public byte[] readBytes(String IP, String filePath, String user, String pass) throws IOException { extractSharedPathFromPath(filePath.replace("\\", "/")); - return read(IP,sharedFolder,sFilePath,user,pass,null); + return readBytes(IP,sharedFolder,sFilePath,user,pass,null); } - public String read(String IP, String sharedFolder, String filePath, String user, String pass, String domain) { + public byte[] readBytes(String IP, String sharedFolder, String filePath, String user, String pass, String domain) throws IOException { + byte[] output="".getBytes(); //AccessMask = FILE_READ_DATA - String output=""; sharedFolder = parsePath(sharedFolder); filePath = parsePath(filePath); SMBClient client = new SMBClient(); @@ -60,28 +66,33 @@ public String read(String IP, String sharedFolder, String filePath, String user, EnumSet.of(SMB2ShareAccess.FILE_SHARE_READ), SMB2CreateDisposition.FILE_OPEN, null); - output=new String(remoteFile.getInputStream().readAllBytes()); + output = Util.toByteArray(remoteFile.getInputStream()); remoteFile.close(); } catch (SMBApiException e) { - output="Erro: Nao foi possivel localizar o caminho "+sharedFolder+"/"+filePath; + output=("Erro: Nao foi possivel localizar o caminho "+sharedFolder+"/"+filePath).getBytes(); } catch (IOException e) { - output="Erro: Nao foi possivel ler o arquivo: "+sharedFolder+"/"+filePath; + output=("Erro: Nao foi possivel ler o arquivo: "+sharedFolder+"/"+filePath).getBytes(); } } catch (IOException e) { - output="Erro: Nao foi possivel ler o arquivo: "+sharedFolder+"/"+filePath; + output=("Erro: Nao foi possivel ler o arquivo: "+sharedFolder+"/"+filePath).getBytes(); } return output; } @Override public String write(String IP, String filePath, String user, String pass, String content) throws IOException { + return new String(writeBytes(IP,filePath,user,pass,content.getBytes())); + } + + @Override + public byte[] writeBytes(String IP, String filePath, String user, String pass, byte[] content) throws IOException { extractSharedPathFromPath(filePath.replace("\\", "/")); - return write(IP,sharedFolder,sFilePath,user,pass,content,null); + return writeBytes(IP, sharedFolder, sFilePath, user, pass, content, null); } - public String write(String IP, String sharedFolder, String filePath, String user, String pass, String content, String domain) { + public byte[] writeBytes(String IP, String sharedFolder, String filePath, String user, String pass, byte[] content, String domain) { //AccessMask = FILE_READ_DATA - String output=""; + byte[] output="".getBytes(); sharedFolder = parsePath(sharedFolder); filePath = parsePath(filePath); File remoteFile=null; @@ -122,16 +133,16 @@ public String write(String IP, String sharedFolder, String filePath, String user ); } OutputStream os = remoteFile.getOutputStream(); - os.write(content.getBytes()); + os.write(content); os.close(); - output="Escrita concluída com sucesso"; + output=("Escrita concluída com sucesso").getBytes(); } catch (SMBApiException e) { - output="Erro: Nao foi possivel escrever no arquivo "+sharedFolder+"/"+filePath; + output=("Erro: Nao foi possivel escrever no arquivo "+sharedFolder+"/"+filePath).getBytes(); } catch (IOException e) { - output="Erro: Nao foi possivel ler o arquivo: "+sharedFolder+"/"+filePath; + output=("Erro: Nao foi possivel ler o arquivo: "+sharedFolder+"/"+filePath).getBytes(); } } catch (IOException e) { - output="Erro: Nao foi possivel ler o arquivo: "+sharedFolder+"/"+filePath; + output=("Erro: Nao foi possivel ler o arquivo: "+sharedFolder+"/"+filePath).getBytes(); } return output; } diff --git a/src/main/java/com/marlonrcfranco/Main.java b/src/main/java/com/marlonrcfranco/Main.java index ba32488..cacc626 100644 --- a/src/main/java/com/marlonrcfranco/Main.java +++ b/src/main/java/com/marlonrcfranco/Main.java @@ -1,30 +1,83 @@ package com.marlonrcfranco; +import java.util.HashMap; +import java.util.Scanner; + public class Main { public static void main(String[] args) { - /** - * Conectores - * ╔═══════════════╦═══════════════════════════════════════╦════════════════════════════════════╗ - * ║ Type ║ Descrição ║ Parâmetros necessários ║ - * ╠═══════════════╩═══════════════════════════════════════╩════════════════════════════════════╣ - * ║ filesystem │ Acesso a arquivos na própria │ basePath ║ - * ║ │ máquina, sem a necessidade de IP. │ ║ - * ║───────────────┼───────────────────────────────────────┼────────────────────────────────────║ - * ║ smb1 │ Acesso a arquivos remotos utilizando │ basePath,ip,username,password ║ - * ║ │ o protocolo SAMBA 1 │ ║ - * ║ │ até Windows 10). │ ║ - * ║───────────────┼───────────────────────────────────────┼────────────────────────────────────║ - * ║ smb23 │ Acesso a arquivos remotos utilziando │ basePath,ip,username,password ║ - * ║ │ o protocolo SAMBA2 ou SAMBA3 │ ║ - * ║ │ (após Windows 10). │ ║ - * ║───────────────┼───────────────────────────────────────┼────────────────────────────────────║ - * ║ nfs │ Acesso a arquivos remotos utilizando │ basePath,ip,username,password ║ - * ║ │ o protocolo NFS (Linux, Unix based OS)│ ║ - * ╚════════════════════════════════════════════════════════════════════════════════════════════╝ + * Examples: + * + * w filesystem C:\Users\marlon.franco\Documents\teste7.xml,Teste conteudo 123 [FileSystem] + * r filesystem C:\Users\marlon.franco\Documents\teste7.xml + * + * w smb1 Marlon\Teste\Teste777.txt,192.168.35.17,Adapcon,1nfr4#2017,Teste conteudo 1234 [SMB1] + * r smb1 Marlon\Teste\Teste777.txt,192.168.35.17,Adapcon,1nfr4#2017 + * + * w smb23 Marlon\Teste\Teste777.txt,192.168.35.17,Adapcon,1nfr4#2017,Teste conteudo 12345 [SMB23] + * r smb23 Marlon\Teste\Teste777.txt,192.168.35.17,Adapcon,1nfr4#2017 * */ + String input=""; + String info = "\n" + + "\n╔════════════════════════════════════════════════╗" + + "\n║ ╔═╗ ╔═╗ ╔╗ ║" + + "\n║ ║ ╠═══╦═══╣ ╚╗ ║║ ║" + + "\n║ ╔═╗ ║ ║ ╔═╣╔═╗║╔╗╚╗║║ ║" + + "\n║ ║ ╚═╝ ║ ╚═╣╚═╝║║╚╗╚╝║ ║" + + "\n║ ╚═════╩═══╩═══╩╝ ╚══╝ v0.1 ║" + + "\n║ @marlonrcfranco ║" + + "\n║ https://github.com/marlonrcfranco/jcon ║" + + "\n║ ║" + + "\n║ Based on the projects: ║" + + "\n║ jCIFS: https://www.jcifs.org/ ║" + + "\n║ (for SMB1) ║" + + "\n║ smbj: https://github.com/hierynomus/smbj ║" + + "\n║ (for SMB2/3) ║" + + "\n╚════════════════════════════════════════════════╝\n" + + " Type \"help\" or \"h\" for help."; + String connectors = + "* Connectors\n" + + " ╔═══════════════╦════════════════════════════════════════╦════════════════════════════════════╗\n" + + " ║ type ║ Description ║ Parameters needed ║\n" + + " ╠═══════════════╩════════════════════════════════════════╩════════════════════════════════════╣\n" + + " ║ filesystem │ Access the local filesystem without │ basePath ║\n" + + " ║ │ informing the IP address. │ ║\n" + + " ║───────────────┼────────────────────────────────────────┼────────────────────────────────────║\n" + + " ║ smb1 │ Access files in remote filesystem │ basePath,ip,username,password ║\n" + + " ║ │ using SMB 1 protocol. │ ║\n" + + " ║ │ (works for versions before Windows 10) │(basePath is a remote shared folder)║\n" + + " ║───────────────┼────────────────────────────────────────┼────────────────────────────────────║\n" + + " ║ smb23 │ Access files in remote filesystem │ basePath,ip,username,password ║\n" + + " ║ │ using SMB 2 or SMB 3 protocols. │ ║\n" + + " ║ │ (works for versions after Windows 10) │(basePath is a remote shared folder)║\n" + + " ║───────────────┼────────────────────────────────────────┼────────────────────────────────────║\n" + + " ║ nfs │ Access files in remote filesystem │ basePath,ip,username,password ║\n" + + " ║ │ using NFS protocol. [NOT IMPLEMENTED] │ ║\n" + + " ║ │ (Linux, Unix based OS) │(basePath is a remote shared folder)║\n" + + " ╚═════════════════════════════════════════════════════════════════════════════════════════════╝\n" + + " \n"; + String help ="" + + "\n help[h] - Show this help" + + "\n info[i] - Show initial info" + + "\n connectors[c][con] - Show info about different connectors" + + "\n examples[e] - Show examples of read and write operations" + + "\n read[r] - Read method (see connectors for more info)" + + "\n write[w] - Write method (see connectors for more info)" + + "\n "; + String examples = "" + + "Examples:\n" + + "\n" + + " w filesystem C:\\Users\\marlon.franco\\Documents\\teste7.xml,Teste conteudo 123 [FileSystem]\n" + + " r filesystem C:\\Users\\marlon.franco\\Documents\\teste7.xml\n" + + "\n" + + " w smb1 Marlon\\Teste\\Teste777.txt,192.168.35.17,Adapcon,1nfr4#2017,Teste conteudo 1234 [SMB1]\n" + + " r smb1 Marlon\\Teste\\Teste777.txt,192.168.35.17,Adapcon,1nfr4#2017\n" + + "\n" + + " w smb23 Marlon\\Teste\\Teste777.txt,192.168.35.17,Adapcon,1nfr4#2017,Teste conteudo 12345 [SMB23]\n" + + " r smb23 Marlon\\Teste\\Teste777.txt,192.168.35.17,Adapcon,1nfr4#2017\n" + + ""; /** * Compartilhamento @@ -33,30 +86,133 @@ public static void main(String[] args) { * pass: 1nfr4#2017 */ -// String IP="192.168.35.17"; - String IP="CSW"; - String user="Adapcon"; - String pass="1nfr4#2017"; - - JconSMB1 smb = new JconSMB1(); - - String response=""; - - //response = smb.write("/123/Teste777.txt", "Texto teste 1237777"); - //response = smb.read("123/Teste777.txt"); - - /** - * Leitura de arquivo remoto sem autenticação - */ - //response = smb.read("/dados/exportQuery.csv","",""); + System.out.println(info); + while((!"exit".equalsIgnoreCase(input) && !"quit".equalsIgnoreCase(input) && !"bye".equalsIgnoreCase(input)) || "".equalsIgnoreCase(input)) { + System.out.print("> "); + Scanner scanner = new Scanner(System.in); + input = scanner.nextLine(); + input = input.trim(); + switch (input) { + case "h": + case "help": + System.out.println(help); + break; + case "i": + case "info": + case "about": + System.out.println(info); + break; + case "c": + case "con": + case "connectors": + System.out.println(connectors); + break; + case "e": + case "examples": + System.out.println(examples); + break; + default:break; + } + if (input.startsWith("read ") || input.startsWith("r ")) { + System.out.println("\n"+read(input)+"\n"); + } + if (input.startsWith("write ") || input.startsWith("w ")) { + System.out.println("\n"+write(input)+"\n"); + } + } + } - /** - * Leitura de arquivo local sem autenticação - */ - //response = smb.read("localhost/Users/marlon.franco/Documents/teste.xml","",""); + private static String read(String input) { + String output=""; + HashMap aInput = verifyInput(input,"read"); + if (!"".equalsIgnoreCase(aInput.get("error"))) return aInput.get("error"); + Jcon jconAccessFiles = new Jcon(IJcon.types.valueOf(aInput.get("type"))); + try { + return jconAccessFiles.read(aInput.get("IP"), aInput.get("basePath"),aInput.get("username"),aInput.get("password")); + } catch (Exception e) { + return ""; + } + } - System.out.println(response); + private static String write(String input) { + String output=""; + HashMap aInput = verifyInput(input,"write"); + if (!"".equalsIgnoreCase(aInput.get("error"))) return aInput.get("error"); + Jcon jconAccessFiles = new Jcon(IJcon.types.valueOf(aInput.get("type"))); + try { + return jconAccessFiles.write(aInput.get("IP"), aInput.get("basePath"),aInput.get("username"),aInput.get("password"),aInput.get("content")); + } catch (Exception e) { + return ""; + } + } + private static HashMap verifyInput(String input, String operation) { + HashMap response = new HashMap<>(); + response.put("error", ""); + response.put("cmd", ""); + response.put("type", ""); + response.put("basePath", ""); + response.put("IP", ""); + response.put("username", ""); + response.put("password", ""); + response.put("content", ""); // only for "write" operation + String oper=operation.substring(0,1); + String cmd,type; + String[] params; + String[] args = input.split(" ",3); + if (args.length < 3) { + response.put("error","Invalid syntax for "+operation+".\n Expected 3 arguments separated by whitespace.\n Type: "+operation+"["+oper+"] "); + return response; + } + cmd=args[0].trim(); + type=args[1].trim(); + params=args[2].trim().split(","); + if ("".equalsIgnoreCase(cmd) || (!operation.equalsIgnoreCase(cmd) && !oper.equalsIgnoreCase(cmd))) { + response.put("error","Invalid syntax for "+operation+".\n Expected 1st argument to be \""+operation+"\" or \""+oper+"\".\n Type: "+operation+"["+oper+"] "); + return response; + } + try { + IJcon.types.valueOf(type.toUpperCase().trim()); + } catch (Exception e) { + response.put("error","Invalid syntax for "+operation+".\n Not regognized type \""+type+"\".\n Type \"con\" for more info about available connectors."); + return response; + } + response.put("cmd",cmd); + response.put("type", type.toUpperCase().trim()); + if (type.equalsIgnoreCase(IJcon.types.FILESYSTEM.toString())) { + if (params.length < 1) { + response.put("error","Invalid syntax for " + operation + ".\n Type \"" + type + "\" expects at least 1 parameter (basePath) separated by commas.\n Type \"con\" for more info about available connectors."); + return response; + } + response.put("basePath", params[0]); // filePath + response.put("IP",""); + response.put("username",""); + response.put("password",""); + if (operation.equalsIgnoreCase("write")) { + if (params.length <2) { + response.put("error","Invalid syntax for " + operation + ".\n Type \"" + type + "\" expects at least 2 parameters (basePath,content) separated by commas.\n Type \"con\" for more info about available connectors."); + return response; + } + response.put("content",params[1]); + } + }else { + if (params.length < 4) { + response.put("error","Invalid syntax for " + operation + ".\n Type \"" + type + "\" expects at least 4 parameters (basePath,ip,username,password) separated by commas.\n Type \"con\" for more info about available connectors."); + return response; + } + response.put("basePath", params[0]); // basePath (remote shared folder) + response.put("IP",params[1]); // IP + response.put("username",params[2]); // username + response.put("password",params[3]); // password + if (operation.equalsIgnoreCase("write")) { + if (params.length <5) { + response.put("error","Invalid syntax for " + operation + ".\n Type \"" + type + "\" expects at least 5 parameters (basePath,ip,username,password,content) separated by commas.\n Type \"con\" for more info about available connectors."); + return response; + } + response.put("content",params[4]); + } + } + return response; } } diff --git a/src/main/java/com/marlonrcfranco/Util.java b/src/main/java/com/marlonrcfranco/Util.java new file mode 100644 index 0000000..664b7f1 --- /dev/null +++ b/src/main/java/com/marlonrcfranco/Util.java @@ -0,0 +1,22 @@ +package com.marlonrcfranco; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +public class Util { + + public Util() { + + } + + public static byte[] toByteArray(InputStream in) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int len; + while((len = in.read(buffer)) != -1) { + os.write(buffer,0,len); + } + return os.toByteArray(); + } +} diff --git a/src/main/java/com/marlonrcfranco/JconFileSystemTest.java b/src/main/java/tests/JconFileSystemTest.java similarity index 96% rename from src/main/java/com/marlonrcfranco/JconFileSystemTest.java rename to src/main/java/tests/JconFileSystemTest.java index 95687c1..179b08e 100644 --- a/src/main/java/com/marlonrcfranco/JconFileSystemTest.java +++ b/src/main/java/tests/JconFileSystemTest.java @@ -1,5 +1,6 @@ -package com.marlonrcfranco; +package tests; +import com.marlonrcfranco.JconFileSystem; import org.junit.jupiter.api.BeforeEach; import java.io.IOException; diff --git a/src/main/java/com/marlonrcfranco/JconSMB1Test.java b/src/main/java/tests/JconSMB1Test.java similarity index 96% rename from src/main/java/com/marlonrcfranco/JconSMB1Test.java rename to src/main/java/tests/JconSMB1Test.java index b50c751..662b3c6 100644 --- a/src/main/java/com/marlonrcfranco/JconSMB1Test.java +++ b/src/main/java/tests/JconSMB1Test.java @@ -1,5 +1,6 @@ -package com.marlonrcfranco; +package tests; +import com.marlonrcfranco.JconSMB1; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/src/main/java/com/marlonrcfranco/JconSMB23Test.java b/src/main/java/tests/JconSMB23Test.java similarity index 83% rename from src/main/java/com/marlonrcfranco/JconSMB23Test.java rename to src/main/java/tests/JconSMB23Test.java index 7be6d6b..89555a4 100644 --- a/src/main/java/com/marlonrcfranco/JconSMB23Test.java +++ b/src/main/java/tests/JconSMB23Test.java @@ -1,5 +1,6 @@ -package com.marlonrcfranco; +package tests; +import com.marlonrcfranco.JconSMB23; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -28,11 +29,11 @@ void setUp() { @Test void read() { - response = jSMBJ.read(IP,"Marlon","Teste/Teste7777.txt",user,pass,null); - System.out.println(response); - assert !response.contains("Erro"); - try { + response = jSMBJ.readBytes(IP,"Marlon","Teste/Teste7777.txt",user,pass,null).toString(); + System.out.println(response); + assert !response.contains("Erro"); + response = jSMBJ.read(IP,"Marlon/Marlon/Teste2/Teste777.txt",user,pass); System.out.println(response); assert !response.contains("Erro"); @@ -60,11 +61,11 @@ void read() { @Test void write() { - response = jSMBJ.write(IP,"Marlon","/Teste/Teste7777.txt",user,pass,"Novo conteudo teste da JconSMB23.......\n\n\n\n\n\n\n7", null); - System.out.println(response); - assert !response.contains("Erro"); - try { + response = jSMBJ.writeBytes(IP,"Marlon","/Teste/Teste7777.txt",user,pass,"Novo conteudo teste da JconSMB23.......\n\n\n\n\n\n\n7".getBytes(), null).toString(); + System.out.println(response); + assert !response.contains("Erro"); + response = jSMBJ.write(IP, "Marlon/Marlon/Teste2/Teste777.txt", user, pass,"Teste Content 1237"); System.out.println(response); assert !response.contains("Erro"); @@ -88,7 +89,7 @@ void write() { } } - @Test + @Test void copyFileTo() { } diff --git a/src/main/java/tests/JconTest.java b/src/main/java/tests/JconTest.java new file mode 100644 index 0000000..00b7a18 --- /dev/null +++ b/src/main/java/tests/JconTest.java @@ -0,0 +1,74 @@ +package tests; + +import com.marlonrcfranco.IJcon; +import com.marlonrcfranco.Jcon; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.*; + +class JconTest { + + private Jcon jcon; + private String response; + private String ip; + private String user; + private String password; + private IJcon.types type; + private String basePath; + private String remoteFilePath= "/Teste/arquivo123.xml"; + private String remoteImagePath= "/Teste/images.png"; + private String remotePDFPath= "/Teste/images.png"; + private String localFilePath= "/Teste/images.png"; + private String localImagePath= "/Teste/images.png"; + private String localPDFPath= "C:\\Users\\marlon.franco\\Documents\\DOUX - Agosto.pdf"; + + @BeforeEach + void setUp() { + ip = "192.168.35.17"; + user = "Adapcon"; + password = "1nfr4#2017"; + type = IJcon.types.SMB23; + basePath = "Marlon"; + this.jcon = new Jcon(type); + this.response=""; + } + @Test + void read() { + } + + @Test + void write() { + String localBasePath = "C:\\Users\\marlon.franco\\"; + try { + + /** Test with txt file*/ + byte[] TextContent = new Jcon(IJcon.types.FILESYSTEM).readBytes("",localBasePath+"Documents\\teste.txt","",""); + this.response = new String(new Jcon(IJcon.types.SMB23).writeBytes(ip,basePath+"/Teste/text.txt",user,password,TextContent)); + System.out.println(response); + // read to check if it was written correctly + byte[] TextContent2 = (new Jcon(IJcon.types.SMB23).readBytes(ip,basePath+"/Teste/text.txt",user,password)); + assert Arrays.equals(TextContent, TextContent2); + + /** Test with PDF file*/ + byte[] PDFcontent = new Jcon(IJcon.types.FILESYSTEM).readBytes("",localBasePath+"Documents\\DOUX - Agosto.pdf","",""); + this.response = new String(new Jcon(IJcon.types.SMB23).writeBytes(ip,basePath+"/Teste/arquivo.pdf",user,password,PDFcontent)); + System.out.println(response); + // read to check if it was written correctly + byte[] PDFcontent2 = (new Jcon(IJcon.types.SMB23).readBytes(ip,basePath+"/Teste/arquivo.pdf",user,password)); + assert Arrays.equals(PDFcontent, PDFcontent2); + + /** Test with png file*/ + byte[] ImageContent = new Jcon(IJcon.types.FILESYSTEM).readBytes("",localBasePath+"Documents\\images.png","",""); + this.response = new String(new Jcon(IJcon.types.SMB23).writeBytes(ip,basePath+"/Teste/imageCopy.png",user,password,ImageContent)); + System.out.println(response); + // read to check if it was written correctly + byte[] ImageContent2 = (new Jcon(IJcon.types.SMB23).readBytes(ip,basePath+"/Teste/imageCopy.png",user,password)); + assert Arrays.equals(ImageContent, ImageContent2); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/target/classes/META-INF/jcon.kotlin_module b/target/classes/META-INF/jcon.kotlin_module deleted file mode 100644 index 2983af7..0000000 Binary files a/target/classes/META-INF/jcon.kotlin_module and /dev/null differ diff --git a/target/classes/com/marlonrcfranco/IJcon$types.class b/target/classes/com/marlonrcfranco/IJcon$types.class new file mode 100644 index 0000000..738f146 Binary files /dev/null and b/target/classes/com/marlonrcfranco/IJcon$types.class differ diff --git a/target/classes/com/marlonrcfranco/IJcon.class b/target/classes/com/marlonrcfranco/IJcon.class index 0489431..a8e1f9e 100644 Binary files a/target/classes/com/marlonrcfranco/IJcon.class and b/target/classes/com/marlonrcfranco/IJcon.class differ diff --git a/target/classes/com/marlonrcfranco/Jcon.class b/target/classes/com/marlonrcfranco/Jcon.class new file mode 100644 index 0000000..e8ea910 Binary files /dev/null and b/target/classes/com/marlonrcfranco/Jcon.class differ diff --git a/target/classes/com/marlonrcfranco/JconFileSystem.class b/target/classes/com/marlonrcfranco/JconFileSystem.class index f319bd3..cf434d7 100644 Binary files a/target/classes/com/marlonrcfranco/JconFileSystem.class and b/target/classes/com/marlonrcfranco/JconFileSystem.class differ diff --git a/target/classes/com/marlonrcfranco/JconFileSystemTest.class b/target/classes/com/marlonrcfranco/JconFileSystemTest.class deleted file mode 100644 index 3aadcf5..0000000 Binary files a/target/classes/com/marlonrcfranco/JconFileSystemTest.class and /dev/null differ diff --git a/target/classes/com/marlonrcfranco/JconSMB1.class b/target/classes/com/marlonrcfranco/JconSMB1.class index f2d5256..e7eed0c 100644 Binary files a/target/classes/com/marlonrcfranco/JconSMB1.class and b/target/classes/com/marlonrcfranco/JconSMB1.class differ diff --git a/target/classes/com/marlonrcfranco/JconSMB1Test.class b/target/classes/com/marlonrcfranco/JconSMB1Test.class deleted file mode 100644 index 8d44672..0000000 Binary files a/target/classes/com/marlonrcfranco/JconSMB1Test.class and /dev/null differ diff --git a/target/classes/com/marlonrcfranco/JconSMB23.class b/target/classes/com/marlonrcfranco/JconSMB23.class index cd7f0b9..c4edd51 100644 Binary files a/target/classes/com/marlonrcfranco/JconSMB23.class and b/target/classes/com/marlonrcfranco/JconSMB23.class differ diff --git a/target/classes/com/marlonrcfranco/JconSMB23Test.class b/target/classes/com/marlonrcfranco/JconSMB23Test.class deleted file mode 100644 index fc9df3f..0000000 Binary files a/target/classes/com/marlonrcfranco/JconSMB23Test.class and /dev/null differ diff --git a/target/classes/com/marlonrcfranco/Main.class b/target/classes/com/marlonrcfranco/Main.class index f8cbfe8..da64cd0 100644 Binary files a/target/classes/com/marlonrcfranco/Main.class and b/target/classes/com/marlonrcfranco/Main.class differ diff --git a/target/classes/com/marlonrcfranco/Util.class b/target/classes/com/marlonrcfranco/Util.class new file mode 100644 index 0000000..d947785 Binary files /dev/null and b/target/classes/com/marlonrcfranco/Util.class differ diff --git a/target/classes/tests/JconFileSystemTest.class b/target/classes/tests/JconFileSystemTest.class new file mode 100644 index 0000000..13d0823 Binary files /dev/null and b/target/classes/tests/JconFileSystemTest.class differ diff --git a/target/classes/tests/JconSMB1Test.class b/target/classes/tests/JconSMB1Test.class new file mode 100644 index 0000000..0b6f65a Binary files /dev/null and b/target/classes/tests/JconSMB1Test.class differ diff --git a/target/classes/tests/JconSMB23Test.class b/target/classes/tests/JconSMB23Test.class new file mode 100644 index 0000000..0bba3fb Binary files /dev/null and b/target/classes/tests/JconSMB23Test.class differ diff --git a/target/classes/tests/JconTest.class b/target/classes/tests/JconTest.class new file mode 100644 index 0000000..aba5cae Binary files /dev/null and b/target/classes/tests/JconTest.class differ diff --git a/target/jcon-1.0-SNAPSHOT.jar b/target/jcon-1.0-SNAPSHOT.jar new file mode 100644 index 0000000..0a7a91f Binary files /dev/null and b/target/jcon-1.0-SNAPSHOT.jar differ diff --git a/target/jcon.jar b/target/jcon.jar new file mode 100644 index 0000000..8f51a27 Binary files /dev/null and b/target/jcon.jar differ diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..bcc57d4 --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Sep 04 09:23:18 BRT 2019 +groupId=groupId +artifactId=jcon +version=1.0-SNAPSHOT