|
| 1 | +package ru.yandex.javacource.golotin.schedule.server; |
| 2 | + |
| 3 | +import com.google.gson.*; |
| 4 | +import com.sun.net.httpserver.HttpExchange; |
| 5 | +import ru.yandex.javacource.golotin.schedule.exception.ManagerSaveException; |
| 6 | +import ru.yandex.javacource.golotin.schedule.exception.NotFoundException; |
| 7 | +import ru.yandex.javacource.golotin.schedule.model.*; |
| 8 | +import ru.yandex.javacource.golotin.schedule.service.TaskManager; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | + |
| 14 | +public class HttpHandler implements com.sun.net.httpserver.HttpHandler { |
| 15 | + TaskManager manager; |
| 16 | + TaskHandler taskHandler; |
| 17 | + EndpointHandler endpointHandler; |
| 18 | + public HttpHandler(TaskManager manager) { |
| 19 | + this.manager = manager; |
| 20 | + this.taskHandler = new TaskHandler(); |
| 21 | + this.endpointHandler = new EndpointHandler(); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public void handle(HttpExchange h) throws IOException { |
| 26 | + GsonBuilder gsonBuilder = new GsonBuilder(); |
| 27 | + gsonBuilder.setPrettyPrinting(); |
| 28 | + Gson gson = gsonBuilder.create(); |
| 29 | + Endpoint endpoint = endpointHandler.getEndpoint(h.getRequestURI().getPath(), h.getRequestMethod()); |
| 30 | + final String path = h.getRequestURI().getPath(); |
| 31 | + switch (endpoint) { |
| 32 | + case GET_TASKS: |
| 33 | + if (path.equals("/tasks")) { |
| 34 | + sendText(h, gson.toJson(manager.getTasks().toString())); |
| 35 | + break; |
| 36 | + }else if(path.equals("/epics")) { |
| 37 | + sendText(h, gson.toJson(manager.getEpics().toString())); |
| 38 | + break; |
| 39 | + } |
| 40 | + case GET_BY_ID: |
| 41 | + try { |
| 42 | + int id = Integer.parseInt(h.getRequestURI().getPath().split("/")[2]); |
| 43 | + if(path.equals("/tasks/"+ id)){ |
| 44 | + sendText(h, gson.toJson(manager.getTask(id).toString())); |
| 45 | + break; |
| 46 | + }else if(path.equals("/epics/"+ id)){ |
| 47 | + sendText(h, gson.toJson(manager.getEpic(id).toString())); |
| 48 | + break; |
| 49 | + }else if(path.equals("/subtasks/"+ id)){ |
| 50 | + sendText(h, gson.toJson(manager.getSubtask(id).toString())); |
| 51 | + break; |
| 52 | + } |
| 53 | + } catch (NotFoundException e) { |
| 54 | + sendNotFound(h, "Данной задачи не существует по id: " + h.getRequestURI().getPath().split("/")[2]); |
| 55 | + break; |
| 56 | + } |
| 57 | + case GET_EPICS_ID_SUBTASKS: |
| 58 | + try { |
| 59 | + sendText(h, gson.toJson(manager.getEpicSubtasks(Integer.parseInt(h.getRequestURI().getPath().split("/")[2])).toString())); |
| 60 | + break; |
| 61 | + } catch (NotFoundException e) { |
| 62 | + sendNotFound(h, "Данного эпика не существует по id: " + e.getMessage()); |
| 63 | + break; |
| 64 | + } |
| 65 | + case GET_HISTORY: |
| 66 | + sendText(h, gson.toJson(manager.getHistory().toString())); |
| 67 | + break; |
| 68 | + case GET_PRIORITIZED: |
| 69 | + sendText(h, gson.toJson(manager.getPrioritizedTasks().toString())); |
| 70 | + break; |
| 71 | + case POST: |
| 72 | + convertTask(h); |
| 73 | + break; |
| 74 | + case DELETE_BY_ID: |
| 75 | + try { |
| 76 | + int id = Integer.parseInt(h.getRequestURI().getPath().split("/")[2]); |
| 77 | + if(path.equals("/tasks/"+id)) { |
| 78 | + manager.deleteTask(id); |
| 79 | + sendText(h,""); |
| 80 | + break; |
| 81 | + } else if(path.equals("/epics/"+id)) { |
| 82 | + manager.deleteEpic(id); |
| 83 | + sendText(h,""); |
| 84 | + break; |
| 85 | + }else if(path.equals("/subtasks/"+id)) { |
| 86 | + manager.deleteSubtask(id); |
| 87 | + sendText(h,""); |
| 88 | + break; |
| 89 | + } |
| 90 | + } catch (NotFoundException e) { |
| 91 | + sendNotFound(h, "Данной задачи не существует по id: " + e.getMessage()); |
| 92 | + break; |
| 93 | + } |
| 94 | + default: |
| 95 | + errorServer(h); |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + protected void sendText(HttpExchange h, String text) throws IOException { |
| 101 | + try (h) { |
| 102 | + byte[] resp = text.getBytes(StandardCharsets.UTF_8); |
| 103 | + h.getResponseHeaders().add("Content-Type", "application/json;charset=utf-8"); |
| 104 | + h.sendResponseHeaders(200, resp.length); |
| 105 | + h.getResponseBody().write(resp); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + protected void sendNotFound(HttpExchange h, String text) throws IOException { |
| 110 | + try (h) { |
| 111 | + byte[] resp = text.getBytes(StandardCharsets.UTF_8); |
| 112 | + h.getResponseHeaders().add("Content-Type", "application/json;charset=utf-8"); |
| 113 | + h.sendResponseHeaders(404, resp.length); |
| 114 | + h.getResponseBody().write(resp); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + protected void updateText(HttpExchange h) throws IOException { |
| 119 | + try (h) { |
| 120 | + h.getResponseHeaders().add("Content-Type", "application/json;charset=utf-8"); |
| 121 | + h.sendResponseHeaders(201, 0); |
| 122 | + } |
| 123 | + } |
| 124 | + protected void errorServer(HttpExchange h) throws IOException { |
| 125 | + try (h) { |
| 126 | + h.getResponseHeaders().add("Content-Type", "application/json;charset=utf-8"); |
| 127 | + h.sendResponseHeaders(500, 0); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + protected void sendHasInteractions(HttpExchange h, String text) throws IOException { |
| 132 | + try (h) { |
| 133 | + byte[] resp = text.getBytes(StandardCharsets.UTF_8); |
| 134 | + h.getResponseHeaders().add("Content-Type", "application/json;charset=utf-8"); |
| 135 | + h.sendResponseHeaders(406, resp.length); |
| 136 | + h.getResponseBody().write(resp); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + private void convertTask(HttpExchange h) throws IOException { |
| 142 | + InputStream inputStream = h.getRequestBody(); |
| 143 | + final String path = h.getRequestURI().getPath(); |
| 144 | + final String[] body = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8).split(","); |
| 145 | + Task task = taskHandler.taskFromString(body); |
| 146 | + if (path.equals("/tasks")) { |
| 147 | + if (body[0].split("=")[0].equals("id")) { |
| 148 | + try { |
| 149 | + manager.updateTask(task); |
| 150 | + updateText(h); |
| 151 | + } catch (ManagerSaveException e) { |
| 152 | + sendHasInteractions(h, e.getMessage()); |
| 153 | + } |
| 154 | + } else { |
| 155 | + try { |
| 156 | + manager.createTask(task); |
| 157 | + updateText(h); |
| 158 | + } catch (ManagerSaveException e) { |
| 159 | + sendHasInteractions(h, e.getMessage()); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + if (path.equals("/epics")) { |
| 164 | + if (body[0].split("=")[0].equals("id")) { |
| 165 | + int taskId = Integer.parseInt(body[0].split("=")[1]); |
| 166 | + try { |
| 167 | + manager.updateEpic(new Epic(task.getId(), task.getName(), task.getDescription(), task.getStatus(), task.getStartTime(), task.getDuration())); |
| 168 | + updateText(h); |
| 169 | + } catch (ManagerSaveException e) { |
| 170 | + sendHasInteractions(h, e.getMessage()); |
| 171 | + } |
| 172 | + } else { |
| 173 | + try { |
| 174 | + manager.createEpic(new Epic(task.getName(), task.getStatus(), task.getDescription(), task.getStartTime(), task.getDuration())); |
| 175 | + updateText(h); |
| 176 | + } catch (ManagerSaveException e) { |
| 177 | + sendHasInteractions(h, e.getMessage()); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + if (path.equals("/subtasks")) { |
| 182 | + final int epicId = Integer.parseInt(body[6].split("=")[1]); |
| 183 | + if (body[0].split("=")[0].equals("id")) { |
| 184 | + int taskId = Integer.parseInt(body[0].split("=")[1]); |
| 185 | + try { |
| 186 | + manager.updateSubtask(new Subtask(task.getId(), task.getName(), task.getDescription(), task.getStatus(), task.getStartTime(), task.getDuration(), epicId)); |
| 187 | + updateText(h); |
| 188 | + } catch (ManagerSaveException e) { |
| 189 | + sendHasInteractions(h, e.getMessage()); |
| 190 | + } |
| 191 | + } else { |
| 192 | + try { |
| 193 | + manager.createSubtask(new Subtask(task.getName(), task.getStatus(), task.getDescription(), task.getStartTime(), task.getDuration(), epicId)); |
| 194 | + updateText(h); |
| 195 | + } catch (ManagerSaveException e) { |
| 196 | + sendHasInteractions(h, e.getMessage()); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments