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
30 changes: 27 additions & 3 deletions src/com/geekhub/hw8/CreateFileServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,36 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.util.Date;

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

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String directory = (String) req.getSession().getAttribute("path");
String fileName = req.getParameter("fileName");
String result;
Path path;

try {
path = Paths.get(directory, fileName);
Files.createFile(path);
result = "file " + path.getFileName() + " has been created";
} catch (InvalidPathException e) {
result = "invalid path";
} catch (IOException e) {
result = "error while creating file: " + e;
}

req.getSession().setAttribute("message", "[" + DateFormat.getDateTimeInstance().format(new Date()) + "] " + result);
resp.sendRedirect("/dir/view");
}
}

}
2 changes: 1 addition & 1 deletion src/com/geekhub/hw8/IndexServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/")
@WebServlet("/index")
public class IndexServlet extends HttpServlet {

@Override
Expand Down
22 changes: 20 additions & 2 deletions src/com/geekhub/hw8/RemoveFileServlet.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.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.util.Date;

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

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
Path path = Paths.get(req.getParameter("path"));
String result;

try {
Files.delete(path);
result = "file " + path.getFileName() + " has been deleted";
} catch (IOException e) {
result = "this file can not be deleted";
}

req.getSession().setAttribute("message", "[" + DateFormat.getDateTimeInstance().format(new Date()) + "] " + result);
resp.sendRedirect("/dir/view");
}

}
58 changes: 46 additions & 12 deletions src/com/geekhub/hw8/ViewDirectoryServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,69 @@
import javax.servlet.http.HttpServletRequest;
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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(value = "/dir/view", initParams = {
@WebInitParam(name = "root", value = "D:\\")
@WebInitParam(name = "root", value = "D:\\")
})
public class ViewDirectoryServlet extends HttpServlet {

private static Path ROOT_PATH;
private static List<String> messages = new ArrayList<>();

@Override
public void init(ServletConfig config) throws ServletException {
ROOT_PATH = Paths.get(config.getInitParameter("root"));
messages.add("[" + DateFormat.getDateTimeInstance().format(new Date()) + "] " + " launched. Enjoy ) ");
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuilder sb = new StringBuilder();
sb.append("<html>");
Map<String, String> directoryLinks = new HashMap<>();
Map<String, String> fileLinks = new HashMap<>();

//TODO: Implement directory listing here
String pathParam = (String) req.getSession().getAttribute("path");
Path currentPath = (pathParam == null) ? ROOT_PATH : Paths.get(pathParam);
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(currentPath)) {
for (Path path : directoryStream) {
String linkView = path.getName(path.getNameCount() - 1).toString();
Path linkPath = path.toAbsolutePath();
if (Files.isDirectory(linkPath, LinkOption.NOFOLLOW_LINKS)) {
directoryLinks.put(linkView, linkPath.toString());
} else {
fileLinks.put(linkView, linkPath.toString());
}
}
} catch (IOException e) {
req.getSession().setAttribute("path", currentPath.getParent().toString());
resp.sendRedirect("/dir/view");
return;
}

sb.append("</html>");
resp.getWriter().write(sb.toString());
req.getSession().setAttribute("path", currentPath.toString());
req.setAttribute("backPath", (!currentPath.equals(ROOT_PATH)) ? currentPath.getParent().toString() : ROOT_PATH.toString());
req.setAttribute("rootPath", ROOT_PATH.toString());
req.setAttribute("directoryLinks", directoryLinks);
req.setAttribute("fileLinks", fileLinks);

Object message = req.getSession().getAttribute("message");
if (message != null) {
messages.add((String) message);
}
req.getSession().setAttribute("message", null);
req.getSession().setAttribute("messages", messages);

req.getRequestDispatcher("/WEB-INF/jsp/viewDirectory.jsp").forward(req, resp);
}

private void appendLink(StringBuilder sb, String text, Path path) {
//TODO: Implement link rendering based on path type (directory or file)
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
req.getSession().setAttribute("path", req.getParameter("path"));
resp.sendRedirect("/dir/view");
}

}
37 changes: 35 additions & 2 deletions src/com/geekhub/hw8/ViewFileServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,47 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
Path path = Paths.get(req.getParameter("path"));

List<String> fileLines = new ArrayList<>();
String exception = "";

try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path.toString()), "windows-1251"))) {
String currentLine;
while ((currentLine = reader.readLine()) != null) {
fileLines.add(currentLine);
}
} catch (IOException e) {
exception = e.getMessage();
}

if (exception.equals("")) {
req.setAttribute("backPath", path.getParent().toString());
req.setAttribute("fileName", path.getFileName());
req.setAttribute("fileLines", fileLines);
req.getSession().setAttribute("message", "[" + DateFormat.getDateTimeInstance().format(new Date()) + "] file " + path.getFileName() + " has been opened");
req.getRequestDispatcher("/WEB-INF/jsp/viewFile.jsp").forward(req, resp);
} else {
req.getSession().setAttribute("message", "[" + DateFormat.getDateTimeInstance().format(new Date()) + "] " + exception);
resp.sendRedirect("/dir/view");
}
}

}
75 changes: 75 additions & 0 deletions web/WEB-INF/jsp/ViewDirectory.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>

<head>
<title>File manager</title>
<link type="text/css" rel="stylesheet" href="/css/style.css"/>
</head>

<body>

<form method="post" action="/file/create">
<input type="text" name="fileName" placeholder="file name">
<input type="submit" value="Create new file">
</form>

<table>

<c:if test="${sessionScope.path != requestScope.rootPath}">
<tr>
<td>
<div class="row">
<form name="main" method="post" action="/dir/view">
<input type="submit" name="text" value="[..]" class="dir">
<input type="hidden" name="path" value="${requestScope.backPath}">
</form>
</div>
</td>
</tr>
</c:if>

<c:forEach items="${directoryLinks}" var="directoryLink">
<tr>
<td>
<div class="row">
<form name="main" method="post" action="/dir/view">
<input type="submit" name="text" value="[${directoryLink.key}]" class="dir">
<input type="hidden" name="path" value="${directoryLink.value}">
</form>
</div>
</td>
</tr>
</c:forEach>

<c:forEach items="${fileLinks}" var="fileLink">
<tr>
<td>
<div class="row">
<form name="main" method="post" action="/file/view">
<input type="submit" name="text" value="${fileLink.key}">
<input type="hidden" name="path" value="${fileLink.value}">
</form>
</div>
</td>
<td>
<div class="row">
<form name="del" method="post" action="/file/remove">
<input type="submit" value="delete" class="del">
<input type="hidden" name="path" value="${fileLink.value}">
</form>
</div>
</td>
</tr>
</c:forEach>

</table>

<br><br>
<jsp:include page="footer.jsp"/>

</body>

</html>
14 changes: 14 additions & 0 deletions web/WEB-INF/jsp/footer.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<div class="operationLog">
<c:forEach items="${messages}" var="message">
${message}
<br>
</c:forEach>
</div>
</body>
</html>
27 changes: 27 additions & 0 deletions web/WEB-INF/jsp/viewFile.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
<meta charset="windows-1251">
<title>File content</title>
<link type="text/css" rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<div>
<form name="back" method="post" action="/dir/view">
<input type="submit" name="back" value="Return">
<input type="hidden" name="path" value="${requestScope.backPath}">
</form>
</div>
<h4> File name: ${fileName}</h4>

<h4> File content:</h4>

<div class="fileContent">
<c:forEach items="${fileLines}" var="fileLine">
${fileLine}
<br>
</c:forEach>
</div>
</body>
</html>
43 changes: 43 additions & 0 deletions web/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
table, tr, td {
width: auto;
margin: 0;
padding: 0;
border-collapse: collapse;
border: solid 2px black;
}

input[type=submit] {
cursor: pointer;
background: none;
padding: 0;
border: none;
color: black;
font: 100% Arial;
}

input[type=submit]:hover {
color: red;
}

input[type=submit].dir {
font-weight: bolder;
color: orange;
}

.row {
background-color: white;
padding: 5px 5px 10px;
height: 15px;
}

div.fileContent {
padding: 10px;
border: solid 1px black;
width: 50%;
}

div.operationLog {
padding: 10px;
border: solid 1px black;
width: 50%;
}