0001: /**
0002: * $Id: WinFile.java,v 1.39 2006/10/05 07:55:26 ss150821 Exp $
0003: * Copyright 2002 Sun Microsystems, Inc. All
0004: * rights reserved. Use of this product is subject
0005: * to license terms. Federal Acquisitions:
0006: * Commercial Software -- Government Users
0007: * Subject to Standard License Terms and
0008: * Conditions.
0009: *
0010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
0011: * are trademarks or registered trademarks of Sun Microsystems,
0012: * Inc. in the United States and other countries.
0013: */package com.sun.portal.netfile.servlet.java2;
0014:
0015: import java.util.*;
0016: import com.sun.portal.log.common.PortalLogger;
0017: import java.util.logging.*;
0018: import java.io.*;
0019: import java.net.*;
0020: import java.text.SimpleDateFormat;
0021: import com.sun.portal.netfile.shared.*;
0022: import jcifs.smb.*;
0023:
0024: /*
0025: * WinFile:
0026: * Uses the jCIFS (Java Common Internet File System) to access Win
0027: * share(s). CIFS is the de-facto standard file sharing protocol on the
0028: * Windows platform and jCIFS is the implementation of CIFS solutions
0029: * using the Java Programming language.
0030: */
0031:
0032: class WinFile implements XFileInterface {
0033:
0034: private static final String FORWARD_SLASH = "/";
0035: private static Logger logger = PortalLogger
0036: .getLogger(WinFile.class);
0037: private String machineEncoding;
0038: private int directoriesTraversed; // Keep track of number of directories scanned during single instance of a search
0039: private String winsServerIpAddress = "";
0040: private int smbPort;
0041: private String smbProtocolName;
0042: ArrayList searchResults = null;
0043: Locale clocale = null;
0044:
0045: public WinFile(String encoding, String winsIp, Locale cloc)
0046: throws NetFileException {
0047: machineEncoding = encoding;
0048: directoriesTraversed = 0;
0049:
0050: winsServerIpAddress = winsIp;
0051: smbPort = 139;
0052: smbProtocolName = "smb";
0053:
0054: // Set the WINS server's IP address for address resolution
0055: writeDebug("WinFile: setting WINS server's IP to-"
0056: + winsServerIpAddress);
0057: jcifs.Config.setProperty("wins", winsServerIpAddress);
0058:
0059: // Set the encoding to be used when accessing multibyte characters.
0060: writeDebug("WinFile: setting machineEncoding to-"
0061: + machineEncoding);
0062: jcifs.Config.setProperty("jcifs.netbios.encoding",
0063: machineEncoding);
0064: clocale = cloc;
0065:
0066: }
0067:
0068: /*
0069: * Constructs the java.net.URL object using:
0070: * protocolName, machineName, protocolPort,directory and SmbHandler
0071: */
0072: private URL constructURL(String machine, String dir,
0073: SmbHandler handler) throws MalformedURLException {
0074: return new URL(smbProtocolName, machine, smbPort, dir, handler);
0075: }
0076:
0077: /*
0078: * Constructs the java.net.URL object using a string form of
0079: * protocolName and domain/workgroup name
0080: */
0081: private URL constructURL(String domain, SmbHandler handler)
0082: throws MalformedURLException {
0083: return new URL(smbProtocolName, domain, smbPort, "", handler);
0084: }
0085:
0086: //-------------------------------------------------
0087: // See if we can determine whether this is a WIN or NT machine
0088: //-------------------------------------------------
0089: public String[] verifyHostType(String domain, String server,
0090: String userName, String password) throws NetFileException {
0091: /*
0092: * If the user has NOT entered domain name, then
0093: * the machine type is assumed to be "WIN", else
0094: * the machine type is assumed to be "NT"
0095: */
0096: if (domain != null) {
0097: SmbHandler handler = new SmbHandler(userName, password,
0098: domain);
0099: domain = domain.trim();
0100: if (domain.equals("")) {
0101: return new String[] { "WIN", "" };
0102: } else {
0103: try {
0104: jcifs.Config
0105: .setProperty(
0106: "jcifs.smb.client.disablePlainTextPasswords",
0107: "false");
0108:
0109: URL hostUrl = constructURL(domain, handler);
0110: SmbFile file = (SmbFile) hostUrl.openConnection();
0111: Object obj = file.list();
0112: if (obj != null) {
0113: /*
0114: String[] hosts = (String[])obj;
0115: for(int i = 0; i < hosts.length; i++) {
0116: if(hosts[i].equalsIgnoreCase(server)) {
0117: return new String[] {"NT",domain};
0118: }
0119: }
0120: throw new NetFileException(NetFileException.NETFILE_SERVER_NOTIN_DOMAIN, "");
0121: */
0122: return new String[] { "NT", domain };
0123: } else {
0124: throw new NetFileException(
0125: NetFileException.NETFILE_INVALID_DOMAIN,
0126: "");
0127: }
0128: } catch (SmbException smbe) {
0129: writeErrorDebug(
0130: "SmbException in determining if domain/server name exists",
0131: smbe);
0132: throw new NetFileException(
0133: NetFileException.NETFILE_INVALID_DOMAIN, "");
0134: } catch (IOException ioe) {
0135: writeErrorDebug(
0136: "Exception in determining if domain/server name exists",
0137: ioe);
0138: throw new NetFileException();
0139: }
0140: }
0141: } else {
0142: return new String[] { "WIN", "" };
0143: }
0144: }
0145:
0146: //-------------------------------------------------
0147: // Get the list of shares for the given system
0148: //-------------------------------------------------
0149: public String getShares(String username, String password,
0150: String machine, String domain, NetFileResource nfRes)
0151: throws Exception, NetFileException {
0152:
0153: String shares = "";
0154: boolean accessible = false;
0155: try {
0156: SmbHandler handler = new SmbHandler(username, password,
0157: domain);
0158: URL sharesUrl = constructURL(machine, FORWARD_SLASH,
0159: handler);
0160: SmbFile file = (SmbFile) sharesUrl.openConnection();
0161:
0162: SmbFile[] files = file.listFiles();
0163:
0164: String shareName = "";
0165: for (int i = 0; i < files.length; i++) {
0166: shareName = files[i].getName();
0167: //Don't consider shareNames that end with "$/"
0168: if (!shareName.endsWith("$/")) {
0169: //remove "/" at the end of share.
0170: shareName = shareName.substring(0, shareName
0171: .lastIndexOf(FORWARD_SLASH));
0172: shares += shareName + "\n";
0173: } else {
0174: /*
0175: * Check if the user has the permission to access $ share.
0176: */
0177: shareName = shareName.substring(0, shareName
0178: .length() - 1);
0179: accessible = isShareAccessible(username, password,
0180: machine, shareName, domain, nfRes);
0181: if (accessible)
0182: shares += shareName + "\n";
0183: }
0184: }
0185: return shares;
0186: } catch (Exception ex) {
0187: writeErrorDebug("Exception getting shares", ex);
0188: if (ex instanceof SmbException) {
0189: SmbException se = (SmbException) ex;
0190: throw new NetFileException(
0191: NetFileException.NETFILE_WINERROR_CODE,
0192: getErrorString(se.getNtStatus(), nfRes));
0193: } else if (ex instanceof NetFileException) {
0194: throw (NetFileException) ex;
0195: } else {
0196: return "ERROR:" + ex.getMessage();
0197: }
0198: }
0199: }
0200:
0201: //-------------------------------------------------
0202: // Generate a directory listing for a pc machine
0203: //-------------------------------------------------
0204: public String[] getDirectory(String username, String password,
0205: String machine, String share, String directory,
0206: String domain, NetFileResource nfRes)
0207: throws NetFileException {
0208:
0209: ArrayList fileListings = new ArrayList();
0210: try {
0211: SmbHandler handler = new SmbHandler(username, password,
0212: domain);
0213: URL listingUrl = constructURL(machine, FORWARD_SLASH
0214: + share + directory + FORWARD_SLASH, handler);
0215: // dir has "/" characters initially. But still the creation of url works.
0216: SmbFile file = (SmbFile) listingUrl.openConnection();
0217:
0218: // Get the listing of files.
0219: SmbFile[] files = file.listFiles();
0220: for (int i = 0; i < files.length; i++) {
0221: extractFileInformation(files[i], fileListings, nfRes
0222: .getString("wf.DateFormat"));
0223: }
0224:
0225: int fileListingSize = fileListings.size();
0226: String[] totalListings = new String[fileListingSize];
0227:
0228: for (int i = 0; i < fileListingSize; i++) {
0229: totalListings[i] = (String) fileListings.get(i);
0230: }
0231:
0232: return totalListings;
0233:
0234: } catch (Exception e) {
0235: writeErrorDebug("Exception in getting the file listing", e);
0236: if (e instanceof SmbException) {
0237: SmbException se = (SmbException) e;
0238: throw new NetFileException(
0239: NetFileException.NETFILE_WINERROR_CODE,
0240: getErrorString(se.getNtStatus(), nfRes));
0241: } else if (e instanceof NetFileException) {
0242: String msg = listingExceptionHandler(
0243: (NetFileException) e, machine, share, domain,
0244: username, password, nfRes);
0245: throw new NetFileException(
0246: NetFileException.NETFILE_WINERROR_CODE, msg);
0247: } else {
0248: throw new NetFileException(
0249: NetFileException.NETFILE_WINERROR_CODE,
0250: //"File/Folder listing could not be obtained. Reason not known");
0251: nfRes.getString("wf.4"));
0252: }
0253: } finally {
0254: fileListings = null;
0255: }
0256: }
0257:
0258: String listingExceptionHandler(NetFileException nfe,
0259: String machine, String share, String domain,
0260: String user_name, String password, NetFileResource nfRes) {
0261:
0262: String szErrCode = nfe.getMessage();
0263: //String szErrMessage = "Access is denied. Permissions may not be there";
0264: String szErrMessage = nfRes.getString("wf.5");
0265:
0266: if ((szErrCode == null) || szErrCode.equals(""))
0267: return "";
0268:
0269: if (szErrCode.equalsIgnoreCase("ERRnosuchshare")) {
0270: //szErrMessage = share + " is not a valid share. Please specify a valid share name.";
0271: szErrMessage = share + " " + nfRes.getString("wf.6");
0272: } else if (szErrCode.equalsIgnoreCase("ERRnoaccess")) {
0273: //szErrMessage = share + " exists. But access to the share is denied.";
0274: szErrMessage = share + " " + nfRes.getString("wf.7");
0275: }
0276:
0277: return szErrMessage;
0278: }
0279:
0280: /*
0281: * Following the old convention of setting file type.
0282: * Old convention means dir listing as returned by smbclient
0283: * D - dir
0284: * A - files.
0285: * AR - read-only files.
0286: * AH - hidden files.
0287: * Only 2 types returned to client
0288: * D - dir
0289: * - - files(including read-only files).
0290: *
0291: * Check if the file is hidden first,
0292: * if yes, then don't return file information.
0293: */
0294: private void extractFileInformation(SmbFile file,
0295: List fileListings, String datef) throws Exception {
0296:
0297: String fileType = "";
0298: String fileLastModified = "";
0299: String fileName = "";
0300: if (file.isHidden()) {
0301: return;
0302: } else if (file.isDirectory()) {
0303: fileType = "D";
0304: String name = file.getName();
0305: // Remove trailing "/" from directory name.
0306: fileName = name.substring(0, name
0307: .lastIndexOf(FORWARD_SLASH));
0308: } else if (file.isFile()) {
0309: fileType = "-";
0310: fileName = file.getName();
0311: } else {
0312: writeErrorDebug("WinFile: file " + file.getName()
0313: + "is of Unknown type.");
0314: return;
0315: }
0316:
0317: Date d = new Date(file.lastModified());
0318: SimpleDateFormat sdf = new SimpleDateFormat(datef, clocale);
0319: sdf.setCalendar(new GregorianCalendar(TimeZone.getDefault()));
0320: fileLastModified = sdf.format(d);
0321:
0322: fileListings.add(fileType);
0323: fileListings.add(fileName);
0324: fileListings.add(new Long(file.length()).toString());
0325: fileListings.add(fileLastModified);
0326: }
0327:
0328: //-------------------------------------------------
0329: // Make a local copy of the requested file.
0330: // Send back the local filename to the calling prog.
0331: //-------------------------------------------------
0332: public String getFile(String username, String password,
0333: String machine, String share, String file,
0334: String directory, String domain, String localTempDir,
0335: NetFileResource nfRes) throws NetFileException {
0336:
0337: String localTempFileName;
0338: try {
0339: Long randomNo = new Long(System.currentTimeMillis());
0340: if (file.startsWith("\"")) {
0341: file = file.substring(1, file.length());
0342: }
0343:
0344: localTempFileName = localTempDir + FORWARD_SLASH
0345: + randomNo.toString() + username.toUpperCase()
0346: + file;
0347: try {
0348: File localFile = new File(localTempDir);
0349: if (!localFile.exists()) {
0350: localFile.mkdirs();
0351: }
0352: } catch (Exception e) {
0353: writeErrorDebug("Error creating temp directory", e);
0354: throw new NetFileException(
0355: NetFileException.NETFILE_GENERROR_CODE,
0356: //"<html>Could not obtain file as the directory to hold temprary files <br> could not be created. Please seek administrator's assistance</html>");
0357: nfRes.getString("wf.8"));
0358: }
0359:
0360: writeDebug("Local temporary file=" + localTempFileName);
0361:
0362: SmbHandler handler = new SmbHandler(username, password,
0363: domain);
0364: URL fromUrl = constructURL(machine, FORWARD_SLASH + share
0365: + directory + FORWARD_SLASH + file, handler);
0366: SmbFile from = (SmbFile) fromUrl.openConnection();
0367:
0368: SmbFileInputStream in = new SmbFileInputStream(from);
0369: FileOutputStream out = new FileOutputStream(
0370: localTempFileName);
0371:
0372: byte[] b = new byte[8192];
0373: int n;
0374: while ((n = in.read(b)) >= 0) {
0375: out.write(b, 0, n);
0376: }
0377:
0378: in.close();
0379: out.close();
0380:
0381: return localTempFileName;
0382: } catch (Exception ex) {
0383: writeErrorDebug("Exception in obtaining the file " + file,
0384: ex);
0385:
0386: if (ex instanceof SmbException) {
0387: SmbException se = (SmbException) ex;
0388: throw new NetFileException(
0389: NetFileException.NETFILE_WINERROR_CODE,
0390: getErrorString(se.getNtStatus(), nfRes));
0391: } else if (ex instanceof NetFileException) {
0392: throw (NetFileException) ex;
0393: } else {
0394: throw new NetFileException(
0395: NetFileException.NETFILE_WINERROR_CODE,
0396: //"File could not be obtained." +
0397: nfRes.getString("wf.9"));
0398: }
0399: }
0400: }
0401:
0402: /*
0403: * Get the Input stream for the source File. This method eliminates the
0404: * need to create temporary files on the portal server side.
0405: */
0406: public InputStream getInputStream(String username, String password,
0407: String domain, String machine, String share,
0408: String directory, String file, NetFileResource nfRes)
0409: throws NetFileException {
0410: try {
0411: SmbHandler handler = new SmbHandler(username, password,
0412: domain);
0413: URL fromUrl = constructURL(machine, FORWARD_SLASH + share
0414: + directory + FORWARD_SLASH + file, handler);
0415: SmbFile from = (SmbFile) fromUrl.openConnection();
0416:
0417: return (new SmbFileInputStream(from));
0418:
0419: } catch (Exception ex) {
0420: writeErrorDebug(
0421: "Exception in obtaining the InputStream for file-"
0422: + file, ex);
0423:
0424: if (ex instanceof SmbException) {
0425: SmbException se = (SmbException) ex;
0426: throw new NetFileException(
0427: NetFileException.NETFILE_WINERROR_CODE,
0428: getErrorString(se.getNtStatus(), nfRes));
0429: } else if (ex instanceof NetFileException) {
0430: throw (NetFileException) ex;
0431: } else {
0432: throw new NetFileException(
0433: NetFileException.NETFILE_WINERROR_CODE,
0434: //"File could not be obtained." +
0435: nfRes.getString("wf.9"));
0436: }
0437: }
0438: }
0439:
0440: /*
0441: * Get the Output Stream for the target file. This method does not use
0442: * the temporary files which were created earlier.
0443: */
0444: public OutputStream getOutputStream(String username,
0445: String password, String domain, String machine,
0446: String share, String directory, String file,
0447: NetFileResource nfRes) throws NetFileException {
0448:
0449: return this .getOutputStream(username, password, domain,
0450: machine, share, directory, file, nfRes, false);
0451: }
0452:
0453: public OutputStream getOutputStream(String username,
0454: String password, String domain, String machine,
0455: String share, String directory, String file,
0456: NetFileResource nfRes, boolean append)
0457: throws NetFileException {
0458: try {
0459: SmbHandler handler = new SmbHandler(username, password,
0460: domain);
0461: URL toUrl = constructURL(machine, FORWARD_SLASH + share
0462: + directory + FORWARD_SLASH + file, handler);
0463: SmbFile to = (SmbFile) toUrl.openConnection();
0464: if (append) {
0465: return (new SmbFileOutputStream(to, true));
0466: } else {
0467: return (new SmbFileOutputStream(to));
0468: }
0469: } catch (Exception ex) {
0470: writeErrorDebug(
0471: "Exception in obtaining the OutputStream for file-"
0472: + file, ex);
0473:
0474: if (ex instanceof SmbException) {
0475: SmbException se = (SmbException) ex;
0476: throw new NetFileException(
0477: NetFileException.NETFILE_WINERROR_CODE,
0478: getErrorString(se.getNtStatus(), nfRes));
0479: } else if (ex instanceof NetFileException) {
0480: throw (NetFileException) ex;
0481: } else {
0482: throw new NetFileException(
0483: NetFileException.NETFILE_WINERROR_CODE,
0484: //"Unable to copy the file to target machine. Reasons not known."
0485: nfRes.getString("wf.15"));
0486: }
0487: }
0488: }
0489:
0490: public String[] doSearch(String usernam, String passwrd,
0491: String machnam, String VMSnam, String pattern,
0492: boolean ignoreCase, boolean searchSubfolders,
0493: String directory, String domainname, int maxsrchdir,
0494: NetFileResource nfRes) throws NetFileException {
0495:
0496: searchResults = new ArrayList();
0497:
0498: search(usernam, passwrd, machnam, VMSnam, pattern, ignoreCase,
0499: searchSubfolders, directory, domainname, maxsrchdir,
0500: nfRes);
0501:
0502: if (directoriesTraversed > maxsrchdir) {
0503: String[] searchStr = new String[searchResults.size() + 1];
0504: searchStr[0] = "ERROR:" + nfRes.getString("maxSearch");
0505: for (int i = 1; i < searchStr.length; i++) {
0506: searchStr[i] = (String) searchResults.get(i - 1);
0507: }
0508: return searchStr;
0509: }
0510:
0511: if (searchResults.size() == 0) {
0512: return new String[] { " " };
0513: } else {
0514: String[] finalSearchResults = new String[searchResults
0515: .size()];
0516: for (int i = 0; i < finalSearchResults.length; ++i) {
0517: finalSearchResults[i] = (String) searchResults.get(i);
0518: }
0519: return finalSearchResults;
0520: }
0521: }
0522:
0523: void search(String usernam, String passwrd, String machnam,
0524: String VMSnam, String pattern, boolean ignoreCase,
0525: boolean searchSubfolders, String directory,
0526: String domainname, int maxsrchdir, NetFileResource nfRes)
0527: throws NetFileException {
0528: try {
0529: ++directoriesTraversed;
0530: writeDebug(this + ":Directory number being searched="
0531: + directoriesTraversed);
0532:
0533: if (directoriesTraversed > maxsrchdir) {
0534: return;
0535: }
0536:
0537: String[] fileListing = getDirectory(usernam, passwrd,
0538: machnam, VMSnam, directory, domainname, nfRes);
0539:
0540: for (int i = 0; ((i < fileListing.length) && (!(directoriesTraversed > maxsrchdir))); i = i + 4) {
0541: if (fileListing[i].startsWith("ERROR:")) {
0542: throw new NetFileException(
0543: NetFileException.NETFILE_WINERROR_CODE,
0544: nfRes.getString("error13"));
0545: }
0546: if (fileListing[i].equalsIgnoreCase("d")) {
0547: String errorString = "";
0548: String newDirectory = directory + FORWARD_SLASH
0549: + fileListing[i + 1];
0550:
0551: try {
0552: search(usernam, passwrd, machnam, VMSnam,
0553: pattern, ignoreCase, searchSubfolders,
0554: newDirectory, domainname, maxsrchdir,
0555: nfRes);
0556: } catch (NetFileException e) {
0557: // logger.log(Level.SEVERE, "Exception while searching" , e);
0558: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ2126");
0559: }
0560:
0561: boolean containsPattern = false;
0562: if (ignoreCase)
0563: containsPattern = (fileListing[i + 1]
0564: .toLowerCase()).indexOf(pattern
0565: .toLowerCase()) > -1;
0566: else
0567: containsPattern = fileListing[i + 1]
0568: .indexOf(pattern) > -1;
0569:
0570: if (containsPattern) {
0571: searchResults.add(FORWARD_SLASH + VMSnam
0572: + newDirectory + FORWARD_SLASH
0573: + errorString);
0574: }
0575: } else {
0576: if (fileListing[i + 1].indexOf(pattern) > -1) {
0577: searchResults.add(FORWARD_SLASH + VMSnam
0578: + directory + FORWARD_SLASH
0579: + fileListing[i + 1]);
0580: }
0581: }
0582: }
0583: } catch (NetFileException e) {
0584: throw e;
0585: } catch (Exception e) {
0586: throw new NetFileException(
0587: NetFileException.NETFILE_WINERROR_CODE, nfRes
0588: .getString("error13"));
0589: }
0590: }
0591:
0592: public String doCreateDirectory(String machine, String share,
0593: String username, String password, String domain,
0594: String parentDir, String dirToCreate, NetFileResource nfRes)
0595: throws NetFileException {
0596:
0597: try {
0598: SmbHandler handler = new SmbHandler(username, password,
0599: domain);
0600: URL mkdirUrl = constructURL(machine, FORWARD_SLASH + share
0601: + parentDir + FORWARD_SLASH + dirToCreate, handler);
0602: SmbFile dir = (SmbFile) mkdirUrl.openConnection();
0603:
0604: dir.mkdir();
0605:
0606: return nfRes.getString("warning52");
0607: } catch (Exception e) {
0608: writeErrorDebug("Problem in creating directory "
0609: + dirToCreate + " on machine " + machine, e);
0610: if (e instanceof SmbException) {
0611: SmbException se = (SmbException) e;
0612: throw new NetFileException(
0613: NetFileException.NETFILE_WINERROR_CODE,
0614: getErrorString(se.getNtStatus(), nfRes));
0615: } else {
0616: throw new NetFileException(
0617: NetFileException.NETFILE_WINERROR_CODE,
0618: nfRes.getString("could_not_create_directory"));
0619: }
0620: }
0621: }
0622:
0623: public String doDeleteDirectory(String username, String password,
0624: String machine, String share, String directory,
0625: String domain, NetFileResource nfRes)
0626: throws NetFileException {
0627:
0628: try {
0629: SmbHandler handler = new SmbHandler(username, password,
0630: domain);
0631: URL deleteUrl = constructURL(machine, FORWARD_SLASH + share
0632: + directory + FORWARD_SLASH, handler);
0633: SmbFile deleteFile = (SmbFile) deleteUrl.openConnection();
0634: deleteFile.delete();
0635: } catch (Exception e) {
0636: writeErrorDebug("Problem in deleting folder " + directory
0637: + " on machine " + machine, e);
0638: if (e instanceof SmbException) {
0639: SmbException se = (SmbException) e;
0640: throw new NetFileException(
0641: NetFileException.NETFILE_WINERROR_CODE,
0642: getErrorString(se.getNtStatus(), nfRes));
0643: } else {
0644: throw new NetFileException(
0645: NetFileException.NETFILE_WINERROR_CODE, nfRes
0646: .getString("wf.13"));
0647: }
0648: }
0649: return nfRes.getString("info5");
0650: }
0651:
0652: public String doDeleteFile(String username, String password,
0653: String machine, String share, String file,
0654: String directory, String domain, NetFileResource nfRes)
0655: throws NetFileException {
0656:
0657: try {
0658: SmbHandler handler = new SmbHandler(username, password,
0659: domain);
0660: URL deleteUrl = constructURL(machine, FORWARD_SLASH + share
0661: + directory + FORWARD_SLASH + file, handler);
0662: SmbFile deleteFile = (SmbFile) deleteUrl.openConnection();
0663: deleteFile.delete();
0664: } catch (Exception e) {
0665: writeErrorDebug("Problem in deleting file " + file
0666: + " on machine " + machine, e);
0667: if (e instanceof SmbException) {
0668: SmbException se = (SmbException) e;
0669: throw new NetFileException(
0670: NetFileException.NETFILE_WINERROR_CODE,
0671: getErrorString(se.getNtStatus(), nfRes));
0672: } else {
0673: throw new NetFileException(
0674: NetFileException.NETFILE_WINERROR_CODE,
0675: //"File deletion could not be done. Reason not known");
0676: nfRes.getString("wf.14"));
0677: }
0678: }
0679: return nfRes.getString("info5");
0680: }
0681:
0682: public void doRenameFile(String username, String password,
0683: String machine, String domain, String share,
0684: String directory, String oldFileName, String newFileName,
0685: NetFileResource nfRes) throws NetFileException {
0686: try {
0687: SmbHandler handler = new SmbHandler(username, password,
0688: domain);
0689: URL fromUrl = constructURL(machine, FORWARD_SLASH + share
0690: + directory + FORWARD_SLASH + oldFileName, handler);
0691: SmbFile from = (SmbFile) fromUrl.openConnection();
0692:
0693: URL toUrl = constructURL(machine, FORWARD_SLASH + share
0694: + directory + FORWARD_SLASH + newFileName, handler);
0695: SmbFile to = (SmbFile) toUrl.openConnection();
0696:
0697: from.renameTo(to);
0698: } catch (SmbException se) {
0699: throw new NetFileException(
0700: NetFileException.NETFILE_WINERROR_CODE,
0701: getErrorString(se.getNtStatus(), nfRes));
0702: } catch (Exception e) {
0703: throw new NetFileException(
0704: NetFileException.NETFILE_WINERROR_CODE, nfRes
0705: .getString("wf.16"));
0706: }
0707: }
0708:
0709: protected void writeDebug(String szMsg) {
0710: writeDebug(szMsg, null);
0711: }
0712:
0713: protected void writeDebug(String szMsg, Exception e) {
0714: if (e != null) {
0715: // logger.log(Level.INFO, szMsg, e);
0716: logger.log(Level.INFO, "PSSRNF_CSPNSJ2127");
0717: } else {
0718: // logger.info(szMsg);
0719: logger.info("PSSRNF_CSPNSJ2128");
0720: }
0721: }
0722:
0723: protected void writeErrorDebug(String msg) {
0724: writeErrorDebug(msg, null);
0725: }
0726:
0727: protected void writeErrorDebug(String szError, Exception e) {
0728: if (e != null)
0729: // logger.log(Level.SEVERE, szError, e);
0730: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ2129");
0731: else
0732: // logger.severe(szError);
0733: logger.severe("PSSRNF_CSPNSJ2130");
0734: }
0735:
0736: public String getErrorString(int errorCode, NetFileResource nfRes) {
0737: /*
0738: * This method handles hundreds of error codes that may be returned
0739: * by a CIFS server. Instead of having an Exception for each, representation
0740: * all of these exceptions are in SmbException class. The SmbException
0741: * class has errorClass and errorCode members to allow catch routines
0742: * to examine these values and potentially act on them accordingly.
0743: */
0744: String result = "";
0745: switch (errorCode) {
0746: // start of dos status code
0747: case 0x00010001:
0748: case 0x00010002:
0749: result += nfRes.getString("ERRDOS_badfunc");
0750: break;
0751: case 0x00020001:
0752: result += nfRes.getString("ERRDOS_badfile");
0753: break;
0754: case 0x00020002:
0755: result += nfRes.getString("ERRDOS_badpswd");
0756: break;
0757: case 0x00030001:
0758: result += nfRes.getString("ERRDOS_badfile");
0759: break;
0760: case 0x00030002:
0761: result += nfRes.getString("ERRDOS_rsrv");
0762: break;
0763: case 0x00040002:
0764: result += nfRes.getString("ERRDOS_noright");
0765: break;
0766: case 0x00050001:
0767: result += nfRes.getString("ERRDOS_noaccess");
0768: break;
0769: case 0x00050002:
0770: result += nfRes.getString("ERRDOS_badtid");
0771: break;
0772: case 0x00060001:
0773: result += nfRes.getString("ERRDOS_badfid");
0774: break;
0775: case 0x00060002:
0776: result += nfRes.getString("ERRDOS_badnetname");
0777: break;
0778: case 0x00080001:
0779: result += nfRes.getString("ERRDOS_nospace");
0780: break;
0781: case 0x00130003:
0782: result += nfRes.getString("ERRDOS_wrtptd");
0783: break;
0784: case 0x00150003:
0785: result += nfRes.getString("ERRDOS_notready");
0786: break;
0787: case 0x001f0001:
0788: result += nfRes.getString("ERRDOS_baddev");
0789: break;
0790: case 0x001f0003:
0791: result += nfRes.getString("ERRDOS_baddev");
0792: break;
0793: case 0x00200001:
0794: result += nfRes.getString("ERRDOS_badshare");
0795: break;
0796: case 0x00200003:
0797: result += nfRes.getString("ERRDOS_badshare");
0798: break;
0799: case 0x00210003:
0800: result += nfRes.getString("ERRDOS_lckfl");
0801: break;
0802: case 0x00270003:
0803: result += nfRes.getString("ERRDOS_dskful");
0804: break;
0805: case 0x00340001:
0806: result += nfRes.getString("ERRDOS_dupname");
0807: break;
0808: case 0x00430001:
0809: result += nfRes.getString("ERRDOS_badnetname");
0810: break;
0811: case 0x00470001:
0812:
0813: result += nfRes.getString("ERRDOS_nomoreconn");
0814: break;
0815: case 0x00500001:
0816: result += nfRes.getString("ERRDOS_filexists");
0817: break;
0818: case 0x00570001:
0819: result += nfRes.getString("ERRDOS_badparm");
0820: break;
0821: case 0x005a0002:
0822: result += nfRes.getString("ERRDOS_manyuid");
0823: break;
0824: case 0x006d0001:
0825: result += nfRes.getString("ERRDOS_brokenpipe");
0826: break;
0827: case 0x007b0001:
0828: result += nfRes.getString("ERRDOS_invname");
0829: break;
0830: case 0x00910001:
0831: result += nfRes.getString("ERRDOS_notempty");
0832: break;
0833: case 0x00b70001:
0834: result += nfRes.getString("ERRDOS_filexists");
0835: break;
0836: case 0x00e70001:
0837: result += nfRes.getString("ERRDOS_pipebusy");
0838: break;
0839: case 0x00e80001:
0840: result += nfRes.getString("ERRDOS_nodata");
0841: break;
0842: case 0x00e90001:
0843: result += nfRes.getString("ERRDOS_noproc");
0844: break;
0845: case 0x00ea0001:
0846: result += nfRes.getString("ERRDOS_moredata");
0847: break;
0848: case 0x08bf0002:
0849: result += nfRes.getString("ERRDOS_accexp");
0850: break;
0851: case 0x08c00002:
0852: result += nfRes.getString("ERRDOS_badusr");
0853: break;
0854: case 0x08c10002:
0855: result += nfRes.getString("ERRDOS_usrnotal");
0856: break;
0857: case 0x08c20002:
0858: result += nfRes.getString("ERRDOS_pswdexp");
0859: break;
0860: // end of dos status code
0861: //start of nt status codes
0862: case SmbException.NT_STATUS_OK:
0863: result += nfRes.getString("NT_STATUS_OK");
0864: break;
0865: case SmbException.NT_STATUS_UNSUCCESSFUL:
0866: result += nfRes.getString("NT_STATUS_UNSUCCESSFUL");
0867: break;
0868: case SmbException.NT_STATUS_NOT_IMPLEMENTED:
0869: result += nfRes.getString("NT_STATUS_NOT_IMPLEMENTED");
0870: break;
0871: case SmbException.NT_STATUS_INVALID_INFO_CLASS:
0872: result += nfRes.getString("NT_STATUS_INVALID_INFO_CLASS");
0873: break;
0874: case SmbException.NT_STATUS_ACCESS_VIOLATION:
0875: result += nfRes.getString("NT_STATUS_ACCESS_VIOLATION");
0876: break;
0877: case SmbException.NT_STATUS_INVALID_HANDLE:
0878: result += nfRes.getString("NT_STATUS_INVALID_HANDLE");
0879: break;
0880: case SmbException.NT_STATUS_NO_SUCH_DEVICE:
0881: result += nfRes.getString("NT_STATUS_NO_SUCH_DEVICE");
0882: break;
0883: case SmbException.NT_STATUS_NO_SUCH_FILE:
0884: result += nfRes.getString("NT_STATUS_NO_SUCH_FILE");
0885: break;
0886: case SmbException.NT_STATUS_ACCESS_DENIED:
0887: result += nfRes.getString("NT_STATUS_ACCESS_DENIED");
0888: break;
0889: case SmbException.NT_STATUS_OBJECT_NAME_INVALID:
0890: result += nfRes.getString("NT_STATUS_OBJECT_NAME_INVALID");
0891: break;
0892: case SmbException.NT_STATUS_OBJECT_NAME_NOT_FOUND:
0893: result += nfRes
0894: .getString("NT_STATUS_OBJECT_NAME_NOT_FOUND");
0895: break;
0896: case SmbException.NT_STATUS_OBJECT_NAME_COLLISION:
0897: result += nfRes
0898: .getString("NT_STATUS_OBJECT_NAME_COLLISION");
0899: break;
0900: case SmbException.NT_STATUS_PORT_DISCONNECTED:
0901: result += nfRes.getString("NT_STATUS_PORT_DISCONNECTED");
0902: break;
0903: case SmbException.NT_STATUS_OBJECT_PATH_NOT_FOUND:
0904: result += nfRes
0905: .getString("NT_STATUS_OBJECT_PATH_NOT_FOUND");
0906: break;
0907: case SmbException.NT_STATUS_OBJECT_PATH_SYNTAX_BAD:
0908: result += nfRes
0909: .getString("NT_STATUS_OBJECT_PATH_SYNTAX_BAD");
0910: break;
0911: case SmbException.NT_STATUS_SHARING_VIOLATION:
0912: result += nfRes.getString("NT_STATUS_SHARING_VIOLATION");
0913: break;
0914: case SmbException.NT_STATUS_DELETE_PENDING:
0915: result += nfRes.getString("NT_STATUS_DELETE_PENDING");
0916: break;
0917: case SmbException.NT_STATUS_NO_SUCH_USER:
0918: result += nfRes.getString("NT_STATUS_NO_SUCH_USER");
0919: break;
0920: case SmbException.NT_STATUS_WRONG_PASSWORD:
0921: result += nfRes.getString("NT_STATUS_WRONG_PASSWORD");
0922: break;
0923: case SmbException.NT_STATUS_LOGON_FAILURE:
0924: result += nfRes.getString("NT_STATUS_LOGON_FAILURE");
0925: break;
0926: case SmbException.NT_STATUS_ACCOUNT_RESTRICTION:
0927: result += nfRes.getString("NT_STATUS_ACCOUNT_RESTRICTION");
0928: break;
0929: case SmbException.NT_STATUS_INVALID_LOGON_HOURS:
0930: result += nfRes.getString("NT_STATUS_INVALID_LOGON_HOURS");
0931: break;
0932: case SmbException.NT_STATUS_INVALID_WORKSTATION:
0933: result += nfRes.getString("NT_STATUS_INVALID_WORKSTATION");
0934: break;
0935: case SmbException.NT_STATUS_PASSWORD_EXPIRED:
0936: result += nfRes.getString("NT_STATUS_PASSWORD_EXPIRED");
0937: break;
0938: case SmbException.NT_STATUS_ACCOUNT_DISABLED:
0939: result += nfRes
0940: .getString("SmbException.NT_STATUS_ACCOUNT_DISABLED");
0941: break;
0942: case SmbException.NT_STATUS_INSTANCE_NOT_AVAILABLE:
0943: result += nfRes
0944: .getString("NT_STATUS_INSTANCE_NOT_AVAILABLE");
0945: break;
0946: case SmbException.NT_STATUS_PIPE_NOT_AVAILABLE:
0947: result += nfRes.getString("NT_STATUS_PIPE_NOT_AVAILABLE");
0948: break;
0949: case SmbException.NT_STATUS_INVALID_PIPE_STATE:
0950: result += nfRes.getString("NT_STATUS_INVALID_PIPE_STATE");
0951: break;
0952: case SmbException.NT_STATUS_PIPE_BUSY:
0953: result += nfRes.getString("NT_STATUS_PIPE_BUSY");
0954: break;
0955: case SmbException.NT_STATUS_PIPE_DISCONNECTED:
0956: result += nfRes.getString("NT_STATUS_PIPE_DISCONNECTED");
0957: break;
0958: case SmbException.NT_STATUS_PIPE_CLOSING:
0959: result += nfRes.getString("NT_STATUS_PIPE_CLOSING");
0960: break;
0961: case SmbException.NT_STATUS_PIPE_LISTENING:
0962: result += nfRes.getString("NT_STATUS_PIPE_LISTENING");
0963: break;
0964: case SmbException.NT_STATUS_FILE_IS_A_DIRECTORY:
0965: result += nfRes.getString("NT_STATUS_FILE_IS_A_DIRECTORY");
0966: break;
0967: case SmbException.NT_STATUS_NETWORK_NAME_DELETED:
0968: result += nfRes.getString("NT_STATUS_NETWORK_NAME_DELETED");
0969: break;
0970: case SmbException.NT_STATUS_BAD_NETWORK_NAME:
0971: result += nfRes.getString("NT_STATUS_BAD_NETWORK_NAME");
0972: break;
0973: case SmbException.NT_STATUS_NOT_A_DIRECTORY:
0974: result += nfRes.getString("NT_STATUS_NOT_A_DIRECTORY");
0975: break;
0976: case SmbException.NT_STATUS_CANNOT_DELETE:
0977: result += nfRes.getString("NT_STATUS_CANNOT_DELETE");
0978: break;
0979: case SmbException.NT_STATUS_PIPE_BROKEN:
0980: result += nfRes.getString("NT_STATUS_PIPE_BROKEN");
0981: break;
0982: case SmbException.NT_STATUS_LOGON_TYPE_NOT_GRANTED:
0983: result += nfRes
0984: .getString("SmbException.NT_STATUS_LOGON_TYPE_NOT_GRANTED");
0985: break;
0986: case SmbException.NT_STATUS_TRUSTED_DOMAIN_FAILURE:
0987: result += nfRes
0988: .getString("NT_STATUS_TRUSTED_DOMAIN_FAILURE");
0989: break;
0990: case SmbException.NT_STATUS_ACCOUNT_LOCKED_OUT:
0991: result += nfRes
0992: .getString("SmbException.NT_STATUS_ACCOUNT_LOCKED_OUT");
0993: break;
0994: case SmbException.NT_STATUS_PATH_NOT_COVERED:
0995: result += nfRes.getString("NT_STATUS_PATH_NOT_COVERED");
0996: break;
0997: //end of nt status codes
0998: default:
0999: result += "unknown error code: " + errorCode;
1000: }
1001: return result;
1002: }
1003:
1004: /*
1005: * Verifies for the given share, if it is accessible. For this, check whether
1006: * the folders under a given share is accessible.
1007: */
1008: public boolean isShareAccessible(String username, String password,
1009: String machine, String share, String domain,
1010: NetFileResource nfRes) {
1011: boolean returnValue = false;
1012: try {
1013: SmbHandler handler = new SmbHandler(username, password,
1014: domain);
1015: URL listingUrl = constructURL(machine, FORWARD_SLASH
1016: + share + FORWARD_SLASH, handler);
1017: SmbFile file = (SmbFile) listingUrl.openConnection();
1018:
1019: SmbFile[] files = file.listFiles();
1020: if (files != null)
1021: returnValue = true;
1022: else
1023: returnValue = false;
1024: } catch (SmbException se) {
1025: // logger.severe("WinFile: share - " + share + " not accessible to user - " + username + "." +
1026: logger.severe("WinFile: share - " + share
1027: + " not accessible to user - " + username + "."
1028: + getErrorString(se.getNtStatus(), nfRes));
1029: returnValue = false;
1030: } catch (Exception e) {
1031: // logger.severe("WinFile: exception when checking for share accessibility," + e.toString());
1032: Object[] params6 = { e.toString() };
1033: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ2132", params6);
1034: returnValue = false;
1035: }
1036: return returnValue;
1037: }
1038:
1039: }
|