Пример #1
0
 /**
  * Changes existing entry in a blacklist.
  *
  * @param blacklistToUse The blacklist which contains the entry.
  * @param supportedBlacklistTypes Types of blacklists which the entry is to changed in.
  * @param oldEntry Entry to be changed.
  * @param newEntry Changed entry.
  * @return The length of the new entry.
  */
 private static int alterEntries(
     final String blacklistToUse,
     final String[] supportedBlacklistTypes,
     final String[] oldEntry,
     final String[] newEntry) {
   removeEntries(blacklistToUse, supportedBlacklistTypes, oldEntry);
   PrintWriter pw = null;
   try {
     pw = new PrintWriter(new FileWriter(new File(ListManager.listsPath, blacklistToUse), true));
     String host, path;
     for (final String n : newEntry) {
       int pos = n.indexOf('/');
       if (pos < 0) {
         host = n;
         path = ".*";
       } else {
         host = n.substring(0, pos);
         path = n.substring(pos + 1);
       }
       pw.println(host + "/" + path);
       for (final String s : supportedBlacklistTypes) {
         if (ListManager.listSetContains(s + ".BlackLists", blacklistToUse)) {
           Switchboard.urlBlacklist.add(s, host, path);
         }
       }
       SearchEventCache.cleanupEvents(true);
     }
     pw.close();
   } catch (final IOException e) {
     Log.logSevere("BLACKLIST-CLEANER", "error on writing altered entries to blacklist", e);
   }
   return newEntry.length;
 }
Пример #2
0
  /**
   * Removes existing entries from a blacklist.
   *
   * @param blacklistToUse The blacklist which contains the
   * @param supportedBlacklistTypes Types of blacklists which the entry is to changed in.
   * @param entries Array of entries to be deleted.
   * @return Length of the list of entries to be removed.
   */
  private static int removeEntries(
      final String blacklistToUse, final String[] supportedBlacklistTypes, final String[] entries) {
    // load blacklist data from file
    final List<String> list =
        FileUtils.getListArray(new File(ListManager.listsPath, blacklistToUse));

    boolean listChanged = false;

    // delete the old entry from file
    for (final String entry : entries) {
      String s = entry;

      if (list != null) {

        // get rid of escape characters which make it impossible to
        // properly use contains()
        if (s.contains("\\\\")) {
          s = s.replaceAll(Pattern.quote("\\\\"), Matcher.quoteReplacement("\\"));
        }

        if (list.contains(s)) {
          listChanged = list.remove(s);
        }
      }

      // remove the entry from the running blacklist engine
      for (final String supportedBlacklistType : supportedBlacklistTypes) {
        if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists", blacklistToUse)) {
          final String host = (s.indexOf('/') == -1) ? s : s.substring(0, s.indexOf('/'));
          final String path = (s.indexOf('/') == -1) ? ".*" : s.substring(s.indexOf('/') + 1);
          try {
            Switchboard.urlBlacklist.remove(supportedBlacklistType, host, path);
          } catch (final RuntimeException e) {
            Log.logSevere("BLACKLIST-CLEANER", e.getMessage() + ": " + host + "/" + path);
          }
        }
      }
      SearchEventCache.cleanupEvents(true);
    }
    if (listChanged) {
      FileUtils.writeList(
          new File(ListManager.listsPath, blacklistToUse), list.toArray(new String[list.size()]));
    }
    return entries.length;
  }
Пример #3
0
  private static int importFromBookmarks(
      BookmarksDB db,
      final DigestURI baseURL,
      final InputStreamReader input,
      final String tag,
      final boolean importPublic) {

    int importCount = 0;

    Map<MultiProtocolURI, Properties> links = new HashMap<MultiProtocolURI, Properties>();
    String title;
    MultiProtocolURI url;
    Bookmark bm;
    final Set<String> tags = ListManager.string2set(tag); // this allow multiple default tags
    try {
      // load the links
      final ContentScraper scraper = new ContentScraper(baseURL);
      // OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
      final Writer writer = new TransformerWriter(null, null, scraper, null, false);
      FileUtils.copy(input, writer);
      writer.close();
      links = scraper.getAnchors();
    } catch (final IOException e) {
      Log.logWarning(
          "BOOKMARKS", "error during load of links: " + e.getClass() + " " + e.getMessage());
    }
    for (final Entry<MultiProtocolURI, Properties> link : links.entrySet()) {
      url = link.getKey();
      title = link.getValue().getProperty("name", "");
      Log.logInfo("BOOKMARKS", "links.get(url)");
      if ("".equals(title)) { // cannot be displayed
        title = url.toString();
      }
      bm = db.new Bookmark(url.toString());
      bm.setProperty(Bookmark.BOOKMARK_TITLE, title);
      bm.setTags(tags);
      bm.setPublic(importPublic);
      db.saveBookmark(bm);

      importCount++;
    }

    return importCount;
  }
Пример #4
0
  private static int parseXMLimport(BookmarksDB db, final Node doc, final boolean importPublic) {
    int importCount = 0;
    if ("post".equals(doc.getNodeName())) {
      final NamedNodeMap attributes = doc.getAttributes();
      final String url = attributes.getNamedItem("href").getNodeValue();
      if ("".equals(url)) {
        return 0;
      }
      final Bookmark bm = db.new Bookmark(url);
      String tagsString = "";
      String title = "";
      String description = "";
      String time = "";
      if (attributes.getNamedItem("tag") != null) {
        tagsString = attributes.getNamedItem("tag").getNodeValue();
      }
      if (attributes.getNamedItem("description") != null) {
        title = attributes.getNamedItem("description").getNodeValue();
      }
      if (attributes.getNamedItem("extended") != null) {
        description = attributes.getNamedItem("extended").getNodeValue();
      }
      if (attributes.getNamedItem("time") != null) {
        time = attributes.getNamedItem("time").getNodeValue();
      }
      Set<String> tags = new HashSet<String>();

      if (title != null) {
        bm.setProperty(Bookmark.BOOKMARK_TITLE, title);
      }
      if (tagsString != null) {
        tags = ListManager.string2set(tagsString.replace(' ', ','));
      }
      bm.setTags(tags, true);
      if (time != null) {

        Date parsedDate = null;
        try {
          parsedDate = ISO8601Formatter.FORMATTER.parse(time);
        } catch (final ParseException e) {
          parsedDate = new Date();
        }
        bm.setTimeStamp(parsedDate.getTime());
      }
      if (description != null) {
        bm.setProperty(Bookmark.BOOKMARK_DESCRIPTION, description);
      }
      bm.setPublic(importPublic);
      db.saveBookmark(bm);

      importCount++;
    }
    final NodeList children = doc.getChildNodes();
    if (children != null) {
      for (int i = 0; i < children.getLength(); i++) {
        importCount += parseXMLimport(db, children.item(i), importPublic);
      }
    }

    return importCount;
  }
Пример #5
0
  public static serverObjects respond(
      final RequestHeader header, final serverObjects post, final serverSwitch env) {
    final serverObjects prop = new serverObjects();

    // initialize the list manager
    ListManager.switchboard = (Switchboard) env;
    ListManager.listsPath =
        new File(env.getDataPath(), env.getConfig("listManager.listsPath", "DATA/LISTS"));
    String blacklistToUse = null;

    // get the list of supported blacklist types
    final String supportedBlacklistTypesStr = Blacklist.BLACKLIST_TYPES_STRING;
    final String[] supportedBlacklistTypes = supportedBlacklistTypesStr.split(",");

    prop.put(DISABLED + "checked", "1");

    if (post != null) {

      final boolean allowRegex =
          post.get("allowRegex", "off").equalsIgnoreCase("on") ? true : false;
      prop.put(DISABLED + "checked", (allowRegex) ? "1" : "0");

      if (post.containsKey("listNames")) {
        blacklistToUse = post.get("listNames");
        if (blacklistToUse.length() == 0
            || !ListManager.listSetContains("listManager.listsPath", blacklistToUse)) {
          prop.put("results", "2");
        }
      }

      putBlacklists(
          prop,
          FileUtils.getDirListing(ListManager.listsPath, BLACKLIST_FILENAME_FILTER),
          blacklistToUse);

      if (blacklistToUse != null) {
        prop.put("results", "1");

        if (post.containsKey("delete")) {
          prop.put(RESULTS + "modified", "1");
          prop.put(
              RESULTS + "modified_delCount",
              removeEntries(
                  blacklistToUse, supportedBlacklistTypes, getKeysByPrefix(post, "select", true)));
        } else if (post.containsKey("alter")) {
          prop.put(RESULTS + "modified", "2");
          prop.put(
              RESULTS + "modified_alterCount",
              alterEntries(
                  blacklistToUse,
                  supportedBlacklistTypes,
                  getKeysByPrefix(post, "select", false),
                  getValuesByPrefix(post, "entry", false)));
        }

        // list illegal entries
        final Map<String, BlacklistError> illegalEntries =
            getIllegalEntries(blacklistToUse, Switchboard.urlBlacklist, allowRegex);
        prop.put(RESULTS + "blList", blacklistToUse);
        prop.put(RESULTS + "entries", illegalEntries.size());
        prop.putHTML(RESULTS + "blEngine", Switchboard.urlBlacklist.getEngineInfo());
        prop.put(RESULTS + "disabled", (illegalEntries.isEmpty()) ? "1" : "0");
        if (!illegalEntries.isEmpty()) {
          prop.put(RESULTS + DISABLED + "entries", illegalEntries.size());
          int i = 0;
          String key;
          for (final Entry<String, BlacklistError> entry : illegalEntries.entrySet()) {
            key = entry.getKey();
            prop.put(RESULTS + DISABLED + ENTRIES + i + "_error", entry.getValue().getLong());
            prop.putHTML(RESULTS + DISABLED + ENTRIES + i + "_entry", key);
            i++;
          }
        }
      }
    } else {
      prop.put("results", "0");
      putBlacklists(
          prop,
          FileUtils.getDirListing(ListManager.listsPath, BLACKLIST_FILENAME_FILTER),
          blacklistToUse);
    }

    return prop;
  }