KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > datamodel > credential > CredentialAvatar


1 /* CredentialAvatar
2  *
3  * Created on Apr 23, 2004
4  *
5  * Copyright (C) 2004 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.archive.crawler.datamodel.credential;
24
25 import java.io.Serializable JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30 import javax.management.AttributeNotFoundException JavaDoc;
31
32 import org.archive.crawler.datamodel.CrawlURI;
33 import org.archive.crawler.datamodel.CredentialStore;
34 import org.archive.crawler.settings.SettingsHandler;
35
36
37 /**
38  * A credential representation.
39  *
40  * Added to the CrawlServer upon successful authentication. Used as a marker
41  * of successful authentication event and for carrying credential
42  * payload to be used subsequently doing preemptive authentications (e.g.
43  * For case of RFC2617, needs to be offered everytime we're accessing inside
44  * a protected area). Also carried by the CrawlURI when cycling through
45  * processing chain trying a credential to see if it will authenticate.
46  *
47  * <p>This class exists because its not safe to keep references
48  * to the settings derived Credential classes so instead of keeping references
49  * to credential classes, we carry around this avatar.
50  *
51  * <p>Scope for avatars is crawlserver. Only used within a CrawlServer
52  * scope.
53  *
54  * <p>Immutable.
55  *
56  * @author stack
57  * @version $Revision: 1.3.18.1 $, $Date: 2007/01/13 01:31:12 $
58  */

59 public class CredentialAvatar
60 implements Serializable JavaDoc {
61
62     private static final long serialVersionUID = 4489542750898404807L;
63
64     private static final Logger JavaDoc logger =
65         Logger.getLogger(CredentialAvatar.class.getName());
66
67     /**
68      * Key for this credential avatar.
69      */

70     private final String JavaDoc key;
71
72     /**
73      * Type represented by this avatar.
74      */

75     private final Class JavaDoc type;
76
77     /**
78      * Data.
79      *
80      * May be null.
81      *
82      * <p>This used to be an Object and I used to store in here
83      * the httpclient AuthScheme but AuthScheme is not serializable
84      * and so there'd be trouble getting this payload to lie down
85      * in a bdb database. Changed it to String. That should be
86      * generic enough for credential purposes.
87      */

88     private final String JavaDoc payload;
89
90
91     /**
92      * Constructor.
93      * @param type Type for this credential avatar.
94      * @param key Key for this credential avatar.
95      */

96     public CredentialAvatar(Class JavaDoc type, String JavaDoc key) {
97         this(type, key, null);
98     }
99
100     /**
101      * Constructor.
102      * @param type Type for this credential avatar.
103      * @param key Key for this credential avatar.
104      * @param payload Data credential needs rerunning or preempting. May be
105      * null and then just the presence is used as signifier of successful
106      * auth.
107      */

108     public CredentialAvatar(Class JavaDoc type, String JavaDoc key, String JavaDoc payload) {
109         if (!checkType(type)) {
110             throw new IllegalArgumentException JavaDoc("Type is unrecognized: " +
111                 type);
112         }
113         this.key = key;
114         this.type = type;
115         this.payload = payload;
116     }
117
118     /**
119      * Shutdown default constructor.
120      */

121     private CredentialAvatar() {
122         super();
123         this.key = null;
124         this.type = null;
125         this.payload = null;
126     }
127
128     /**
129      * @param candidateType Type to check.
130      * @return True if this is a known credential type.
131      */

132     protected boolean checkType(Class JavaDoc candidateType) {
133         boolean result = false;
134         List JavaDoc types = CredentialStore.getCredentialTypes();
135         for (Iterator JavaDoc i = types.iterator(); i.hasNext();) {
136             if (((Class JavaDoc)i.next()).equals(candidateType)) {
137                 result = true;
138                 break;
139             }
140         }
141         return result;
142     }
143
144     /**
145      * @return Returns the payload. May be null.
146      */

147     public String JavaDoc getPayload() {
148         return this.payload;
149     }
150
151     /**
152      * @return Returns the key.
153      */

154     public String JavaDoc getKey() {
155         return this.key;
156     }
157
158     /**
159      * @return Type represented by this avatar.
160      */

161     public Class JavaDoc getType() {
162         return this.type;
163     }
164
165     /**
166      * @param otherType Class to match.
167      * @return True if this credential avatar is of same type.
168      */

169     public boolean match(Class JavaDoc otherType) {
170         return this.type.equals(otherType);
171     }
172
173     /**
174      * @param otherType Credential to match.
175      * @param otherKey Key to test.
176      * @return True if this is avatar for passed credential.
177      */

178     public boolean match(Class JavaDoc otherType, String JavaDoc otherKey) {
179         return match(otherType) &&
180             (otherKey != null && this.key != null &&
181                     this.key.equals(otherKey));
182     }
183
184     public String JavaDoc toString() {
185         return getType() + "." + this.getKey();
186     }
187
188     /**
189      * @param handler Settings handler.
190      * @param curi CrawlURI to use for context.
191      * @return The credential this avatar represents.
192      */

193     public Credential getCredential(SettingsHandler handler, CrawlURI curi) {
194         Credential result = null;
195
196         CredentialStore cs = CredentialStore.getCredentialStore(handler);
197         if (cs == null) {
198             logger.severe("No credential store for " + curi);
199             return result;
200         }
201
202         Iterator JavaDoc i = cs.iterator(curi);
203         if (i == null) {
204             logger.severe("Have CredentialAvatar " + toString() +
205                 " but no iterator: " + curi);
206             return result;
207         }
208
209         while (i.hasNext()) {
210             Credential c = (Credential)i.next();
211             if (!this.type.isInstance(c)) {
212                 continue;
213             }
214             String JavaDoc credKey = null;
215             try {
216                 credKey = c.getKey(curi);
217             }
218             catch (AttributeNotFoundException JavaDoc e) {
219                 logger.severe("Failed to get key for " + c + " from " + curi);
220             }
221             if (credKey != null && credKey.equals(getKey())) {
222                 result = c;
223                 break;
224             }
225         }
226
227         if (result == null) {
228             logger.severe("Have CredentialAvatar " + toString() +
229                 " but no corresponding credential: " + curi);
230         }
231
232         return result;
233     }
234 }
235
Popular Tags