+ Documentation
Signed-off-by: Johannes Theiner <j.theiner@live.de> #SPM-7: add work documentation 20m
This commit is contained in:
parent
b50a490cd4
commit
d5b4888d5d
|
@ -5,13 +5,15 @@ import org.jetbrains.annotations.Contract;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* manages data about .csv files.
|
||||||
*
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
|
@ -23,17 +25,44 @@ public class Data {
|
||||||
private static ConcurrentLinkedQueue<SavedFile> files = new ConcurrentLinkedQueue<>();
|
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
|
@NotNull
|
||||||
static Path add(@NotNull String name) {
|
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;
|
Path path;
|
||||||
UUID uuid = UUID.randomUUID();
|
UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
if (files.size() >= 5) {
|
if (files.size() >= 5) {
|
||||||
|
//remove last and add new one
|
||||||
SavedFile file = files.poll();
|
SavedFile file = files.poll();
|
||||||
if (!get(file.getUuid()).delete()) {
|
if (!get(file.getUuid()).delete()) {
|
||||||
System.out.println("failed");
|
System.out.println("failed to delete file...");
|
||||||
}
|
}
|
||||||
path = Paths.get(getFileName(uuid));
|
path = Paths.get(getFileName(uuid));
|
||||||
files.offer(new SavedFile(uuid, name));
|
files.offer(new SavedFile(uuid, name));
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
files.add(new SavedFile(uuid, name));
|
files.add(new SavedFile(uuid, name));
|
||||||
path = Paths.get(getFileName(uuid));
|
path = Paths.get(getFileName(uuid));
|
||||||
|
@ -41,11 +70,24 @@ public class Data {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* generates File from uuid.
|
||||||
|
*
|
||||||
|
* @param uuid uuid
|
||||||
|
* @return file
|
||||||
|
*/
|
||||||
@Contract
|
@Contract
|
||||||
@NotNull public static File get(@NotNull UUID uuid) {
|
@NotNull
|
||||||
|
public static File get(@NotNull UUID uuid) {
|
||||||
return new File(getFileName(uuid));
|
return new File(getFileName(uuid));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* generates file name from uuid.
|
||||||
|
*
|
||||||
|
* @param uuid uuid
|
||||||
|
* @return file name
|
||||||
|
*/
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
@NotNull
|
@NotNull
|
||||||
private static String getFileName(@NotNull UUID uuid) {
|
private static String getFileName(@NotNull UUID uuid) {
|
||||||
|
|
|
@ -22,12 +22,21 @@ import java.nio.charset.StandardCharsets;
|
||||||
@WebServlet("/files")
|
@WebServlet("/files")
|
||||||
public class Files extends HttpServlet {
|
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
|
@Override
|
||||||
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
|
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp)
|
||||||
|
throws IOException {
|
||||||
resp.setContentType("application/json");
|
resp.setContentType("application/json");
|
||||||
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||||
PrintWriter out = resp.getWriter();
|
PrintWriter out = resp.getWriter();
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
out.println(gson.toJson(Data.getFiles()));
|
out.print(gson.toJson(Data.getFiles()));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,11 +2,20 @@ package de.hsel.spm.baudas.web;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* meta data about file.
|
||||||
|
*
|
||||||
|
* @author Johannes Theiner
|
||||||
|
* @version 0.1
|
||||||
|
* @since 0.1
|
||||||
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ToString
|
||||||
public class SavedFile {
|
public class SavedFile {
|
||||||
|
|
||||||
private UUID uuid;
|
private UUID uuid;
|
||||||
|
|
|
@ -29,8 +29,19 @@ import java.nio.file.StandardCopyOption;
|
||||||
@MultipartConfig
|
@MultipartConfig
|
||||||
public class Upload extends HttpServlet {
|
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
|
@Override
|
||||||
protected void doPost(@NotNull HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doPost(@NotNull HttpServletRequest req, HttpServletResponse resp)
|
||||||
|
throws ServletException, IOException {
|
||||||
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||||
|
|
||||||
Part filePart = req.getPart("file");
|
Part filePart = req.getPart("file");
|
||||||
|
@ -39,13 +50,12 @@ public class Upload extends HttpServlet {
|
||||||
|
|
||||||
|
|
||||||
Path path = Data.add(fileName);
|
Path path = Data.add(fileName);
|
||||||
System.out.println(path);
|
|
||||||
if (!Files.exists(path)) {
|
if (!Files.exists(path)) {
|
||||||
Files.createFile(path);
|
Files.createFile(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
resp.sendRedirect("files");
|
resp.sendRedirect("index.jsf");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue