1 16 17 package org.apache.webapp.admin.valve; 18 19 import java.lang.IllegalArgumentException ; 20 import java.net.InetAddress ; 21 import java.util.List ; 22 import java.util.regex.Pattern ; 23 import javax.servlet.http.HttpServletRequest ; 24 25 import org.apache.struts.action.ActionError; 26 import org.apache.struts.action.ActionErrors; 27 import org.apache.struts.action.ActionForm; 28 import org.apache.struts.action.ActionMapping; 29 30 import org.apache.webapp.admin.ApplicationServlet; 31 import org.apache.webapp.admin.LabelValueBean; 32 33 39 40 public final class RemoteAddrValveForm extends ValveForm { 41 42 44 45 50 private String allow = ""; 51 52 55 private String deny = ""; 56 57 60 private Pattern allows[] = new Pattern [0]; 61 62 65 private Pattern denies[] = new Pattern [0]; 66 67 68 70 73 public String getAllow() { 74 75 return this.allow; 76 77 } 78 79 82 public void setAllow(String allow) { 83 84 this.allow = allow; 85 86 } 87 88 91 public String getDeny() { 92 93 return this.deny; 94 95 } 96 97 100 public void setDeny(String deny) { 101 102 this.deny = deny; 103 104 } 105 106 108 109 115 public void reset(ActionMapping mapping, HttpServletRequest request) { 116 117 super.reset(mapping, request); 118 this.allow = null; 119 this.deny = null; 120 this.allows = null; 121 this.denies = null; 122 123 } 124 125 128 public String toString() { 129 130 StringBuffer sb = new StringBuffer ("RemoteAddrValveForm[adminAction="); 131 sb.append(getAdminAction()); 132 sb.append("',valveType="); 133 sb.append(getValveType()); 134 sb.append(",allow="); 135 sb.append(getAllow()); 136 sb.append(",deny="); 137 sb.append(getDeny()); 138 sb.append("',objectName='"); 139 sb.append(getObjectName()); 140 sb.append("]"); 141 return (sb.toString()); 142 143 } 144 145 155 156 public ActionErrors validate(ActionMapping mapping, 157 HttpServletRequest request) { 158 159 ActionErrors errors = new ActionErrors(); 160 161 String submit = request.getParameter("submit"); 162 163 if ((allow == null) || (allow.length() < 1)) { 167 if ((deny == null) || (deny.length() < 1)) { 168 errors.add("allow", 169 new ActionError("error.allow.deny.required")); 170 } 171 } 172 174 try { 175 allows = ValveUtil.precalculate(allow); 176 } catch (IllegalArgumentException e) { 177 errors.add("allow", new ActionError("error.syntax")); 178 return errors; 179 } 180 181 try { 182 denies = ValveUtil.precalculate(deny); 183 } catch (IllegalArgumentException e) { 184 errors.add("allow", new ActionError("error.syntax")); 185 return errors; 186 } 187 188 String ip = request.getRemoteAddr(); 189 190 if (ip == null) { 191 return errors; 192 } 193 194 for (int i = 0; i < denies.length; i++) { 195 if (denies[i].matcher(ip).matches()) { 196 if (allows.length < 1) { 197 errors.add("deny", 198 new ActionError("error.denyIP")); 199 } 200 for (int j = 0; j < allows.length; j++) { 201 if (!allows[j].matcher(ip).matches()) { 202 errors.add("deny", 203 new ActionError("error.denyIP")); 204 } 205 } 206 } 207 } 208 209 boolean allowMatch = true; 210 if (allows.length > 0) { 211 allowMatch = false; 212 } 213 for (int i = 0; i < allows.length; i++) { 214 if (allows[i].matcher(ip).matches()) { 215 allowMatch = true; 216 } 217 } 218 if (!allowMatch) { 219 errors.add("allow", new ActionError("error.allowIP")); 220 } 221 222 return errors; 223 } 224 } 225 | Popular Tags |