1 19 20 package com.sslexplorer.security.forms; 21 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.struts.Globals; 25 import org.apache.struts.action.ActionErrors; 26 import org.apache.struts.action.ActionMapping; 27 import org.apache.struts.action.ActionMessage; 28 29 import com.sslexplorer.core.forms.CoreForm; 30 import com.sslexplorer.input.validators.IPAddressValidator; 31 import com.sslexplorer.security.IpRestriction; 32 33 41 public class IpRestrictionForm extends CoreForm { 42 43 46 public final static String ALLOW_TYPE = "allow"; 47 48 51 public final static String DENY_TYPE = "deny"; 52 53 54 private String type; 55 private boolean addressEnabled; 56 private boolean editing; 57 private IpRestriction restriction; 58 59 65 public void initialize(IpRestriction restriction, boolean editing) { 66 this.restriction = restriction; 67 addressEnabled = !editing || !restriction.isDefault(); 68 type = restriction.getAllowed() ? ALLOW_TYPE : DENY_TYPE; 69 this.editing = editing; 70 } 71 72 77 public IpRestriction getRestriction() { 78 return restriction; 79 } 80 81 86 public boolean isEditing() { 87 return editing; 88 } 89 90 96 public boolean isAddressEnabled() { 97 return addressEnabled; 98 } 99 100 104 public void apply() { 105 restriction.setType(IpRestriction.getType(restriction.getAddress(), type.equals(ALLOW_TYPE))); 106 } 107 108 111 public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) { 112 ActionErrors errors = new ActionErrors(); 113 if (isCommiting()) { 114 if (restriction.getAddress() == null || restriction.getAddress().equals("")) { 115 errors.add(Globals.ERROR_KEY, new ActionMessage("editIpRestriction.error.noIpAddress")); 116 } 117 if(!isEditing() && restriction.getAddress().equals("*.*.*.*")) { 118 errors.add(Globals.ERROR_KEY, new ActionMessage("editIpRestriction.error.usingDefaultPattern")); 119 } 120 else if(!IPAddressValidator.isIpAddressExpressionValid(restriction.getAddress())) { 121 errors.add(Globals.ERROR_KEY, new ActionMessage("editIpRestriction.error.invalidIpAddress")); 122 } 123 } 124 return errors; 125 } 126 127 133 public String getType() { 134 return type; 135 } 136 137 143 public void setType(String type) { 144 this.type = type; 145 } 146 } | Popular Tags |