Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/com/geekhub/hw8/CreateFileServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,30 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@WebServlet("/file/create")
public class CreateFileServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
String filePath = req.getParameter("path") + "\\" + req.getParameter("fileName");
String type = req.getParameter("type");

try {
switch (type) {
case "file" :
Files.createFile(Paths.get(filePath));
break;
case "directory" :
Files.createDirectories(Paths.get(filePath));
break;
}
} catch (Exception e) {
req.setAttribute("message", "Error create new file " + e.getClass().getSimpleName());
}

req.getRequestDispatcher("/dir/view?path=" + req.getParameter("path")).forward(req, resp);
}
}
45 changes: 44 additions & 1 deletion src/com/geekhub/hw8/RemoveFileServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,55 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

@WebServlet("/file/remove")
public class RemoveFileServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
Path path = null;
String message;

try {
path = Paths.get(req.getParameter("path"));
if(Files.isDirectory(path)) {
deleteDirectory(path);
} else {
Files.delete(path);
}
message = path.toString() + " deleted";
} catch (FileSystemException e) {
message = "Error delete file";
} catch (NullPointerException e) {
message = "Error delete file";
path = Paths.get("/");
}

req.setAttribute("message", message.replace("\\", "/"));
req.getRequestDispatcher("/dir/view?path=" + path.getParent()).forward(req, resp);
}

private void deleteDirectory(Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if(exc == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
throw exc;
}
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
});

}
}
53 changes: 43 additions & 10 deletions src/com/geekhub/hw8/ViewDirectoryServlet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.geekhub.hw8;

import com.geekhub.hw8.file.FileInformation;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
Expand All @@ -9,9 +11,9 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet(value = "/dir/view", initParams = {
@WebInitParam(name = "root", value = "D:\\")
Expand All @@ -27,16 +29,47 @@ public void init(ServletConfig config) throws ServletException {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuilder sb = new StringBuilder();
sb.append("<html>");
Path currentPath;
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
String path = req.getParameter("path");

if (path == null || path.equals("null")) {
currentPath = ROOT_PATH;
} else {
currentPath = Paths.get(path);
}

//TODO: Implement directory listing here
if (Files.exists(currentPath)) {
req.setAttribute("fileList", createFileList(currentPath));
req.setAttribute("currentPath", currentPath);
req.getRequestDispatcher("/WEB-INF/pages/fileList.jsp").forward(req, resp);
} else {
req.setAttribute("message", "File: " + currentPath.toString().replace("\\", "/") + " not found");
req.getRequestDispatcher("/dir/view?path=" + getLastExistDirectory(currentPath)).forward(req, resp);
}
}

sb.append("</html>");
resp.getWriter().write(sb.toString());
private List<FileInformation> createFileList(Path path) {
List<FileInformation> fileList = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
for (Path filePath : stream) {
fileList.add(new FileInformation(filePath));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return fileList;
}

private void appendLink(StringBuilder sb, String text, Path path) {
//TODO: Implement link rendering based on path type (directory or file)
private Path getLastExistDirectory(Path path) {
while(path != null) {
path = path.getParent();
if (Files.exists(path) & Files.isDirectory(path)) {
break;
}
}
return path;
}


}
30 changes: 29 additions & 1 deletion src/com/geekhub/hw8/ViewFileServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,42 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@WebServlet("/file/view")
public class ViewFileServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
Path path = null;

try {
path = Paths.get(req.getParameter("path"));
req.setAttribute("backLink", path.getParent());
req.setAttribute("content", readFile(path));
req.getRequestDispatcher("/WEB-INF/pages/fileContent.jsp").forward(req, resp);
} catch (IOException e) {
req.setAttribute("message", "Error read file ");
req.getRequestDispatcher("/dir/view?path=" + path.getParent()).forward(req, resp);
} catch (NullPointerException e) {
req.setAttribute("message", "Not selected file");
req.getRequestDispatcher("/dir/view").forward(req, resp);
}
}

private static String readFile(Path path) throws IOException {
StringBuilder content = new StringBuilder();
try(BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset())) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append(System.lineSeparator());
}
}
return content.toString();
}
}
32 changes: 32 additions & 0 deletions src/com/geekhub/hw8/file/FileInformation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.geekhub.hw8.file;

import java.nio.file.Files;
import java.nio.file.Path;

public class FileInformation {
private String name;
private String path;
private Type type;

public FileInformation(Path path) {
this.name = path.getFileName().toString();
this.path = path.toString();
if (Files.isDirectory(path)) {
this.type = Type.DIRECTORY;
} else {
this.type = Type.FILE;
}
}

public String getName() {
return name;
}

public String getPath() {
return path;
}

public Type getType() {
return type;
}
}
5 changes: 5 additions & 0 deletions src/com/geekhub/hw8/file/Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.geekhub.hw8.file;

public enum Type {
FILE, DIRECTORY
}
16 changes: 16 additions & 0 deletions web/WEB-INF/pages/fileContent.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>

<textarea cols="150" rows="30">
${content}
</textarea><br>

Back to: <a href="/dir/view?path=${backLink}" > ${backLink}</a>

</body>
</html>
57 changes: 57 additions & 0 deletions web/WEB-INF/pages/fileList.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title></title>
<style type="text/css">
table, td{
border: 2px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>

<p>Current path: ${currentPath}</p>

<c:if test="${not empty currentPath.parent}">
<p>
Back to: <a href="/dir/view?path=${currentPath.parent}" > ${currentPath.parent}</a>
</p>
</c:if>

<form action="/file/create">
<input type=hidden name=path value="${currentPath}">
<label for="new_file">Create new</label>
<input type=text name=fileName placeholder="name" id="new_file"><br>
<label for="file">File</label>
<input type="radio" name="type" value="file" id="file" checked><br>
<label for="directory">Directory</label>
<input type="radio" name="type" value="directory" id="directory"><br>
<input type=submit value=Create>
</form>

<table>
<c:forEach items="${fileList}" var="file">
<tr>
<c:choose>
<c:when test="${file.type == 'FILE'}">
<td><a href="/file/view?path=${file.path}" >${file.name}</a></td>
</c:when>
<c:when test="${file.type == 'DIRECTORY'}">
<td><a href="/dir/view?path=${file.path}" > ${file.name} </a></td>
</c:when>
</c:choose>
<td><a href="/file/remove?path=${file.path}" >Delete</a></td>
</tr>
</c:forEach>
</table>

<c:if test="${not empty message}">
<script>
alert("${message}")
</script>
</c:if>

</body>
</html>