diff --git a/src/main/java/de/hsel/spm/baudas/web/ErrorCode.java b/src/main/java/de/hsel/spm/baudas/web/ErrorCode.java new file mode 100644 index 0000000..906aef6 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/ErrorCode.java @@ -0,0 +1,14 @@ +package de.hsel.spm.baudas.web; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum ErrorCode { + FILE_NOT_FOUND ("Die Hochgeladene Datei konnte nicht gefunden werden"), + EMPTY_FILE ("Die Hochgeladene Datei ist leer"), + INVALID_FORMAT("Die Hochgeladene Datei hat ein falsches Format"); + + private String message; +} diff --git a/src/main/java/de/hsel/spm/baudas/web/Upload.java b/src/main/java/de/hsel/spm/baudas/web/Upload.java index 28f7c9d..c5adec8 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Upload.java +++ b/src/main/java/de/hsel/spm/baudas/web/Upload.java @@ -1,6 +1,7 @@ package de.hsel.spm.baudas.web; import org.jetbrains.annotations.NotNull; +import weka.core.converters.CSVLoader; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; @@ -32,15 +33,57 @@ public class Upload extends HttpServlet { private static final long serialVersionUID = 14144111845151L; + private boolean checkFormat(InputStream stream) { + CSVLoader loader = new CSVLoader(); + try { + loader.setSource(stream); + return loader.getDataSet() != null; + } catch (IOException e) { + return false; + } + } + @Override protected void doPost(@NotNull HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding(StandardCharsets.UTF_8.name()); Part filePart = req.getPart("file"); + + if(filePart == null){ + resp.sendRedirect("error.jsp?code=" + ErrorCode.FILE_NOT_FOUND); + return; + } + + if(filePart.getSubmittedFileName() == null){ + resp.sendRedirect("error.jsp?code=" + ErrorCode.FILE_NOT_FOUND); + return; + } + String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); + + if(fileName == null){ + resp.sendRedirect("error.jsp?code=" + ErrorCode.FILE_NOT_FOUND); + return; + } + InputStream inputStream = filePart.getInputStream(); + if(inputStream == null){ + resp.sendRedirect("error.jsp?code=" + ErrorCode.FILE_NOT_FOUND); + return; + } + if(inputStream.available() == 0) { + resp.sendRedirect("error.jsp?code=" + ErrorCode.EMPTY_FILE); + return; + } + + if(!checkFormat(inputStream)){ + resp.sendRedirect("error.jsp?code=" + ErrorCode.INVALID_FORMAT); + return; + }else{ + inputStream = filePart.getInputStream(); + } Path path = Data.add(fileName); if (!Files.exists(path)) { @@ -49,6 +92,7 @@ public class Upload extends HttpServlet { Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING); + resp.sendRedirect(""); } } \ No newline at end of file diff --git a/src/main/webapp/emptyHeader.jsp b/src/main/webapp/emptyHeader.jsp new file mode 100644 index 0000000..d779ff1 --- /dev/null +++ b/src/main/webapp/emptyHeader.jsp @@ -0,0 +1,13 @@ + +<%@include file="head.jsp"%> + + + +
diff --git a/src/main/webapp/error.jsp b/src/main/webapp/error.jsp new file mode 100644 index 0000000..c1cfc0f --- /dev/null +++ b/src/main/webapp/error.jsp @@ -0,0 +1,36 @@ +<%-- + Created by IntelliJ IDEA. + User: julian + Date: 21.05.19 + Time: 14:21 + To change this template use File | Settings | File Templates. +--%> + +<%@ page import="java.nio.charset.StandardCharsets" %> +<%@ page import="de.hsel.spm.baudas.web.ErrorCode" %> +<%@ page contentType="text/html;charset=UTF-8" %> +<%@include file="emptyHeader.jsp"%> +<% response.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> +<% request.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> + +<% + String code = request.getParameter("code"); + if(code != null){ + ErrorCode errorCode = ErrorCode.valueOf(code); +%> + +
+
+
+
+ <%= errorCode.getMessage()%> +
+
+
+
+<% + } +%> + + +<%@include file="theming.jsp"%> \ No newline at end of file diff --git a/src/main/webapp/footer.jsp b/src/main/webapp/footer.jsp index e6dd7bc..173d2e2 100644 --- a/src/main/webapp/footer.jsp +++ b/src/main/webapp/footer.jsp @@ -6,10 +6,8 @@ - - + +<%@include file="theming.jsp"%> diff --git a/src/main/webapp/head.jsp b/src/main/webapp/head.jsp new file mode 100644 index 0000000..fab1aeb --- /dev/null +++ b/src/main/webapp/head.jsp @@ -0,0 +1,21 @@ +<% response.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> +<% request.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> + + + + BauDas + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 8cf6ee5..4052050 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -1,6 +1,6 @@ <%@ page import="java.nio.charset.StandardCharsets" %> <%@ page contentType="text/html;charset=UTF-8" %> -<%@include file="header.jsp"%> +<%@include file="menu.jsp"%> <% response.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> <% request.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> diff --git a/src/main/webapp/header.jsp b/src/main/webapp/menu.jsp similarity index 76% rename from src/main/webapp/header.jsp rename to src/main/webapp/menu.jsp index 77aceae..be68ab7 100644 --- a/src/main/webapp/header.jsp +++ b/src/main/webapp/menu.jsp @@ -1,26 +1,6 @@ -<% response.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> -<% request.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> - - - - BauDas - - - - - - - - - +<%@include file="head.jsp"%> - - -