01: // URLLicense.java
02: // (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
03: // first published 03.07.2007 on http://yacy.net
04: //
05: // This is a part of YaCy, a peer-to-peer based web search engine
06: //
07: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
08: // $LastChangedRevision: 1986 $
09: // $LastChangedBy: orbiter $
10: //
11: // LICENSE
12: //
13: // This program is free software; you can redistribute it and/or modify
14: // it under the terms of the GNU General Public License as published by
15: // the Free Software Foundation; either version 2 of the License, or
16: // (at your option) any later version.
17: //
18: // This program is distributed in the hope that it will be useful,
19: // but WITHOUT ANY WARRANTY; without even the implied warranty of
20: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: // GNU General Public License for more details.
22: //
23: // You should have received a copy of the GNU General Public License
24: // along with this program; if not, write to the Free Software
25: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26:
27: package de.anomic.data;
28:
29: import java.util.HashMap;
30: import java.util.Random;
31:
32: import de.anomic.yacy.yacyURL;
33:
34: public class URLLicense {
35:
36: // this class defines a license-generation for URLs
37: // it is used in case of snippet- and preview-Image-fetching to grant also non-authorized users the usage of a image-fetcher servlet
38:
39: private Random random;
40: private HashMap<String, yacyURL> permissions;
41: private int keylen;
42:
43: public URLLicense(int keylen) {
44: this .permissions = new HashMap<String, yacyURL>();
45: this .random = new Random(System.currentTimeMillis());
46: this .keylen = keylen;
47: }
48:
49: public String aquireLicense(yacyURL url) {
50: // generate license key
51: String license = "";
52: while (license.length() < keylen)
53: license += Integer.toHexString(random.nextInt());
54: license = license.substring(0, keylen);
55: // store reference to url with license key
56: synchronized (permissions) {
57: permissions.put(license, url);
58: }
59: // return the license key
60: return license;
61: }
62:
63: public yacyURL releaseLicense(String license) {
64: yacyURL url = null;
65: synchronized (permissions) {
66: url = permissions.remove(license);
67: }
68: /*
69: if (url == null) {
70: System.out.println("DEBUG-URLLICENSE: no URL license present for code=" + license);
71: } else {
72: System.out.println("DEBUG-URLLICENSE: granted download of " + url.toString());
73: }
74: */
75: return url;
76: }
77:
78: }
|