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
62 changes: 62 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.geekhub.hw9</groupId>
<artifactId>HomeTaskWeek9</artifactId>
<packaging>war</packaging>
<version>1.0</version>

<properties>
<spring.version>3.2.5.RELEASE</spring.version>
<javax.servlet>3.0.1</javax.servlet>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

</dependencies>

<build>
<finalName>HomeTaskWeek9</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

</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.

18 changes: 0 additions & 18 deletions src/com/geekhub/hw8/ViewFileServlet.java

This file was deleted.

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

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class AppContextListener implements ServletContextListener{

@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
context.setAttribute("context", context.getContextPath());
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {}
}
42 changes: 42 additions & 0 deletions src/main/java/com/geekhub/hw9/CreateFileServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.geekhub.hw9;

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.*;

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

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pathParam = (String) req.getSession().getAttribute("path");
String filename = req.getParameter("filename");
Path path = null;
try {
path = Paths.get(pathParam, filename);
} catch (InvalidPathException e) {
e.printStackTrace();
}
String result = "";
try {
if (Files.exists(Files.createFile(path))) {
result = String.format("File \"%s\" created successfully!", filename);
}
} catch (FileAlreadyExistsException x) {
result = String.format("File \"%s\" already exists!", filename);
} catch (IOException x) {
result = String.format("Error creating file \"%s\" !", filename);
}
req.getSession().setAttribute("created", result);
resp.sendRedirect("/dir/view");
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.geekhub.hw8;
package com.geekhub.hw9;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
Expand All @@ -7,7 +7,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

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

@Override
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/geekhub/hw9/RemoveFileServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.geekhub.hw9;

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.DirectoryNotEmptyException;
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 doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String parameter = req.getParameter("path");
Path path = Paths.get(parameter);
String result = "";
try {
if (Files.deleteIfExists(path)) {
result = String.format("File \"%s\" deleted!", path.getFileName());
}
} catch (DirectoryNotEmptyException e) {
result = "Directory is not empty!";
} catch (IOException e) {
e.printStackTrace();
}
req.getSession().setAttribute("deleted", result);
req.getSession().setAttribute("path", path.getParent().toString());
resp.sendRedirect("/dir/view");
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/geekhub/hw9/ViewDirectoryServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.geekhub.hw9;

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.file.*;
import java.util.HashMap;
import java.util.Map;

@WebServlet(value = "/dir/view", initParams = {
@WebInitParam(name = "root", value = "D:\\temp")
})
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 doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String parameter = req.getParameter("path");
req.getSession().setAttribute("path", parameter);
resp.sendRedirect("/dir/view");
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String, String> dirLinks = new HashMap<>();
Map<String, String> fileLinks = new HashMap<>();

String pathParam = (String) req.getSession().getAttribute("path");
Path path = (pathParam == null) ? ROOT_PATH : Paths.get(pathParam);
// append files and directories list
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path)) {
for (Path entry: directoryStream) {
String linkText = entry.getName(entry.getNameCount()-1).toString();
Path linkPath = entry.toAbsolutePath();
if (Files.isDirectory(linkPath, LinkOption.NOFOLLOW_LINKS)) {
dirLinks.put(linkText, linkPath.toString());
} else if (Files.isReadable(entry.toAbsolutePath())) {
fileLinks.put(linkText, linkPath.toString());
} else {
fileLinks.put(linkText, "");
}
}
} catch (IOException e) {
e.printStackTrace();
}
req.setAttribute("dirLinks", dirLinks);
req.setAttribute("fileLinks", fileLinks);
req.getSession().setAttribute("path", path.toString());
req.setAttribute("backPath", path.getParent().toString());
req.setAttribute("root", ROOT_PATH);
req.getRequestDispatcher("/WEB-INF/jsp/viewDirectory.jsp").forward(req, resp);
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/geekhub/hw9/ViewFileServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.geekhub.hw9;

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.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

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

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String parameter = req.getParameter("path");
Path path = Paths.get(parameter);
ArrayList<String> lines = new ArrayList<>();
String result = "";

try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path.toString()), "windows-1251"))) {
/* try (BufferedReader reader = Files.newBufferedReader(path)) {*/
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (UnsupportedEncodingException e) {
result = "Unknown Encoding!";
} catch (IOException e) {
result = "Error open file!";
} catch (Exception e) {
System.out.println(e.getMessage());
result = "Unknown exception!";
}
req.getSession().setAttribute("opened", result);
if (result.equals("")) {
req.setAttribute("lines", lines);
req.setAttribute("backPath", path.getParent().toString());
req.setAttribute("filename", path.getFileName());
req.getRequestDispatcher("/WEB-INF/jsp/viewFile.jsp").forward(req, resp);
} else {
resp.sendRedirect("/dir/view");
}
}
}
5 changes: 5 additions & 0 deletions src/main/webapp/WEB-INF/jsp/footer.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<footer>
<div class="copy"> &copy; 2015 Sergiy Govorukhin All Rights Reserved :) </div>
</footer>
Loading