+ Added form data validator in java Beans

main
flavien.ancel@pm.me 1 year ago
parent b80a2e22d2
commit 3dc9b97d1e

@ -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();

@ -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])";
}
}

@ -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";
}
}

@ -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);
}

@ -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();
}
}

@ -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");
%>
<!DOCTYPE html>
<html lang="fr">
<head>
@ -14,13 +18,15 @@
<h1>6-Deploy</h1>
<h2>CT creation</h2>
<ul>
<li><strong>Hostname : </strong>${hostname}</li>
<li><strong>Cores : </strong>${cores}</li>
<li><strong>RAM : </strong>${ram}</li>
<li><strong>Disk (in GB) : </strong>${disk}</li>
<li><strong>Hostname : </strong>${sysconf.hostname}</li>
<li><strong>Cores : </strong>${sysconf.cores}</li>
<li><strong>RAM : </strong>${sysconf.ram}</li>
<li><strong>Disk (in GB) : </strong>${sysconf.disk}</li>
</ul>
<h3>IPv4 :</h3>
<%= netconf4.toHTML() %>
<h3>IPv6</h3>
<%= netconf6.toHTML() %>
</div>
</body>
</html>
Loading…
Cancel
Save