All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.io.SMBFileObj Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util.io;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Properties;

import com.hfg.exception.ProgrammingException;

import jcifs.CIFSContext;
import jcifs.CIFSException;
import jcifs.SmbWatchHandle;
import jcifs.config.PropertyConfiguration;
import jcifs.context.BaseContext;
import jcifs.smb.NtlmPasswordAuthenticator;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

import com.hfg.security.LoginCredentials;

//------------------------------------------------------------------------------
/**
 * Implementation of FileObj for Samba (SMB) files
 * 

* @author J. Alex Taylor, hairyfatguy.com *

*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class SMBFileObj extends FileObjImpl { //########################################################################### // PRIVATE FIELDS //########################################################################### private URL mURL; private SmbFile mSmbFile; private CIFSContext mCIFSContext; private String mServer; private String mParentDirPath; private String mName; private String mRequestedFilePath; private static final String SCHEME = "smb"; static { // Ensure that the SMB protocol handler is registered jcifs.Config.registerSmbURLHandler(); } //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public SMBFileObj(String inURL) throws MalformedURLException { this(new URL(inURL)); } //--------------------------------------------------------------------------- public SMBFileObj(URI inURI) throws MalformedURLException { this(inURI.toURL()); } //--------------------------------------------------------------------------- public SMBFileObj(URL inURL) { // Workaround for space-containing URLs try { mURL = new URL(URLDecoder.decode(inURL.toString())); } catch (Exception e) { throw new RuntimeException("Problem with URL!", e); } mServer = inURL.getHost(); String path = inURL.getPath(); if (path.endsWith("/")) { path = path.substring(0, path.length() - 1); } mName = path.substring(path.lastIndexOf("/") + 1); if (path.length() > 0) { int index = path.lastIndexOf("/"); mParentDirPath = (index >= 0 ? path.substring(0, index + 1) : ""); } } //--------------------------------------------------------------------------- public SMBFileObj(URL inURL, CIFSContext inContext) { this(inURL); mCIFSContext = inContext; } //--------------------------------------------------------------------------- public SMBFileObj(String inURL, CIFSContext inContext) throws MalformedURLException { this(new URL(inURL), inContext); } //--------------------------------------------------------------------------- public SMBFileObj(SMBFileObj inParent, String inFilename) { mServer = inParent.getServer(); mCIFSContext = inParent.mCIFSContext; setCredentials(inParent.getCredentials()); try { mURL = new URL(inParent.getURL() + inFilename); } catch (MalformedURLException e) { throw new ProgrammingException(e); } String path = mURL.getPath(); int index = path.substring(0, path.length() - 1).lastIndexOf("/"); mParentDirPath = (index >= 0 ? path.substring(0, index + 1) : ""); mName = (index >= 0 ? path.substring(index + 1) : ""); if (mName.endsWith("/")) { mName = mName.substring(0, mName.length() - 1); } } //--------------------------------------------------------------------------- public SMBFileObj(SmbFile inFile) { mSmbFile = inFile; mName = inFile.getName(); mServer = inFile.getServer(); mCIFSContext = inFile.getContext(); try { String url = inFile.getCanonicalPath(); if (! url.endsWith("/") && inFile.isDirectory()) { url = url + "/"; } mURL = new URL(url); String path = mURL.getPath(); if (path.length() > 0) { if (path.endsWith("/") && path.length() > 1) { path = path.substring(0, path.length() - 1); } int index = path.lastIndexOf("/"); mParentDirPath = (index >= 0 ? path.substring(0, index + 1) : ""); } } catch (Exception e) { throw new RuntimeIOException(e); } } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- @Override public String getScheme() { return SCHEME; } //--------------------------------------------------------------------------- @Override public String getName() { return mName; } //--------------------------------------------------------------------------- public SMBFileObj setRequestedPath(String inValue) { mRequestedFilePath = inValue; return this; } //--------------------------------------------------------------------------- @Override public String getRequestedPath() { return mRequestedFilePath != null ? mRequestedFilePath : getPath(); } //--------------------------------------------------------------------------- @Override public String getPath() { return mURL.getPath(); } //--------------------------------------------------------------------------- public String getServer() { return mServer; } //--------------------------------------------------------------------------- @Override public URI getURI() { URI uri; try { uri = new URI(getScheme(), getServer(), getPath(), null); } catch (Exception e) { throw new RuntimeIOException(e); } return uri; } //--------------------------------------------------------------------------- public String getURL() { return mURL.toString(); } //--------------------------------------------------------------------------- @Override public long length() { SmbFile file = getSmbFile(); long result; try { result = (file != null ? file.length() : -1); } catch (SmbException e) { throw new RuntimeIOException(e); } return result; } //--------------------------------------------------------------------------- @Override public Long lastModified() { Long lastModified; Long lastModifiedOverride = super.lastModified(); if (lastModifiedOverride != null) { lastModified = lastModifiedOverride; } else { try { lastModified = getSmbFile().lastModified(); } catch (SmbException e) { throw new RuntimeIOException(e); } } return lastModified; } //--------------------------------------------------------------------------- @Override public Calendar getTimestamp() { Calendar timestamp = new GregorianCalendar(); timestamp.setTimeInMillis(lastModified()); return timestamp; } //--------------------------------------------------------------------------- public SMBFileObj setCredentials(LoginCredentials inValue) { return (SMBFileObj) super.setCredentials(inValue); } //--------------------------------------------------------------------------- @Override public List listFiles() { List fileObjs = null; if (isDirectory()) { if (! mURL.toString().endsWith("/")) { // SmbFile behaves badly if the directory path doesn't end with a '/' // so we will tack it on. try { mURL = new URL(mURL + "/"); } catch (MalformedURLException e) { // Ignore } mSmbFile = null; // Force the SmbFile to be recreated } try { SmbFile[] files = getSmbFile().listFiles(); if (files.length > 0) { fileObjs = new ArrayList<>(files.length); for (SmbFile file : files) { fileObjs.add(new SMBFileObj(file).setCredentials(getCredentials())); } } } catch (Exception e) { throw new RuntimeIOException(e); } } return fileObjs; } //--------------------------------------------------------------------------- @Override public boolean exists() { SmbFile file = getSmbFile(); boolean result; try { result = (file != null && file.exists()); } catch (SmbException e) { throw new RuntimeIOException(e); } return result; } //--------------------------------------------------------------------------- @Override public boolean canRead() { SmbFile file = getSmbFile(); boolean result; try { result = (file != null && file.canRead()); // The root won't return an SmbFile } catch (SmbException e) { throw new RuntimeIOException(e); } return result; } //--------------------------------------------------------------------------- @Override public boolean canWrite() { SmbFile file = getSmbFile(); boolean result; try { result = (file != null && file.canWrite()); // The root won't return an SmbFile } catch (SmbException e) { throw new RuntimeIOException(e); } return result; } //--------------------------------------------------------------------------- @Override public boolean isFile() { SmbFile file = getSmbFile(); boolean result; try { result = (file != null && file.isFile()); // The root won't return an SmbFile } catch (SmbException e) { throw new RuntimeIOException(e); } return result; } //--------------------------------------------------------------------------- @Override public boolean isDirectory() { SmbFile file = getSmbFile(); boolean result; try { if (null == file) { result = true; // The root won't return a SmbFile } else { result = file.isDirectory() // Guess if it's a directory by seeing if the URI ends with a '/' || (! file.exists() && getURI().toString().endsWith("/")); } /* if (file != null && ! file.exists()) { // Guess if it's a directory by seeing if the URI ends with a '/' result = getURI().toString().endsWith("/"); } else { result = (file == null || file.isDirectory()); // The root won't return an SmbFile } */ } catch (SmbException e) { throw new RuntimeIOException("Problem accessing " + getURI(), e); } return result; } //-------------------------------------------------------------------------- @Override public boolean moveTo(FileObj inDest) throws IOException { boolean result; if (inDest instanceof SMBFileObj) { SmbFile dest = ((SMBFileObj) inDest).getSmbFile(); if (dest.exists()) { dest.delete(); } getSmbFile().renameTo(dest); result = true; } else { // The destination isn't a SMB location so use the generic moveTo(). result = super.moveTo(inDest); } return result; } //-------------------------------------------------------------------------- // TODO: This may still need some work. The SmbFile's copyTo() seems touchy. @Override public boolean copyTo(FileObj inDest) throws IOException { boolean result; if (inDest instanceof SMBFileObj) { SmbFile dest = ((SMBFileObj) inDest).getSmbFile(); if (dest.exists()) { dest.delete(); } if (dest.isDirectory()) { dest.mkdirs(); } else { dest.createNewFile(); } getSmbFile().copyTo(dest); result = true; } else { // The destination isn't a SMB location so use the generic copyTo(). result = super.copyTo(inDest); } return result; } //--------------------------------------------------------------------------- @Override public SMBFileObj getParentDir() throws IOException { SMBFileObj parentDir = null; if (getPath().length() > 1) { URL url = new URL("smb", mURL.getHost(), getParentDirPath()); parentDir = new SMBFileObj(url, mCIFSContext).setCredentials(getCredentials()); } return parentDir; } //--------------------------------------------------------------------------- @Override public boolean mkdirs() throws IOException { SmbFile file = getSmbFile(); file.mkdirs(); return (file.exists()); } //--------------------------------------------------------------------------- @Override public boolean delete() throws IOException { SmbFile file = getSmbFile(); file.delete(); return (! file.exists()); } //--------------------------------------------------------------------------- @Override public InputStream getInputStream() throws IOException { return new BufferedInputStream(getSmbFile().getInputStream()); } //--------------------------------------------------------------------------- @Override public OutputStream getOutputStream() throws IOException { return getSmbFile().getOutputStream(); } //--------------------------------------------------------------------------- @Override public long writeToStream(OutputStream inStream) throws IOException { StreamUtil.writeToStream(getSmbFile().getInputStream(), inStream); return length(); } //--------------------------------------------------------------------------- public SmbWatchHandle watch(int inFilter, boolean inRecursive) throws IOException { return getSmbFile().watch(inFilter, inRecursive); } //--------------------------------------------------------------------------- public Integer getResopnseTimeout() { return getCIFSContext().getConfig().getResponseTimeout(); } //--------------------------------------------------------------------------- public Integer getSoTimeout() { return getCIFSContext().getConfig().getSoTimeout(); } //########################################################################### // PRIVATE METHODS //########################################################################### //--------------------------------------------------------------------------- private SmbFile getSmbFile() { if (null == mSmbFile) { try { mSmbFile = new SmbFile(getURL(), getCIFSContext()); } catch (IOException e) { throw new RuntimeIOException(e); } } return mSmbFile; } //--------------------------------------------------------------------------- private CIFSContext getCIFSContext() { if (null == mCIFSContext) { Properties properties = new Properties(); PropertyConfiguration configuration; try { configuration = new PropertyConfiguration(properties); } catch (CIFSException e) { throw new RuntimeIOException(e); } CIFSContext context = new BaseContext(configuration); if (getCredentials() != null) { NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator(getCredentials().getDomain(), getCredentials().getUser(), getCredentials().getPasswordString()); context = context.withCredentials(auth); } mCIFSContext = context; } return mCIFSContext; } //--------------------------------------------------------------------------- private String getParentDirPath() { if (null == mParentDirPath) { String parentDirPath = ""; String path = mURL.getPath(); if (path.length() > 0) { int index = path.substring(0, path.length() - 1).lastIndexOf("/"); if (index >= 0) { parentDirPath = path.substring(0, index + 1); } } mParentDirPath = parentDirPath; } return mParentDirPath; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy