KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.matuschek.http;
2
3 /*********************************************
4     Copyright (c) 2001 by Daniel Matuschek
5 *********************************************/

6
7 /**
8  * This class implements a rule what documents should be
9  * downloaded based on file size and mime type
10  *
11  * @author Daniel Matuschek
12  * @version $Id: DownloadRule.java,v 1.4 2003/02/25 13:21:03 oliver_schmidt Exp $
13  */

14 public class DownloadRule {
15
16   private final static int MINDEFAULT = 0;
17   private final static int MAXDEFAULT = Integer.MAX_VALUE;
18
19   /** basic mime type **/
20   private String JavaDoc mimeBaseType=null;
21   private String JavaDoc mimeSubType=null;
22   private int minSize=MINDEFAULT;
23   private int maxSize=MAXDEFAULT;
24   
25   /** allow or deny download ? **/
26   private boolean allow;
27   
28   /** allow or deny processing ? **/
29   private boolean processAllowed = true;
30
31   /**
32      empty constructor that does nothing
33   **/

34   public DownloadRule() {
35   }
36
37   
38   /**
39      Get the value of minSize.
40      @return Value of minSize.
41   **/

42   public int getMinSize () {
43     return minSize;
44   }
45   
46   /**
47      Set the value of minSize.
48      @param v Value to assign to minSize.
49   **/

50   public void setMinSize(int minSize ) {
51     if (minSize >= MINDEFAULT) {
52       this.minSize = minSize;
53     }
54   }
55   
56   /**
57      Get the value of maxSize.
58      @return Value of maxSize.
59   **/

60   public int getMaxSize () {
61     return maxSize;
62   }
63   
64   /**
65      Set the value of maxSize.
66      @param v Value to assign to maxSize.
67   **/

68   public void setMaxSize(int maxSize ) {
69     if (maxSize >= MINDEFAULT) {
70       this.maxSize = maxSize;
71     }
72   }
73   
74   /**
75      Get the value of allow.
76      @return Value of allow.
77   **/

78   public boolean getAllow () {
79     return allow;
80   }
81   
82   /**
83      Set the value of allow.
84      @param v Value to assign to allow.
85   **/

86   public void setAllow(boolean allow ) {
87     this.allow = allow;
88   }
89   
90   /**
91    * Gets the value of processAllowed
92    * @return true, if this rule allows indexing, false if it denies
93    * something
94    */

95   public boolean getProcessAllowed() {
96     return processAllowed && allow;
97   }
98
99   /**
100    * Sets the value of processAllowed
101    * @param processAllowed true, if this rule allows indexing, false if it denies
102    * something
103    */

104   public void setProcessAllowed(boolean processAllowed) {
105     this.processAllowed = processAllowed;
106   }
107   
108   /**
109      Get the value of mimeBaseType.
110      @return Value of mimeBaseType.
111   **/

112   public String JavaDoc getMimeBaseType () {
113     return mimeBaseType;
114   }
115   
116   /**
117      Set the value of mimeBaseType.
118      @param v Value to assign to mimeBaseType.
119   **/

120   public void setMimeBaseType(String JavaDoc mimeBaseType) {
121     this.mimeBaseType = mimeBaseType;
122   }
123   
124   /**
125      Get the value of mimeSubType.
126      @return Value of mimeSubType.
127   **/

128   public String JavaDoc getMimeSubType () {
129     return mimeSubType;
130   }
131   
132   /**
133      Set the value of mimeSubType.
134      @param v Value to assign to mimeSubType.
135   **/

136   public void setMimeSubType(String JavaDoc mimeSubType ) {
137     this.mimeSubType = mimeSubType;
138   }
139   
140   /**
141      Get the value of mimeType.
142      @return Value of mimeType.
143   **/

144   public String JavaDoc getMimeType () {
145         return mimeBaseType+"/"+mimeSubType;
146   }
147   
148   /**
149      Set the value of mimeType.
150      @param v Value to assign to mimeType.
151   **/

152   public void setMimeType(String JavaDoc mimeType)
153     throws IllegalArgumentException JavaDoc
154   {
155     int pos = mimeType.indexOf("/");
156     if (pos < 0) {
157       throw new IllegalArgumentException JavaDoc("mime type must be in the format "+
158                      " basetype/subtype");
159     }
160
161     this.mimeBaseType = mimeType.substring(0,pos);
162     this.mimeSubType = mimeType.substring(pos+1);
163   }
164
165
166   public boolean matches(String JavaDoc mimeBaseType,
167              String JavaDoc mimeSubType,
168              int size) {
169     if (simpleStringMatch(mimeBaseType,this.mimeBaseType) &&
170     simpleStringMatch(mimeSubType,this.mimeSubType)) {
171       if (size >= 0) {
172     if ((size>=this.minSize) && (size<= this.maxSize)) {
173       return true;
174     }
175       } else {
176     // if sizes are default (0, MAXINT), this rule belongs
177
// to ALL documents of this type (it matches), otherwise it depends
178
// on size and doesn't match !
179
if ((this.minSize == MINDEFAULT) &&
180         (this.maxSize == MAXDEFAULT)) {
181       return true;
182     } else {
183       return false;
184     }
185       }
186     }
187     return false;
188   }
189     
190   /**
191      matches the given string to the rule string
192      @param value a string to test
193      @param rule rule used (can be a value or "* that will
194      match anything
195      @return true is value is equal to rule or rule is "*"
196    **/

197   protected boolean simpleStringMatch(String JavaDoc value, String JavaDoc rule) {
198     if (rule.equals("*")) {
199       return true;
200     }
201     if (value.equalsIgnoreCase(rule)) {
202       return true;
203     }
204     return false;
205   }
206   
207
208   /**
209      Convert rule to a String
210      @return a String representation of this rule. Format may change
211      without notice (only useful for debugging or logging)
212    **/

213   public String JavaDoc toString() {
214     String JavaDoc s=null;
215     if (allow) {
216       s="allow";
217     } else {
218       s="deny";
219     }
220     return mimeBaseType+"/"+mimeSubType+" >"+minSize+" <"+maxSize+" "+s;
221   }
222   
223 } // DownloadRule
224
Popular Tags