diff options
-rw-r--r-- | client/trunk/src/com/fourisland/instadisc/Item/WellFormedItem.java | 139 |
1 files changed, 139 insertions, 0 deletions
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 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | package com.fourisland.instadisc.Item; | ||
6 | |||
7 | import com.fourisland.instadisc.Database.Filter; | ||
8 | import com.fourisland.instadisc.Database.Wrapper; | ||
9 | import com.fourisland.instadisc.Item.Categories.Category; | ||
10 | import com.fourisland.instadisc.XmlRpc; | ||
11 | import java.net.MalformedURLException; | ||
12 | import java.net.URI; | ||
13 | import java.net.URISyntaxException; | ||
14 | import java.net.URL; | ||
15 | import java.util.HashMap; | ||
16 | import java.util.logging.Level; | ||
17 | import java.util.logging.Logger; | ||
18 | |||
19 | /** | ||
20 | * | ||
21 | * @author hatkirby | ||
22 | */ | ||
23 | public class WellFormedItem { | ||
24 | |||
25 | Item aThis; | ||
26 | |||
27 | public WellFormedItem(Item aThis) { | ||
28 | this.aThis = aThis; | ||
29 | } | ||
30 | |||
31 | public boolean check() { | ||
32 | boolean good = true; | ||
33 | good = (good ? checkForRequiredHeaders() : false); | ||
34 | good = (good ? checkForSubscription() : false); | ||
35 | good = (good ? checkForLegalCategory() : false); | ||
36 | good = (good ? Category.checkForRequiredSemantics(aThis.headerMap) : false); | ||
37 | good = (good ? checkForProperVerification() : false); | ||
38 | good = (good ? checkForFilterInvalidation() : false); | ||
39 | good = (good ? checkForSafeURL() : false); | ||
40 | return good; | ||
41 | } | ||
42 | |||
43 | private boolean checkForFilterInvalidation() { | ||
44 | boolean good = true; | ||
45 | |||
46 | Filter[] filters = Wrapper.getAllFilter(); | ||
47 | int i = 0; | ||
48 | for (i = 0; i < filters.length; i++) { | ||
49 | if (filters[i].getSubscription().equals(aThis.headerMap.get("Subscription"))) { | ||
50 | if (filters[i].getEqual()) | ||
51 | { | ||
52 | good = (good ? (!aThis.headerMap.get(filters[i].getField()).equals(filters[i].getTest())) : false); | ||
53 | } else { | ||
54 | good = (good ? (aThis.headerMap.get(filters[i].getField()).equals(filters[i].getTest())) : false); | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 | return good; | ||
60 | } | ||
61 | |||
62 | private boolean checkForLegalCategory() { | ||
63 | boolean good = false; | ||
64 | good = checkForLegalCategory("blog-post", good); | ||
65 | good = checkForLegalCategory("blog-comment", good); | ||
66 | good = checkForLegalCategory("forum-post", good); | ||
67 | good = checkForLegalCategory("instadisc", good); | ||
68 | good = checkForLegalCategory("email", good); | ||
69 | return good; | ||
70 | } | ||
71 | |||
72 | private boolean checkForLegalCategory(String string, boolean good) { | ||
73 | return (good ? true : Wrapper.getSubscription(aThis.headerMap.get("Subscription")).getCategory().equals(string)); | ||
74 | } | ||
75 | |||
76 | private boolean checkForProperVerification() { | ||
77 | boolean good = false; | ||
78 | try { | ||
79 | String vid = aThis.headerMap.get("Verification-ID"); | ||
80 | int ivid = Integer.decode(vid); | ||
81 | Verification ver = new Verification(ivid); | ||
82 | good = aThis.headerMap.get("Verification").equals(ver.getHash()); | ||
83 | } catch (VerificationIDReusedException ex) { | ||
84 | XmlRpc xmlrpc = new XmlRpc("resendItem"); | ||
85 | String id = aThis.headerMap.get("ID"); | ||
86 | int iid = Integer.decode(id); | ||
87 | xmlrpc.addParam(iid); | ||
88 | xmlrpc.execute(); | ||
89 | } catch (Exception ex) { | ||
90 | Logger.getLogger(WellFormedItem.class.getName()).log(Level.SEVERE, null, ex); | ||
91 | } | ||
92 | return good; | ||
93 | } | ||
94 | |||
95 | private boolean checkForRequiredHeaders() { | ||
96 | boolean good = true; | ||
97 | good = (good ? checkForRequiredHeader("ID") : false); | ||
98 | good = (good ? checkForRequiredHeader("Verification") : false); | ||
99 | good = (good ? checkForRequiredHeader("Verification-ID") : false); | ||
100 | good = (good ? checkForRequiredHeader("Subscription") : false); | ||
101 | good = (good ? checkForRequiredHeader("Title") : false); | ||
102 | good = (good ? checkForRequiredHeader("Author") : false); | ||
103 | good = (good ? checkForRequiredHeader("URL") : false); | ||
104 | return good; | ||
105 | } | ||
106 | |||
107 | private boolean checkForRequiredHeader(String string) { | ||
108 | return checkForRequiredHeader(aThis.headerMap, string); | ||
109 | } | ||
110 | |||
111 | public static boolean checkForRequiredHeader(HashMap<String, String> headerMap, String string) | ||
112 | { | ||
113 | return headerMap.containsKey(string); | ||
114 | } | ||
115 | |||
116 | private boolean checkForSafeURL() { | ||
117 | try { | ||
118 | URL url = new URL(aThis.headerMap.get("URL")); | ||
119 | URI subUrl = new URI(aThis.headerMap.get("Subscription")); | ||
120 | |||
121 | return url.getHost().equals(subUrl.getHost()); | ||
122 | } catch (URISyntaxException ex) { | ||
123 | Logger.getLogger(WellFormedItem.class.getName()).log(Level.SEVERE, null, ex); | ||
124 | return false; | ||
125 | } catch (MalformedURLException ex) { | ||
126 | Logger.getLogger(WellFormedItem.class.getName()).log(Level.SEVERE, null, ex); | ||
127 | return false; | ||
128 | } | ||
129 | } | ||
130 | |||
131 | private boolean checkForSubscription() { | ||
132 | boolean good = Wrapper.existsSubscription(aThis.headerMap.get("Subscription")); | ||
133 | if (!good) | ||
134 | { | ||
135 | SubscriptionFile.deleteSubscription(Wrapper.getSubscription(aThis.headerMap.get("Subscription")), false); | ||
136 | } | ||
137 | return good; | ||
138 | } | ||
139 | } | ||