generated from yandex-praktikum/java-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseHttpHandler.java
More file actions
103 lines (86 loc) · 3.87 KB
/
BaseHttpHandler.java
File metadata and controls
103 lines (86 loc) · 3.87 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
package ru.yandex.task.manager.apimanagers;
import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import ru.yandex.task.manager.exception.IntersectionException;
import ru.yandex.task.manager.managers.TaskManager;
import ru.yandex.task.manager.model.Subtask;
import ru.yandex.task.manager.utils.GsonUtils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public abstract class BaseHttpHandler implements HttpHandler {
private static final String UPDATED_SUCCESSFULLY = "TASK UPDATED SUCCESSFULLY";
private static final String ADDED_SUCCESSFULLY = "SUBTASK ADDED SUCCESSFULLY";
private static final String INTERNAL_SERVER_ERROR = "Error occurred during request";
protected static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
protected final Gson gson = GsonUtils.getGson();
protected TaskManager manager;
public BaseHttpHandler(TaskManager manager) {
this.manager = manager;
}
protected void sendText(HttpExchange exchange, String text) throws IOException {
byte[] resp = text.getBytes(DEFAULT_CHARSET);
exchange.getResponseHeaders().add("Content-Type", "application/json;charset=utf-8");
exchange.sendResponseHeaders(200, resp.length);
exchange.getResponseBody().write(resp);
exchange.close();
}
protected void sendCode(HttpExchange h, int code) throws IOException {
h.sendResponseHeaders(code, 0);
h.close();
}
protected void sendBadRequest(HttpExchange exchange, String response) throws IOException {
byte[] responseBytes = response.getBytes(DEFAULT_CHARSET);
exchange.sendResponseHeaders(400, responseBytes.length);
exchange.getResponseBody().write(responseBytes);
}
protected void sendNotFound(HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(404, 0);
}
protected void sendMethodNotAllowed(HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(405, 0);
}
protected void sendHasInteractions(HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(406, -1);
exchange.getResponseBody().close();
}
protected void sendServerError(HttpExchange exchange) throws IOException {
byte[] responseBytes = INTERNAL_SERVER_ERROR.getBytes(DEFAULT_CHARSET);
exchange.sendResponseHeaders(500, responseBytes.length);
exchange.getResponseBody().write(responseBytes);
}
private void sendUpdatedSuccessfully(HttpExchange exchange) throws IOException {
byte[] responseBytes = UPDATED_SUCCESSFULLY.getBytes(DEFAULT_CHARSET);
exchange.sendResponseHeaders(201, responseBytes.length);
exchange.getResponseBody().write(responseBytes);
}
protected void sendUpdateResult(HttpExchange exchange, Subtask incomeSub) throws IOException {
try {
manager.updateSubtask(incomeSub);
sendUpdatedSuccessfully(exchange);
} catch (IntersectionException e) {
sendHasInteractions(exchange);
}
}
protected void sendAddResult(HttpExchange exchange, Subtask subtask) throws IOException {
try {
manager.addSubtask(subtask);
byte[] responseBytes = ADDED_SUCCESSFULLY.getBytes(DEFAULT_CHARSET);
exchange.sendResponseHeaders(201, responseBytes.length);
exchange.getResponseBody().write(responseBytes);
} catch (IntersectionException e) {
sendHasInteractions(exchange);
} catch (Exception e) {
sendBadRequest(exchange, e.getMessage());
}
}
protected int parseId(String path) {
String id = path.replaceAll("\\D*", "");
try {
return Integer.parseInt(id);
} catch (NumberFormatException e) {
return -1;
}
}
}