1 19 20 package com.sslexplorer.security; 21 22 47 public class IpRestriction implements Comparable <IpRestriction> { 48 49 53 public final static int ALLOWED = 1; 54 55 59 public final static int ALLOWED_WILDCARD = 2; 60 61 65 public final static int DENIED = 3; 66 67 71 public final static int DENIED_WILDCARD = 4; 72 73 75 private final int id; 76 private String addressPattern; 77 private int type; 78 private int priority; 79 80 85 public IpRestriction(boolean isAllow) { 86 this("", isAllow); 87 } 88 89 95 public IpRestriction(String addressPattern, boolean isAllow) { 96 this(addressPattern, isAllow, Integer.MAX_VALUE); 97 } 98 99 106 public IpRestriction(String addressPattern, boolean isAllow, int priority) { 107 this(-1, addressPattern, getType(addressPattern, isAllow), 0); 108 } 109 110 118 public IpRestriction(int id, String addressPattern, int type, int priority) { 119 this.addressPattern = addressPattern; 120 this.type = type; 121 this.id = id; 122 this.priority = priority; 123 } 124 125 130 public int getID() { 131 return id; 132 } 133 134 140 public String getAddress() { 141 return addressPattern; 142 } 143 144 150 public void setAddress(String addressPattern) { 151 this.addressPattern = addressPattern; 152 } 153 154 160 public boolean getAllowed() { 161 return ALLOWED == type || ALLOWED_WILDCARD == type; 162 } 163 164 170 public boolean getDenied() { 171 return DENIED == type || DENIED_WILDCARD == type; 172 } 173 174 179 public boolean isWildcardMatch() { 180 return ALLOWED_WILDCARD == getType() || DENIED_WILDCARD == getType(); 181 } 182 183 192 public static int getType(String address, boolean isAllow) { 193 if (address.indexOf('*') > -1) { 194 return isAllow ? ALLOWED_WILDCARD : DENIED_WILDCARD; 195 } else { 196 return isAllow ? ALLOWED : DENIED; 197 } 198 } 199 200 206 public int getPriority() { 207 return priority; 208 } 209 210 216 public void setPriority(int priority) { 217 this.priority = priority; 218 } 219 220 227 public int getType() { 228 return type; 229 } 230 231 239 public void setType(int type) { 240 this.type = type; 241 } 242 243 246 public boolean equals(Object obj) { 247 return obj instanceof IpRestriction && ((IpRestriction)obj).getID() == getID(); 248 } 249 250 256 public int compareTo(IpRestriction o) { 257 return getAddress().compareTo(o.getAddress()); 258 } 259 260 266 public boolean isDefault() { 267 return getAddress().equals("*.*.*.*"); 268 } 269 } | Popular Tags |