| 1 | package control.maglietta; | |
| 2 | ||
| 3 | import model.maglietta.MagliettaBean; | |
| 4 | import model.maglietta.MagliettaDAO; | |
| 5 | import model.misura.MisuraBean; | |
| 6 | import model.misura.MisuraDAO; | |
| 7 | ||
| 8 | import javax.servlet.ServletException; | |
| 9 | import javax.servlet.annotation.MultipartConfig; | |
| 10 | import javax.servlet.annotation.WebServlet; | |
| 11 | import javax.servlet.http.HttpServlet; | |
| 12 | import javax.servlet.http.HttpServletRequest; | |
| 13 | import javax.servlet.http.HttpServletResponse; | |
| 14 | import javax.servlet.http.Part; | |
| 15 | ||
| 16 | import java.io.IOException; | |
| 17 | import java.io.InputStream; | |
| 18 | import java.nio.file.Files; | |
| 19 | import java.nio.file.Path; | |
| 20 | import java.nio.file.Paths; | |
| 21 | import java.sql.SQLException; | |
| 22 | ||
| 23 | @WebServlet("/SaveMaglietta") | |
| 24 | @MultipartConfig | |
| 25 | public class SaveMaglietta extends HttpServlet { | |
| 26 | ||
| 27 | @Override | |
| 28 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) | |
| 29 | throws ServletException, IOException { | |
| 30 | ||
| 31 | final String ERROR_PAGE = "/pages/errorpage.jsp"; | |
| 32 | final String PATH = req.getServletContext().getRealPath("/images/grafiche/"); | |
| 33 | Path uploadDir = Paths.get(PATH).toAbsolutePath().normalize(); | |
| 34 | ||
| 35 | String nome = req.getParameter("nome"); | |
| 36 | String colore = req.getParameter("colore"); | |
| 37 | String tipo = req.getParameter("tipo"); | |
| 38 | int iva; | |
| 39 | try { | |
| 40 | iva = (int) Float.parseFloat(req.getParameter("IVA")); | |
| 41 | } catch (NumberFormatException | NullPointerException e) { | |
| 42 |
1
1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
req.getRequestDispatcher(ERROR_PAGE).forward(req, resp); |
| 43 | return; | |
| 44 | } | |
| 45 | ||
| 46 | float prezzo; | |
| 47 | try { | |
| 48 | prezzo = Float.parseFloat(req.getParameter("prezzo")); | |
| 49 | } catch (NumberFormatException | NullPointerException e) { | |
| 50 |
1
1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
req.getRequestDispatcher(ERROR_PAGE).forward(req, resp); |
| 51 | return; | |
| 52 | } | |
| 53 | String descrizione = req.getParameter("descrizione"); | |
| 54 | ||
| 55 | Part grafica; | |
| 56 | try { | |
| 57 | grafica = req.getPart("grafica"); | |
| 58 | } catch (IOException | ServletException e) { | |
| 59 |
1
1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
req.getRequestDispatcher(ERROR_PAGE).forward(req, resp); |
| 60 | return; | |
| 61 | } | |
| 62 | ||
| 63 | MagliettaDAO magliettaDAO = new MagliettaDAO(); | |
| 64 | ||
| 65 | String nomeFile; | |
| 66 | int extensionIndex = grafica.getSubmittedFileName().lastIndexOf("."); | |
| 67 | ||
| 68 | try { | |
| 69 | String estensione = grafica.getSubmittedFileName().substring(extensionIndex); | |
| 70 | nomeFile = magliettaDAO.getMaxID() + tipo + estensione; | |
| 71 | } catch (SQLException e) { | |
| 72 |
1
1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
req.getRequestDispatcher(ERROR_PAGE).forward(req, resp); |
| 73 | return; | |
| 74 | } | |
| 75 | ||
| 76 | nomeFile = nomeFile.replaceAll("[^a-zA-Z0-9._-]", "_"); | |
| 77 | ||
| 78 | String relativePath = "images/grafiche/" + nomeFile; | |
| 79 | ||
| 80 | Path destinationFile = uploadDir.resolve(nomeFile).normalize(); | |
| 81 | ||
| 82 |
1
1. doPost : negated conditional → KILLED |
if (!destinationFile.startsWith(uploadDir)) { |
| 83 |
1
1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
req.getRequestDispatcher(ERROR_PAGE).forward(req, resp); |
| 84 | return; | |
| 85 | } | |
| 86 | ||
| 87 | try (InputStream inputStream = grafica.getInputStream()) { | |
| 88 | Files.copy(inputStream, destinationFile); | |
| 89 | } catch (IOException e) { | |
| 90 |
1
1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
req.getRequestDispatcher(ERROR_PAGE).forward(req, resp); |
| 91 | return; | |
| 92 | } | |
| 93 | ||
| 94 | MagliettaBean maglietta = new MagliettaBean(); | |
| 95 |
1
1. doPost : removed call to model/maglietta/MagliettaBean::setNome → KILLED |
maglietta.setNome(nome); |
| 96 |
1
1. doPost : removed call to model/maglietta/MagliettaBean::setColore → KILLED |
maglietta.setColore(colore); |
| 97 |
1
1. doPost : removed call to model/maglietta/MagliettaBean::setTipo → KILLED |
maglietta.setTipo(tipo); |
| 98 |
1
1. doPost : removed call to model/maglietta/MagliettaBean::setPrezzo → KILLED |
maglietta.setPrezzo(prezzo); |
| 99 |
1
1. doPost : removed call to model/maglietta/MagliettaBean::setIVA → KILLED |
maglietta.setIVA(iva); |
| 100 |
1
1. doPost : removed call to model/maglietta/MagliettaBean::setDescrizione → KILLED |
maglietta.setDescrizione(descrizione); |
| 101 |
1
1. doPost : removed call to model/maglietta/MagliettaBean::setGrafica → KILLED |
maglietta.setGrafica(relativePath); |
| 102 | ||
| 103 | String taglia = req.getParameter("taglia"); | |
| 104 | int quantita; | |
| 105 | try { | |
| 106 | quantita = Integer.parseInt(req.getParameter("quantita")); | |
| 107 | } catch (NumberFormatException | NullPointerException e) { | |
| 108 |
1
1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
req.getRequestDispatcher(ERROR_PAGE).forward(req, resp); |
| 109 | return; | |
| 110 | } | |
| 111 | ||
| 112 | MisuraDAO misuraDAO = new MisuraDAO(); | |
| 113 | ||
| 114 | try { | |
| 115 |
1
1. doPost : removed call to model/maglietta/MagliettaDAO::doSave → KILLED |
magliettaDAO.doSave(maglietta); |
| 116 | MisuraBean misuraBean = | |
| 117 |
1
1. doPost : Replaced integer subtraction with addition → KILLED |
new MisuraBean(magliettaDAO.getMaxID() - 1, quantita, taglia); |
| 118 |
1
1. doPost : removed call to model/misura/MisuraDAO::doSave → KILLED |
misuraDAO.doSave(misuraBean); |
| 119 | } catch (SQLException e) { | |
| 120 |
1
1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
req.getRequestDispatcher(ERROR_PAGE).forward(req, resp); |
| 121 | return; | |
| 122 | } | |
| 123 | ||
| 124 |
1
1. doPost : removed call to javax/servlet/http/HttpServletResponse::sendRedirect → KILLED |
resp.sendRedirect("./Catalogo"); |
| 125 | } | |
| 126 | } | |
Mutations | ||
| 42 |
1.1 |
|
| 50 |
1.1 |
|
| 59 |
1.1 |
|
| 72 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 90 |
1.1 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 97 |
1.1 |
|
| 98 |
1.1 |
|
| 99 |
1.1 |
|
| 100 |
1.1 |
|
| 101 |
1.1 |
|
| 108 |
1.1 |
|
| 115 |
1.1 |
|
| 117 |
1.1 |
|
| 118 |
1.1 |
|
| 120 |
1.1 |
|
| 124 |
1.1 |