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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
target
pom.xml


out
gen
# Created by .ignore support plugin (hsz.mobi)
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Manifest-Version: 1.0

Binary file added lib/jstl-1.2.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Built-By: sergei
Created-By: IntelliJ IDEA
Build-Jdk: 1.8.0_45

Binary file added output/artifacts/ROOT/ROOT.war
Binary file not shown.
Binary file added output/artifacts/unnamed/unnamed.war
Binary file not shown.
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>HomeTaskWeek9</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>war</packaging>


</project>
17 changes: 0 additions & 17 deletions src/com/geekhub/hw8/CreateFileServlet.java

This file was deleted.

17 changes: 0 additions & 17 deletions src/com/geekhub/hw8/RemoveFileServlet.java

This file was deleted.

42 changes: 0 additions & 42 deletions src/com/geekhub/hw8/ViewDirectoryServlet.java

This file was deleted.

35 changes: 35 additions & 0 deletions src/main/java/com/geekhub/hw8/CreateFileServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.geekhub.hw8;

import javax.servlet.ServletException;
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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

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

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String folder = req.getParameter("folder");
String fileName = req.getParameter("fileName");
String fileExtension = req.getParameter("fileExtension");

Path path = Paths.get(folder + "/" + fileName + "." + fileExtension);

if (!Files.exists(path)) {
Files.createFile(path);

}else try {
throw new Exception("File with this name exist");
} catch (Exception e) {
e.printStackTrace();
}
req.getRequestDispatcher("/dir/view?path=" + folder + "/").forward(req, resp);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
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;

@WebServlet("/")
public class IndexServlet extends HttpServlet {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/geekhub/hw8/RemoveFileServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.geekhub.hw8;


import javax.servlet.ServletException;
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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

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


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String removeFile = req.getParameter("remove");
Path path = Paths.get(removeFile);
String folder = path.getParent().toString();

if (!Files.deleteIfExists(path)) try {
throw new Exception("File is not exist");
} catch (Exception e) {
e.printStackTrace();
}
req.getRequestDispatcher("/dir/view?path=" + folder + "/").forward(req, resp);

}
}
142 changes: 142 additions & 0 deletions src/main/java/com/geekhub/hw8/ViewDirectoryServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.geekhub.hw8;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
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.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

private static Path ROOT_PATH;

@Override
public void init(ServletConfig config) throws ServletException {
ROOT_PATH = Paths.get(config.getInitParameter("root"));
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Path path;
String pathParameter = req.getParameter("path");
if (pathParameter != null) {
path = Paths.get(pathParameter);
} else path = ROOT_PATH;


StringBuilder sb = new StringBuilder();
sb.append("<html>");
sb.append("<head>");
sb.append("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css\" integrity=\"sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7\" crossorigin=\"anonymous\">");
sb.append("</head>");
sb.append("<body>");
sb.append("<div class =\"jumbotron\">");
sb.append("<h3> Create file :</h3>");

sb.append("<form action=\"/file/create\" method=\"get\">\n" +
" <input type=\"text\" name=\"folder\" hidden value=\"" + path + "\">\n" +
" <label>File name</label>\n" +
" <input type=\"text\" name=\"fileName\">\n" +
" <label>File extension</label>\n" +
" <input type=\"text\" name=\"fileExtension\">\n" +
" <button type=\"submit\">Create file</button>\n" +
"</form>");
sb.append("</div>");


sb.append("<div class=\"container\">");
sb.append("<div class =\"row\">");
sb.append("<div class = \"col-lg-8\">");
sb.append("</div>");
sb.append("<table class=\"table table-striped\">" +
" <thead> " +
"<tr>" +
" <th>#</th>" +
" <th>File name</th> " +
"<th>Type</th> " +
"<th>Remove</th>" +
"</tr> " +
"</thead>");

sb.append("<tbody>");
int rowNumber = 0;
for (Path pathToFile : pathList(path)) {
appendRow(sb, path.getFileName().toString(), pathToFile, ++rowNumber);
}
sb.append("</tbody>");
sb.append("</table>");
sb.append("</div>");
sb.append("</div>");

sb.append(" <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>");
sb.append("<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\" integrity=\"sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS\" crossorigin=\"anonymous\"></script>");
sb.append("</body>");
sb.append("</html>");

resp.getWriter().write(sb.toString());
}

private void appendRow(StringBuilder sb, String text, Path path, int rowNumber) {
sb.append("<tr>");

sb.append("<td>");
sb.append(rowNumber);
sb.append("</td>");

if (Files.isDirectory(path)) {
sb.append("<td>");
sb.append("<a href=\"" + "/dir/view" + "?path=" + path.toString() + "\">" + path.getFileName().toString() + "</a>");
sb.append("</td>");

sb.append("<td>");
sb.append("<img src =\"http://findicons.com/files/icons/2221/folder/128/normal_folder.png\" height=\"15\" width=\"15\">");
sb.append("</td>");
} else {
sb.append("<td>");
sb.append("<a href=\"" + "/file/view" + "?path=" + path.toString() + "\">" + path.getFileName().toString() + "</a>");
sb.append("</td>");

sb.append("<td>");
sb.append("<img src =\"http://megaicons.net/static/img/icons_sizes/8/178/256/very-basic-file-icon.png\" height=\"15\" width=\"15\">");
sb.append("</td>");
}

sb.append("<td>");
sb.append("<form action=\"/file/remove\" method=\"get\">\n" +
" <input type=\"text\" name=\"remove\" value=\"" + path.toString() + "/" + "\" hidden>\n" +
" <button type=\"submit\">remove</button>\n" +
"</form>");
sb.append("</td>");

sb.append("</tr>");

}


public List<Path> pathList(Path directory) {
List<Path> paths = new ArrayList<>();
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
for (Path path : directoryStream) {
paths.add(path);
}
} catch (IOException ex) {
System.out.println(" IOException");
}
Collections.sort(paths, (Path p1, Path p2) -> p1.getFileName().toString().compareTo(p2.getFileName().toString()));
return paths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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 = Paths.get(req.getParameter("path"));
resp.setHeader("Content-Disposition", "attachment;filename=\"" + path.getFileName().toString() + "\"");

byte[] file = Files.readAllBytes(path);
resp.setContentLength(file.length);

resp.getOutputStream().write(file);
}
}
5 changes: 5 additions & 0 deletions target/HomeTaskWeek9-1.0-SNAPSHOT/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Built-By: sergei
Created-By: IntelliJ IDEA
Build-Jdk: 1.8.0_45

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added target/classes/com/geekhub/hw8/IndexServlet.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

</web-app>