KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > http > DownloadRuleSet


1 package net.matuschek.http;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9 import java.util.Vector JavaDoc;
10 /*********************************************
11     Copyright (c) 2001 by Daniel Matuschek
12 *********************************************/

13
14
15 /**
16  * This download selector decides if a file should be downloaded
17  * based on the mime type and the size of the content (content-length
18  * header)
19  *
20  * @author Daniel Matuschek daniel@matuschek.net
21  * @version $Id: DownloadRuleSet.java,v 1.5 2003/02/27 18:46:58 oliver_schmidt Exp $
22  */

23 public class DownloadRuleSet {
24
25
26   /**********************************************************************/
27   /* instance variables */
28   /**********************************************************************/
29
30   /** a vector containing all rules **/
31   protected Vector JavaDoc<DownloadRule> rules;
32
33   /** default behavior, if no rule matches (allow or deny)
34       true = allow, false = deny **/

35   protected boolean defaultBehavior=true;
36
37   
38   /**********************************************************************/
39   /* methods */
40   /**********************************************************************/
41
42   /**
43    * initializes the DownloadRuleSet with an empty rule set
44    * if no other rules will be added, it will allow all downloads
45    */

46   public DownloadRuleSet() {
47     rules=new Vector JavaDoc<DownloadRule>();
48     defaultBehavior=true;
49   }
50
51
52   /**
53    * initializes the DownloadRuleSet with a rule set
54    * read from a file
55    * @see #loadRuleFile(String)
56    */

57   public DownloadRuleSet(String JavaDoc filename)
58     throws IOException JavaDoc
59   {
60     this();
61     loadRuleFile(filename);
62   }
63
64
65   /**
66    * adds a set of rules that are defined in a rule file
67    * a rule file consists of lines in the format<br />
68    * allow|deny mimetype/subtype <xxxx >yyyyy
69    */

70   public void loadRuleFile(String JavaDoc filename)
71     throws IOException JavaDoc
72   {
73     InputStream JavaDoc is = new FileInputStream JavaDoc(filename);
74     BufferedReader JavaDoc reader =
75       new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
76
77     String JavaDoc line = "";
78     int lineno=0;
79
80     while (line != null) {
81       line=reader.readLine();
82       lineno++;
83
84       if ((line != null) &&
85       (! line.trim().equals("")) &&
86       (! line.startsWith("#"))) {
87     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(line);
88     // we need at least 2 tokens
89
if (st.countTokens() < 2) {
90       throw new IOException JavaDoc("line "+lineno+" has less then 2 fields");
91     }
92
93     String JavaDoc allowStr = st.nextToken();
94     boolean allow = true;
95     String JavaDoc mime = st.nextToken();
96
97     // allow or deny ?
98
if (allowStr.equalsIgnoreCase("allow")) {
99       allow=true;
100     } else if (allowStr.equalsIgnoreCase("deny")) {
101       allow=false;
102     } else {
103       throw new IOException JavaDoc("first token in line "+lineno+
104                 " has to be allow or deny");
105     }
106       
107     
108     DownloadRule r = new DownloadRule();
109     r.setAllow(allow);
110     try {
111       r.setMimeType(mime);
112     } catch (IllegalArgumentException JavaDoc e) {
113       throw new IOException JavaDoc(e.getMessage());
114     }
115     
116
117     // parse < and > rules
118
while (st.hasMoreTokens()) {
119       boolean isMin=true;
120
121       String JavaDoc descr=st.nextToken();
122       
123       if (descr.startsWith("<")) {
124         // it is a maximum value
125
isMin=false;
126       } else if (descr.startsWith(">")) {
127         isMin=true;
128       } else {
129         throw new IOException JavaDoc("can't understand "+descr+
130                   " in line "+lineno);
131       }
132
133       int size=0;
134       try {
135         size = Integer.parseInt(descr.substring(1));
136       } catch (NumberFormatException JavaDoc e) {
137         throw new IOException JavaDoc("no numerical value "+descr+
138                   " in line "+lineno);
139       }
140
141       if (isMin) {
142         r.setMinSize(size);
143       } else {
144         r.setMaxSize(size);
145       }
146     }
147
148     rules.add(r);
149       }
150     }
151   }
152     
153
154
155   /**
156    * sets the default behavior
157    * @param allow allow or deny download if no matching rule was
158    * found
159    */

160   public void setDefault(boolean allow) {
161     this.defaultBehavior=allow;
162   }
163
164   /**
165    * gets the default behaviour
166    */

167   public boolean getDefault() {
168     return this.defaultBehavior;
169   }
170
171   /**
172    * Get the value of downloadRules.
173    * @return Value of downloadRules as a Vector fo DownloadRule objects.
174    */

175   public Vector JavaDoc getDownloadRules () {
176     return this.rules;
177   }
178   
179   /**
180    * Set the value of downloadRules.
181    * @param v Value to assign to downloadRules. Must be a vector
182    * of DownloadRule objects
183    */

184   public void setDownloadRules(Vector JavaDoc<DownloadRule> downloadRules) {
185     this.rules = downloadRules;
186   }
187   
188
189   /**
190    * adds a download rule for the given mimetype/subtype
191    * @param mimeType basic mime type (the part before /)
192    * @param mimeSubtype mime sub type (part after /)
193    * @param minSize minimal size (in bytes)
194    * @param maxSize maximal size (in bytes)
195    * @param allow allow or deny this download ?
196    * wildchar "*" can be used as mimeType and mimeSubtype that means
197    * "all". there will be no pattern matching, that means "*" matches
198    * all types, but "t*" doesn't match all types that start with t
199    */

200   public void addRule(String JavaDoc mimeBaseType, String JavaDoc mimeSubtype,
201               int minSize, int maxSize, boolean allow) {
202     DownloadRule newrule = new DownloadRule();
203     newrule.setMimeBaseType(mimeBaseType);
204     newrule.setMimeSubType(mimeSubtype);
205     newrule.setMinSize(minSize);
206     newrule.setMaxSize(maxSize);
207     newrule.setAllow(allow);
208     rules.add(newrule);
209   }
210
211
212   /**
213    * finds the first matching rule
214    * @param mimetype mimeType ("type/subtype")
215    * @return a rule or null if no rule was found
216    */

217   private DownloadRule findRule(String JavaDoc mimeType, int size) {
218     // is it a valid mime string
219
if (mimeType.indexOf("/")<0) {
220       return null;
221     }
222     
223     // modified Dominic Betts 28/5/02
224
// mimetype like:
225
// Content-Type: text/html; Charset=iso-8859-1
226
if (mimeType.indexOf(";") > 0) {
227         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(mimeType, ";");
228         mimeType = st.nextToken();
229     }
230
231     String JavaDoc basetype = null;
232     String JavaDoc subtype = null;
233     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(mimeType,"/");
234     basetype = st.nextToken();
235     subtype = st.nextToken();
236
237     for (int i=0; i<rules.size(); i++) {
238       DownloadRule rule = (DownloadRule)rules.elementAt(i);
239       if (rule.matches(basetype,subtype,size)) {
240     return rule;
241       }
242     }
243
244     return null;
245   }
246
247
248
249   /**
250    * gets the value of a httpHeader from a vector of httpHeaders
251    * @param httpHeaders a Vector of HttpHeader objects
252    * @param name name of the header (e.g. content-length) not case-sensitive
253    * @return the value of this header or null if this header doesn't
254    * exists
255    */

256   protected String JavaDoc getHeaderValue(Vector JavaDoc httpHeaders, String JavaDoc name) {
257     for (int i=0; i<httpHeaders.size(); i++) {
258       HttpHeader h = (HttpHeader)httpHeaders.elementAt(i);
259       if (h.getName().equalsIgnoreCase(name)) {
260     return h.getValue();
261       }
262     }
263     return null;
264   }
265   
266
267   public boolean downloadAllowed(Vector JavaDoc httpHeaders) {
268     String JavaDoc mimeType = getHeaderValue(httpHeaders, HttpHeader.CONTENT_TYPE);
269     String JavaDoc sizeStr = getHeaderValue(httpHeaders, HttpHeader.CONTENT_LENGTH);
270
271     // mimeType must exists in any HTTP response !!!
272
if (mimeType == null) {
273       return false;
274     }
275
276     // size MAY exist, if not use -1 for unknown
277
int size=-1;
278     try {
279       size = Integer.parseInt(sizeStr);
280     } catch (NumberFormatException JavaDoc e) {}
281
282     DownloadRule r = findRule(mimeType,size);
283
284     if (r == null) {
285       return defaultBehavior;
286     } else {
287       // System.err.println(size+" "+r);
288
return r.getAllow();
289     }
290   }
291
292   public boolean processAllowed(Vector JavaDoc httpHeaders) {
293     String JavaDoc mimeType = getHeaderValue(httpHeaders, HttpHeader.CONTENT_TYPE);
294     String JavaDoc sizeStr = getHeaderValue(httpHeaders, HttpHeader.CONTENT_LENGTH);
295
296     // mimeType must exists in any HTTP response !!!
297
if (mimeType == null) {
298       return false;
299     }
300
301     // size MAY exist, if not use -1 for unknown
302
int size=-1;
303     try {
304       size = Integer.parseInt(sizeStr);
305     } catch (NumberFormatException JavaDoc e) {}
306
307     DownloadRule r = findRule(mimeType,size);
308
309     if (r == null) {
310       return defaultBehavior;
311     } else {
312       // System.err.println(size+" "+r);
313
return r.getProcessAllowed();
314     }
315   }
316
317   /**
318    * converts the object to a String represenation. the format may
319    * change without notice. Use it only for debugging and logging.
320    */

321   public String JavaDoc toString() {
322     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
323     sb.append("DownloadRule default=");
324     if (defaultBehavior) {
325       sb.append("true");
326     } else {
327       sb.append("false");
328     }
329     sb.append("\n");
330
331     for (int i=0; i<rules.size(); i++) {
332       sb.append(" ");
333       sb.append(((DownloadRule)rules.elementAt(i)).toString());
334       sb.append("\n");
335     }
336     return sb.toString();
337   }
338
339   
340 } // DownloadRuleSet
341
Popular Tags