<commit_before>package com.google.tripmeout.frontend.servlet;

import com.google.appengine.repackaged.com.google.api.client.util.Strings;
import com.google.common.flogger.FluentLogger;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.tripmeout.frontend.TripModel;
import com.google.tripmeout.frontend.error.TripAlreadyExistsException;
import com.google.tripmeout.frontend.storage.TripStorage;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import javax.inject.Inject;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TripParentServlet extends HttpServlet {
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  static final String STUB_USER_ID = "fakeUserId";

  private final Gson gson;
  private final TripStorage storage;

  @Inject
  public TripParentServlet(TripStorage storage, Gson gson) {
    this.gson = gson;
    this.storage = storage;
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String userId = getUserId();
    response.setContentType("application/json");
    response.setStatus(HttpServletResponse.SC_OK);
    PrintWriter writer = response.getWriter();
    writer.print(gson.toJson(storage.getAllUserTrips(userId)));
    writer.flush();
    writer.close();
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    logger.atInfo().log("received doPost");
    try {
      Optional<TripModel> requestTrip = extractFromRequestBody(request, gson, TripModel.class);

      if (requestTrip.isPresent()) {
        try {
          TripModel resolvedTrip = resolveDefaults(requestTrip.get());
          storage.addTrip(resolvedTrip);
          response.setContentType("application/json");
          PrintWriter writer = response.getWriter();
          writer.println(gson.toJson((resolvedTrip)));
          writer.flush();
          writer.close();
        } catch (TripAlreadyExistsException e) {
          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }

      } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      }
    } catch (IOException e) {
      logger.atWarning().withCause(e).log("Error serving post request");
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }

  private String getUserId() {
    // TODO(issues/66): Gte userId from Userservice
    return STUB_USER_ID;
  }

  private TripModel resolveDefaults(TripModel trip) {
    return trip.toBuilder().setId(UUID.randomUUID().toString()).setUserId(getUserId()).build();
  }

  // TODO: use servletutil onced pushed
  private static <T> Optional<T> extractFromRequestBody(
      HttpServletRequest request, Gson gson, Class<T> clazz) throws IOException {
    try (Reader reader = request.getReader()) {
      return Optional.ofNullable(gson.fromJson(reader, clazz));
    } catch (JsonParseException e) {
      logger.atWarning().withCause(e).log(
          "received exception while attempting to parse json from reader!");
      return Optional.empty();
    }
  }
}
<commit_msg>Use ServletUtil and remove unused imports<commit_after>package com.google.tripmeout.frontend.servlet;

import com.google.common.flogger.FluentLogger;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.tripmeout.frontend.TripModel;
import com.google.tripmeout.frontend.error.TripAlreadyExistsException;
import com.google.tripmeout.frontend.servlets.ServletUtil;
import com.google.tripmeout.frontend.storage.TripStorage;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Optional;
import java.util.UUID;
import javax.inject.Inject;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TripParentServlet extends HttpServlet {
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  static final String STUB_USER_ID = "fakeUserId";

  private final Gson gson;
  private final TripStorage storage;

  @Inject
  public TripParentServlet(TripStorage storage, Gson gson) {
    this.gson = gson;
    this.storage = storage;
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String userId = getUserId();
    response.setContentType("application/json");
    response.setStatus(HttpServletResponse.SC_OK);
    PrintWriter writer = response.getWriter();
    writer.print(gson.toJson(storage.getAllUserTrips(userId)));
    writer.flush();
    writer.close();
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    logger.atInfo().log("received doPost");
    try {
      Optional<TripModel> requestTrip =
          ServletUtil.extractFromRequestBody(request, gson, TripModel.class);

      if (requestTrip.isPresent()) {
        try {
          TripModel resolvedTrip = resolveDefaults(requestTrip.get());
          storage.addTrip(resolvedTrip);
          response.setContentType("application/json");
          PrintWriter writer = response.getWriter();
          writer.println(gson.toJson((resolvedTrip)));
          writer.flush();
          writer.close();
        } catch (TripAlreadyExistsException e) {
          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }

      } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      }
    } catch (IOException e) {
      logger.atWarning().withCause(e).log("Error serving post request");
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }

  private String getUserId() {
    // TODO(issues/66): Gte userId from Userservice
    return STUB_USER_ID;
  }

  private TripModel resolveDefaults(TripModel trip) {
    return trip.toBuilder().setId(UUID.randomUUID().toString()).setUserId(getUserId()).build();
  }
}

{'fraction_non_alphanumeric': 0.10405558248505413, 'fraction_numerical': 0.0006463079657456778, 'mean_word_length': 4.111478117258464, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3960373', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1777, 'n_words': 345}