-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDummyAPI.java
More file actions
116 lines (105 loc) · 3.45 KB
/
DummyAPI.java
File metadata and controls
116 lines (105 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
public class DummyAPI implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
HashMap<String, String> body = GetBody(exchange.getRequestBody());
int statusCode = 200;
String content = "";
if("GET".equals(exchange.getRequestMethod()))
{
File file = new File(body.get("path"));
if (!file.exists())
{
statusCode = 404;
content = "{ error: \"File not found!\" }";
}
else
{
content = new String(Files.readAllBytes(file.toPath()));
}
}
else if("POST".equals(exchange.getRequestMethod()))
{
File file = new File(body.get("path"));
if(file.exists())
{
content = "{ error: \"File already exists!\" }";
statusCode = 409;
}
else
{
Files.write(file.toPath(), body.get("content").getBytes(), StandardOpenOption.CREATE);
content = "{ success: \"File created!\" }";
}
}
else if ("PUT".equals(exchange.getRequestMethod()))
{
File file = new File(body.get("path"));
if(!file.exists())
{
content = "{ error: \"File not found!\" }";
statusCode = 404;
}
else
{
Files.write(file.toPath(), body.get("content").getBytes());
content = "{ success: \"File updated!\" }";
}
}
else if("PATCH".equals(exchange.getRequestMethod()))
{
File file = new File(body.get("path"));
if(!file.exists())
{
content = "{ error: \"File not found!\" }";
statusCode = 404;
}
else
{
Files.write(file.toPath(), body.get("content").getBytes(), StandardOpenOption.APPEND);
content = "{ success: \"File patched!\" }";
}
}
else if ("DELETE".equals(exchange.getRequestMethod()))
{
File file = new File(body.get("path"));
if(!file.exists())
{
content = "{ error: \"File not found!\" }";
statusCode = 404;
}
else
{
file.delete();
content = "{ success: \"File deleted!\" }";
}
}
else
{
statusCode = 405;
}
exchange.sendResponseHeaders(statusCode, content.length());
exchange.getResponseBody().write(content.getBytes());
exchange.close();
}
HashMap<String, String> GetBody(InputStream stream) throws IOException
{
String body = new String(stream.readAllBytes());
stream.close();
body = java.net.URLDecoder.decode(body, "UTF-8");
String[] pair = body.split("&");
HashMap<String, String> map = new HashMap<>();
for(String p : pair)
{
map.put(p.split("=")[0], p.split("=")[1]);
}
return map;
}
}