+ Added network data validation to NetConf
parent
7fc8b6362c
commit
b80a2e22d2
@ -1,17 +1,147 @@
|
|||||||
package fr.sixthemes.beans;
|
package fr.sixthemes.beans;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class NetConf {
|
public class NetConf {
|
||||||
|
|
||||||
|
// Attributes
|
||||||
protected String ipAddress;
|
protected String ipAddress;
|
||||||
protected String gateway;
|
protected String gateway;
|
||||||
protected String network;
|
protected String network;
|
||||||
protected String configuration;
|
protected String configuration;
|
||||||
|
|
||||||
|
// Constants
|
||||||
|
protected String ipRegex = "(\\b25[0-5]|\\b2[0-4][0-9]|\\b[01]?[0-9][0-9]?)(\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}";
|
||||||
|
protected String cidrRegex = "/((1|2)?[0-9]|3[0-2])";
|
||||||
|
|
||||||
|
// Constructors
|
||||||
public NetConf() {
|
public NetConf() {
|
||||||
this.ipAddress = "";
|
this.setIPAddress("");
|
||||||
this.gateway = "";
|
this.setGateway("");
|
||||||
this.network = "vmbr0";
|
this.setNetwork("");
|
||||||
|
this.setConfiguration("");
|
||||||
|
}
|
||||||
|
|
||||||
|
public NetConf(String ipAddress, String gateway, String network, String configuration) {
|
||||||
|
this.setIPAddress(ipAddress);
|
||||||
|
this.setGateway(gateway);
|
||||||
|
this.setNetwork(network);
|
||||||
|
this.setConfiguration(configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
protected boolean validateIP(String ipAddress, boolean checksCIDR) {
|
||||||
|
Pattern p = Pattern.compile("^" + this.ipRegex + (checksCIDR ? this.cidrRegex : "") + "$");
|
||||||
|
Matcher m = p.matcher(ipAddress);
|
||||||
|
|
||||||
|
if (!m.find())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toHTML() {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
|
builder.append("<ul>\n");
|
||||||
|
builder.append("<li><strong>IP Address : </strong>" + this.ipAddress + "</li>\n");
|
||||||
|
builder.append("<li><strong>Gateway : </strong>" + this.gateway + "</li>\n");
|
||||||
|
builder.append("<li><strong>Network : </strong>" + this.network + "</li>\n");
|
||||||
|
builder.append("<li><strong>IP Configuration : </strong>" + this.configuration + "</li>\n");
|
||||||
|
builder.append("</ul>\n");
|
||||||
|
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ╔═════════╗
|
||||||
|
* ║ GETTERS ║
|
||||||
|
* ╚═════════╝
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
public String getIPAddress() {
|
||||||
|
return this.ipAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGateway() {
|
||||||
|
return this.gateway;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNetwork() {
|
||||||
|
return this.network;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConfiguration() {
|
||||||
|
return this.configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ╔═════════╗
|
||||||
|
* ║ SETTERS ║
|
||||||
|
* ╚═════════╝
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the IP Address for the network configuration.<br>
|
||||||
|
*
|
||||||
|
* If an incorret IP Address / CIDR is provided, sets the field to an empty String
|
||||||
|
*
|
||||||
|
* @param ipAddress the IP address to assign
|
||||||
|
*/
|
||||||
|
public void setIPAddress(String ipAddress) {
|
||||||
|
this.ipAddress = this.validateIP(ipAddress, true) ? ipAddress : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the gateway for the network configuration.<br>
|
||||||
|
*
|
||||||
|
* If an incorrect IP Address is provided, sets the field to an empty String
|
||||||
|
*
|
||||||
|
* @param gateway the gateway to assign
|
||||||
|
*/
|
||||||
|
public void setGateway(String gateway) {
|
||||||
|
this.gateway = this.validateIP(gateway, false) ? gateway : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Sets the name of the proxmox network to be used.</p>
|
||||||
|
*
|
||||||
|
* <p>If an empty String is provided, defaults to <i>vmbr0</i></p>
|
||||||
|
*
|
||||||
|
* @param network the name of the network to be set
|
||||||
|
*/
|
||||||
|
public void setNetwork(String network) {
|
||||||
|
this.network = !network.isEmpty() ? network : "vmbr0";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Sets the network configuration type. </p>
|
||||||
|
*
|
||||||
|
* <p>Accepts :</p>
|
||||||
|
* <ul>
|
||||||
|
* <li>dhcp</li>
|
||||||
|
* <li>static</li>
|
||||||
|
* <li>empty</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <p>If and empty String is provided, defaults to <i>dhcp</i></p>
|
||||||
|
*
|
||||||
|
* @param configuration the configuation type to be set
|
||||||
|
*/
|
||||||
|
public void setConfiguration(String configuration) {
|
||||||
|
switch(configuration) {
|
||||||
|
case "dhcp":
|
||||||
|
case "static":
|
||||||
|
case "empty":
|
||||||
|
this.configuration = configuration;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
this.configuration = "dhcp";
|
this.configuration = "dhcp";
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue