diff --git a/src/main/java/fr/sixthemes/beans/NetConf.java b/src/main/java/fr/sixthemes/beans/NetConf.java index 4e1ab57..ca168e3 100644 --- a/src/main/java/fr/sixthemes/beans/NetConf.java +++ b/src/main/java/fr/sixthemes/beans/NetConf.java @@ -11,12 +11,13 @@ public class NetConf { 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])"; + protected String ipRegex; + protected String cidrRegex; // Constructors public NetConf() { + setIPValidator(); + this.setIPAddress(""); this.setGateway(""); this.setNetwork(""); @@ -24,6 +25,8 @@ public class NetConf { } public NetConf(String ipAddress, String gateway, String network, String configuration) { + setIPValidator(); + this.setIPAddress(ipAddress); this.setGateway(gateway); this.setNetwork(network); @@ -32,7 +35,7 @@ public class NetConf { // Methods protected boolean validateIP(String ipAddress, boolean checksCIDR) { - Pattern p = Pattern.compile("^" + this.ipRegex + (checksCIDR ? this.cidrRegex : "") + "$"); + Pattern p = Pattern.compile("^" + ipRegex + (checksCIDR ? cidrRegex : "") + "$"); Matcher m = p.matcher(ipAddress); if (!m.find()) @@ -41,6 +44,11 @@ public class NetConf { return true; } + protected void setIPValidator() { + 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}"; + cidrRegex = "/((1|2)?[0-9]|3[0-2])"; + } + public String toHTML() { StringBuilder builder = new StringBuilder(); diff --git a/src/main/java/fr/sixthemes/beans/NetConf6.java b/src/main/java/fr/sixthemes/beans/NetConf6.java new file mode 100644 index 0000000..112fa3d --- /dev/null +++ b/src/main/java/fr/sixthemes/beans/NetConf6.java @@ -0,0 +1,36 @@ +package fr.sixthemes.beans; + +public class NetConf6 extends NetConf { + + public NetConf6() { + super(); + } + + public NetConf6(String ipAddress, String gateway, String network, String configuration) { + super(ipAddress, gateway, network, configuration); + } + + @Override + public void setConfiguration(String configuration) { + switch(configuration) { + case "dhcp": + case "static": + case "slaac": + case "empty": + this.configuration = configuration; + break; + + default: + this.configuration = "dhcp"; + break; + } + } + + @Override + protected void setIPValidator() { + ipRegex = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"; + + cidrRegex = "/([1-5]?[0-9]|6[0-4])"; + } + +} diff --git a/src/main/java/fr/sixthemes/beans/SysConf.java b/src/main/java/fr/sixthemes/beans/SysConf.java new file mode 100644 index 0000000..3dfceef --- /dev/null +++ b/src/main/java/fr/sixthemes/beans/SysConf.java @@ -0,0 +1,82 @@ +package fr.sixthemes.beans; + +import fr.sixthemes.utils.Utils; + +public class SysConf { + + /* + * ╔════════════╗ + * ║ ATTRIBUTES ║ + * ╚════════════╝ + */ + + protected String hostname; + protected String cores; + protected String ram; + protected String disk; + + /* + * ╔══════════════╗ + * ║ CONSTRUCTORS ║ + * ╚══════════════╝ + */ + + public SysConf() { + this.setHostname(""); + this.setCores(""); + this.setRam(""); + this.setDisk(""); + } + + public SysConf(String hostname, String cores, String ram, String disk) { + this.setHostname(hostname); + this.setCores(cores); + this.setRam(ram); + this.setDisk(disk); + } + + /* + * ╔═════════╗ + * ║ GETTERS ║ + * ╚═════════╝ + */ + + public String getHostname() { + return this.hostname; + } + + public String getCores() { + return this.cores; + } + + public String getRam() { + return this.ram; + } + + public String getDisk() { + return this.disk; + } + + /* + * ╔═════════╗ + * ║ SETTERS ║ + * ╚═════════╝ + */ + + public void setHostname(String hostname) { + this.hostname = !hostname.isEmpty() ? hostname : "default-hostname"; + } + + public void setCores(String cores) { + this.cores = Utils.isInteger(cores) ? cores : "2"; + } + + public void setRam(String ram) { + this.ram = Utils.isInteger(ram) ? ram : "1024"; + } + + public void setDisk(String disk) { + this.disk = Utils.isInteger(disk) ? disk : "16"; + } + +} diff --git a/src/main/java/fr/sixthemes/servlets/CreateCTHandler.java b/src/main/java/fr/sixthemes/servlets/CreateCTHandler.java index 0efeb1c..56113ad 100644 --- a/src/main/java/fr/sixthemes/servlets/CreateCTHandler.java +++ b/src/main/java/fr/sixthemes/servlets/CreateCTHandler.java @@ -9,6 +9,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import fr.sixthemes.beans.NetConf; +import fr.sixthemes.beans.NetConf6; +import fr.sixthemes.beans.SysConf; @WebServlet(urlPatterns = "/createct") public class CreateCTHandler extends HttpServlet { @@ -20,16 +22,14 @@ public class CreateCTHandler extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - String hostname = !req.getParameter("hostname").equals("") ? req.getParameter("hostname") : "default-hostname"; - String cores = !req.getParameter("cores").equals("") ? req.getParameter("cores") : "2"; - String ram = !req.getParameter("ram").equals("") ? req.getParameter("ram") : "1024"; - String disk = !req.getParameter("disk").equals("") ? req.getParameter("disk") : "16"; - req.setAttribute("hostname", hostname); - req.setAttribute("cores", cores); - req.setAttribute("ram", ram); - req.setAttribute("disk", disk); + String hostname = req.getParameter("hostname"); + String cores = req.getParameter("cores"); + String ram = req.getParameter("ram"); + String disk = req.getParameter("disk"); + + SysConf sysconf = new SysConf(hostname, cores, ram, disk); + req.setAttribute("sysconf", sysconf); // ------------------- @@ -41,6 +41,17 @@ public class CreateCTHandler extends HttpServlet { NetConf netconf4 = new NetConf(ipaddress4, gateway4, network, ipconf4); req.setAttribute("netconf4", netconf4); + // -------------------- + + String ipaddress6 = req.getParameter("ipaddress6"); + String gateway6 = req.getParameter("gateway6"); + String ipconf6 = req.getParameter("dhcpv6"); + + NetConf6 netconf6 = new NetConf6(ipaddress6, gateway6, network, ipconf6); + req.setAttribute("netconf6", netconf6); + + // -------------------- + this.getServletContext().getRequestDispatcher("/WEB-INF/createct.jsp").forward(req, resp); } diff --git a/src/main/java/fr/sixthemes/utils/Utils.java b/src/main/java/fr/sixthemes/utils/Utils.java index f7fce4a..31fe9dc 100644 --- a/src/main/java/fr/sixthemes/utils/Utils.java +++ b/src/main/java/fr/sixthemes/utils/Utils.java @@ -1,18 +1,15 @@ package fr.sixthemes.utils; -import java.util.regex.Matcher; import java.util.regex.Pattern; public abstract class Utils { - public static boolean isValidIP(String ipAddress) { + public static boolean isInteger(String strNum) { + if(strNum.isEmpty() || strNum == null) return false; - Pattern p = Pattern.compile(""); - Matcher m = p.matcher(ipAddress); - - if(!m.find()) return false; + Pattern p = Pattern.compile("^\\d*$"); - return true; + return p.matcher(strNum).matches(); } } diff --git a/src/main/webapp/WEB-INF/createct.jsp b/src/main/webapp/WEB-INF/createct.jsp index e5e3e30..6e02965 100644 --- a/src/main/webapp/WEB-INF/createct.jsp +++ b/src/main/webapp/WEB-INF/createct.jsp @@ -1,6 +1,10 @@ <%@ page pageEncoding="UTF-8" %> -<%@ page import="fr.sixthemes.beans.NetConf" %> -<% NetConf netconf4 = (NetConf)request.getAttribute("netconf4"); %> +<%@ page import="fr.sixthemes.beans.NetConf,fr.sixthemes.beans.NetConf6,fr.sixthemes.beans.SysConf" %> +<% + SysConf sysconf = (SysConf)request.getAttribute("sysconf"); + NetConf netconf4 = (NetConf)request.getAttribute("netconf4"); + NetConf6 netconf6 = (NetConf6)request.getAttribute("netconf6"); +%> @@ -14,13 +18,15 @@

6-Deploy

CT creation

IPv4 :

<%= netconf4.toHTML() %> +

IPv6

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