Пример #1
0
 private Response doGet(HttpExchange httpExchange) {
   String query = httpExchange.getRequestURI().getQuery();
   if (query == null) {
     return Response.badRequest("Absent query in request");
   }
   Map<String, String> map = queryToMap(query);
   String token = map.get(Constants.REQUEST_PARAM_TOKEN);
   if (StringUtils.isEmpty(token)) {
     return Response.badRequest("Token query parameter is required");
   }
   try {
     int index = MessageHelper.parseToken(token);
     if (index > messageStorage.size()) {
       return Response.badRequest(
           String.format(
               "Incorrect token in request: %s. Server does not have so many messages", token));
     }
     Portion portion = new Portion(index);
     List<Message> messages = messageStorage.getPortion(portion);
     String responseBody = MessageHelper.buildServerResponseBody(messages, messageStorage.size());
     return Response.ok(responseBody);
   } catch (InvalidTokenException e) {
     return Response.badRequest(e.getMessage());
   }
 }
Пример #2
0
 private Response doDelete(HttpExchange httpExchange) {
   String query = httpExchange.getRequestURI().getQuery();
   if (query == null) {
     return Response.badRequest("Absent query in request");
   }
   Map<String, String> map = queryToMap(query);
   String messageId = map.get(Constants.REQUEST_PARAM_MESSAGE_ID);
   if (StringUtils.isEmpty(messageId)) {
     return Response.badRequest("Message id query parameter is required");
   }
   if (messageStorage.removeMessage(messageId)) {
     return Response.ok();
   }
   return Response.badRequest("This message does not exist");
 }