Consolidate Gson creation into a class

A Gson instance is created in three different places. Consolidate
them all into a single class so that we only have one instance, and
so that any changes to the way it's created only need to be done in
one place.

Change-Id: Id8cbc5c44114b43a50e3900c7ad9921b8c056744
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGson.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGson.java
new file mode 100644
index 0000000..66398fe
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGson.java
@@ -0,0 +1,49 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.lfs;
+
+import com.google.gson.FieldNamingPolicy;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonSyntaxException;
+import com.google.inject.Singleton;
+import java.io.Reader;
+
+@Singleton
+public class LfsGson {
+  private final Gson gson;
+
+  LfsGson() {
+    this.gson =
+        new GsonBuilder()
+            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
+            .disableHtmlEscaping()
+            .create();
+  }
+
+  public void toJson(Object src, Appendable writer) throws JsonIOException {
+    gson.toJson(src, writer);
+  }
+
+  public String toJson(Object src) {
+    return gson.toJson(src);
+  }
+
+  public <T> T fromJson(Reader json, Class<T> classOfT)
+      throws JsonSyntaxException, JsonIOException {
+    return gson.fromJson(json, classOfT);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsSshAuth.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsSshAuth.java
index 08d7ca7..d02403a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsSshAuth.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsSshAuth.java
@@ -19,9 +19,6 @@
 import com.google.gerrit.sshd.BaseCommand.Failure;
 import com.google.gerrit.sshd.BaseCommand.UnloggedFailure;
 import com.google.gerrit.sshd.plugin.LfsPluginAuthCommand;
-import com.google.gson.FieldNamingPolicy;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.google.inject.Singleton;
@@ -33,17 +30,16 @@
 public class LfsSshAuth implements LfsPluginAuthCommand.LfsSshPluginAuth {
   private final LfsSshRequestAuthorizer auth;
   private final String canonicalWebUrl;
-  private final Gson gson;
+  private final LfsGson gson;
 
   @Inject
-  LfsSshAuth(LfsSshRequestAuthorizer auth, @CanonicalWebUrl Provider<String> canonicalWebUrl) {
+  LfsSshAuth(
+      LfsSshRequestAuthorizer auth,
+      @CanonicalWebUrl Provider<String> canonicalWebUrl,
+      LfsGson gson) {
     this.auth = auth;
     this.canonicalWebUrl = canonicalWebUrl.get();
-    this.gson =
-        new GsonBuilder()
-            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
-            .disableHtmlEscaping()
-            .create();
+    this.gson = gson;
   }
 
   @Override
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksContext.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksContext.java
index 7ac56c7..5e4094c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksContext.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksContext.java
@@ -21,9 +21,7 @@
 import com.google.common.base.Supplier;
 import com.google.common.base.Suppliers;
 import com.google.common.flogger.FluentLogger;
-import com.google.gson.FieldNamingPolicy;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
+import com.googlesource.gerrit.plugins.lfs.LfsGson;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.IOException;
@@ -43,9 +41,9 @@
   private final HttpServletResponse res;
   private final Supplier<Writer> writer;
   private final Supplier<Reader> reader;
-  private final Gson gson;
+  private final LfsGson gson;
 
-  LfsLocksContext(final HttpServletRequest req, final HttpServletResponse res) {
+  LfsLocksContext(LfsGson gson, final HttpServletRequest req, final HttpServletResponse res) {
     this.path = req.getPathInfo().startsWith("/") ? req.getPathInfo() : "/" + req.getPathInfo();
     this.req = req;
     this.res = res;
@@ -73,7 +71,7 @@
                 }
               }
             });
-    this.gson = createGson();
+    this.gson = gson;
     setLfsResponseType();
   }
 
@@ -118,13 +116,6 @@
     res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON);
   }
 
-  private Gson createGson() {
-    return new GsonBuilder()
-        .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
-        .disableHtmlEscaping()
-        .create();
-  }
-
   /** copied from org.eclipse.jgit.lfs.server.LfsProtocolServlet.Error */
   static class Error {
     String message;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksServlet.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksServlet.java
index 9cdeb62..1e6146d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksServlet.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksServlet.java
@@ -20,6 +20,7 @@
 
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
+import com.googlesource.gerrit.plugins.lfs.LfsGson;
 import java.io.IOException;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -35,24 +36,27 @@
 
   private final LfsGetLocksAction.Factory getters;
   private final LfsPutLocksAction.Factory putters;
+  private final LfsGson gson;
 
   @Inject
-  LfsLocksServlet(LfsGetLocksAction.Factory getters, LfsPutLocksAction.Factory putters) {
+  LfsLocksServlet(
+      LfsGetLocksAction.Factory getters, LfsPutLocksAction.Factory putters, LfsGson gson) {
     this.getters = getters;
     this.putters = putters;
+    this.gson = gson;
   }
 
   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
-    LfsLocksContext context = new LfsLocksContext(req, resp);
+    LfsLocksContext context = new LfsLocksContext(gson, req, resp);
     getters.create(context).run();
   }
 
   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
-    LfsLocksContext context = new LfsLocksContext(req, resp);
+    LfsLocksContext context = new LfsLocksContext(gson, req, resp);
     putters.create(context).run();
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java
index 4276fba..4526fa4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java
@@ -19,12 +19,10 @@
 import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.CurrentUser;
-import com.google.gson.FieldNamingPolicy;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 import com.googlesource.gerrit.plugins.lfs.LfsDateTime;
+import com.googlesource.gerrit.plugins.lfs.LfsGson;
 import com.googlesource.gerrit.plugins.lfs.locks.LfsLocksHandler.LfsLockExistsException;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
@@ -44,11 +42,7 @@
   }
 
   private static final FluentLogger log = FluentLogger.forEnclosingClass();
-  private static final Gson gson =
-      new GsonBuilder()
-          .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
-          .disableHtmlEscaping()
-          .create();
+  private final LfsGson gson;
   private final PathToLockId toLockId;
   private final String project;
   private final Path locksPath;
@@ -56,7 +50,11 @@
 
   @Inject
   LfsProjectLocks(
-      PathToLockId toLockId, LfsLocksPathProvider locksPath, @Assisted Project.NameKey project) {
+      LfsGson gson,
+      PathToLockId toLockId,
+      LfsLocksPathProvider locksPath,
+      @Assisted Project.NameKey project) {
+    this.gson = gson;
     this.toLockId = toLockId;
     this.project = project.get();
     this.locksPath = Paths.get(locksPath.get(), this.project);