BUG (!?) in HTTPFrame: Non-existing files are not removed...

Hi !

----

Yesterday I digged out a problem with jigsaw:
It seems that directory listings contains file (resources) which have
been removed long-long ago, wasting the database.

I wrote a small fix in HTTPFrame.java's getDirectoryListing(Request);
which removes non-existing file resources (removing other resources may
be tricky - servlets for example may exists only as a "virtual"
HTTP name and not on disk at that location...).
Better would be to find a genernal way of automatic cleanup (which
should be enabled as default). The following solution only keeps the
directory listing clean&&valid:

-- snip --
    public synchronized Reply getDirectoryListing(Request request)
 throws ProtocolException, ResourceException
    {
 if (dresource == null)
     throw new ResourceException("this frame is not attached to a "+
     "DirectoryResource. ("+
     resource.getIdentifier()+")");
 // delete us if the directory was deleted
 if (! dresource.getDirectory().exists()) {
     //delete us and emit an error
     String msg = dresource.getIdentifier()+
  ": deleted, removing the DirectoryResource";
     getServer().errlog(dresource, msg);
     try {
  dresource.delete();
     } catch (MultipleLockException ex) {
     }
     // Emit an error back:
     Reply error = request.makeReply(HTTP.NOT_FOUND) ;
     error.setContent ("<h1>Document not found</h1>"+
         "<p>The document "+
         request.getURL()+
         " is indexed but not available."+
         "<p>The server is misconfigured.") ;
     throw new HTTPException (error) ;
 }
 // Have we already an up-to-date computed a listing ?
 if ((listing == null)
     || (dresource.getDirectory().lastModified() > listing_stamp)
     || (dresource.getLastModified() > listing_stamp)
     || (getLastModified() > listing_stamp)) {

     Class http_class = null;
     try {
  http_class = Class.forName("org.w3c.jigsaw.frames.HTTPFrame");
     } catch (ClassNotFoundException ex) {
  http_class = null;
     }

     Enumeration   enum      =
  dresource.enumerateResourceIdentifiers() ;
     Vector        resources = Sorter.sortStringEnumeration(enum) ;
     HtmlGenerator g         =
  new HtmlGenerator("Directory of "+
      dresource.getIdentifier());
     // Add style link
     addStyleSheet(g);
     g.append("<h1>"+dresource.getIdentifier()+"</h1>");
     // Link to the parent, when possible:
     if ( dresource.getParent() != null )
  g.append("<p><a href=\"..\">Parent</a><br>");
     // List the children:
     for (int i = 0 ; i < resources.size() ; i++) {
  String       name     = (String) resources.elementAt(i);
  ResourceReference rr = null;
  rr = dresource.lookup(name);
  FramedResource resource = null;
  if (rr != null) {
      try {
   resource = (FramedResource) rr.lock();

   // Cleanup added by gisburn 10.10.1999: Remove non-existing file
resources here
   if( resource instanceof FileResource )
   {
     FileResource fr = (FileResource)resource;

     if( !fr.getFile().exists() )
     {
                     try
                     {
                System.err.println( "deleting resource " + fr.getFile()
+ "." );
                fr.delete();
                     } catch (MultipleLockException ex) {}

                     continue;
     }
   }

   HTTPFrame itsframe = null;
   if (http_class != null)
       itsframe =
    (HTTPFrame) resource.getFrame(http_class);
   if (itsframe != null) {
       // Icon first, if available
       String icon = itsframe.getIcon() ;
       if ( icon != null )
    g.append("<img src=\""+
      getIconDirectory() +"/" + icon+
      "\">");
       // Resource's name with link:
       g.append("<a href=\""
         , URLEncoder.encode(name)
         , "\">"+name+"</a>");
       // resource's title, if any:
       String title = itsframe.getTitle();
       if ( title != null )
    g.append(" "+title);
       g.append("<br>\n");
   } else {
       // Resource's name with link:
       g.append(name+" (<i>Not available via HTTP.</i>)");
       g.append("<br>\n");
   }
      } catch (InvalidResourceException ex) {
   g.append(name+
     " cannot be loaded (server misconfigured)");
   g.append("<br>\n");
   continue;
      } finally {
   rr.unlock();
      }
  }
     }
     g.close() ;
     listing_stamp = getLastModified() ;
     listing       = g ;
 } else if ( checkIfModifiedSince(request) == COND_FAILED ) {
     // Is it an IMS request ?
     Reply reply = createDefaultReply(request, HTTP.NOT_MODIFIED) ;
     reply.setContentMD5(null);
     return reply;
 }
 // New content or need update:
 Reply reply = createDefaultReply(request, HTTP.OK) ;
 reply.setLastModified(listing_stamp) ;
 reply.setStream(listing) ;
 // check MD5
 return reply ;
    }
-- snip --

----

Bye,
Roland

--
  __ .  . __
 (o.\ \/ /.o) Roland.Mainz@informatik.med.uni-giessen.de
  \__\/\/__/  gisburn@informatik.med.uni-giessen.de
  /O /==\ O\  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
 (;O/ \/ \O;) TEL +49 (0) 641/99-13193 FAX +49 (0) 641/99-41359

Received on Sunday, 10 October 1999 12:12:38 UTC