SmbFile[] dirents;
SmbFile f;
int i, j, len, maxLen, dirCount, fileCount, sort;
String str, name, path, fmt;
LinkedList sorted;
ListIterator iter;
SimpleDateFormat sdf = new SimpleDateFormat( "MM/d/yy h:mm a" );
GregorianCalendar cal = new GregorianCalendar();
sdf.setCalendar( cal );
dirents = dir.listFiles();
sorted = new LinkedList();
if(( fmt = req.getParameter( "fmt" )) == null ) {
fmt = "col";
}
sort = 0;
if(( str = req.getParameter( "sort" )) == null || str.equals( "name" )) {
sort = 0;
} else if( str.equals( "size" )) {
sort = 1;
} else if( str.equals( "type" )) {
sort = 2;
} else if( str.equals( "date" )) {
sort = 3;
}
dirCount = fileCount = 0;
maxLen = 28;
for( i = 0; i < dirents.length; i++ ) {
try {
if( dirents[i].getType() == SmbFile.TYPE_NAMED_PIPE ) {
continue;
}
} catch( SmbAuthException sae ) {
} catch( SmbException se ) {
if( se.getNtStatus() != se.NT_STATUS_UNSUCCESSFUL ) {
throw se;
}
}
if( dirents[i].isDirectory() ) {
dirCount++;
} else {
fileCount++;
}
name = dirents[i].getName();
len = name.length();
if( len > maxLen ) {
maxLen = len;
}
iter = sorted.listIterator();
for( j = 0; iter.hasNext(); j++ ) {
if( sort == 0 ) {
if( compareNames( dirents[i], name, (SmbFile)iter.next() ) < 0 ) {
break;
}
} else if( sort == 1 ) {
if( compareSizes( dirents[i], name, (SmbFile)iter.next() ) < 0 ) {
break;
}
} else if( sort == 2 ) {
if( compareTypes( dirents[i], name, (SmbFile)iter.next() ) < 0 ) {
break;
}
} else if( sort == 3 ) {
if( compareDates( dirents[i], name, (SmbFile)iter.next() ) < 0 ) {
break;
}
}
}
sorted.add( j, dirents[i] );
}
if( maxLen > 50 ) {
maxLen = 50;
}
maxLen *= 9; /* convert to px */
out = resp.getWriter();
resp.setContentType( "text/html" );
out.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" );
out.println( "<html><head><title>Network Explorer</title>" );
out.println( "<meta HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">" );
out.println( "<style TYPE=\"text/css\">" );
out.println( style );
if( dirents.length < 200 ) {
out.println( " a:hover {" );
out.println( " background: #a2ff01;" );
out.println( " }" );
}
out.println( "</STYLE>" );
out.println( "</head><body>" );
out.print( "<a class=\"sort\" style=\"width: " + maxLen + ";\" href=\"?fmt=detail&sort=name\">Name</a>" );
out.println( "<a class=\"sort\" href=\"?fmt=detail&sort=size\">Size</a>" );
out.println( "<a class=\"sort\" href=\"?fmt=detail&sort=type\">Type</a>" );
out.println( "<a class=\"sort\" style=\"width: 180\" href=\"?fmt=detail&sort=date\">Modified</a><br clear='all'><p>" );
path = dir.getCanonicalPath();
if( path.length() < 7 ) {
out.println( "<b><big>smb://</big></b><br>" );
path = ".";
} else {
out.println( "<b><big>" + path + "</big></b><br>" );
path = "../";
}
out.println( (dirCount + fileCount) + " objects (" + dirCount + " directories, " + fileCount + " files)<br>" );
out.println( "<b><a class=\"plain\" href=\".\">normal</a> | <a class=\"plain\" href=\"?fmt=detail\">detailed</a></b>" );
out.println( "<p><table border='0' cellspacing='0' cellpadding='0'><tr><td>" );
out.print( "<A style=\"width: " + maxLen );
out.print( "; height: 18;\" HREF=\"" );
out.print( path );
out.println( "\"><b>↑</b></a>" );
if( fmt.equals( "detail" )) {
out.println( "<br clear='all'>" );
}
if( path.length() == 1 || dir.getType() != SmbFile.TYPE_WORKGROUP ) {
path = "";
}
iter = sorted.listIterator();
while( iter.hasNext() ) {
f = (SmbFile)iter.next();
name = f.getName();
if( fmt.equals( "detail" )) {
out.print( "<A style=\"width: " + maxLen );
out.print( "; height: 18;\" HREF=\"" );