00001 package net.threebit.utils.sosc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 import com.oreilly.servlet.*;
00023 import java.io.*;
00024 import java.sql.*;
00025 import java.util.*;
00026
00031 public class SmbTool {
00032
00033 static String smbClient = null;
00034 static {
00035
00036 String[] p = new String[] {
00037 "/bin/smbclient",
00038 "/sbin/smbclient",
00039 "/usr/bin/smbclient",
00040 "/usr/sbin/smbclient",
00041 "/usr/local/bin/smbclient",
00042 "/usr/local/sbin/smbclient",
00043 "/usr/local/samba/bin/smbclient",
00044 "/usr/local/samba/sbin/smbclient"
00045 };
00046 for (int x = 0; x < p.length; x++) {
00047 if (new File(p[x]).exists()) {
00048 smbClient = p[x];
00049 break;
00050 }
00051 }
00052 if (smbClient == null) {
00053 String paths = "";
00054 for (int x = 0; x < p.length; x++) { paths = p[x] + ";"; }
00055 throw new RuntimeException("Could not find 'smbClient' {"+paths+")");
00056 }
00057 }
00058
00059 private String userName = null;
00060 private String password = null;
00061
00062 public SmbTool (String userName, String password) {
00063 this.userName = userName;
00064 this.password = password;
00065 }
00066
00070 public static String getHostAndShare (String uncPath) throws Exception {
00071 if (!uncPath.startsWith("//")) { throw new Exception("UNC paths must begin with '//': '" + uncPath + "'"); }
00072 String[] path = uncPath.split("/");
00073 if (path.length < 4) { throw new Exception("Ugly UNC Path: '" + uncPath + "'"); }
00074 return "//" + path[2] + "/" + path[3];
00075 }
00076
00080 public static String getRemotePath (String uncPath) throws Exception {
00081 String remotePath = "";
00082 if (!uncPath.startsWith("//")) { throw new Exception("UNC paths must begin with '//': '" + uncPath + "'"); }
00083 String[] path = uncPath.split("/");
00084 for (int x = 4; x < path.length-1; x++) { remotePath = remotePath + path[x] + "\\"; }
00085 remotePath = remotePath + path[path.length-1];
00086 return remotePath;
00087 }
00088
00092 public StringBuffer get (String uncPath) throws Exception {
00093 String sharePath = getHostAndShare(uncPath);
00094 String remotePath = getRemotePath(uncPath);
00095
00096 File tempFile = File.createTempFile("SmbToolGet","filedata");
00097 String[] cmd = new String[] {
00098 smbClient,
00099 sharePath,
00100 password,
00101 "-U",userName,
00102 "-c",
00103 "get " + remotePath + " " + tempFile.getAbsolutePath()
00104 };
00105
00106 ProcTool worker = new ProcTool(cmd);
00107 worker.waitFor();
00108
00109
00110 StringBuffer sb = new StringBuffer();
00111 BufferedReader br = new BufferedReader( new FileReader( tempFile ));
00112 String line;
00113 while ((line = br.readLine()) != null) {
00114 sb.append(line + "\n");
00115 }
00116
00117 return sb;
00118 }
00119
00124 public ProcTool put (StringBuffer source, String uncPath) throws Exception {
00125
00126 File tempFile = File.createTempFile("SmbToolPutbyStringBuffer","filedata");
00127 PrintWriter pw = new PrintWriter( new FileWriter(tempFile));
00128 pw.print(source.toString());
00129 pw.close();
00130
00131 return put(tempFile.getAbsolutePath(), uncPath);
00132 }
00133
00137 public ProcTool put (String source, String uncPath) throws Exception {
00138 String sharePath = getHostAndShare(uncPath);
00139 String remotePath = getRemotePath(uncPath);
00140
00141 String[] cmd = new String[] {
00142 smbClient,
00143 sharePath,
00144 password,
00145 "-U",userName,
00146 "-c",
00147 "put " + source + " " + remotePath
00148 };
00149 return(new ProcTool(cmd));
00150
00151 }
00152
00153 }