001: package net.matuschek.http;
002:
003: /*********************************************
004: Copyright (c) 2001 by Daniel Matuschek
005: *********************************************/
006:
007: /**
008: * This class implements a rule what documents should be
009: * downloaded based on file size and mime type
010: *
011: * @author Daniel Matuschek
012: * @version $Id: DownloadRule.java,v 1.4 2003/02/25 13:21:03 oliver_schmidt Exp $
013: */
014: public class DownloadRule {
015:
016: private final static int MINDEFAULT = 0;
017: private final static int MAXDEFAULT = Integer.MAX_VALUE;
018:
019: /** basic mime type **/
020: private String mimeBaseType = null;
021: private String mimeSubType = null;
022: private int minSize = MINDEFAULT;
023: private int maxSize = MAXDEFAULT;
024:
025: /** allow or deny download ? **/
026: private boolean allow;
027:
028: /** allow or deny processing ? **/
029: private boolean processAllowed = true;
030:
031: /**
032: empty constructor that does nothing
033: **/
034: public DownloadRule() {
035: }
036:
037: /**
038: Get the value of minSize.
039: @return Value of minSize.
040: **/
041: public int getMinSize() {
042: return minSize;
043: }
044:
045: /**
046: Set the value of minSize.
047: @param v Value to assign to minSize.
048: **/
049: public void setMinSize(int minSize) {
050: if (minSize >= MINDEFAULT) {
051: this .minSize = minSize;
052: }
053: }
054:
055: /**
056: Get the value of maxSize.
057: @return Value of maxSize.
058: **/
059: public int getMaxSize() {
060: return maxSize;
061: }
062:
063: /**
064: Set the value of maxSize.
065: @param v Value to assign to maxSize.
066: **/
067: public void setMaxSize(int maxSize) {
068: if (maxSize >= MINDEFAULT) {
069: this .maxSize = maxSize;
070: }
071: }
072:
073: /**
074: Get the value of allow.
075: @return Value of allow.
076: **/
077: public boolean getAllow() {
078: return allow;
079: }
080:
081: /**
082: Set the value of allow.
083: @param v Value to assign to allow.
084: **/
085: public void setAllow(boolean allow) {
086: this .allow = allow;
087: }
088:
089: /**
090: * Gets the value of processAllowed
091: * @return true, if this rule allows indexing, false if it denies
092: * something
093: */
094: public boolean getProcessAllowed() {
095: return processAllowed && allow;
096: }
097:
098: /**
099: * Sets the value of processAllowed
100: * @param processAllowed true, if this rule allows indexing, false if it denies
101: * something
102: */
103: public void setProcessAllowed(boolean processAllowed) {
104: this .processAllowed = processAllowed;
105: }
106:
107: /**
108: Get the value of mimeBaseType.
109: @return Value of mimeBaseType.
110: **/
111: public String getMimeBaseType() {
112: return mimeBaseType;
113: }
114:
115: /**
116: Set the value of mimeBaseType.
117: @param v Value to assign to mimeBaseType.
118: **/
119: public void setMimeBaseType(String mimeBaseType) {
120: this .mimeBaseType = mimeBaseType;
121: }
122:
123: /**
124: Get the value of mimeSubType.
125: @return Value of mimeSubType.
126: **/
127: public String getMimeSubType() {
128: return mimeSubType;
129: }
130:
131: /**
132: Set the value of mimeSubType.
133: @param v Value to assign to mimeSubType.
134: **/
135: public void setMimeSubType(String mimeSubType) {
136: this .mimeSubType = mimeSubType;
137: }
138:
139: /**
140: Get the value of mimeType.
141: @return Value of mimeType.
142: **/
143: public String getMimeType() {
144: return mimeBaseType + "/" + mimeSubType;
145: }
146:
147: /**
148: Set the value of mimeType.
149: @param v Value to assign to mimeType.
150: **/
151: public void setMimeType(String mimeType)
152: throws IllegalArgumentException {
153: int pos = mimeType.indexOf("/");
154: if (pos < 0) {
155: throw new IllegalArgumentException(
156: "mime type must be in the format "
157: + " basetype/subtype");
158: }
159:
160: this .mimeBaseType = mimeType.substring(0, pos);
161: this .mimeSubType = mimeType.substring(pos + 1);
162: }
163:
164: public boolean matches(String mimeBaseType, String mimeSubType,
165: int size) {
166: if (simpleStringMatch(mimeBaseType, this .mimeBaseType)
167: && simpleStringMatch(mimeSubType, this .mimeSubType)) {
168: if (size >= 0) {
169: if ((size >= this .minSize) && (size <= this .maxSize)) {
170: return true;
171: }
172: } else {
173: // if sizes are default (0, MAXINT), this rule belongs
174: // to ALL documents of this type (it matches), otherwise it depends
175: // on size and doesn't match !
176: if ((this .minSize == MINDEFAULT)
177: && (this .maxSize == MAXDEFAULT)) {
178: return true;
179: } else {
180: return false;
181: }
182: }
183: }
184: return false;
185: }
186:
187: /**
188: matches the given string to the rule string
189: @param value a string to test
190: @param rule rule used (can be a value or "* that will
191: match anything
192: @return true is value is equal to rule or rule is "*"
193: **/
194: protected boolean simpleStringMatch(String value, String rule) {
195: if (rule.equals("*")) {
196: return true;
197: }
198: if (value.equalsIgnoreCase(rule)) {
199: return true;
200: }
201: return false;
202: }
203:
204: /**
205: Convert rule to a String
206: @return a String representation of this rule. Format may change
207: without notice (only useful for debugging or logging)
208: **/
209: public String toString() {
210: String s = null;
211: if (allow) {
212: s = "allow";
213: } else {
214: s = "deny";
215: }
216: return mimeBaseType + "/" + mimeSubType + " >" + minSize + " <"
217: + maxSize + " " + s;
218: }
219:
220: } // DownloadRule
|