Skip to content

Commit b6c60ef

Browse files
committed
Separate path/file operations into separate classes and change to be real objects instead of static methods
1 parent ef08008 commit b6c60ef

File tree

13 files changed

+296
-258
lines changed

13 files changed

+296
-258
lines changed

src/main/java/httpserver/App.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package httpserver;
22

3-
import httpserver.fileutils.Files;
3+
import httpserver.file.PathExaminer;
44

55
import java.io.IOException;
66
import java.net.ServerSocket;
@@ -13,7 +13,8 @@ public class App {
1313
public static void main(String[] args) throws IOException {
1414
int port = Integer.parseInt(args[1]);
1515
String fileDirectory = args[3];
16-
Path root = Files.getPath(fileDirectory);
16+
17+
Path root = new PathExaminer().getPath(fileDirectory);
1718

1819
ExecutorService pool = Executors.newFixedThreadPool(16);
1920
ServerSocket serverSocket = new ServerSocket(port);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package httpserver.file;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.nio.file.StandardOpenOption;
6+
7+
import static java.nio.file.Files.createFile;
8+
import static java.nio.file.Files.deleteIfExists;
9+
import static java.nio.file.Files.write;
10+
11+
public class FileOperator {
12+
public void createFileAtPath(Path path) {
13+
try {
14+
createFile(path);
15+
} catch (IOException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
20+
public void deleteFileAtPath(Path path) {
21+
try {
22+
deleteIfExists(path);
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
28+
public void appendToFile(Path path, byte[] contents) {
29+
try {
30+
write(path, contents, StandardOpenOption.APPEND);
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
36+
public void replaceContents(Path path, byte[] contents) {
37+
try {
38+
write(path, contents);
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}

src/main/java/httpserver/fileutils/Html.java renamed to src/main/java/httpserver/file/Html.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package httpserver.fileutils;
1+
package httpserver.file;
22

33
import java.nio.file.Path;
44

5-
import static httpserver.fileutils.Files.isDir;
6-
75
public class Html {
86
public static String hrefString(Path root, Path fullFilePath) {
97
Path diff = root.relativize(fullFilePath);
10-
if (isDir(fullFilePath)) {
8+
if (new PathExaminer().isDir(fullFilePath)) {
119
return diff.toString() + "/";
1210
}
1311
return diff.toString();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package httpserver.file;
2+
3+
4+
import java.io.IOException;
5+
import java.nio.file.DirectoryStream;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import static java.nio.file.Files.*;
12+
13+
public class PathExaminer {
14+
15+
public Path fullPathForRequestPath(Path root, String requestPathString) {
16+
return root.resolve(requestPathString.substring(1));
17+
}
18+
19+
public Path getPath(String input) {
20+
String[] parts;
21+
parts = input.split("/");
22+
Path path = Paths.get("/", parts);
23+
return path;
24+
}
25+
26+
public boolean pathExists(Path path) {
27+
return exists(path);
28+
}
29+
30+
public boolean isDir(Path path) {
31+
return exists(path) && isDirectory(path);
32+
}
33+
34+
public boolean isFile(Path path) {
35+
return exists(path) && !isDirectory(path);
36+
}
37+
38+
public byte[] fileContents(Path file) {
39+
try {
40+
return readAllBytes(file);
41+
} catch (IOException e) {
42+
return new byte[0];
43+
}
44+
}
45+
46+
public Path[] directoryContents(Path dir) {
47+
List<Path> result = new ArrayList<>();
48+
try (DirectoryStream<Path> stream = newDirectoryStream(dir)) {
49+
for (Path entry: stream) {
50+
result.add(entry);
51+
}
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
return result.toArray(new Path[0]);
56+
}
57+
}

src/main/java/httpserver/fileutils/Files.java

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/main/java/httpserver/responder/GetResponder.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@
44
import httpserver.Request;
55
import httpserver.response.OkResponse;
66
import httpserver.response.Response;
7-
import httpserver.fileutils.Files;
7+
import httpserver.file.PathExaminer;
88

99
import java.nio.file.Path;
1010

11-
import static httpserver.fileutils.Html.linkString;
11+
import static httpserver.file.Html.linkString;
1212

1313
public class GetResponder implements Responder {
14+
15+
private final PathExaminer pathExaminer;
16+
17+
public GetResponder() {
18+
pathExaminer = new PathExaminer();
19+
}
20+
1421
@Override
1522
public Response respond(Path root, Request request) {
16-
Path path = Files.fullPathForRequestPath(root, request.getPath());
23+
Path path = pathExaminer.fullPathForRequestPath(root, request.getPath());
1724

18-
if (Files.pathExists(path)) {
19-
if (Files.isFile(path)) {
25+
if (pathExaminer.pathExists(path)) {
26+
if (pathExaminer.isFile(path)) {
2027
return responseForFile(path);
2128
} else {
2229
return responseForDir(root, path);
@@ -27,7 +34,7 @@ public Response respond(Path root, Request request) {
2734
}
2835

2936
private Response responseForDir(Path root, Path path) {
30-
Path[] paths = Files.directoryContents(path);
37+
Path[] paths = pathExaminer.directoryContents(path);
3138
String result = htmlLinksForContents(root, paths);
3239
return new OkResponse(result.getBytes());
3340
}
@@ -41,7 +48,7 @@ private String htmlLinksForContents(Path root, Path[] paths) {
4148
}
4249

4350
private Response responseForFile(Path path) {
44-
byte[] payload = Files.fileContents(path);
51+
byte[] payload = pathExaminer.fileContents(path);
4552
return new OkResponse(payload);
4653
}
4754

src/test/java/httpserver/SocketHandlerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.io.*;
66
import java.nio.file.Path;
77

8-
import static httpserver.fileutils.FileHelpers.tempDir;
9-
import static httpserver.fileutils.FileHelpers.tempFileOptions;
8+
import static httpserver.file.FileHelpers.tempDir;
9+
import static httpserver.file.FileHelpers.tempFileOptions;
1010
import static java.nio.file.Files.write;
1111
import static org.junit.Assert.*;
1212

src/test/java/httpserver/fileutils/FileHelpers.java renamed to src/test/java/httpserver/file/FileHelpers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpserver.fileutils;
1+
package httpserver.file;
22

33
import java.io.IOException;
44
import java.nio.file.Path;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package httpserver.file;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
9+
import static httpserver.file.FileHelpers.tempDir;
10+
import static java.nio.file.Files.exists;
11+
import static java.nio.file.Files.readAllBytes;
12+
import static org.junit.Assert.*;
13+
14+
public class FileOperatorTest {
15+
16+
private final Path path;
17+
private final Path dir;
18+
private FileOperator fileOperator;
19+
20+
public FileOperatorTest() throws IOException {
21+
dir = tempDir();
22+
path = Paths.get(dir.toString(), "/example.txt");
23+
fileOperator = new FileOperator();
24+
}
25+
26+
@Test
27+
public void createsFileAtPath() throws Exception {
28+
fileOperator.createFileAtPath(path);
29+
30+
assertTrue(exists(path));
31+
}
32+
33+
@Test
34+
public void deletesFileAtPath() throws Exception {
35+
fileOperator.createFileAtPath(path);
36+
fileOperator.deleteFileAtPath(path);
37+
38+
assertFalse(exists(path));
39+
}
40+
41+
@Test
42+
public void appendsToFileContentsAtPath() throws Exception {
43+
fileOperator.createFileAtPath(path);
44+
assertEquals("", fileContents(path));
45+
46+
fileOperator.appendToFile(path, "first line\r\n".getBytes());
47+
assertEquals("first line\r\n",
48+
fileContents(path));
49+
50+
fileOperator.appendToFile(path, "second line\r\n".getBytes());
51+
assertEquals("first line\r\nsecond line\r\n",
52+
fileContents(path));
53+
}
54+
55+
@Test
56+
public void replacesFileContentsAtPath() throws Exception {
57+
fileOperator.createFileAtPath(path);
58+
assertEquals("", fileContents(path));
59+
60+
fileOperator.replaceContents(path, "first line\r\n".getBytes());
61+
assertEquals("first line\r\n", fileContents(path));
62+
63+
fileOperator.replaceContents(path, "second line\r\n".getBytes());
64+
assertEquals("second line\r\n", fileContents(path));
65+
}
66+
67+
private String fileContents(Path path) throws IOException {
68+
return new String(readAllBytes(path));
69+
}
70+
}

src/test/java/httpserver/fileutils/HtmlTest.java renamed to src/test/java/httpserver/file/HtmlTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package httpserver.fileutils;
1+
package httpserver.file;
22

33
import org.junit.Test;
44

55
import java.io.IOException;
66
import java.nio.file.Path;
77
import java.nio.file.Paths;
88

9-
import static httpserver.fileutils.FileHelpers.tempDir;
10-
import static httpserver.fileutils.FileHelpers.tempDirOptions;
11-
import static httpserver.fileutils.FileHelpers.tempFileOptions;
9+
import static httpserver.file.FileHelpers.tempDir;
10+
import static httpserver.file.FileHelpers.tempDirOptions;
11+
import static httpserver.file.FileHelpers.tempFileOptions;
1212
import static org.junit.Assert.*;
1313

1414
public class HtmlTest {

0 commit comments

Comments
 (0)