1 package net.matuschek.http; 2 3 6 7 14 public class DownloadRule { 15 16 private final static int MINDEFAULT = 0; 17 private final static int MAXDEFAULT = Integer.MAX_VALUE; 18 19 20 private String mimeBaseType=null; 21 private String mimeSubType=null; 22 private int minSize=MINDEFAULT; 23 private int maxSize=MAXDEFAULT; 24 25 26 private boolean allow; 27 28 29 private boolean processAllowed = true; 30 31 34 public DownloadRule() { 35 } 36 37 38 42 public int getMinSize () { 43 return minSize; 44 } 45 46 50 public void setMinSize(int minSize ) { 51 if (minSize >= MINDEFAULT) { 52 this.minSize = minSize; 53 } 54 } 55 56 60 public int getMaxSize () { 61 return maxSize; 62 } 63 64 68 public void setMaxSize(int maxSize ) { 69 if (maxSize >= MINDEFAULT) { 70 this.maxSize = maxSize; 71 } 72 } 73 74 78 public boolean getAllow () { 79 return allow; 80 } 81 82 86 public void setAllow(boolean allow ) { 87 this.allow = allow; 88 } 89 90 95 public boolean getProcessAllowed() { 96 return processAllowed && allow; 97 } 98 99 104 public void setProcessAllowed(boolean processAllowed) { 105 this.processAllowed = processAllowed; 106 } 107 108 112 public String getMimeBaseType () { 113 return mimeBaseType; 114 } 115 116 120 public void setMimeBaseType(String mimeBaseType) { 121 this.mimeBaseType = mimeBaseType; 122 } 123 124 128 public String getMimeSubType () { 129 return mimeSubType; 130 } 131 132 136 public void setMimeSubType(String mimeSubType ) { 137 this.mimeSubType = mimeSubType; 138 } 139 140 144 public String getMimeType () { 145 return mimeBaseType+"/"+mimeSubType; 146 } 147 148 152 public void setMimeType(String mimeType) 153 throws IllegalArgumentException 154 { 155 int pos = mimeType.indexOf("/"); 156 if (pos < 0) { 157 throw new IllegalArgumentException ("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 mimeBaseType, 167 String 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 ((this.minSize == MINDEFAULT) && 180 (this.maxSize == MAXDEFAULT)) { 181 return true; 182 } else { 183 return false; 184 } 185 } 186 } 187 return false; 188 } 189 190 197 protected boolean simpleStringMatch(String value, String 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 213 public String toString() { 214 String s=null; 215 if (allow) { 216 s="allow"; 217 } else { 218 s="deny"; 219 } 220 return mimeBaseType+"/"+mimeSubType+" >"+minSize+" <"+maxSize+" "+s; 221 } 222 223 } | Popular Tags |