generated from yandex-praktikum/java-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
Реализовал HTTP API #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VectorID1
wants to merge
2
commits into
main
Choose a base branch
from
sprint_9-solution-http-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
id,type,name,discribtion,status,startTime,durations,endTime,epic | ||
1,TASK,nameTask1,discriprionTask1,NEW, | ||
2,TASK,nameTask2,discriprionTask2,NEW, | ||
3,TASK,nameTaskTime1,discriptionTaskTime1,NEW,2025-10-15T18:45,50,2025-10-15T19:35, | ||
1,TASK,nameTaskTime1,discriptionTaskTime1,NEW,2025-10-15T18:45,50,2025-10-15T19:35, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
id,type,name,discribtion,status,startTime,durations,endTime,epic | ||
1,TASK,nameTask1,discriprionTask1,NEW | ||
2,TASK,nameTask2,discriprionTask2,NEW |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
src/manager/ManagerSaveException.java → src/exeptions/ManagerSaveException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package manager; | ||
package exeptions; | ||
|
||
|
||
public class ManagerSaveException extends RuntimeException { | ||
ManagerSaveException(String messege) { | ||
public ManagerSaveException(String messege) { | ||
super(messege); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package exeptions; | ||
|
||
public class NotFoundExeption extends Exception { | ||
public NotFoundExeption(String massage) { | ||
super(massage); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package exeptions; | ||
|
||
public class TimeConflictExeption extends RuntimeException { | ||
public TimeConflictExeption(String message) { | ||
super(message); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package http; | ||
|
||
import com.google.gson.*; | ||
import com.sun.net.httpserver.HttpServer; | ||
import exeptions.NotFoundExeption; | ||
import http.handler.*; | ||
import http.handler.adapter.DurationAdapter; | ||
import http.handler.adapter.LocalDateTimeAdapter; | ||
import manager.Managers; | ||
import manager.TaskManager; | ||
import model.Status; | ||
import model.Task; | ||
import model.TypeTask; | ||
|
||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.time.LocalDateTime; | ||
|
||
public class HttpTaskServer { | ||
private static final int PORT = 8080; | ||
private HttpServer server; | ||
protected TaskManager taskManager; | ||
protected Gson gson; | ||
|
||
public HttpTaskServer(TaskManager taskManager) throws IOException { | ||
this.taskManager = taskManager; | ||
this.gson = new GsonBuilder() | ||
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()) | ||
.registerTypeAdapter(long.class, new DurationAdapter()) | ||
.create(); | ||
|
||
this.server = HttpServer.create(new InetSocketAddress(PORT), 0); | ||
server.createContext("/tasks", new TaskHandler(taskManager, gson)); | ||
VectorID1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
server.createContext("/subtasks", new SubtaskHandler(taskManager, gson)); | ||
server.createContext("/epics", new EpicsHandler(taskManager, gson)); | ||
server.createContext("/history", new HistoryHandler(taskManager, gson)); | ||
server.createContext("/priorityzed", new PriorityHandler(taskManager, gson)); | ||
|
||
} | ||
|
||
public Gson getGson() { | ||
return this.gson; | ||
} | ||
|
||
|
||
public void start() { | ||
server.start(); | ||
System.out.println("Сервер запущен на порте " + PORT); | ||
|
||
|
||
} | ||
|
||
public void stop() { | ||
server.stop(1); | ||
System.out.println("Сервер остановлен."); | ||
} | ||
|
||
public static void main(String[] args) throws IOException, NotFoundExeption { | ||
|
||
TaskManager manager = Managers.getDefault(); | ||
new HttpTaskServer(manager).start(); | ||
Task task = new Task(1, TypeTask.TASK,"name","dis", Status.NEW); | ||
manager.addTask(task); | ||
} | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package http.handler; | ||
|
||
import com.google.gson.*; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import manager.TaskManager; | ||
|
||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
|
||
public abstract class BaseHttpHandler implements HttpHandler { | ||
protected TaskManager taskManager; | ||
protected Gson gson; | ||
|
||
|
||
public BaseHttpHandler(TaskManager taskManager, Gson gson) { | ||
this.taskManager = taskManager; | ||
this.gson = gson; | ||
} | ||
|
||
public void sendText(HttpExchange exchange, String json, int statusCode) throws IOException { | ||
byte[] responce = json.getBytes(StandardCharsets.UTF_8); | ||
exchange.getResponseHeaders().add("Content-Type", "application/json"); | ||
exchange.sendResponseHeaders(statusCode, responce.length); | ||
try (OutputStream os = exchange.getResponseBody()) { | ||
os.write(responce); | ||
} | ||
} | ||
|
||
public void sendNotFound(HttpExchange exchange, String text) throws IOException { | ||
byte[] responce = text.getBytes(StandardCharsets.UTF_8); | ||
exchange.getResponseHeaders().add("Content-Type", "application/json"); | ||
exchange.sendResponseHeaders(404, responce.length); | ||
try (OutputStream os = exchange.getResponseBody()) { | ||
os.write(responce); | ||
} | ||
} | ||
|
||
public void sendHashInteraction(HttpExchange exchange, String text) throws IOException { | ||
byte[] responce = text.getBytes(StandardCharsets.UTF_8); | ||
exchange.getResponseHeaders().add("Content-Type", "application/json"); | ||
exchange.sendResponseHeaders(406, responce.length); | ||
try (OutputStream os = exchange.getResponseBody()) { | ||
os.write(responce); | ||
} | ||
} | ||
|
||
public Optional<Integer> parseId(String path) { | ||
try { | ||
String[] parts = path.split("/"); | ||
return Optional.of(Integer.parseInt(parts[2])); | ||
} catch (Exception e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
public String requestMethod(HttpExchange exchange) { | ||
return exchange.getRequestMethod(); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.