1 package net.matuschek.http; 2 3 import java.io.BufferedReader ; 4 import java.io.FileInputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.InputStreamReader ; 8 import java.util.StringTokenizer ; 9 import java.util.Vector ; 10 13 14 15 23 public class DownloadRuleSet { 24 25 26 27 28 29 30 31 protected Vector <DownloadRule> rules; 32 33 35 protected boolean defaultBehavior=true; 36 37 38 39 40 41 42 46 public DownloadRuleSet() { 47 rules=new Vector <DownloadRule>(); 48 defaultBehavior=true; 49 } 50 51 52 57 public DownloadRuleSet(String filename) 58 throws IOException 59 { 60 this(); 61 loadRuleFile(filename); 62 } 63 64 65 70 public void loadRuleFile(String filename) 71 throws IOException 72 { 73 InputStream is = new FileInputStream (filename); 74 BufferedReader reader = 75 new BufferedReader (new InputStreamReader (is)); 76 77 String 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 st = new StringTokenizer (line); 88 if (st.countTokens() < 2) { 90 throw new IOException ("line "+lineno+" has less then 2 fields"); 91 } 92 93 String allowStr = st.nextToken(); 94 boolean allow = true; 95 String mime = st.nextToken(); 96 97 if (allowStr.equalsIgnoreCase("allow")) { 99 allow=true; 100 } else if (allowStr.equalsIgnoreCase("deny")) { 101 allow=false; 102 } else { 103 throw new IOException ("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 e) { 113 throw new IOException (e.getMessage()); 114 } 115 116 117 while (st.hasMoreTokens()) { 119 boolean isMin=true; 120 121 String descr=st.nextToken(); 122 123 if (descr.startsWith("<")) { 124 isMin=false; 126 } else if (descr.startsWith(">")) { 127 isMin=true; 128 } else { 129 throw new IOException ("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 e) { 137 throw new IOException ("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 160 public void setDefault(boolean allow) { 161 this.defaultBehavior=allow; 162 } 163 164 167 public boolean getDefault() { 168 return this.defaultBehavior; 169 } 170 171 175 public Vector getDownloadRules () { 176 return this.rules; 177 } 178 179 184 public void setDownloadRules(Vector <DownloadRule> downloadRules) { 185 this.rules = downloadRules; 186 } 187 188 189 200 public void addRule(String mimeBaseType, String 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 217 private DownloadRule findRule(String mimeType, int size) { 218 if (mimeType.indexOf("/")<0) { 220 return null; 221 } 222 223 if (mimeType.indexOf(";") > 0) { 227 StringTokenizer st = new StringTokenizer (mimeType, ";"); 228 mimeType = st.nextToken(); 229 } 230 231 String basetype = null; 232 String subtype = null; 233 StringTokenizer st = new StringTokenizer (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 256 protected String getHeaderValue(Vector httpHeaders, String 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 httpHeaders) { 268 String mimeType = getHeaderValue(httpHeaders, HttpHeader.CONTENT_TYPE); 269 String sizeStr = getHeaderValue(httpHeaders, HttpHeader.CONTENT_LENGTH); 270 271 if (mimeType == null) { 273 return false; 274 } 275 276 int size=-1; 278 try { 279 size = Integer.parseInt(sizeStr); 280 } catch (NumberFormatException e) {} 281 282 DownloadRule r = findRule(mimeType,size); 283 284 if (r == null) { 285 return defaultBehavior; 286 } else { 287 return r.getAllow(); 289 } 290 } 291 292 public boolean processAllowed(Vector httpHeaders) { 293 String mimeType = getHeaderValue(httpHeaders, HttpHeader.CONTENT_TYPE); 294 String sizeStr = getHeaderValue(httpHeaders, HttpHeader.CONTENT_LENGTH); 295 296 if (mimeType == null) { 298 return false; 299 } 300 301 int size=-1; 303 try { 304 size = Integer.parseInt(sizeStr); 305 } catch (NumberFormatException e) {} 306 307 DownloadRule r = findRule(mimeType,size); 308 309 if (r == null) { 310 return defaultBehavior; 311 } else { 312 return r.getProcessAllowed(); 314 } 315 } 316 317 321 public String toString() { 322 StringBuffer sb = new StringBuffer (); 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 } | Popular Tags |