1 16 17 package org.apache.commons.latka.validators; 18 19 import org.apache.commons.latka.Validator; 20 import org.apache.commons.latka.ValidationException; 21 22 import org.apache.commons.latka.http.Response; 23 24 import java.text.MessageFormat ; 25 32 public class ByteLengthValidator extends BaseValidator implements Validator { 33 34 36 protected int _minLength = 0; 37 protected int _maxLength = -1; 38 39 protected static final MessageFormat MAX_MESSAGE = new MessageFormat ("Expected at most {0,number,integer} bytes. Found {1,number,integer}."); 40 protected static final MessageFormat MIN_MESSAGE = new MessageFormat ("Expected at least {0,number,integer} bytes. Found {1,number,integer}."); 41 42 44 public ByteLengthValidator() { 45 this(null,0,-1); 46 } 47 48 public ByteLengthValidator(String label) { 49 this(label,0,-1); 50 } 51 52 public ByteLengthValidator(int min, int max) { 53 this(null,min,max); 54 } 55 56 public ByteLengthValidator(String label, int min, int max) { 57 super(label); 58 _minLength = min; 59 _maxLength = max; 60 } 61 62 64 public void setMinLength(int length) { 65 _minLength = length; 66 } 67 68 public void setMaxLength(int length) { 69 _maxLength = length; 70 } 71 72 public void validate(Response response) 73 throws ValidationException { 74 75 int byteLength = response.getByteLength(); 76 77 if (_maxLength > -1) { 78 if (byteLength > _maxLength) { 79 fail(MAX_MESSAGE.format(new Object [] { new Integer (_maxLength), new Integer (byteLength) }).toString()); 80 } 81 } 82 83 if (byteLength < _minLength) { 84 fail(MIN_MESSAGE.format(new Object [] { new Integer (_minLength), new Integer (byteLength) }).toString()); 85 } 86 } 87 88 } 89 | Popular Tags |