|
| 1 | +package ru.vk.itmo.test.andreycheshev; |
| 2 | + |
| 3 | +import one.nio.http.HttpServer; |
| 4 | +import one.nio.http.HttpServerConfig; |
| 5 | +import one.nio.http.HttpSession; |
| 6 | +import one.nio.http.Request; |
| 7 | +import one.nio.http.Response; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | +import ru.vk.itmo.dao.BaseEntry; |
| 11 | +import ru.vk.itmo.dao.Config; |
| 12 | +import ru.vk.itmo.dao.Entry; |
| 13 | +import ru.vk.itmo.test.andreycheshev.dao.PersistentReferenceDao; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.UncheckedIOException; |
| 17 | +import java.lang.foreign.MemorySegment; |
| 18 | +import java.lang.foreign.ValueLayout; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | + |
| 21 | +import static one.nio.http.Request.METHOD_DELETE; |
| 22 | +import static one.nio.http.Request.METHOD_GET; |
| 23 | +import static one.nio.http.Request.METHOD_PUT; |
| 24 | +import static one.nio.http.Response.ACCEPTED; |
| 25 | +import static one.nio.http.Response.BAD_REQUEST; |
| 26 | +import static one.nio.http.Response.CREATED; |
| 27 | +import static one.nio.http.Response.INTERNAL_ERROR; |
| 28 | +import static one.nio.http.Response.METHOD_NOT_ALLOWED; |
| 29 | +import static one.nio.http.Response.NOT_FOUND; |
| 30 | +import static one.nio.http.Response.OK; |
| 31 | + |
| 32 | +public class ServerImpl extends HttpServer { |
| 33 | + private static final Logger logger = LoggerFactory.getLogger(ServerImpl.class); |
| 34 | + private static final String REQUEST_PATH = "/v0/entity"; |
| 35 | + private static final String ID = "id="; |
| 36 | + |
| 37 | + private final PersistentReferenceDao dao; |
| 38 | + |
| 39 | + public ServerImpl(HttpServerConfig config, Config daoConfig) throws IOException { |
| 40 | + super(config); |
| 41 | + |
| 42 | + this.dao = new PersistentReferenceDao(daoConfig); |
| 43 | + } |
| 44 | + |
| 45 | + private Response get(final Request request) { |
| 46 | + String id = request.getParameter(ID); |
| 47 | + if (id == null || id.isEmpty()) { |
| 48 | + return new Response(BAD_REQUEST, Response.EMPTY); |
| 49 | + } |
| 50 | + |
| 51 | + Entry<MemorySegment> entry = dao.get(fromString(id)); |
| 52 | + |
| 53 | + return (entry == null) |
| 54 | + ? new Response(NOT_FOUND, Response.EMPTY) |
| 55 | + : new Response(OK, entry.value().toArray(ValueLayout.JAVA_BYTE)); |
| 56 | + } |
| 57 | + |
| 58 | + private Response put(final Request request) { |
| 59 | + String id = request.getParameter(ID); |
| 60 | + if (id == null || id.isEmpty()) { |
| 61 | + return new Response(BAD_REQUEST, Response.EMPTY); |
| 62 | + } |
| 63 | + |
| 64 | + Entry<MemorySegment> entry = new BaseEntry<>( |
| 65 | + fromString(id), |
| 66 | + MemorySegment.ofArray(request.getBody()) |
| 67 | + ); |
| 68 | + dao.upsert(entry); |
| 69 | + |
| 70 | + return new Response(CREATED, Response.EMPTY); |
| 71 | + } |
| 72 | + |
| 73 | + private Response delete(final Request request) { |
| 74 | + String id = request.getParameter(ID); |
| 75 | + if (id == null || id.isEmpty()) { |
| 76 | + return new Response(BAD_REQUEST, Response.EMPTY); |
| 77 | + } |
| 78 | + |
| 79 | + Entry<MemorySegment> entry = new BaseEntry<>(fromString(id), null); |
| 80 | + dao.upsert(entry); |
| 81 | + |
| 82 | + return new Response(ACCEPTED, Response.EMPTY); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void handleRequest(Request request, HttpSession session) { |
| 87 | + String path = request.getPath(); |
| 88 | + if (!path.equals(REQUEST_PATH)) { |
| 89 | + sendResponse( |
| 90 | + new Response(BAD_REQUEST, Response.EMPTY), |
| 91 | + session |
| 92 | + ); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + int method = request.getMethod(); |
| 97 | + |
| 98 | + Response response; |
| 99 | + try { |
| 100 | + response = switch (method) { |
| 101 | + case METHOD_GET -> get(request); |
| 102 | + case METHOD_PUT -> put(request); |
| 103 | + case METHOD_DELETE -> delete(request); |
| 104 | + default -> new Response(METHOD_NOT_ALLOWED, Response.EMPTY); |
| 105 | + }; |
| 106 | + } catch (Exception e) { |
| 107 | + logger.error("Internal error of the DAO operation", e); |
| 108 | + sendResponse(new Response(INTERNAL_ERROR, Response.EMPTY), session); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + sendResponse(response, session); |
| 113 | + } |
| 114 | + |
| 115 | + private void sendResponse(Response response, HttpSession session) { |
| 116 | + try { |
| 117 | + session.sendResponse(response); |
| 118 | + } catch (IOException e) { |
| 119 | + throw new UncheckedIOException("Error while sending response to the client", e); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private MemorySegment fromString(String data) { |
| 124 | + return MemorySegment.ofArray(data.getBytes(StandardCharsets.UTF_8)); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public synchronized void stop() { |
| 129 | + super.stop(); |
| 130 | + try { |
| 131 | + dao.close(); |
| 132 | + } catch (IOException e) { |
| 133 | + throw new UncheckedIOException(e); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments