📄 iprestrictionform.java
字号:
package com.sslexplorer.security.forms;
import java.net.InetAddress;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.forms.CoreForm;
/**
* Implementation of a {@link com.sslexplorer.core.forms.CoreForm}
* that allows an administrator to create an <i>IP Restrictions</i>.
*
* @author P.J.King <a href="mailto:peter@3sp.com"><peter@3sp.com></a>
* @see com.sslexplorer.security.IpRestrictionItem
*
*/
public class IpRestrictionForm extends CoreForm {
// Private instance variables
private String ipAddress;
private String type;
/**
* Sets the IP Restriction to empty strings.
*/
public void initialize() {
ipAddress = "";
type = "";
}
public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
ActionErrors errors = new ActionErrors();
if(isCommiting()) {
if (ipAddress.equals("") || ipAddress == null) {
// No IP Address was entered.
errors.add(Globals.ERROR_KEY, new ActionMessage("availableIpRestrictions.noIpAddress"));
} else {
// Create IP Address patterns n.n.n.n, n.n.n.*,
// n.n.*, and you guessed it, n.* to test if a valid
// IPv4 format.
String ipPattern = "^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})$|^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\*)$";
String ipPattern2 = "^(\\d{1,3}\\.\\d{1,3}\\.\\*)$|^(\\d{1,3}\\.\\*)$";
// CIDR Format as above but ending in "/n"
String ipCidrPattern = "^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\/\\d{1,2})$|^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\*\\/\\d{1,2})$";
String ipCidrPattern2 = "^(\\d{1,3}\\.\\d{1,3}\\.\\*\\/\\d{1,2})$|^(\\d{1,3}\\.\\*\\/\\d{1,2})$";
// The following tests do not yet try the database to see if an
// IP Restriction rule based on the entered IP Address is already
// present.
if ((ipAddress.matches(ipPattern))||(ipAddress.matches(ipPattern2))) {
// ipAddress is a valid pattern
if (ipAddress.indexOf("0.0.0.0") != -1) {
// Error
errors.add(Globals.ERROR_KEY, new ActionMessage("availableIpRestrictions.invalidIpAddress"));
} else {
if (ipAddress.indexOf("255.255.255.255") != -1) {
// Error
errors.add(Globals.ERROR_KEY, new ActionMessage("availableIpRestrictions.invalidIpAddress"));
} else {
// Perform split and that values are in range (i.e. < 255).
String[] ipArray2 = ipAddress.split("\\.");
int failedOnSegment = 0;
try {
for (int i=0; i < 4; i++) {
failedOnSegment = i;
int thisSegment = Integer.parseInt(ipArray2[i]);
if (thisSegment > 255) {
// An invalid IP Address was entered.
errors.add(Globals.ERROR_KEY, new ActionMessage("availableIpRestrictions.invalidIpAddress"));
i = 4;
}
}
} catch (Exception e) {
// When here we either have an "*" and/or a CIDR element
String[] lastSegment = ipArray2[failedOnSegment].split("\\/");
if (lastSegment.length == 2 && lastSegment[1].equals(null)) {
// then not a cidr address
}
return errors;
}
}
}
} else {
// ipAddress is an invalid pattern
errors.add(Globals.ERROR_KEY, new ActionMessage("availableIpRestrictions.invalidIpAddress"));
}
}
}
return errors;
}
public void reset(ActionMapping arg0, ServletRequest arg1) {
// Resets ActionMapping and ServletRequest
super.reset(arg0, arg1);
}
/**
* Gets the IP Restriction address.
*
* @return ipAddress
*/
public String getIpAddress() {
return ipAddress;
}
/**
* Sets the IP Restriction address.
*
* @param ipAddress
*/
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
/**
* Gets the IP Restriction type.
*
* @return type
*/
public String getType() {
return type;
}
/**
* Sets the IP Restriction type.
*
* @param type
*/
public void setType(String type) {
this.type = type;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -