@Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {

      AbstractHttpEntity entity;
      String uri = request.getRequestLine().getUri();
      int i = uri.indexOf(LIBRARY_URL);
      String sha1 = uri.substring(i + LIBRARY_URL.length());
      if (sha1.indexOf("?") != -1) {
        sha1 = sha1.substring(0, sha1.indexOf("?"));
      }
      System.out.println("sha1:" + sha1);

      URN urn = new URNImpl(com.limegroup.gnutella.URN.createSHA1Urn(sha1));
      if (libraryManager.getLibraryManagedList().contains(urn)) {
        FileDesc fileDesc = libraryManager.getLibraryManagedList().getFileDescsByURN(urn).get(0);
        entity = new NFileEntity(fileDesc.getFile(), "application/binary");
        entity.setContentType("application/binary");
        response.setHeader(
            "Content-disposition", "attachment; filename=\"" + fileDesc.getFileName() + "\";");
      } else {
        entity = new NStringEntity("File not found: " + sha1);
        entity.setContentType("text/plain");
      }
      response.setEntity(entity);
    }
 @Override
 public LocalFileItem getFileItem(File file) {
   FileDesc fileDesc = getCoreCollection().getFileDesc(file);
   if (fileDesc != null) {
     return (LocalFileItem) fileDesc.getClientProperty(FILE_ITEM_PROPERTY);
   }
   return null;
 }
 private LocalFileItem getOrCreateLocalFileItem(FileDesc fileDesc) {
   LocalFileItem item;
   Object object = fileDesc.getClientProperty(FILE_ITEM_PROPERTY);
   if (object != null) {
     item = (LocalFileItem) object;
   } else {
     item = fileItemFactory.createCoreLocalFileItem(fileDesc);
     fileDesc.putClientProperty(FILE_ITEM_PROPERTY, item);
   }
   return item;
 }
  public LimeXMLDocument getXMLDocument() {
    // TODO - use fileDesc.getLimeXMLDoc(); note that it has slightly different logic

    // legacy LW had multiple docs per file.
    // now only one is allowed.
    // could potentially pick the one that
    // corresponds to the files type.
    List<LimeXMLDocument> docs = fileDesc.getLimeXMLDocuments();
    if (docs.size() == 1) {
      return docs.get(0);
    } else {
      return null;
    }
  }
  /**
   * kills all in-network downloaders whose URNs are not listed in the list of updates. Deletes any
   * files in the folder that are not listed in the update message.
   */
  private void killObsoleteUpdates(List<? extends DownloadInformation> toDownload) {
    if (!downloadManager.get().isSavedDownloadsLoaded()
        || !fileManager.get().getManagedFileList().isLoadFinished()) return;

    if (_killingObsoleteNecessary) {
      _killingObsoleteNecessary = false;
      downloadManager.get().killDownloadersNotListed(toDownload);

      Set<URN> urns = new HashSet<URN>(toDownload.size());
      for (DownloadInformation data : toDownload) urns.add(data.getUpdateURN());

      List<FileDesc> shared =
          fileManager
              .get()
              .getGnutellaFileList()
              .getFilesInDirectory(LibraryUtils.PREFERENCE_SHARE);
      for (FileDesc fd : shared) {
        if (fd.getSHA1Urn() != null && !urns.contains(fd.getSHA1Urn())) {
          fileManager.get().getManagedFileList().remove(fd.getFile());
          fd.getFile().delete();
        }
      }
    }
  }
 /** Notification that meta information has changed in the filedesc. */
 protected void updateFileDesc(FileDesc fd) {
   LocalFileItem item = (LocalFileItem) fd.getClientProperty(FILE_ITEM_PROPERTY);
   if (item != null) {
     threadSafeList.getReadWriteLock().writeLock().lock();
     try {
       int idx = threadSafeList.indexOf(item);
       if (idx > 0) {
         threadSafeList.set(idx, item);
       } else {
         LOG.warnf(
             "Attempted to update FD w/ LocalFileItem that is not in list anymore. Item {0}",
             item);
       }
     } finally {
       threadSafeList.getReadWriteLock().writeLock().unlock();
     }
   } else {
     LOG.warnf("Attempted to update FD without LocalFileItem, FD {0}", fd);
   }
 }
 public URN getSHA1Urn() {
   return fileDesc.getSHA1Urn();
 }
 public long getSize() {
   return fileDesc.getFileSize();
 }
 public String getFileName() {
   return fileDesc.getFileName();
 }
 @Override
 protected LocalFileItem convertSource(FileDesc source) {
   return (LocalFileItem) source.getClientProperty(FILE_ITEM_PROPERTY);
 }
 protected void removeFileDesc(FileDesc fd) {
   LocalFileItem item = (LocalFileItem) fd.getClientProperty(FILE_ITEM_PROPERTY);
   threadSafeList.remove(item);
 }