You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by Paul Hussein <pa...@jrc.it> on 2004/05/27 10:32:14 UTC

RE: JFileChooser for WebDAV

Thanks for the reply Ingo,

My intention was to include it into the Slide CVS tree when finished.

However, I am finding it difficult to track errors as the classloading and
callback mechanisms used in the JFIleBrowser mean its very difficult to
track the origin of calls.

How can I test the FileSystemView standalone, or can someone help me to
track the problem ?

I am developing alone at the moment, and a second pair of eyes is always
useful. I cant see where the call is made that produces the file path with
no separator

getFiles : file fullpath :
http://139.191.67.52:80/WebDAV/confobjectclass_shapefile.xml

should read

getFiles : file fullpath :
http://139.191.67.52:80/WebDAV/conf/objectclass_shapefile.xml




I am guessing this causes the Exception to be thrown. Any help would be
appreciated greatly.

Included below is debug, stack trace and code.

Cheers

Paul.


import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.HttpsURL;
import org.apache.commons.httpclient.URIException;
import org.apache.webdav.lib.WebdavResource;

/**
*
* @author hussepa
*/
public class WebDAVFileSystemView extends FileSystemView {


/** The WebDAV resource. */
private WebdavResource webdavResource = null;
private HttpURL rootURL = null;
private WebdavFile homedir = null;
private String username = null;
private String password = null;
private String uri = null;
private String rootPath = null;

private static final String newFolderString =
UIManager.getString("FileChooser.other.newFolder");
static FileSystemView fsv = null;


public WebDAVFileSystemView( String uri, String rootPath, String username,
String password ) throws java.lang.IllegalAccessError, URIException {
try {
this.rootURL = this.uriToHttpURL( uri + rootPath );
this.uri = uri;
this.rootURL.setUserinfo( username, password );
this.username = username;
this.password = password;
this.rootPath = rootPath;


this.connect();
System.out.println( "Connected successfully to : " + this.rootURL );
this.disconnect( );

// Create home directory object
this.homedir = new WebdavFile( this.rootURL );
System.out.println( "Homedir : " + this.homedir );
}
catch ( java.lang.IllegalAccessError e ) {
System.err.println( e.toString() );
e.printStackTrace( );
throw e;
}

}

private static HttpURL uriToHttpURL(String uri) throws URIException {
HttpURL url = null;
if (uri.startsWith("http://")) {
url = new HttpURL(uri);
}else if (uri.startsWith("https://")) {
url = new HttpsURL(uri);
} else {
throw new URIException("Unknown protocol in URL " + uri);
}
return url;
}

public void disconnect() throws java.lang.UnknownError {
try {
this.webdavResource.close();
}
catch ( Exception e ) {
System.out.println( e.toString() );
throw new UnknownError( );
}
}
public void connect( ) throws java.lang.IllegalAccessError {
try {

this.webdavResource = new WebdavResource( this.rootURL );
}
catch ( Exception e ) {
System.err.println( e.toString() );
throw new IllegalAccessError( );
}
}

public static FileSystemView getFileSystemView() {
try {
if(fsv == null) {
fsv = new WebDAVFileSystemView( "http://127.0.0.1", "/WebDAV", "", "");
}
return fsv;
}
catch ( Exception e ) {
System.err.println( e.toString() );
return null;
}

}

/**
* Returns a File object constructed in dir from the given filename.
*/
public File createFileObject(File dir, String filename) {
File file = null;
if(dir == null) {
file = new File(filename);
} else {
file = new File(dir, filename);
}
return file;
}

/**
* Returns a File object constructed from the given path string.
*/
public File createFileObject(String path) {
File f = new File(path);
if (isFileSystemRoot(f)) {
f = createFileSystemRoot(f);
}
return f;
}


/**
* Creates a new folder with a default folder name.
*/
public File createNewFolder(File containingDir) throws IOException {
try {
if(containingDir == null) {
throw new IOException("Containing directory is null:");
}
WebdavFile newFolder = null;
HttpURL url = null;

url = this.uriToHttpURL( containingDir.getPath() + WebdavFile.davSeparator +
newFolderString );
// Need to add user info so has access for queries
url.setUserinfo( username, password );
newFolder = new WebdavFile( url );
System.out.println( "new folder : " + newFolder.toString( ) );

this.connect( );
if ( this.webdavResource.mkcolMethod( newFolder.getAbsolutePath() ) ) {
System.out.println("succeeded.");
return newFolder;
} else {
System.err.println("failed.");
System.err.println(this.webdavResource.getStatusMessage());
throw new IOException( this.webdavResource.getStatusMessage() );
}
}
catch ( IOException e ) {
throw e;
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return null;
}
finally {
this.disconnect();
}
}
/**
* Returns all root partitions on this system. For example, on
* Windows, this would be the "Desktop" folder, while on DOS this
* would be the A: through Z: drives.
*/
public File[] getRoots() {
try {
return new WebdavFile[] { this.homedir };
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return null;
}
}


/**
* Returns true if the file (directory) can be visited.
* Returns false if the directory cannot be traversed.
*
* @param f the <code>File</code>
* @return <code>true</code> if the file/directory can be traversed,
otherwise <code>false</code>
* @see JFileChooser#isTraversable
* @see FileView#isTraversable
*/
public Boolean isTraversable(File f) {
try {
// System.out.println( "isTraversable : " + f.getPath( ) );
// WebdavFile webdavFile = null;
// this.connect();
// webdavFile = ( WebdavFile ) f;
// this.webdavResource.setHttpURL( new HttpURL( f.getPath( ) ) );
// System.out.println( this.webdavResource.getPath( ) + " : collection : " +
this.webdavResource.isCollection() );
// return Boolean.valueOf( this.webdavResource.isCollection() );
return Boolean.valueOf( f.isDirectory() );
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return Boolean.valueOf( false );
}
finally {
this.disconnect();
}
}

/**
* Name of a file, directory, or folder as it would be displayed in
* a system file browser. Example from Windows: the "M:\" directory
* displays as "CD-ROM (M:)"
*
* The default implementation gets information from the ShellFolder class.
*
* @param f a <code>File</code> object
* @return the file name as it would be displayed by a native file chooser
* @see JFileChooser#getName
*/
public String getSystemDisplayName(File f) {
try {
WebdavFile wdf = null;
wdf = ( WebdavFile ) f;
System.out.println( "getSystemDisplayName : getName : " + f.getName() );
System.out.println( "getSystemDisplayName : getAbsolutePath : " +
f.getAbsolutePath() );
System.out.println( "getSystemDisplayName : getCanonicalPath : " +
f.getCanonicalPath() );
System.out.println( "getSystemDisplayName : getPath : " + f.getPath( ) );
return f.getName( );
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return null;
}
finally {
this.disconnect();
}
}

/**
* Type description for a file, directory, or folder as it would be displayed
in
* a system file browser. Example from Windows: the "Desktop" folder
* is desribed as "Desktop".
*
* Override for platforms with native ShellFolder implementations.
*
* @param f a <code>File</code> object
* @return the file type description as it would be displayed by a native
file chooser
* or null if no native information is available.
* @see JFileChooser#getTypeDescription
*/
public String getSystemTypeDescription(File f) {
return null;
}



/**
* Checks if <code>f</code> represents a real directory or file as opposed to
a
* special folder such as <code>"Desktop"</code>. Used by UI classes to
decide if
* a folder is selectable when doing directory choosing.
*
* @param f a <code>File</code> object
* @return <code>true</code> if <code>f</code> is a real file or directory.
*/
public boolean isFileSystem(File f) {
return true;
}


/**
* Returns whether a file is hidden or not.
*/
public boolean isHiddenFile(File f) {
return f.isHidden();
}


/**
* Is dir the root of a tree in the file system, such as a drive
* or partition. Example: Returns true for "C:\" on Windows 98.
*
* @param f a <code>File</code> object representing a directory
* @return <code>true</code> if <code>f</code> is a root of a filesystem
* @see #isRoot
*/
public boolean isFileSystemRoot(File dir) {
try {
return ( rootURL.getPath().equals( dir.getPath( ) ) );
}
catch ( Exception e ) {
System.out.println( "isFileSystemRoot" + e.toString( ) );
e.printStackTrace( );
return false;
}
}

/**
* Used by UI classes to decide whether to display a special icon
* for drives or partitions, e.g. a "hard disk" icon.
*
* The default implementation has no way of knowing, so always returns false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isDrive(File dir) {
return false;
}

/**
* Used by UI classes to decide whether to display a special icon
* for a floppy disk. Implies isDrive(dir).
*
* The default implementation has no way of knowing, so always returns false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isFloppyDrive(File dir) {
return false;
}

/**
* Used by UI classes to decide whether to display a special icon
* for a computer node, e.g. "My Computer" or a network server.
*
* The default implementation has no way of knowing, so always returns false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isComputerNode(File dir) {
return false;
}

// Providing default implementations for the remaining methods
// because most OS file systems will likely be able to use this
// code. If a given OS can't, override these methods in its
// implementation.

public File getHomeDirectory() {
return this.homedir;
}

/**
* Return the user's default starting directory for the file chooser.
*
* @return a <code>File</code> object representing the default
* starting folder
*/
public File getDefaultDirectory() {
return this.homedir;
}


/**
* Gets the list of shown (i.e. not hidden) files.
*/
public File[] getFiles(File dir, boolean useFileHiding) {
try {

String filenames[] = null;
WebdavFile files[] = null;
HttpURL url = null;
String path = null;

this.connect();
// Now we try to list files

path = dir.getPath( );
System.out.println( "getFiles : RAW PATH : '" + path + "'" );

// If path contains the server preamble, we need to extract that
// and have the path only
if ( path.startsWith( "http" ) ) {
System.out.println( "getFiles : preample : " + this.uri );
path = path.replaceAll(this.uri, "" );
}
if ( !path.endsWith("/") ) {
path = path + "/";
}

System.out.println( "getFiles : path : " + path );







this.webdavResource.setPath( path );
filenames = this.webdavResource.list();
files = new WebdavFile[filenames.length];
for ( int i = 0; i < filenames.length; i++ ) {
System.out.println( "file : " + filenames );
// Lets try to construct a uri from the dir
// given and the current file

String filepath = dir.getPath() + filenames;
System.out.println( "getFiles : file fullpath : " + filepath );
url = this.uriToHttpURL( filepath );
// Need to add user info so has access for queries
url.setUserinfo( username, password );
files = new WebdavFile( url );
}
return files;
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return null;
}
finally {
this.disconnect();
}
}



/**
* Returns the parent directory of <code>dir</code>.
* @param dir the <code>File</code> being queried
* @return the parent directory of <code>dir</code>, or
* <code>null</code> if <code>dir</code> is <code>null</code>
*/
public File getParentDirectory(File dir) {
System.out.println( "dir : " + dir );
if ( dir == null ) {
return null;
}
else if ( dir.equals(this.homedir) ) {
return this.homedir;
}
else {
System.out.println( "getParentFile : " + dir.getParentFile( ) );
return dir.getParentFile();
}
}

/**
* Creates a new <code>File</code> object for <code>f</code> with correct
* behavior for a file system root directory.
*
* @param f a <code>File</code> object representing a file system root
* directory, for example "/" on Unix or "C:\" on Windows.
* @return a new <code>File</code> object
*/
protected File createFileSystemRoot(File f) {
return new FileSystemRoot(f);
}

static class WebdavFile extends org.apache.webdav.lib.WebdavFile {
public WebdavFile( HttpURL httpUrl ) throws URIException {
super( httpUrl );
}
public String getName() {
String name = null;

// Get the base name
name = super.getName( );

// If is a directory, we need to add a trailing slash
if ( this.isDirectory() ) {
name = name + "/";
}
return name;
}
public boolean isDirectory ( )
{
return super.isDirectory();
}
// public String getPath ( )
// {
// String path = null;
//
// // Get the base name
// path = super.getName( );
//
// // If is a directory, we need to add a trailing slash
// if ( this.isDirectory() ) {
// path = path + "/";
// }
// System.out.println ( "WebdavFile getPath : " + path );
// return path;
// }

}



static class FileSystemRoot extends File {
public FileSystemRoot(File f) {
super(f,"");
}

public FileSystemRoot(String s) {
super(s);
}

public boolean isDirectory() {
return true;
}

public String getName() {
return getPath();
}
}
// Test code
public static void main( String args[] ) throws Exception {
JFrame frame = null;

// Setup
JFileChooser fc = new JFileChooser(new WebDAVFileSystemView(
"http://139.191.67.52:80", "/WebDAV", "compass", "compass" ) );
frame = new JFrame( );
fc.showOpenDialog(frame);
}

}

===========================================================================
getSystemDisplayName : getCanonicalPath :
http://139.191.67.52:80/WebDAV/FC2-i386-disc3.iso
getSystemDisplayName : getPath :
http://139.191.67.52:80/WebDAV/FC2-i386-disc3.iso
getSystemDisplayName : getName : WebDAV/
getSystemDisplayName : getAbsolutePath : http://139.191.67.52:80/WebDAV/
getSystemDisplayName : getCanonicalPath : http://139.191.67.52:80/WebDAV/
getSystemDisplayName : getPath : http://139.191.67.52:80/WebDAV/
getFiles : RAW PATH : 'http://139.191.67.52:80/WebDAV/conf'
getFiles : preample : http://139.191.67.52:80
getFiles : path : /WebDAV/conf/
getSystemDisplayName : getName : conf/
getSystemDisplayName : getAbsolutePath : http://139.191.67.52:80/WebDAV/conf
getSystemDisplayName : getCanonicalPath :
http://139.191.67.52:80/WebDAV/conf
getSystemDisplayName : getPath : http://139.191.67.52:80/WebDAV/conf
file : objectclass_collection.xml
getFiles : file fullpath :
http://139.191.67.52:80/WebDAV/confobjectclass_collection.xml
file : objectclasses.xml
getFiles : file fullpath :
http://139.191.67.52:80/WebDAV/confobjectclasses.xml
file : objectclass_shapefile.xml
getFiles : file fullpath :
http://139.191.67.52:80/WebDAV/confobjectclass_shapefile.xml
org.apache.webdav.lib.WebdavException
at org.apache.webdav.lib.WebdavFile.isDirectory(WebdavFile.java:270)
at
it.jrc.compass.webdav.WebDAVFileSystemView$WebdavFile.isDirectory(WebDAVFile
SystemView.java:485)
at
it.jrc.compass.webdav.WebDAVFileSystemView$WebdavFile.getName(WebDAVFileSyst
emView.java:478)
at
sun.awt.shell.Win32ShellFolderManager2.compareFiles(Win32ShellFolderManager2
.java:335)
at
sun.awt.shell.Win32ShellFolderManager2$2.compare(Win32ShellFolderManager2.ja
va:356)
at sun.awt.shell.WingetSystemDisplayName : getName : FC2-i386-disc3.iso
getSystemDisplayName : getAbsolutePath :
http://139.191.67.52:80/WebDAV/FC2-i386-disc3.iso
getSystemDisplayName : getCanonicalPath :
http://139.191.67.52:80/WebDAV/FC2-i386-disc3.iso
getSyste32ShellFolderManager2$2.compare(Win32ShellFolderManager2.java:352)mD
isplayName : getPath : http://139.191.67.52:80/WebDAV/FC2-i386-disc3.iso

> -----Original Message-----
> From: Ingo Brunberg [mailto:ib@fiz-chemie.de]
> Sent: Wednesday, May 26, 2004 6:08 PM
> To: slide-user@jakarta.apache.org
> Subject: Re: JFileChooser for WebDAV
>
>
> Yes, I think there would be a great amount of interest and I would
> like to see it integrated in Slide's client library.
>
> As I have explained many times, we once had two such approaches in
> Slide, one that was never finished and one that was IMHO too complex
> and never working quite right. Now, I am glad you started from scratch
> with a simple approach I had in mind, but not the time to implement.
>
> Should you encounter serious problems I'll promise to have a look and
> help although I'm limited in time.
>
> Ingo
>
> > Hi,
> >
> >
> > I am in the process of developing a FileSystemView for WebDAV using the
> > Slide Client API. This will give the JFileChosser the ability
> to browse a
> > WebDAV repository.
> >
> >
> > Would anyone else be interested in this ?
> >
> >
> >
> > It would be nice to have a hand if anyone is interested in helping.
> >
> >
> > Cheers
> >
> >
> > Paul.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: slide-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: slide-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org