diff --git a/htroot/Blog.java b/htroot/Blog.java index ef2654539..08092f749 100644 --- a/htroot/Blog.java +++ b/htroot/Blog.java @@ -188,7 +188,7 @@ public class Blog { map.put("page", pagename); map.put("subject", StrSubject.replace(',', ' ')); map.put("author", StrAuthor.replace(',', ' ')); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_BLOG_ADD, map)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_BLOG_ADD, map)); } page = switchboard.blogDB.read(pagename); //maybe "if(page == null)" diff --git a/htroot/Bookmarks.java b/htroot/Bookmarks.java index d7653676a..b0d4863e0 100644 --- a/htroot/Bookmarks.java +++ b/htroot/Bookmarks.java @@ -322,7 +322,7 @@ public class Bookmarks { map.put("title", title.replace(',', ' ')); map.put("description", description.replace(',', ' ')); map.put("tags", tagsString.replace(',', ' ')); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_BOOKMARK_ADD, map)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_BOOKMARK_ADD, map)); } } diff --git a/htroot/ConfigProfile_p.java b/htroot/ConfigProfile_p.java index 03315f40f..62a0aac1e 100644 --- a/htroot/ConfigProfile_p.java +++ b/htroot/ConfigProfile_p.java @@ -113,7 +113,7 @@ public class ConfigProfile_p { // generate a news message Properties news = profile; news.remove("comment"); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_PROFILE_UPDATE, news)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_PROFILE_UPDATE, news)); //yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsRecord.CATEGORY_PROFILE_UPDATE, profile)); } catch(IOException e) { } finally { diff --git a/htroot/Supporter.java b/htroot/Supporter.java index 920d77c1c..2c396ecbe 100644 --- a/htroot/Supporter.java +++ b/htroot/Supporter.java @@ -78,7 +78,7 @@ public class Supporter { map.put("urlhash", hash); map.put("vote", "negative"); map.put("refid", post.get("refid", "")); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_SURFTIPP_VOTE_ADD, map)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_SURFTIPP_VOTE_ADD, map)); } if ((post != null) && ((hash = post.get("votePositive", null)) != null)) { if (!sb.verifyAuthentication(header, false)) { diff --git a/htroot/Surftips.java b/htroot/Surftips.java index 236740bac..f5d29bbd3 100644 --- a/htroot/Surftips.java +++ b/htroot/Surftips.java @@ -86,7 +86,7 @@ public class Surftips { map.put("urlhash", hash); map.put("vote", "negative"); map.put("refid", post.get("refid", "")); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_SURFTIPP_VOTE_ADD, map)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_SURFTIPP_VOTE_ADD, map)); } if ((post != null) && ((hash = post.get("votePositive", null)) != null)) { if (!sb.verifyAuthentication(header, false)) { diff --git a/htroot/WatchCrawler_p.java b/htroot/WatchCrawler_p.java index 362c65bbf..1d4582ec3 100644 --- a/htroot/WatchCrawler_p.java +++ b/htroot/WatchCrawler_p.java @@ -210,7 +210,7 @@ public class WatchCrawler_p { m.remove("generalFilter"); m.remove("specificFilter"); m.put("intention", post.get("intention", "").replace(',', '/')); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_CRAWL_START, m)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_CRAWL_START, m)); } } else { diff --git a/htroot/Wiki.java b/htroot/Wiki.java index 8fb544ac9..2a8cd998a 100644 --- a/htroot/Wiki.java +++ b/htroot/Wiki.java @@ -136,7 +136,7 @@ public class Wiki { map.put("page", pagename); map.put("author", author.replace(',', ' ')); if (post.get("content", "").trim().length() > 0 && !page.page().equals(content)) - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_WIKI_UPDATE, map)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_WIKI_UPDATE, map)); page = newEntry; prop.put("LOCATION", "/Wiki.html?page=" + pagename); } diff --git a/htroot/yacysearch.java b/htroot/yacysearch.java index 49d296218..5c427648a 100644 --- a/htroot/yacysearch.java +++ b/htroot/yacysearch.java @@ -232,7 +232,7 @@ public class yacysearch { map.put("urlhash", delHash); map.put("vote", "negative"); map.put("refid", ""); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_SURFTIPP_VOTE_ADD, map)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_SURFTIPP_VOTE_ADD, map)); } // if aplus-button was hit, create new voting message diff --git a/source/de/anomic/plasma/plasmaSwitchboard.java b/source/de/anomic/plasma/plasmaSwitchboard.java index fe7b7853e..456a8c012 100644 --- a/source/de/anomic/plasma/plasmaSwitchboard.java +++ b/source/de/anomic/plasma/plasmaSwitchboard.java @@ -2053,7 +2053,7 @@ public final class plasmaSwitchboard extends serverAbstractSwitch implements ser if ((homepage != null) && (homepage.length() > 10)) { Properties news = new Properties(); news.put("homepage", profile.get("homepage")); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_PROFILE_BROADCAST, news)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_PROFILE_BROADCAST, news)); } } diff --git a/source/de/anomic/soap/services/AdminService.java b/source/de/anomic/soap/services/AdminService.java index e2dab037b..675ccd800 100644 --- a/source/de/anomic/soap/services/AdminService.java +++ b/source/de/anomic/soap/services/AdminService.java @@ -760,7 +760,7 @@ public class AdminService extends AbstractService { // generate a news message Properties news = profile; news.remove(PEERPROFILE_COMMENT); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_PROFILE_UPDATE, news)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_PROFILE_UPDATE, news)); } catch(IOException e) { throw new AxisFault("Unable to write profile data to file"); } finally { diff --git a/source/de/anomic/soap/services/BookmarkService.java b/source/de/anomic/soap/services/BookmarkService.java index afbe92421..f837445a6 100644 --- a/source/de/anomic/soap/services/BookmarkService.java +++ b/source/de/anomic/soap/services/BookmarkService.java @@ -184,7 +184,7 @@ public class BookmarkService extends AbstractService { map.put("title", title.replace(',', ' ')); map.put("description", description.replace(',', ' ')); map.put("tags", tagString); - yacyCore.newsPool.publishMyNews(new yacyNewsRecord(yacyNewsPool.CATEGORY_BOOKMARK_ADD, map)); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_BOOKMARK_ADD, map)); } /** diff --git a/source/de/anomic/yacy/yacyNewsAction.java b/source/de/anomic/yacy/yacyNewsAction.java index 3bdbe877f..0272ce382 100644 --- a/source/de/anomic/yacy/yacyNewsAction.java +++ b/source/de/anomic/yacy/yacyNewsAction.java @@ -62,18 +62,20 @@ public class yacyNewsAction implements yacyPeerAction { //System.out.println("### triggered news arrival from peer " + peer.getName() + ", news " + ((recordString == null) ? "empty" : "attached")); if ((recordString == null) || (recordString.length() == 0)) return; String decodedString = de.anomic.tools.crypt.simpleDecode(recordString, ""); - yacyNewsRecord record = new yacyNewsRecord(decodedString); - //System.out.println("### news arrival from peer " + peer.getName() + ", decoded=" + decodedString + ", record=" + recordString + ", news=" + record.toString()); - String cre1 = (String) serverCodings.string2map(decodedString, ",").get("cre"); - String cre2 = (String) serverCodings.string2map(record.toString(), ",").get("cre"); - if ((cre1 == null) || (cre2 == null) || (!(cre1.equals(cre2)))) { - System.out.println("### ERROR - cre are not equal: cre1=" + cre1 + ", cre2=" + cre2); - return; - } - try { - synchronized (pool) {this.pool.enqueueIncomingNews(record);} - } catch (IOException e) { - serverLog.logSevere("YACY", "processPeerArrival", e); + yacyNewsRecord record = yacyNewsRecord.newRecord(decodedString); + if (record != null) { + //System.out.println("### news arrival from peer " + peer.getName() + ", decoded=" + decodedString + ", record=" + recordString + ", news=" + record.toString()); + String cre1 = (String) serverCodings.string2map(decodedString, ",").get("cre"); + String cre2 = (String) serverCodings.string2map(record.toString(), ",").get("cre"); + if ((cre1 == null) || (cre2 == null) || (!(cre1.equals(cre2)))) { + System.out.println("### ERROR - cre are not equal: cre1=" + cre1 + ", cre2=" + cre2); + return; + } + try { + synchronized (pool) {this.pool.enqueueIncomingNews(record);} + } catch (IOException e) { + serverLog.logSevere("YACY", "processPeerArrival", e); + } } } diff --git a/source/de/anomic/yacy/yacyNewsDB.java b/source/de/anomic/yacy/yacyNewsDB.java index c70ee26ef..60d611825 100644 --- a/source/de/anomic/yacy/yacyNewsDB.java +++ b/source/de/anomic/yacy/yacyNewsDB.java @@ -139,7 +139,7 @@ public class yacyNewsDB { protected final static yacyNewsRecord b2r(kelondroRow.Entry b) { if (b == null) return null; - return new yacyNewsRecord( + return yacyNewsRecord.newRecord( b.getColString(0, null), b.getColString(1, "UTF-8"), (b.empty(2)) ? null : yacyCore.parseUniversalDate(b.getColString(2, null), serverDate.UTCDiffString()), @@ -149,8 +149,8 @@ public class yacyNewsDB { } protected final kelondroRow.Entry r2b(yacyNewsRecord r) { + if (r == null) return null; try { - if (r == null) return null; String attributes = r.attributes().toString(); if (attributes.length() > yacyNewsRecord.attributesMaxLength) throw new IllegalArgumentException("attribute length=" + attributes.length() + " exceeds maximum size=" + yacyNewsRecord.attributesMaxLength); kelondroRow.Entry entry = this.news.row().newEntry(); diff --git a/source/de/anomic/yacy/yacyNewsPool.java b/source/de/anomic/yacy/yacyNewsPool.java index 68b6f6dfe..a2abc4383 100644 --- a/source/de/anomic/yacy/yacyNewsPool.java +++ b/source/de/anomic/yacy/yacyNewsPool.java @@ -295,6 +295,7 @@ public class yacyNewsPool { public void publishMyNews(yacyNewsRecord record) { // this shall be called if our peer generated a new news record and wants to publish it + if (record == null) return; try { if (newsDB.get(record.id()) == null) { incomingNews.push(record); // we want to see our own news.. diff --git a/source/de/anomic/yacy/yacyNewsRecord.java b/source/de/anomic/yacy/yacyNewsRecord.java index 9e177d902..b10ecfb82 100644 --- a/source/de/anomic/yacy/yacyNewsRecord.java +++ b/source/de/anomic/yacy/yacyNewsRecord.java @@ -80,19 +80,48 @@ public class yacyNewsRecord { kelondroNaturalOrder.naturalOrder, 0 ); + public static yacyNewsRecord newRecord(String newsString) { + try { + return new yacyNewsRecord(newsString); + } catch (IllegalArgumentException e) { + yacyCore.log.logWarning("rejected bad yacy news record: " + e.getMessage()); + return null; + } + } + + public static yacyNewsRecord newRecord(String category, Map attributes) { + try { + return new yacyNewsRecord(category, attributes); + } catch (IllegalArgumentException e) { + yacyCore.log.logWarning("rejected bad yacy news record: " + e.getMessage()); + return null; + } + } + + public static yacyNewsRecord newRecord(String id, String category, Date received, int distributed, Map attributes) { + try { + return new yacyNewsRecord(id, category, received, distributed, attributes); + } catch (IllegalArgumentException e) { + yacyCore.log.logWarning("rejected bad yacy news record: " + e.getMessage()); + return null; + } + } + public yacyNewsRecord(String newsString) { this.attributes = serverCodings.string2map(newsString, ","); + if (attributes.toString().length() > attributesMaxLength) throw new IllegalArgumentException("attributes length (" + attributes.toString().length() + ") exceeds maximum (" + attributesMaxLength + ")"); + this.category = (attributes.containsKey("cat")) ? (String) attributes.get("cat") : ""; + if (category.length() > categoryStringLength) throw new IllegalArgumentException("category length (" + category.length() + ") exceeds maximum (" + categoryStringLength + ")"); this.received = (attributes.containsKey("rec")) ? yacyCore.parseUniversalDate((String) attributes.get("rec"), serverDate.UTCDiffString()) : new Date(); this.created = (attributes.containsKey("cre")) ? yacyCore.parseUniversalDate((String) attributes.get("cre"), serverDate.UTCDiffString()) : new Date(); - this.category = (attributes.containsKey("cat")) ? (String) attributes.get("cat") : ""; this.distributed = (attributes.containsKey("dis")) ? Integer.parseInt((String) attributes.get("dis")) : 0; this.originator = (attributes.containsKey("ori")) ? (String) attributes.get("ori") : ""; removeStandards(); - } public yacyNewsRecord(String category, Map attributes) { - if (category.length() > categoryStringLength) throw new IllegalArgumentException("category length exceeds maximum"); + if (category.length() > categoryStringLength) throw new IllegalArgumentException("category length (" + category.length() + ") exceeds maximum (" + categoryStringLength + ")"); + if (attributes.toString().length() > attributesMaxLength) throw new IllegalArgumentException("attributes length (" + attributes.toString().length() + ") exceeds maximum (" + attributesMaxLength + ")"); this.attributes = attributes; this.received = null; this.created = new Date(); @@ -100,10 +129,11 @@ public class yacyNewsRecord { this.distributed = 0; this.originator = yacyCore.seedDB.mySeed.hash; removeStandards(); - truncAttrLengthToMax(); } protected yacyNewsRecord(String id, String category, Date received, int distributed, Map attributes) { + if (category.length() > categoryStringLength) throw new IllegalArgumentException("category length (" + category.length() + ") exceeds maximum (" + categoryStringLength + ")"); + if (attributes.toString().length() > attributesMaxLength) throw new IllegalArgumentException("attributes length (" + attributes.toString().length() + ") exceeds maximum (" + attributesMaxLength + ")"); this.attributes = attributes; this.received = received; this.created = yacyCore.parseUniversalDate(id.substring(0, yacyCore.universalDateShortPattern.length()), serverDate.UTCDiffString()); @@ -111,7 +141,6 @@ public class yacyNewsRecord { this.distributed = distributed; this.originator = id.substring(yacyCore.universalDateShortPattern.length()); removeStandards(); - truncAttrLengthToMax(); } private void removeStandards() { @@ -122,13 +151,6 @@ public class yacyNewsRecord { attributes.remove("dis"); } - private void truncAttrLengthToMax() { - String s = this.attributes.toString(); - if (s.length() <= attributesMaxLength) return; - this.attributes = serverCodings.string2map( - s.substring(0, s.lastIndexOf(',', attributesMaxLength)), ","); - } - public String toString() { // this creates the string that shall be distributed // attention: this has no additional encoding @@ -181,6 +203,6 @@ public class yacyNewsRecord { } public static void main(String[] args) { - System.out.println((new yacyNewsRecord(args[0])).toString()); + System.out.println((newRecord(args[0])).toString()); } } \ No newline at end of file