KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > Access


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
35 import java.io.BufferedReader JavaDoc;
36 import java.io.ByteArrayInputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStreamReader JavaDoc;
39 import java.net.MalformedURLException JavaDoc;
40 import java.net.URL JavaDoc;
41 import java.sql.Timestamp JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * Stores Access information for a snip like viewCount, backLinks, ...
50  *
51  * @author stephan
52  * @version $Id: Access.java 1783 2004-12-09 11:30:06Z leo $
53  */

54
55 public class Access {
56
57   private final static String JavaDoc BLACKLIST = "SnipSnap/blacklist/referrer";
58
59   // cache of the blacklist
60
private static ApplicationAwareMap blackListCache = new ApplicationAwareMap(HashMap JavaDoc.class, ArrayList JavaDoc.class);
61   private static Map lastModified = new HashMap JavaDoc();
62
63   /**
64    * Get a list of blacklisted referrers as a list of patterns.
65    *
66    * @return the blacklist patterns
67    */

68   public static List JavaDoc getReferrerBlackList() {
69     List JavaDoc cachedBlackList = (List JavaDoc) blackListCache.getObject();
70
71     SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
72     if (space.exists(BLACKLIST)) {
73       Snip blackListSnip = space.load(BLACKLIST);
74       Timestamp JavaDoc mTime = blackListSnip.getMTime();
75       String JavaDoc appOid = (String JavaDoc) Application.get().getObject(Application.OID);
76       Timestamp JavaDoc cachedMTime = (Timestamp JavaDoc) lastModified.get(appOid);
77
78       // update blacklist from snip if it does not exist or is new
79
if (null == cachedMTime || cachedMTime.getTime() < mTime.getTime()) {
80         cachedBlackList.clear();
81         lastModified.put(appOid, mTime);
82
83         String JavaDoc content = blackListSnip.getContent();
84         BufferedReader JavaDoc reader =
85                 new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new ByteArrayInputStream JavaDoc(content.getBytes())));
86         String JavaDoc 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 JavaDoc 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   // DO NOT store snip in Acess this creates problems with Aspects
119
public void handle(String JavaDoc snipName, HttpServletRequest JavaDoc request) {
120     User user = Application.get().getUser();
121     if (!user.isNonUser()) {
122       incViewCount();
123 // preparation for better link statistics
124
// HttpSession session = request.getSession();
125
// if(session != null) {
126
// String lastSnip = (String)session.getAttribute("fromSnip");
127
// session.setAttribute("fromSnip", snipName);
128
// }
129

130       String JavaDoc referrer = request.getHeader("REFERER");
131       if (null != referrer) {
132         // Decode URL to remove jsessionid for example
133
// referrer =
134
String JavaDoc domain = Application.get().getConfiguration().getUrl();
135         if (referrer.startsWith(domain)) {
136           int index = referrer.indexOf("/space/");
137           // Does the referrer point to a snip ?
138
// Forget otherwise (e.g. "/exec/login.jsp")
139
if (index != -1) {
140             // @TODO replace all with regex.
141
String JavaDoc 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             // Hack to remove possible jsessionid
151
index = url.indexOf(";jsessionid");
152             if (index != -1) {
153               url = url.substring(0, index);
154             }
155
156             String JavaDoc 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           // do not count localhosts, single hosts and ignored urls. Will
165
// not find local network IPs and MacOS X
166
// hosts like megid.local
167
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 JavaDoc 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 JavaDoc url) {
217     try {
218       URL JavaDoc refURL = new URL JavaDoc(url);
219       if (refURL.getHost().indexOf(".") == -1) {
220         return false;
221       }
222       List JavaDoc blackList = Access.getReferrerBlackList();
223       if (null != blackList && !blackList.isEmpty()) {
224         Iterator JavaDoc blackListIt = blackList.iterator();
225         while (blackListIt.hasNext()) {
226           String JavaDoc entry = ((String JavaDoc) blackListIt.next()).toLowerCase();
227           if (entry.startsWith("pattern:")) {
228             String JavaDoc 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 JavaDoc host = new URL JavaDoc(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 JavaDoc e) {
243       Logger.warn("invalid referrer url '" + url + "': " + e.getMessage());
244       return false;
245     }
246     return true;
247   }
248
249 }
250
Popular Tags