Пример #1
0
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    Long id = getId(req);
    if (id == null) {
      resp.setStatus(SC_BAD_REQUEST);
      return;
    }

    Movie movie;
    try {
      movie = gson.fromJson(req.getReader(), Movie.class);
    } catch (Exception ignore) {
      resp.setStatus(SC_BAD_REQUEST);
      return;
    }

    if (!isMovieValid(movie) || movie.getId() != null && !movie.getId().equals(id)) {
      resp.setStatus(SC_BAD_REQUEST);
      return;
    }
    movie.setId(id);
    Movie prevMovieVersion = service.updateMovie(movie);
    if (prevMovieVersion == null) {
      resp.setStatus(SC_NOT_FOUND);
    }
  }
Пример #2
0
  @Override
  protected void doPut(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    Movie movie;
    try {
      movie = gson.fromJson(req.getReader(), Movie.class);
    } catch (Exception ignore) {
      resp.setStatus(SC_BAD_REQUEST);
      return;
    }

    if (!isMovieValid(movie) || movie.getId() != null) {
      resp.setStatus(SC_BAD_REQUEST);
      return;
    }
    Long id = service.addMovie(movie);

    resp.setContentType("application/json");
    resp.setCharacterEncoding("UTF-8");

    JsonWriter jsonWriter = gson.newJsonWriter(resp.getWriter());
    jsonWriter.beginObject().name("id").value(id).endObject();
    jsonWriter.close();
  }
Пример #3
0
 private boolean isMovieValid(Movie movie) {
   return !isBlank(movie.getTitle())
       && !isEmpty(movie.getLocations())
       && isLocationsValid(movie.getLocations());
 }