+ Added network data validation to NetConf

main
Flavien 1 year ago
parent 7fc8b6362c
commit b80a2e22d2

@ -1,17 +1,147 @@
package fr.sixthemes.beans;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NetConf {
// Attributes
protected String ipAddress;
protected String gateway;
protected String network;
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() {
this.ipAddress = "";
this.gateway = "";
this.network = "vmbr0";
this.setIPAddress("");
this.setGateway("");
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";
break;
}
}
}

@ -8,6 +8,8 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import fr.sixthemes.beans.NetConf;
@WebServlet(urlPatterns = "/createct")
public class CreateCTHandler extends HttpServlet {
@ -29,6 +31,16 @@ public class CreateCTHandler extends HttpServlet {
req.setAttribute("ram", ram);
req.setAttribute("disk", disk);
// -------------------
String ipaddress4 = req.getParameter("ipaddress4");
String gateway4 = req.getParameter("gateway4");
String network = req.getParameter("net");
String ipconf4 = req.getParameter("dhcp");
NetConf netconf4 = new NetConf(ipaddress4, gateway4, network, ipconf4);
req.setAttribute("netconf4", netconf4);
this.getServletContext().getRequestDispatcher("/WEB-INF/createct.jsp").forward(req, resp);
}

@ -1,11 +1,13 @@
<%@ page pageEncoding="UTF-8" %>
<%@ page import="fr.sixthemes.beans.NetConf" %>
<% NetConf netconf4 = (NetConf)request.getAttribute("netconf4"); %>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<%@include file="/WEB-INF/templates/style.jsp"%>
<%@ include file="/WEB-INF/templates/style.jsp" %>
</head>
<body>
<div class="container">
@ -17,6 +19,8 @@
<li><strong>RAM : </strong>${ram}</li>
<li><strong>Disk (in GB) : </strong>${disk}</li>
</ul>
<h3>IPv4 :</h3>
<%= netconf4.toHTML() %>
</div>
</body>
</html>
Loading…
Cancel
Save