1 25 26 package org.snipsnap.snip; 27 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.app.Application; 30 import org.snipsnap.container.Components; 31 import org.snipsnap.user.User; 32 import org.snipsnap.util.ApplicationAwareMap; 33 34 import javax.servlet.http.HttpServletRequest ; 35 import java.io.BufferedReader ; 36 import java.io.ByteArrayInputStream ; 37 import java.io.IOException ; 38 import java.io.InputStreamReader ; 39 import java.net.MalformedURLException ; 40 import java.net.URL ; 41 import java.sql.Timestamp ; 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 54 55 public class Access { 56 57 private final static String BLACKLIST = "SnipSnap/blacklist/referrer"; 58 59 private static ApplicationAwareMap blackListCache = new ApplicationAwareMap(HashMap .class, ArrayList .class); 61 private static Map lastModified = new HashMap (); 62 63 68 public static List getReferrerBlackList() { 69 List cachedBlackList = (List ) blackListCache.getObject(); 70 71 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 72 if (space.exists(BLACKLIST)) { 73 Snip blackListSnip = space.load(BLACKLIST); 74 Timestamp mTime = blackListSnip.getMTime(); 75 String appOid = (String ) Application.get().getObject(Application.OID); 76 Timestamp cachedMTime = (Timestamp ) lastModified.get(appOid); 77 78 if (null == cachedMTime || cachedMTime.getTime() < mTime.getTime()) { 80 cachedBlackList.clear(); 81 lastModified.put(appOid, mTime); 82 83 String content = blackListSnip.getContent(); 84 BufferedReader reader = 85 new BufferedReader (new InputStreamReader (new ByteArrayInputStream (content.getBytes()))); 86 String line; 87 try { 88 while ((line = reader.readLine()) != null) { 89 if (!line.startsWith("#")) { 90 line = line.trim(); 91 if (!"".equals(line)) { 92 cachedBlackList.add(line.trim()); 93 } 94 } 95 } 96 } catch (IOException e) { 97 Logger.warn("Referrer Blacklist Error: " + e.getLocalizedMessage()); 98 e.printStackTrace(); 99 } 100 } 101 } 102 return cachedBlackList; 103 } 104 105 private Links backLinks, snipLinks; 106 private int viewCount = 0; 107 private boolean isModified; 108 109 public Access() { 110 } 111 112 public Access(Links backLinks, Links snipLinks, int viewCount) { 113 this.backLinks = backLinks; 114 this.snipLinks = snipLinks; 115 this.viewCount = viewCount; 116 } 117 118 public void handle(String snipName, HttpServletRequest request) { 120 User user = Application.get().getUser(); 121 if (!user.isNonUser()) { 122 incViewCount(); 123 130 String referrer = request.getHeader("REFERER"); 131 if (null != referrer) { 132 String domain = Application.get().getConfiguration().getUrl(); 135 if (referrer.startsWith(domain)) { 136 int index = referrer.indexOf("/space/"); 137 if (index != -1) { 140 String url = referrer.substring(index + "/space/".length()); 142 index = url.indexOf("?"); 143 if (index != -1) { 144 url = url.substring(0, index); 145 } 146 index = url.indexOf("#"); 147 if (index != -1) { 148 url = url.substring(0, index); 149 } 150 index = url.indexOf(";jsessionid"); 152 if (index != -1) { 153 url = url.substring(0, index); 154 } 155 156 String name = SnipLink.decode(url); 157 158 if (!Application.get().getConfiguration().getStartSnip().equals(name) 159 && !snipName.equals(name)) { 160 snipLinks.addLink(name); 161 } 162 } 163 } else { 164 if (isValidReferrer(referrer)) { 168 backLinks.addLink(referrer); 169 } 170 } 171 } 172 } 173 } 174 175 public boolean isModified() { 176 return isModified; 177 } 178 179 public void addLink(String url) { 180 isModified = true; 181 snipLinks.addLink(url); 182 } 183 184 public Links getBackLinks() { 185 return backLinks; 186 } 187 188 public void setBackLinks(Links backLinks) { 189 isModified = true; 190 this.backLinks = backLinks; 191 } 192 193 public Links getSnipLinks() { 194 return snipLinks; 195 } 196 197 public void setSnipLinks(Links snipLinks) { 198 isModified = true; 199 this.snipLinks = snipLinks; 200 } 201 202 public int getViewCount() { 203 return viewCount; 204 } 205 206 public void setViewCount(int viewCount) { 207 isModified = true; 208 this.viewCount = viewCount; 209 } 210 211 public int incViewCount() { 212 isModified = true; 213 return ++this.viewCount; 214 } 215 216 public static boolean isValidReferrer(String url) { 217 try { 218 URL refURL = new URL (url); 219 if (refURL.getHost().indexOf(".") == -1) { 220 return false; 221 } 222 List blackList = Access.getReferrerBlackList(); 223 if (null != blackList && !blackList.isEmpty()) { 224 Iterator blackListIt = blackList.iterator(); 225 while (blackListIt.hasNext()) { 226 String entry = ((String ) blackListIt.next()).toLowerCase(); 227 if (entry.startsWith("pattern:")) { 228 String pattern = entry.substring("pattern:".length()).trim(); 229 if (url.matches(pattern)) { 230 Logger.warn("invalid referrer url '" + url + "' by pattern '" + pattern + "'"); 231 return false; 232 } 233 } else { 234 String host = new URL (url).getHost().toLowerCase(); 235 if (host.endsWith(entry.trim())) { 236 Logger.warn("invalid referrer url '" + url + "' by domain '" + entry + "'"); 237 return false; 238 } 239 } 240 } 241 } 242 } catch (MalformedURLException e) { 243 Logger.warn("invalid referrer url '" + url + "': " + e.getMessage()); 244 return false; 245 } 246 return true; 247 } 248 249 } 250 | Popular Tags |