|
| 1 | +package ru.vk.itmo.test.volkovnikita; |
| 2 | + |
| 3 | +import one.nio.http.HttpServer; |
| 4 | +import one.nio.http.HttpServerConfig; |
| 5 | +import one.nio.http.HttpSession; |
| 6 | +import one.nio.http.Param; |
| 7 | +import one.nio.http.Path; |
| 8 | +import one.nio.http.Request; |
| 9 | +import one.nio.http.RequestMethod; |
| 10 | +import one.nio.http.Response; |
| 11 | +import one.nio.server.AcceptorConfig; |
| 12 | +import ru.vk.itmo.ServiceConfig; |
| 13 | +import ru.vk.itmo.dao.BaseEntry; |
| 14 | +import ru.vk.itmo.dao.Dao; |
| 15 | +import ru.vk.itmo.dao.Entry; |
| 16 | +import ru.vk.itmo.test.reference.dao.ReferenceDao; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.lang.foreign.MemorySegment; |
| 20 | +import java.lang.foreign.ValueLayout; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +public class HttpServerImpl extends HttpServer { |
| 24 | + |
| 25 | + private final Dao<MemorySegment, Entry<MemorySegment>> dao; |
| 26 | + |
| 27 | + static final Set<String> METHODS = Set.of( |
| 28 | + "GET", |
| 29 | + "PUT", |
| 30 | + "DELETE" |
| 31 | + ); |
| 32 | + |
| 33 | + public HttpServerImpl(ServiceConfig config, ReferenceDao dao) throws IOException { |
| 34 | + super(createServerConfig(config)); |
| 35 | + this.dao = dao; |
| 36 | + } |
| 37 | + |
| 38 | + private static HttpServerConfig createServerConfig(ServiceConfig config) { |
| 39 | + HttpServerConfig serverConfig = new HttpServerConfig(); |
| 40 | + AcceptorConfig acceptorConfig = new AcceptorConfig(); |
| 41 | + acceptorConfig.port = config.selfPort(); |
| 42 | + acceptorConfig.reusePort = true; |
| 43 | + |
| 44 | + serverConfig.acceptors = new AcceptorConfig[]{acceptorConfig}; |
| 45 | + serverConfig.closeSessions = true; |
| 46 | + return serverConfig; |
| 47 | + } |
| 48 | + |
| 49 | + @Path(value = "/v0/status") |
| 50 | + public Response status() { |
| 51 | + return Response.ok("It's okay (c) Tinkoff"); |
| 52 | + } |
| 53 | + |
| 54 | + @Path(value = "/v0/entity") |
| 55 | + @RequestMethod(Request.METHOD_GET) |
| 56 | + public Response getEntry(@Param(value = "id", required = true) String id) { |
| 57 | + if (isIdUncorrect(id)) { |
| 58 | + return new Response(Response.BAD_REQUEST, Response.EMPTY); |
| 59 | + } |
| 60 | + MemorySegment key = MemorySegment.ofArray(id.toCharArray()); |
| 61 | + Entry<MemorySegment> entry = dao.get(key); |
| 62 | + return entry == null ? new Response(Response.NOT_FOUND, Response.EMPTY) : |
| 63 | + new Response(Response.OK, entry.value().toArray(ValueLayout.JAVA_BYTE)); |
| 64 | + } |
| 65 | + |
| 66 | + @Path(value = "/v0/entity") |
| 67 | + @RequestMethod(Request.METHOD_PUT) |
| 68 | + public Response up(@Param(value = "id", required = true) String id, Request request) { |
| 69 | + if (isIdUncorrect(id)) { |
| 70 | + return new Response(Response.BAD_REQUEST, Response.EMPTY); |
| 71 | + } |
| 72 | + MemorySegment key = MemorySegment.ofArray(id.toCharArray()); |
| 73 | + MemorySegment value = MemorySegment.ofArray(request.getBody()); |
| 74 | + dao.upsert(new BaseEntry<>(key, value)); |
| 75 | + return new Response(Response.CREATED, Response.EMPTY); |
| 76 | + } |
| 77 | + |
| 78 | + @Path(value = "/v0/entity") |
| 79 | + @RequestMethod(Request.METHOD_DELETE) |
| 80 | + public Response deleteEntry(@Param(value = "id", required = true) String id) { |
| 81 | + if (isIdUncorrect(id)) { |
| 82 | + return new Response(Response.BAD_REQUEST, Response.EMPTY); |
| 83 | + } |
| 84 | + MemorySegment key = MemorySegment.ofArray(id.toCharArray()); |
| 85 | + dao.upsert(new BaseEntry<>(key, null)); |
| 86 | + return new Response(Response.ACCEPTED, Response.EMPTY); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void handleDefault(Request request, HttpSession session) throws IOException { |
| 91 | + Response response; |
| 92 | + if (METHODS.contains(request.getMethodName())) { |
| 93 | + response = new Response(Response.BAD_REQUEST, Response.EMPTY); |
| 94 | + session.sendResponse(response); |
| 95 | + } else { |
| 96 | + response = new Response(Response.METHOD_NOT_ALLOWED, Response.EMPTY); |
| 97 | + session.sendResponse(response); |
| 98 | + } |
| 99 | + session.sendResponse(response); |
| 100 | + } |
| 101 | + |
| 102 | + private boolean isIdUncorrect(String id) { |
| 103 | + return id == null || id.isEmpty() || id.isBlank(); |
| 104 | + } |
| 105 | +} |
0 commit comments