Merge branch 'SPM-7' of Studium/Softwareprojektmanagement into master

This commit is contained in:
Johannes Theiner 2019-05-06 13:48:13 +00:00 committed by Gitea
commit 29a2e39eb1
8 changed files with 253 additions and 45 deletions

13
pom.xml
View File

@ -80,6 +80,13 @@
<server>TomcatServer</server>
<path>/bauDas</path>
</configuration>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -258,6 +265,12 @@
<version>3.8.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<!--Annotations-->
<dependency>

View File

@ -1,45 +0,0 @@
package de.hsel.spm.baudas;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Hello World.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
*/
@WebServlet("/")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 156790693L;
/**
* doGet.
*
* @param req Request
* @param resp Response
* @throws IOException failed
*/
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
final PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hallo Welt!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hallo Ostfriesland!</h1>");
out.println("</body>");
out.println("</html>");
}
}

View File

@ -0,0 +1,96 @@
package de.hsel.spm.baudas.web;
import lombok.Getter;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* manages data about .csv files.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
**/
public class Data {
@Getter
private static ConcurrentLinkedQueue<SavedFile> files = new ConcurrentLinkedQueue<>();
/**
* adds file to list and generates a new filename.
*
* @param name File name
* @return path to save file to
*/
@NotNull
static Path add(@NotNull String name) {
//cleanup old files
if (files.isEmpty()) {
try {
Files.list(Paths.get("target/")).forEach(path -> {
if (path.toFile().getName().endsWith(".csv")) {
try {
Files.delete(path);
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (IOException ex) {
ex.printStackTrace();
}
}
Path path;
UUID uuid = UUID.randomUUID();
if (files.size() >= 5) {
//remove last and add new one
SavedFile file = files.poll();
if (!get(file.getUuid()).delete()) {
System.out.println("failed to delete file...");
}
path = Paths.get(getFileName(uuid));
files.offer(new SavedFile(uuid, name));
} else {
files.add(new SavedFile(uuid, name));
path = Paths.get(getFileName(uuid));
}
return path;
}
/**
* generates File from uuid.
*
* @param uuid uuid
* @return file
*/
@Contract
@NotNull
public static File get(@NotNull UUID uuid) {
return new File(getFileName(uuid));
}
/**
* generates file name from uuid.
*
* @param uuid uuid
* @return file name
*/
@Contract(pure = true)
@NotNull
private static String getFileName(@NotNull UUID uuid) {
return "target/" + uuid + ".csv";
}
}

View File

@ -0,0 +1,42 @@
package de.hsel.spm.baudas.web;
import com.google.gson.Gson;
import org.jetbrains.annotations.NotNull;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
/**
* list all currently available files.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
**/
@WebServlet("/files")
public class Files extends HttpServlet {
private static final long serialVersionUID = 148451844848L;
/**
* do get.
* @param req request
* @param resp response
* @throws IOException writer could not be retrieved
*/
@Override
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp)
throws IOException {
resp.setContentType("application/json");
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
PrintWriter out = resp.getWriter();
Gson gson = new Gson();
out.print(gson.toJson(Data.getFiles()));
}
}

View File

@ -0,0 +1,23 @@
package de.hsel.spm.baudas.web;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import java.util.UUID;
/**
* meta data about file.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
*/
@Getter
@AllArgsConstructor
@ToString
public class SavedFile {
private UUID uuid;
private String name;
}

View File

@ -0,0 +1,61 @@
package de.hsel.spm.baudas.web;
import org.jetbrains.annotations.NotNull;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/**
* saves uploaded files.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
**/
@WebServlet("/upload")
@MultipartConfig
public class Upload extends HttpServlet {
private static final long serialVersionUID = 14144111845151L;
/**
* doPost.
*
* @param req request
* @param resp response
* @throws ServletException could not get file part
* @throws IOException writer could not be retrieved
*/
@Override
protected void doPost(@NotNull HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
Part filePart = req.getPart("file");
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
InputStream inputStream = filePart.getInputStream();
Path path = Data.add(fileName);
if (!Files.exists(path)) {
Files.createFile(path);
}
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
resp.sendRedirect("index.jsf");
}
}

View File

@ -0,0 +1,11 @@
<!doctype html>
<html>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">
Hochladen
</button>
</form>
</body>
</html>

View File

@ -0,0 +1,7 @@
<%@ page import="weka.core.Version" %>
Server info: <%= application.getServerInfo() %><br>
Servlet version: <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %><br>
JSP version: <%= JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion() %><br>
Java version: <%= System.getProperty("java.version") %><br>
Weka version: <%= Version.VERSION %>