Add JSON endpoint for file data

Change-Id: I86d4fd850b897a0ae8cb813f3e57c615ba050ee6
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/FileJsonData.java b/gitiles-servlet/src/main/java/com/google/gitiles/FileJsonData.java
new file mode 100644
index 0000000..4409bde
--- /dev/null
+++ b/gitiles-servlet/src/main/java/com/google/gitiles/FileJsonData.java
@@ -0,0 +1,39 @@
+// Copyright (C) 2017 Google Inc. All Rights Reserved.
+//
+// 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.google.gitiles;
+
+import java.io.IOException;
+import org.eclipse.jgit.lib.ObjectId;
+
+class FileJsonData {
+  static class File {
+    String id;
+    String repo;
+    String revision;
+    String path;
+  }
+
+  static File toJsonData(ObjectId id, String repo, String revision, String path)
+      throws IOException {
+    File file = new File();
+    file.id = id.name();
+    file.repo = repo;
+    file.revision = revision;
+    file.path = path;
+    return file;
+  }
+
+  private FileJsonData() {}
+}
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/PathServlet.java b/gitiles-servlet/src/main/java/com/google/gitiles/PathServlet.java
index 804d5b3..629985d 100644
--- a/gitiles-servlet/src/main/java/com/google/gitiles/PathServlet.java
+++ b/gitiles-servlet/src/main/java/com/google/gitiles/PathServlet.java
@@ -251,14 +251,32 @@
 
     try (RevWalk rw = new RevWalk(repo);
         WalkResult wr = WalkResult.forPath(rw, view, recursive)) {
-      if (wr == null || wr.type != FileType.TREE) {
+      if (wr == null) {
         res.setStatus(SC_NOT_FOUND);
-      } else {
-        renderJson(
-            req,
-            res,
-            TreeJsonData.toJsonData(wr.id, wr.tw, includeSizes, recursive),
-            TreeJsonData.Tree.class);
+        return;
+      }
+      switch (wr.type) {
+        case REGULAR_FILE:
+          renderJson(
+              req,
+              res,
+              FileJsonData.toJsonData(
+                  wr.id,
+                  view.getRepositoryName(),
+                  view.getRevision().getName(),
+                  wr.path),
+              FileJsonData.File.class);
+          break;
+        case TREE:
+          renderJson(
+              req,
+              res,
+              TreeJsonData.toJsonData(wr.id, wr.tw, includeSizes, recursive),
+              TreeJsonData.Tree.class);
+          break;
+        default:
+          res.setStatus(SC_NOT_FOUND);
+          break;
       }
     } catch (LargeObjectException e) {
       res.setStatus(SC_INTERNAL_SERVER_ERROR);
diff --git a/gitiles-servlet/src/test/java/com/google/gitiles/PathServletTest.java b/gitiles-servlet/src/test/java/com/google/gitiles/PathServletTest.java
index a32567e..7d6f737 100644
--- a/gitiles-servlet/src/test/java/com/google/gitiles/PathServletTest.java
+++ b/gitiles-servlet/src/test/java/com/google/gitiles/PathServletTest.java
@@ -20,6 +20,7 @@
 
 import com.google.common.io.BaseEncoding;
 import com.google.common.net.HttpHeaders;
+import com.google.gitiles.FileJsonData.File;
 import com.google.gitiles.TreeJsonData.Tree;
 import com.google.template.soy.data.SoyListData;
 import com.google.template.soy.data.restricted.StringData;
@@ -158,6 +159,19 @@
   }
 
   @Test
+  public void fileJson() throws Exception {
+    RevBlob blob = repo.blob("contents");
+    repo.branch("master").commit().add("path/to/file", blob).create();
+
+    File file = buildJson(File.class, "/repo/+/master/path/to/file");
+
+    assertThat(file.id).isEqualTo(blob.name());
+    assertThat(file.repo).isEqualTo("repo");
+    assertThat(file.revision).isEqualTo("master");
+    assertThat(file.path).isEqualTo("path/to/file");
+  }
+
+  @Test
   public void symlinkText() throws Exception {
     final RevBlob link = repo.blob("foo");
     repo.branch("master")