From 5543ea08ad54aa6b2cf375634d799d3840d1d870 Mon Sep 17 00:00:00 2001 From: borg-0300 Date: Fri, 16 Sep 2005 20:28:07 +0000 Subject: [PATCH] sorted directory/file list; dont list responseHeader.db; StringBuffers, finals; cleaned; git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@739 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- htroot/CacheAdmin_p.java | 204 ++++++++++++++++++++++----------------- 1 file changed, 116 insertions(+), 88 deletions(-) diff --git a/htroot/CacheAdmin_p.java b/htroot/CacheAdmin_p.java index a7db2e9f8..4c1745edd 100644 --- a/htroot/CacheAdmin_p.java +++ b/htroot/CacheAdmin_p.java @@ -4,7 +4,10 @@ // (C) by Michael Peter Christen; mc@anomic.de // first published on http://www.anomic.de // Frankfurt, Germany, 2004 -// last major change: 28.06.2003 +// +// $LastChangedDate$ +// $LastChangedRevision$ +// $LastChangedBy$ // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -50,6 +53,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.Map; +import java.util.TreeSet; import de.anomic.htmlFilter.htmlFilterContentScraper; import de.anomic.htmlFilter.htmlFilterOutputStream; import de.anomic.http.httpHeader; @@ -65,24 +69,26 @@ import de.anomic.server.serverSwitch; public class CacheAdmin_p { private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); - public static String dateString(Date date) { - return SimpleFormatter.format(date); + public static String dateString(final Date date) { + return SimpleFormatter.format(date); } public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch env) { - plasmaSwitchboard switchboard = (plasmaSwitchboard) env; - serverObjects prop = new serverObjects(); + final plasmaSwitchboard switchboard = (plasmaSwitchboard) env; + final serverObjects prop = new serverObjects(); - String action = ((post == null) ? "info" : post.get("action", "info")); + final String action = ((post == null) ? "info" : post.get("action", "info")); String pathString = ((post == null) ? "" : post.get("path", "/")); - String fileString = pathString; + while (pathString.startsWith("//")) { // where is the BUG ? + pathString = pathString.substring(1); + } + final String fileString = pathString; - // we dont need check the path, because we have do that in plasmaSwitchboard.java - Borg-0300 - File cache = new File(switchboard.getConfig("proxyCache", "DATA/HTCACHE").toString()); + final File cache = new File(switchboard.getConfig("proxyCache", "DATA/HTCACHE")); - File file = new File(cache, pathString); - File dir; - URL url = plasmaHTCache.getURL(cache, file); + File dir; + final File file = new File(cache, pathString); + final URL url = plasmaHTCache.getURL(cache, file); if (file.isDirectory()) { dir = file; @@ -91,110 +97,132 @@ public class CacheAdmin_p { pathString = (new File(pathString)).getParent().replace('\\','/'); } - // generate dir listing - String[] list = dir.list(); - File f; - StringBuffer tree = new StringBuffer(list.length * 200); - tree.append("Directory of
" + ((pathString.length() == 0) ? "domain list" : linkPathString(pathString)) + "

"); - if (list == null) + // generate sorted dir/file listing + final String[] list = dir.list(); + final StringBuffer tree = new StringBuffer((list.length + 2) * 256); + tree.append("Directory of
").append((pathString.length() == 0) ? "domain list" : linkPathString(pathString)).append("

"); + if (list == null) { tree.append("[empty]"); - else { - for (int i = 0; i < list.length; i++) { - f = new File(dir, list[i]); - if (!f.getName().equalsIgnoreCase("responseHeader.db")) - if (f.isDirectory()) - tree.append("\"Folder\" " + list[i] + "
" + serverCore.crlfString); - else - tree.append("\"File\" " + list[i] + "
" + serverCore.crlfString); + } else { + final TreeSet dList = new TreeSet(); + final TreeSet fList = new TreeSet(); + File object; + int size = list.length - 1; + for (int i = size; i >= 0 ; i--) { // Rückwärts ist schneller + object = new File(dir, list[i]); + if (!object.getName().equalsIgnoreCase("responseHeader.db")) { + if (object.isDirectory()) { + dList.add(list[i]); + } else { + fList.add(list[i]); + } + } + } + Iterator iter = dList.iterator(); + String str; + while (iter.hasNext()) { + str = iter.next().toString(); + tree.append("\"Folder\" ").append(str).append("
").append(serverCore.crlfString); + } + iter = fList.iterator(); + while (iter.hasNext()) { + str = iter.next().toString(); + tree.append("\"File\" ").append(str).append("
").append(serverCore.crlfString); } } - String info = ""; - - if (action.equals("info")) { - if (!(file.isDirectory())) { - String urls = htmlFilterContentScraper.urlNormalform(url); - info += "Info for URL " + urls + ":

"; - try { - httpHeader fileheader = switchboard.cacheManager.getCachedResponse(plasmaURL.urlHash(url)); - info += "HTTP Header:
" + formatHeader(fileheader) + "
"; - String ff = file.toString(); - int p = ff.lastIndexOf('.'); - String ext = (p >= 0) ? ff.substring(p + 1).toLowerCase() : ""; - if ((ext.equals("gif")) || (ext.equals("jpg")) || (ext.equals("jpeg")) || (ext.equals("png"))) - info += ""; - else { - htmlFilterContentScraper scraper = new htmlFilterContentScraper(url); - OutputStream os = new htmlFilterOutputStream(null, scraper, null, false); - serverFileUtils.copy(file, os); - os.flush(); - plasmaParserDocument document = switchboard.parser.transformScraper(url, "text/html", scraper); - info += "HEADLINE:
" + scraper.getHeadline() + "

"; - info += "HREF:
" + formatAnchor(document.getHyperlinks()) + "
"; - info += "MEDIA:
" + formatAnchor(document.getMedialinks()) + "
"; - info += "EMAIL:
" + formatAnchor(document.getEmaillinks()) + "
"; - info += "TEXT:
" + new String(scraper.getText()) + "
"; - info += "LINES:
"; - String[] sentences = document.getSentences(); - for (int i = 0; i < sentences.length; i++) info += sentences[i] + "
"; - info += "

"; + final StringBuffer info = new StringBuffer(); + if (action.equals("info") && !file.isDirectory()) { + info.ensureCapacity(40000); + final String urls = htmlFilterContentScraper.urlNormalform(url); + info.append("Info for URL ").append(urls).append("

"); + try { + final httpHeader fileheader = switchboard.cacheManager.getCachedResponse(plasmaURL.urlHash(url)); + info.append("HTTP Header:
").append(formatHeader(fileheader)).append("
"); + final String ff = file.toString(); + final int dotpos = ff.lastIndexOf('.'); + final String ext = (dotpos >= 0) ? ff.substring(dotpos + 1).toLowerCase() : ""; + if (ext.equals("gif") || ext.equals("jpg") || ext.equals("jpeg") || ext.equals("png")) { + info.append(""); + } else { + final htmlFilterContentScraper scraper = new htmlFilterContentScraper(url); + final OutputStream os = new htmlFilterOutputStream(null, scraper, null, false); + serverFileUtils.copy(file, os); +// os.flush(); + final plasmaParserDocument document = switchboard.parser.transformScraper(url, "text/html", scraper); + info.append("HEADLINE:
").append(scraper.getHeadline()).append("
").append("
") + .append("HREF:
").append(formatAnchor(document.getHyperlinks())).append("
") + .append("MEDIA:
").append(formatAnchor(document.getMedialinks())).append("
") + .append("EMAIL:
").append(formatAnchor(document.getEmaillinks())).append("
") + .append("TEXT:
").append(new String(scraper.getText())).append("
") + .append("LINES:
"); + final String[] sentences = document.getSentences(); + for (int i = 0; i < sentences.length; i++) { + info.append(sentences[i]).append("
"); } - } catch (Exception e) { - info += e.toString(); - e.printStackTrace(); + info.append("

"); } - } + } catch (Exception e) { + info.append("- This file is not cached -"); + info.append(e.toString()); + // e.printStackTrace(); + } } prop.put("cachesize", Long.toString(switchboard.cacheManager.currCacheSize/1024)); prop.put("cachemax", Long.toString(switchboard.cacheManager.maxCacheSize/1024)); prop.put("tree", tree.toString()); - prop.put("info", info); + prop.put("info", info.toString()); // return rewrite properties return prop; } private static String formatHeader(httpHeader header) { - if (header == null) return "- no header in header cache -"; - String out = ""; - Iterator it = header.entrySet().iterator(); - Map.Entry entry; - while (it.hasNext()) { - entry = (Map.Entry) it.next(); - out += ""; + final StringBuffer result = new StringBuffer(2048); + if (header == null) { + result.append("- no header in header cache -"); + } else { + result.append("
" + entry.getKey() + " = " + entry.getValue() + "
"); + final Iterator iter = header.entrySet().iterator(); + Map.Entry entry; + while (iter.hasNext()) { + entry = (Map.Entry) iter.next(); + result.append(""); + } + result.append("
").append(entry.getKey()).append(" = ").append(entry.getValue()).append("
"); } - out += ""; - return out; + return result.toString(); } - - private static String formatAnchor(Map a) { - String out = ""; - Iterator i = a.entrySet().iterator(); + + private static String formatAnchor(Map anchor) { + final StringBuffer result = new StringBuffer((anchor.entrySet().size() + 1) * 256); + result.append("
"); + final Iterator iter = anchor.entrySet().iterator(); String url, descr; Map.Entry entry; - while (i.hasNext()) { - entry = (Map.Entry) i.next(); + while (iter.hasNext()) { + entry = (Map.Entry) iter.next(); url = (String) entry.getKey(); descr = ((String) entry.getValue()).trim(); - if (descr.length() == 0) descr = "-"; - out += ""; + if (descr.length() == 0) { descr = "-"; } + result.append(""); } - out += "
" + descr + " " + url + "
").append(descr).append(" ").append(url).append("
"; - return out; + return result.append("").toString(); } - + private static String linkPathString(String Path){ // contributed by Alexander Schier - String Elements[] = Path.split("/"); - String result = ""; - String tmpPath = ""; + final String[] Elements = Path.split("/"); + final StringBuffer result = new StringBuffer(Elements.length * 256); + final StringBuffer tmpPath = new StringBuffer(256); for(int i=0;i<(Elements.length-1);i++){ - tmpPath += Elements[i] + "/"; - result += "" + Elements[i] + "/"; + tmpPath.append(Elements[i]).append("/"); + result.append("").append(Elements[i]).append("/"); } if (Elements.length > 0) { - tmpPath += Elements[Elements.length - 1] + "/"; - result += "" + Elements[Elements.length - 1] + "/"; + tmpPath.append(Elements[Elements.length - 1]).append("/"); + result.append("").append(Elements[Elements.length - 1]).append("/"); } - return result; + return result.toString(); } + }