From f4daa9b8928dd7529bd620626dc9787b2153be7b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 2 Aug 2008 20:47:27 +0000 Subject: Client: Added WellFormedItem back In revision 9, it was discovered that WellFormedItem.java contained my password, so it was obliberated from VCS and it needs to be added back in. --- .../fourisland/instadisc/Item/WellFormedItem.java | 139 +++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 client/trunk/src/com/fourisland/instadisc/Item/WellFormedItem.java diff --git a/client/trunk/src/com/fourisland/instadisc/Item/WellFormedItem.java b/client/trunk/src/com/fourisland/instadisc/Item/WellFormedItem.java new file mode 100644 index 0000000..9b54f96 --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/Item/WellFormedItem.java @@ -0,0 +1,139 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.instadisc.Item; + +import com.fourisland.instadisc.Database.Filter; +import com.fourisland.instadisc.Database.Wrapper; +import com.fourisland.instadisc.Item.Categories.Category; +import com.fourisland.instadisc.XmlRpc; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author hatkirby + */ +public class WellFormedItem { + + Item aThis; + + public WellFormedItem(Item aThis) { + this.aThis = aThis; + } + + public boolean check() { + boolean good = true; + good = (good ? checkForRequiredHeaders() : false); + good = (good ? checkForSubscription() : false); + good = (good ? checkForLegalCategory() : false); + good = (good ? Category.checkForRequiredSemantics(aThis.headerMap) : false); + good = (good ? checkForProperVerification() : false); + good = (good ? checkForFilterInvalidation() : false); + good = (good ? checkForSafeURL() : false); + return good; + } + + private boolean checkForFilterInvalidation() { + boolean good = true; + + Filter[] filters = Wrapper.getAllFilter(); + int i = 0; + for (i = 0; i < filters.length; i++) { + if (filters[i].getSubscription().equals(aThis.headerMap.get("Subscription"))) { + if (filters[i].getEqual()) + { + good = (good ? (!aThis.headerMap.get(filters[i].getField()).equals(filters[i].getTest())) : false); + } else { + good = (good ? (aThis.headerMap.get(filters[i].getField()).equals(filters[i].getTest())) : false); + } + } + } + + return good; + } + + private boolean checkForLegalCategory() { + boolean good = false; + good = checkForLegalCategory("blog-post", good); + good = checkForLegalCategory("blog-comment", good); + good = checkForLegalCategory("forum-post", good); + good = checkForLegalCategory("instadisc", good); + good = checkForLegalCategory("email", good); + return good; + } + + private boolean checkForLegalCategory(String string, boolean good) { + return (good ? true : Wrapper.getSubscription(aThis.headerMap.get("Subscription")).getCategory().equals(string)); + } + + private boolean checkForProperVerification() { + boolean good = false; + try { + String vid = aThis.headerMap.get("Verification-ID"); + int ivid = Integer.decode(vid); + Verification ver = new Verification(ivid); + good = aThis.headerMap.get("Verification").equals(ver.getHash()); + } catch (VerificationIDReusedException ex) { + XmlRpc xmlrpc = new XmlRpc("resendItem"); + String id = aThis.headerMap.get("ID"); + int iid = Integer.decode(id); + xmlrpc.addParam(iid); + xmlrpc.execute(); + } catch (Exception ex) { + Logger.getLogger(WellFormedItem.class.getName()).log(Level.SEVERE, null, ex); + } + return good; + } + + private boolean checkForRequiredHeaders() { + boolean good = true; + good = (good ? checkForRequiredHeader("ID") : false); + good = (good ? checkForRequiredHeader("Verification") : false); + good = (good ? checkForRequiredHeader("Verification-ID") : false); + good = (good ? checkForRequiredHeader("Subscription") : false); + good = (good ? checkForRequiredHeader("Title") : false); + good = (good ? checkForRequiredHeader("Author") : false); + good = (good ? checkForRequiredHeader("URL") : false); + return good; + } + + private boolean checkForRequiredHeader(String string) { + return checkForRequiredHeader(aThis.headerMap, string); + } + + public static boolean checkForRequiredHeader(HashMap headerMap, String string) + { + return headerMap.containsKey(string); + } + + private boolean checkForSafeURL() { + try { + URL url = new URL(aThis.headerMap.get("URL")); + URI subUrl = new URI(aThis.headerMap.get("Subscription")); + + return url.getHost().equals(subUrl.getHost()); + } catch (URISyntaxException ex) { + Logger.getLogger(WellFormedItem.class.getName()).log(Level.SEVERE, null, ex); + return false; + } catch (MalformedURLException ex) { + Logger.getLogger(WellFormedItem.class.getName()).log(Level.SEVERE, null, ex); + return false; + } + } + + private boolean checkForSubscription() { + boolean good = Wrapper.existsSubscription(aThis.headerMap.get("Subscription")); + if (!good) + { + SubscriptionFile.deleteSubscription(Wrapper.getSubscription(aThis.headerMap.get("Subscription")), false); + } + return good; + } +} -- cgit 1.4.1