diff --git a/src/main/java/fr/sixthemes/beans/NetConf.java b/src/main/java/fr/sixthemes/beans/NetConf.java index 5ad470e..4e1ab57 100644 --- a/src/main/java/fr/sixthemes/beans/NetConf.java +++ b/src/main/java/fr/sixthemes/beans/NetConf.java @@ -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.configuration = "dhcp"; + 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("\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.
+ * + * 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.
+ * + * 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 : ""; + } + + /** + *

Sets the name of the proxmox network to be used.

+ * + *

If an empty String is provided, defaults to vmbr0

+ * + * @param network the name of the network to be set + */ + public void setNetwork(String network) { + this.network = !network.isEmpty() ? network : "vmbr0"; + } + + /** + *

Sets the network configuration type.

+ * + *

Accepts :

+ * + * + *

If and empty String is provided, defaults to dhcp

+ * + * @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; + } } } diff --git a/src/main/java/fr/sixthemes/servlets/CreateCTHandler.java b/src/main/java/fr/sixthemes/servlets/CreateCTHandler.java index bc71eec..0efeb1c 100644 --- a/src/main/java/fr/sixthemes/servlets/CreateCTHandler.java +++ b/src/main/java/fr/sixthemes/servlets/CreateCTHandler.java @@ -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); } diff --git a/src/main/webapp/WEB-INF/createct.jsp b/src/main/webapp/WEB-INF/createct.jsp index 1a8a1af..e5e3e30 100644 --- a/src/main/webapp/WEB-INF/createct.jsp +++ b/src/main/webapp/WEB-INF/createct.jsp @@ -1,11 +1,13 @@ <%@ page pageEncoding="UTF-8" %> +<%@ page import="fr.sixthemes.beans.NetConf" %> +<% NetConf netconf4 = (NetConf)request.getAttribute("netconf4"); %> Document - <%@include file="/WEB-INF/templates/style.jsp"%> + <%@ include file="/WEB-INF/templates/style.jsp" %>
@@ -17,6 +19,8 @@
  • RAM : ${ram}
  • Disk (in GB) : ${disk}
  • +

    IPv4 :

    + <%= netconf4.toHTML() %>
    \ No newline at end of file