001: //plasmaCrawlRobotsTxt.java
002: //-------------------------------------
003: //part of YACY
004: //(C) by Michael Peter Christen; mc@anomic.de
005: //first published on http://www.anomic.de
006: //Frankfurt, Germany, 2004
007: //
008: //This file ist contributed by Martin Thelian
009: //last major change: $LastChangedDate$ by $LastChangedBy$
010: //Revision: $LastChangedRevision$
011: //
012: //This program is free software; you can redistribute it and/or modify
013: //it under the terms of the GNU General Public License as published by
014: //the Free Software Foundation; either version 2 of the License, or
015: //(at your option) any later version.
016: //
017: //This program is distributed in the hope that it will be useful,
018: //but WITHOUT ANY WARRANTY; without even the implied warranty of
019: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: //GNU General Public License for more details.
021: //
022: //You should have received a copy of the GNU General Public License
023: //along with this program; if not, write to the Free Software
024: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: //Using this software in any meaning (reading, learning, copying, compiling,
027: //running) means that you agree that the Author(s) is (are) not responsible
028: //for cost, loss of data or any harm that may be caused directly or indirectly
029: //by usage of this softare or this documentation. The usage of this software
030: //is on your own risk. The installation and usage (starting/running) of this
031: //software may allow other people or application to access your computer and
032: //any attached devices and is highly dependent on the configuration of the
033: //software which must be done by the user of the software; the author(s) is
034: //(are) also not responsible for proper configuration and usage of the
035: //software, even if provoked by documentation provided together with
036: //the software.
037: //
038: //Any changes to this file according to the GPL as documented in the file
039: //gpl.txt aside this file in the shipment you received can be done to the
040: //lines that follows this copyright notice here, but changes must not be
041: //done inside the copyright notive above. A re-distribution must contain
042: //the intact and unchanged copyright notice.
043: //Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.plasma;
046:
047: import java.io.File;
048: import java.io.IOException;
049: import java.util.ArrayList;
050: import java.util.Arrays;
051: import java.util.Date;
052: import java.util.HashMap;
053: import java.util.Iterator;
054: import java.util.LinkedList;
055:
056: import de.anomic.kelondro.kelondroDyn;
057: import de.anomic.kelondro.kelondroException;
058: import de.anomic.kelondro.kelondroMapObjects;
059: import de.anomic.kelondro.kelondroNaturalOrder;
060:
061: public class plasmaCrawlRobotsTxt {
062:
063: public static final String ROBOTS_DB_PATH_SEPARATOR = ";";
064:
065: kelondroMapObjects robotsTable;
066: private final File robotsTableFile;
067: private long preloadTime;
068:
069: public plasmaCrawlRobotsTxt(File robotsTableFile, long preloadTime) {
070: this .robotsTableFile = robotsTableFile;
071: this .preloadTime = preloadTime;
072: robotsTableFile.getParentFile().mkdirs();
073: robotsTable = new kelondroMapObjects(new kelondroDyn(
074: robotsTableFile, true, true, preloadTime, 256, 512,
075: '_', kelondroNaturalOrder.naturalOrder, false, false,
076: true), 100);
077: }
078:
079: private void resetDatabase() {
080: // deletes the robots.txt database and creates a new one
081: if (robotsTable != null)
082: robotsTable.close();
083: if (!(robotsTableFile.delete()))
084: throw new RuntimeException(
085: "cannot delete robots.txt database");
086: robotsTableFile.getParentFile().mkdirs();
087: robotsTable = new kelondroMapObjects(new kelondroDyn(
088: robotsTableFile, true, true, preloadTime, 256, 512,
089: '_', kelondroNaturalOrder.naturalOrder, false, false,
090: true), 100);
091: }
092:
093: public void close() {
094: this .robotsTable.close();
095: }
096:
097: public int size() {
098: return this .robotsTable.size();
099: }
100:
101: public void removeEntry(String hostName) {
102: try {
103: this .robotsTable.remove(hostName.toLowerCase());
104: } catch (IOException e) {
105:
106: } catch (kelondroException e) {
107: resetDatabase();
108: }
109: }
110:
111: public Entry getEntry(String hostName) {
112: try {
113: HashMap<String, String> record = this .robotsTable
114: .getMap(hostName);
115: if (record == null)
116: return null;
117: return new Entry(hostName, record);
118: } catch (kelondroException e) {
119: resetDatabase();
120: return null;
121: }
122: }
123:
124: public Entry addEntry(String hostName,
125: ArrayList<String> disallowPathList, Date loadedDate,
126: Date modDate, String eTag, String sitemap,
127: Integer crawlDelay) {
128: Entry entry = new Entry(hostName, disallowPathList, loadedDate,
129: modDate, eTag, sitemap, crawlDelay);
130: addEntry(entry);
131: return entry;
132: }
133:
134: public String addEntry(Entry entry) {
135: // writes a new page and returns key
136: try {
137: this .robotsTable.set(entry.hostName, entry.mem);
138: return entry.hostName;
139: } catch (IOException e) {
140: return null;
141: }
142: }
143:
144: public class Entry {
145: public static final String DISALLOW_PATH_LIST = "disallow";
146: public static final String LOADED_DATE = "date";
147: public static final String MOD_DATE = "modDate";
148: public static final String ETAG = "etag";
149: public static final String SITEMAP = "sitemap";
150: public static final String CRAWL_DELAY = "crawlDelay";
151:
152: // this is a simple record structure that hold all properties of a single crawl start
153: HashMap<String, String> mem;
154: private LinkedList<String> disallowPathList;
155: String hostName;
156:
157: public Entry(String hostName, HashMap<String, String> mem) {
158: this .hostName = hostName.toLowerCase();
159: this .mem = mem;
160:
161: if (this .mem.containsKey(DISALLOW_PATH_LIST)) {
162: this .disallowPathList = new LinkedList<String>();
163: String csPl = (String) this .mem.get(DISALLOW_PATH_LIST);
164: if (csPl.length() > 0) {
165: String[] pathArray = csPl
166: .split(ROBOTS_DB_PATH_SEPARATOR);
167: if ((pathArray != null) && (pathArray.length > 0)) {
168: this .disallowPathList.addAll(Arrays
169: .asList(pathArray));
170: }
171: }
172: } else {
173: this .disallowPathList = new LinkedList<String>();
174: }
175: }
176:
177: public Entry(String hostName,
178: ArrayList<String> disallowPathList, Date loadedDate,
179: Date modDate, String eTag, String sitemap,
180: Integer crawlDelay) {
181: if ((hostName == null) || (hostName.length() == 0))
182: throw new IllegalArgumentException(
183: "The hostname is missing");
184:
185: this .hostName = hostName.trim().toLowerCase();
186: this .disallowPathList = new LinkedList<String>();
187:
188: this .mem = new HashMap<String, String>(5);
189: if (loadedDate != null)
190: this .mem.put(LOADED_DATE, Long.toString(loadedDate
191: .getTime()));
192: if (modDate != null)
193: this .mem
194: .put(MOD_DATE, Long.toString(modDate.getTime()));
195: if (eTag != null)
196: this .mem.put(ETAG, eTag);
197: if (sitemap != null)
198: this .mem.put(SITEMAP, sitemap);
199: if (crawlDelay != null)
200: this .mem.put(CRAWL_DELAY, crawlDelay.toString());
201:
202: if ((disallowPathList != null)
203: && (disallowPathList.size() > 0)) {
204: this .disallowPathList.addAll(disallowPathList);
205:
206: StringBuffer pathListStr = new StringBuffer();
207: for (int i = 0; i < disallowPathList.size(); i++) {
208: pathListStr.append(disallowPathList.get(i)).append(
209: ROBOTS_DB_PATH_SEPARATOR);
210: }
211: this .mem.put(DISALLOW_PATH_LIST, pathListStr.substring(
212: 0, pathListStr.length() - 1));
213: }
214: }
215:
216: public String toString() {
217: StringBuffer str = new StringBuffer();
218: str
219: .append(
220: (this .hostName == null) ? "null"
221: : this .hostName).append(": ");
222:
223: if (this .mem != null) {
224: str.append(this .mem.toString());
225: }
226:
227: return str.toString();
228: }
229:
230: public String getSitemap() {
231: return this .mem.containsKey(SITEMAP) ? (String) this .mem
232: .get(SITEMAP) : null;
233: }
234:
235: public Date getLoadedDate() {
236: if (this .mem.containsKey(LOADED_DATE)) {
237: return new Date(Long.valueOf(
238: (String) this .mem.get(LOADED_DATE)).longValue());
239: }
240: return null;
241: }
242:
243: public void setLoadedDate(Date newLoadedDate) {
244: if (newLoadedDate != null) {
245: this .mem.put(LOADED_DATE, Long.toString(newLoadedDate
246: .getTime()));
247: }
248: }
249:
250: public Date getModDate() {
251: if (this .mem.containsKey(MOD_DATE)) {
252: return new Date(Long.valueOf(
253: (String) this .mem.get(MOD_DATE)).longValue());
254: }
255: return null;
256: }
257:
258: public String getETag() {
259: if (this .mem.containsKey(ETAG)) {
260: return (String) this .mem.get(ETAG);
261: }
262: return null;
263: }
264:
265: public Integer getCrawlDelay() {
266: if (this .mem.containsKey(CRAWL_DELAY)) {
267: return Integer.valueOf((String) this .mem
268: .get(CRAWL_DELAY));
269: }
270: return null;
271: }
272:
273: public boolean isDisallowed(String path) {
274: if ((this .mem == null)
275: || (this .disallowPathList.size() == 0))
276: return false;
277:
278: // if the path is null or empty we set it to /
279: if ((path == null) || (path.length() == 0))
280: path = "/";
281: // escaping all occurences of ; because this char is used as special char in the Robots DB
282: else
283: path = path.replaceAll(ROBOTS_DB_PATH_SEPARATOR, "%3B");
284:
285: Iterator<String> pathIter = this .disallowPathList
286: .iterator();
287: while (pathIter.hasNext()) {
288: String nextPath = pathIter.next();
289: // allow rule
290: if (nextPath.startsWith("!") && nextPath.length() > 1
291: && path.startsWith(nextPath.substring(1))) {
292: return false;
293: }
294:
295: // disallow rule
296: if (path.startsWith(nextPath)) {
297: return true;
298: }
299: }
300: return false;
301: }
302:
303: }
304:
305: }
|