Add web API support for branch and tag operations

Change-Id: I40be9c1f9dd6ff91919cc12c067fe5ed82f64003
diff --git a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/BranchManifestServlet.java b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/BranchManifestServlet.java
new file mode 100644
index 0000000..4245db1
--- /dev/null
+++ b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/BranchManifestServlet.java
@@ -0,0 +1,68 @@
+// Copyright (C) 2016 Advanced Micro Devices, 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.amd.gerrit.plugins.manifestsubscription;
+
+import com.google.gerrit.extensions.annotations.Export;
+import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@Export("/branch")
+@Singleton
+public class BranchManifestServlet extends HttpServlet {
+
+  @Inject
+  private GitRepositoryManager gitRepoManager;
+
+  protected void doPost(HttpServletRequest req, HttpServletResponse res)
+                                                            throws IOException {
+
+    Map<String, String[]> input = req.getParameterMap();
+
+    if (inputValid(req)) {
+      res.setContentType("application/json");
+      res.setCharacterEncoding("UTF-8");
+
+      Utilities.branchManifest(gitRepoManager,
+                                input.get("manifest-repo")[0],
+                                input.get("manifest-commit-ish")[0],
+                                input.get("manifest-path")[0],
+                                input.get("new-branch")[0],
+                                res.getWriter(), null, true);
+
+    } else {
+      res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+    }
+  }
+
+  private boolean inputValid(HttpServletRequest request) {
+    Map<String, String[]> input = request.getParameterMap();
+    if(input.containsKey("manifest-repo") &&
+        input.containsKey("manifest-commit-ish") &&
+        input.containsKey("manifest-path") &&
+        input.containsKey("new-branch")) {
+      return true;
+    }
+
+    return false;
+  }
+}
diff --git a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/HttpModule.java b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/HttpModule.java
index d460828..43514a0 100644
--- a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/HttpModule.java
+++ b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/HttpModule.java
@@ -19,6 +19,9 @@
 class HttpModule extends ServletModule {
   @Override
   protected void configureServlets() {
-    // TODO
+    serve("/show").with(ShowSubscriptionServlet.class);
+    serve("/branch").with(BranchManifestServlet.class);
+    serve("/tag").with(TagManifestServlet.class);
   }
 }
+
diff --git a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/ShowSubscriptionServlet.java b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/ShowSubscriptionServlet.java
new file mode 100644
index 0000000..bd0bd32
--- /dev/null
+++ b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/ShowSubscriptionServlet.java
@@ -0,0 +1,41 @@
+// Copyright (C) 2016 Advanced Micro Devices, 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.amd.gerrit.plugins.manifestsubscription;
+
+import com.google.gerrit.extensions.annotations.Export;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@Export("/show")
+@Singleton
+public class ShowSubscriptionServlet extends HttpServlet {
+  @Inject
+  private ManifestSubscription manifestSubscription;
+
+  protected void doGet(HttpServletRequest req, HttpServletResponse res)
+                                                            throws IOException {
+    res.setContentType("application/json");
+    res.setCharacterEncoding("UTF-8");
+
+    res.getWriter().write("Hello\n");
+    Utilities.showSubscription(manifestSubscription, res.getWriter(), true);
+  }
+}
diff --git a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/TagManifestServlet.java b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/TagManifestServlet.java
new file mode 100644
index 0000000..434d3c0
--- /dev/null
+++ b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/TagManifestServlet.java
@@ -0,0 +1,69 @@
+// Copyright (C) 2016 Advanced Micro Devices, 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.amd.gerrit.plugins.manifestsubscription;
+
+import com.google.gerrit.extensions.annotations.Export;
+import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Map;
+
+@Export("/tag")
+@Singleton
+public class TagManifestServlet extends HttpServlet {
+
+  @Inject
+  private GitRepositoryManager gitRepoManager;
+
+  protected void doPost(HttpServletRequest req, HttpServletResponse res)
+                                                            throws IOException {
+    res.setContentType("application/json");
+    res.setCharacterEncoding("UTF-8");
+    Map<String, String[]> input = req.getParameterMap();
+
+    if (inputValid(req)) {
+      res.setContentType("application/json");
+      res.setCharacterEncoding("UTF-8");
+
+      Utilities.tagManifest(gitRepoManager,
+          input.get("manifest-repo")[0],
+          input.get("manifest-commit-ish")[0],
+          input.get("manifest-path")[0],
+          input.get("new-tag")[0],
+          res.getWriter(), null, true);
+
+    } else {
+      res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+    }
+  }
+
+  private boolean inputValid(HttpServletRequest request) {
+    Map<String, String[]> input = request.getParameterMap();
+    if(input.containsKey("manifest-repo") &&
+        input.containsKey("manifest-commit-ish") &&
+        input.containsKey("manifest-path") &&
+        input.containsKey("new-tag")) {
+      return true;
+    }
+
+    return false;
+  }
+
+}
diff --git a/src/main/resources/Documentation/cmd-branch.md b/src/main/resources/Documentation/cmd-branch.md
index 8cd65a3..acf913b 100644
--- a/src/main/resources/Documentation/cmd-branch.md
+++ b/src/main/resources/Documentation/cmd-branch.md
@@ -45,3 +45,7 @@
 ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ branch -r demo/build_manifest -c v0.9-15-g5f51acb -p default.xml -b releases/1.0.0
 
 ```
+
+SEE ALSO
+--------
+* [@PLUGIN@ tag SSH command](cmd-tag.html)
diff --git a/src/main/resources/Documentation/cmd-tag.md b/src/main/resources/Documentation/cmd-tag.md
index 50c04fb..bff73cf 100644
--- a/src/main/resources/Documentation/cmd-tag.md
+++ b/src/main/resources/Documentation/cmd-tag.md
@@ -45,3 +45,7 @@
 ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ tag -r demo/build_manifest -c v0.9-15-g5f51acb -p default.xml -t releases/1.0.0
 
 ```
+
+SEE ALSO
+--------
+* [@PLUGIN@ branch SSH command](cmd-branch.html)
\ No newline at end of file
diff --git a/src/main/resources/Documentation/rest-api-branch.md b/src/main/resources/Documentation/rest-api-branch.md
new file mode 100644
index 0000000..67f138d
--- /dev/null
+++ b/src/main/resources/Documentation/rest-api-branch.md
@@ -0,0 +1,77 @@
+@PLUGIN@ - /branch REST API
+==============================
+This page describes the project related REST endpoints that are added
+by the @PLUGIN@.
+Please also take note of the general information on the
+[REST API](../../../Documentation/rest-api.html).
+
+branch Endpoint
+---------------
+
+_POST /plugins/@PLUGIN@/branch?manifest-repo=repo/name&manifest-commit-ish=commitish&manifest-path=default.xml&new-branch=branch/name_
+
+* manifest-repo: name of the manifest repository that defines the projects to be branched
+* manifest-commit-ish: commit-ish that points to the commit that contain the manifest (branch name, git describe string, etc.)
+* manifest-path: path to the manifest that defines the projects to be branched
+* new-branch: name of the branch to be created for each of the project defined in the manifest above
+
+#### Request
+```
+http://gerritserver/a/plugins/manifest-subscription/branch?manifest-repo=demo/build_manifest&manifest-commit-ish=m/topic/dupl.xml&manifest-path=default.xml&new-branch=r/1.12.0
+```
+
+#### Response
+The content of the manifest used by the branching operation in JSON
+
+```
+HTTP/1.1 200
+Content-Type: application/json;charset=UTF-8
+)]}'
+{
+  "remote": [
+    {
+      "name": "testGerrit",
+      "fetch": "ssh://gerritserver/",
+      "review": "gerritserver"
+    }
+  ],
+  "_default": {
+    "remote": {
+      "name": "testGerrit",
+      "fetch": "ssh://gerritserver/",
+      "review": "gerritserver"
+    },
+    "revision": "master",
+    "sync_j": "4"
+  },
+  "remove_project": [],
+  "project": [
+    {
+      "name": "demo/project1",
+      "path": "ws/project1",
+      "revision": "ebc8392b32494a03767d794dcaa8c4bcbb538be9",
+      "upstream": "master",
+      "project": []
+    },
+    {
+      "name": "demo/project2",
+      "path": "ws/project2",
+      "revision": "f4737c3bf124d5fd9437bbc0883be92219f7f8da",
+      "upstream": "master",
+      "project": []
+    },
+    {
+      "name": "demo/project3",
+      "path": "ext/project3",
+      "revision": "ce2b81d9c04d45e2ef5b561c99d08709aa19d300",
+      "upstream": "master",
+      "project": []
+    }
+  ],
+  "include": []
+}
+```
+
+SEE ALSO
+--------
+* [/tag REST endpoint](rest-api-tag.html)
\ No newline at end of file
diff --git a/src/main/resources/Documentation/rest-api-tag.md b/src/main/resources/Documentation/rest-api-tag.md
new file mode 100644
index 0000000..4648301
--- /dev/null
+++ b/src/main/resources/Documentation/rest-api-tag.md
@@ -0,0 +1,77 @@
+@PLUGIN@ - /tag REST API
+==============================
+This page describes the project related REST endpoints that are added
+by the @PLUGIN@.
+Please also take note of the general information on the
+[REST API](../../../Documentation/rest-api.html).
+
+tag Endpoint
+---------------
+
+_POST /plugins/@PLUGIN@/tag?manifest-repo=repo/name&manifest-commit-ish=commitish&manifest-path=default.xml&new-tag=tag/name_
+
+* manifest-repo: name of the manifest repository that defines the projects to be tagged
+* manifest-commit-ish: commit-ish that points to the commit that contain the manifest (tag name, branch name, git describe string, etc.)
+* manifest-path: path to the manifest that defines the projects to be tagged
+* new-tag: name of the tag to be created for each of the project defined in the manifest above
+
+#### Request
+```
+http://gerritserver/a/plugins/manifest-subscription/tag?manifest-repo=demo/build_manifest&manifest-commit-ish=m/topic/dupl.xml&manifest-path=default.xml&new-tag=r/1.12.0
+```
+
+#### Response
+The content of the manifest used by the tagging operation in JSON
+
+```
+HTTP/1.1 200
+Content-Type: application/json;charset=UTF-8
+)]}'
+{
+  "remote": [
+    {
+      "name": "testGerrit",
+      "fetch": "ssh://gerritserver/",
+      "review": "gerritserver"
+    }
+  ],
+  "_default": {
+    "remote": {
+      "name": "testGerrit",
+      "fetch": "ssh://gerritserver/",
+      "review": "gerritserver"
+    },
+    "revision": "master",
+    "sync_j": "4"
+  },
+  "remove_project": [],
+  "project": [
+    {
+      "name": "demo/project1",
+      "path": "ws/project1",
+      "revision": "ebc8392b32494a03767d794dcaa8c4bcbb538be9",
+      "upstream": "master",
+      "project": []
+    },
+    {
+      "name": "demo/project2",
+      "path": "ws/project2",
+      "revision": "f4737c3bf124d5fd9437bbc0883be92219f7f8da",
+      "upstream": "master",
+      "project": []
+    },
+    {
+      "name": "demo/project3",
+      "path": "ext/project3",
+      "revision": "ce2b81d9c04d45e2ef5b561c99d08709aa19d300",
+      "upstream": "master",
+      "project": []
+    }
+  ],
+  "include": []
+}
+```
+
+SEE ALSO
+--------
+* [/branch REST endpoint](rest-api-branch.html)
\ No newline at end of file