Build with 2.16 API

Change-Id: I291cf659fdce5c146910ef068bad5722d71034e3
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/DeleteImage.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/DeleteImage.java
index 9f71e9d..8c17842 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/DeleteImage.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/DeleteImage.java
@@ -71,15 +71,15 @@
       throws AuthException, ResourceConflictException, RepositoryNotFoundException, IOException,
           ResourceNotFoundException, PermissionBackendException {
 
-    if (!permissionBackend.user(self).ref(rsrc.getBranchKey()).testOrFalse(RefPermission.DELETE)) {
+    if (!permissionBackend.currentUser().ref(rsrc.getBranchKey()).testOrFalse(RefPermission.DELETE)) {
       permissionBackend
-          .user(self)
+          .currentUser()
           .test(new PluginPermission(pluginName, DeleteOwnImagesCapability.DELETE_OWN_IMAGES));
     }
 
     try (Repository r = repoManager.openRepository(rsrc.getProject())) {
       if (!permissionBackend
-          .user(self)
+          .currentUser()
           .ref(rsrc.getBranchKey())
           .testOrFalse(RefPermission.DELETE)) {
         validateOwnImage(r, rsrc.getRef());
@@ -101,7 +101,7 @@
         case NO_CHANGE:
         case FAST_FORWARD:
         case FORCED:
-          referenceUpdated.fire(rsrc.getProject(), u, self.get().getAccount());
+          referenceUpdated.fire(rsrc.getProject(), u, self.get().state());
           break;
 
         case REJECTED_CURRENT_BRANCH:
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/GetPreference.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/GetPreference.java
index 4c5041b..e69b27d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/GetPreference.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/GetPreference.java
@@ -60,10 +60,10 @@
   @Override
   public ConfigInfo apply(AccountResource rsrc) throws AuthException, PermissionBackendException {
     if (self.get() != rsrc.getUser()) {
-      permissionBackend.user(self).check(ADMINISTRATE_SERVER);
+      permissionBackend.currentUser().check(ADMINISTRATE_SERVER);
     }
 
-    String username = self.get().getUserName();
+    String username = self.get().getUserName().get();
 
     ConfigInfo globalCfg = getConfig.get().apply(new ConfigResource());
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/ImageResource.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/ImageResource.java
index 131ae51..4fab002 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/ImageResource.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/ImageResource.java
@@ -18,32 +18,28 @@
 import com.google.gerrit.extensions.restapi.RestView;
 import com.google.gerrit.reviewdb.client.Branch;
 import com.google.gerrit.reviewdb.client.Project;
-import com.google.gerrit.server.project.RefControl;
+
 import com.google.inject.TypeLiteral;
 
 public class ImageResource implements RestResource {
   public static final TypeLiteral<RestView<ImageResource>> IMAGE_KIND =
       new TypeLiteral<RestView<ImageResource>>() {};
 
-  private final RefControl refControl;
+  private Branch.NameKey branchName;
 
-  ImageResource(RefControl refControl) {
-    this.refControl = refControl;
-  }
-
-  public RefControl getControl() {
-    return refControl;
+  ImageResource(Branch.NameKey branchName) {
+    this.branchName = branchName;
   }
 
   public Project.NameKey getProject() {
-    return refControl.getProjectControl().getProject().getNameKey();
+    return branchName.getParentKey();
   }
 
   public String getRef() {
-    return refControl.getRefName();
+    return branchName.get();
   }
 
   public Branch.NameKey getBranchKey() {
-    return new Branch.NameKey(getProject(), getRef());
+    return branchName;
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/ImageServlet.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/ImageServlet.java
index 6c52b46..f05d7e7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/ImageServlet.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/ImageServlet.java
@@ -31,14 +31,13 @@
 import com.google.gerrit.server.permissions.PermissionBackend;
 import com.google.gerrit.server.permissions.PermissionBackendException;
 import com.google.gerrit.server.permissions.RefPermission;
-import com.google.gerrit.server.project.CommitsCollection;
-import com.google.gerrit.server.project.GetHead;
+import com.google.gerrit.server.restapi.project.CommitsCollection;
+import com.google.gerrit.server.restapi.project.GetHead;
 import com.google.gerrit.server.project.NoSuchProjectException;
 import com.google.gerrit.server.project.ProjectCache;
-import com.google.gerrit.server.project.ProjectControl;
 import com.google.gerrit.server.project.ProjectResource;
 import com.google.gerrit.server.project.ProjectState;
-import com.google.gwtexpui.server.CacheHeaders;
+import com.google.gerrit.util.http.CacheHeaders;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.google.inject.Singleton;
@@ -67,7 +66,6 @@
   public static final String PATH_PREFIX = "/project/";
 
   private final String pluginName;
-  private final ProjectControl.Factory projectControlFactory;
   private final ProjectCache projectCache;
   private final Provider<GetHead> getHead;
   private final GitRepositoryManager repoManager;
@@ -79,7 +77,6 @@
   @Inject
   ImageServlet(
       @PluginName String pluginName,
-      ProjectControl.Factory projectControlFactory,
       ProjectCache projectCache,
       Provider<GetHead> getHead,
       GitRepositoryManager repoManager,
@@ -88,7 +85,6 @@
       Provider<CurrentUser> self,
       PermissionBackend permissionBackend) {
     this.pluginName = pluginName;
-    this.projectControlFactory = projectControlFactory;
     this.projectCache = projectCache;
     this.getHead = getHead;
     this.repoManager = repoManager;
@@ -120,16 +116,16 @@
     }
 
     try {
-      ProjectControl projectControl = projectControlFactory.controlFor(key.project);
+      ProjectState projectState = projectCache.get(key.project);
       String rev = key.revision;
       if (rev == null || Constants.HEAD.equals(rev)) {
-        rev = getHead.get().apply(new ProjectResource(projectControl));
+        rev = getHead.get().apply(new ProjectResource(projectState, self.get()));
       } else {
         if (!ObjectId.isId(rev)) {
           if (!rev.startsWith(Constants.R_REFS)) {
             rev = Constants.R_HEADS + rev;
           }
-          PermissionBackend.ForProject perm = permissionBackend.user(self).project(key.project);
+          PermissionBackend.ForProject perm = permissionBackend.currentUser().project(key.project);
           try {
             perm.ref(rev).check(RefPermission.READ);
           } catch (AuthException e) {
@@ -203,7 +199,6 @@
         }
       }
     } catch (RepositoryNotFoundException
-        | NoSuchProjectException
         | ResourceNotFoundException
         | AuthException
         | RevisionSyntaxException
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/ImagesCollection.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/ImagesCollection.java
index 2edaae3..47aea1c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/ImagesCollection.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/ImagesCollection.java
@@ -15,7 +15,6 @@
 package com.googlesource.gerrit.plugins.imagare;
 
 import com.google.gerrit.extensions.registration.DynamicMap;
-import com.google.gerrit.extensions.restapi.AcceptsPost;
 import com.google.gerrit.extensions.restapi.ChildCollection;
 import com.google.gerrit.extensions.restapi.IdString;
 import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
@@ -29,7 +28,7 @@
 import com.google.inject.Provider;
 
 public class ImagesCollection
-    implements ChildCollection<ProjectResource, ImageResource>, AcceptsPost<ProjectResource> {
+    implements ChildCollection<ProjectResource, ImageResource> {
   private final DynamicMap<RestView<ImageResource>> views;
   private final Provider<PostImage> createImage;
   private final PermissionBackend permissionBackend;
@@ -53,10 +52,10 @@
   public ImageResource parse(ProjectResource parent, IdString id) throws ResourceNotFoundException {
     Branch.NameKey branchName = new Branch.NameKey(parent.getNameKey(), id.get());
     if (permissionBackend
-        .user(parent.getControl().getUser())
+        .user(parent.getUser())
         .ref(branchName)
         .testOrFalse(RefPermission.READ)) {
-      return new ImageResource(parent.getControl().controlForRef(branchName));
+      return new ImageResource(branchName);
     }
     throw new ResourceNotFoundException(id);
   }
@@ -65,9 +64,4 @@
   public DynamicMap<RestView<ImageResource>> views() {
     return views;
   }
-
-  @Override
-  public PostImage post(ProjectResource parent) throws RestApiException {
-    return createImage.get();
-  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/Module.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/Module.java
index b9ecf0a..c6aade0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/Module.java
@@ -55,6 +55,7 @@
               bind(ImagesCollection.class);
               child(PROJECT_KIND, "images").to(ImagesCollection.class);
               delete(IMAGE_KIND).to(DeleteImage.class);
+              post(PROJECT_KIND, "images").to(PostImage.class);
             }
           });
     }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/PostImage.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/PostImage.java
index f1b54d3..0a330f1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/PostImage.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/PostImage.java
@@ -34,10 +34,11 @@
 import com.google.gerrit.server.permissions.PermissionBackendException;
 import com.google.gerrit.server.project.CreateRefControl;
 import com.google.gerrit.server.project.NoSuchProjectException;
-import com.google.gerrit.server.project.ProjectControl;
 import com.google.gerrit.server.project.ProjectResource;
+import com.google.gerrit.server.project.ProjectState;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
+import com.google.inject.Singleton;
 import com.googlesource.gerrit.plugins.imagare.PostImage.Input;
 import eu.medsea.mimeutil.MimeType;
 import java.io.IOException;
@@ -56,8 +57,8 @@
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.util.Base64;
 
+@Singleton
 public class PostImage implements RestModifyView<ProjectResource, Input> {
-
   public static class Input {
     public String imageData;
     public String fileName;
@@ -102,14 +103,14 @@
     }
     ImageInfo info;
     if (input.imageData != null) {
-      info = storeImage(rsrc.getControl(), input.imageData, input.fileName);
+      info = storeImage(rsrc.getProjectState(), input.imageData, input.fileName);
     } else {
       throw new BadRequestException("no image data");
     }
     return Response.created(info);
   }
 
-  private ImageInfo storeImage(ProjectControl pc, String imageData, String fileName)
+  private ImageInfo storeImage(ProjectState ps, String imageData, String fileName)
       throws RestApiException, IOException, PermissionBackendException, NoSuchProjectException {
     Matcher m = imageDataPattern.matcher(imageData);
     if (m.matches()) {
@@ -135,17 +136,17 @@
         if (!receivedMimeType.equals(mimeType.toString())) {
           throw new BadRequestException("incorrect mime type");
         }
-        return new ImageInfo(storeImage(pc, content, fileName));
+        return new ImageInfo(storeImage(ps, content, fileName));
       }
       throw new MethodNotAllowedException("unsupported encoding");
     }
     throw new BadRequestException("invalid image data");
   }
 
-  private String storeImage(ProjectControl pc, byte[] content, String fileName)
+  private String storeImage(ProjectState ps, byte[] content, String fileName)
       throws AuthException, IOException, ResourceConflictException, PermissionBackendException,
           NoSuchProjectException {
-    long maxSize = pc.getProjectState().getEffectiveMaxObjectSizeLimit().value;
+    long maxSize = ps.getEffectiveMaxObjectSizeLimit().value;
     // maxSize == 0 means that there is no limit
     if (maxSize > 0 && content.length > maxSize) {
       throw new ResourceConflictException("image too large");
@@ -153,11 +154,11 @@
 
     String ref = getRef(content, fileName);
 
-    try (Repository repo = repoManager.openRepository(pc.getProject().getNameKey())) {
+    try (Repository repo = repoManager.openRepository(ps.getProject().getNameKey())) {
       ObjectId commitId = repo.resolve(ref);
       if (commitId != null) {
         // this image exists already
-        return getUrl(pc.getProject().getNameKey(), ref, fileName);
+        return getUrl(ps.getProject().getNameKey(), ref, fileName);
       }
 
       try (RevWalk rw = new RevWalk(repo);
@@ -185,11 +186,11 @@
           createRefControl.checkCreateRef(
               self,
               repo,
-              new Branch.NameKey(pc.getProject().getNameKey(), ref),
+              new Branch.NameKey(ps.getProject().getNameKey(), ref),
               rw.parseCommit(commitId));
         } catch (AuthException e) {
           throw new AuthException(
-              String.format("Project %s doesn't allow image upload.", pc.getProject().getName()));
+              String.format("Project %s doesn't allow image upload.", ps.getProject().getName()));
         }
 
         RefUpdate ru = repo.updateRef(ref);
@@ -197,15 +198,15 @@
         ru.setNewObjectId(commitId);
         ru.disableRefLog();
         if (ru.update(rw) == RefUpdate.Result.NEW) {
-          referenceUpdated.fire(pc.getProject().getNameKey(), ru, self.get().getAccount());
+          referenceUpdated.fire(ps.getProject().getNameKey(), ru, self.get().state());
         } else {
           throw new IOException(
               String.format(
                   "Failed to create ref %s in %s: %s",
-                  ref, pc.getProject().getName(), ru.getResult()));
+                  ref, ps.getProject().getName(), ru.getResult()));
         }
 
-        return getUrl(pc.getProject().getNameKey(), ref, fileName);
+        return getUrl(ps.getProject().getNameKey(), ref, fileName);
       }
     }
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/PutPreference.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/PutPreference.java
index 025475a..ffd00d4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/PutPreference.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/PutPreference.java
@@ -29,11 +29,11 @@
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.account.AccountResource;
-import com.google.gerrit.server.git.MetaDataUpdate;
-import com.google.gerrit.server.git.ProjectLevelConfig;
+import com.google.gerrit.server.git.meta.MetaDataUpdate;
 import com.google.gerrit.server.permissions.PermissionBackend;
 import com.google.gerrit.server.permissions.PermissionBackendException;
 import com.google.gerrit.server.project.ProjectCache;
+import com.google.gerrit.server.project.ProjectLevelConfig;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.googlesource.gerrit.plugins.imagare.PutConfig.Input;
@@ -67,13 +67,13 @@
       throws AuthException, RepositoryNotFoundException, IOException, UnprocessableEntityException,
           PermissionBackendException {
     if (self.get() != rsrc.getUser()) {
-      permissionBackend.user(self).check(ADMINISTRATE_SERVER);
+      permissionBackend.currentUser().check(ADMINISTRATE_SERVER);
     }
     if (input == null) {
       input = new Input();
     }
 
-    String username = self.get().getUserName();
+    String username = self.get().getUserName().get();
 
     ProjectLevelConfig storage = projectCache.getAllProjects().getConfig(pluginName + ".config");
     Config db = storage.get();