// Copyright (C) 2013 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.google.gerrit.server.change;

import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import java.io.IOException;

@Singleton
public class GetContent implements RestReadView<FileResource> {
  private final FileContentUtil fileContentUtil;

  @Inject
  GetContent(FileContentUtil fileContentUtil) {
    this.fileContentUtil = fileContentUtil;
  }

  @Override
  public BinaryResult apply(FileResource rsrc)
      throws ResourceNotFoundException, IOException {
    return fileContentUtil.getContent(
        rsrc.getRevision().getControl().getProject().getNameKey(),
        rsrc.getRevision().getPatchSet().getRevision().get(),
        rsrc.getPatchKey().get());
  }
}

@@ -831,16 +831,217 @@
 adView;%0A
+import com.google.gerrit.reviewdb.client.Patch;%0Aimport com.google.gerrit.server.ChangeUtil;%0Aimport com.google.gerrit.server.project.NoSuchChangeException;%0Aimport com.google.gwtorm.server.OrmException;%0A
 import c
@@ -1254,16 +1254,55 @@
 entUtil;
+%0A  private final ChangeUtil changeUtil;
 %0A%0A  @Inj
@@ -1349,16 +1349,45 @@
 tentUtil
+,%0A      ChangeUtil changeUtil
 ) %7B%0A    
@@ -1426,16 +1426,50 @@
 ntUtil;%0A
+    this.changeUtil = changeUtil;%0A
   %7D%0A%0A  @
@@ -1575,18 +1575,261 @@
 xception
- %7B
+, NoSuchChangeException,%0A      OrmException %7B%0A    String path = rsrc.getPatchKey().get();%0A    if (Patch.COMMIT_MSG.equals(path)) %7B%0A      return BinaryResult.create(%0A          changeUtil.getMessage(rsrc.getRevision().getChange())).base64();%0A    %7D
 %0A    ret
@@ -2001,32 +2001,12 @@
     
-rsrc.getPatchKey().get()
+path
 );%0A 

Fix /content endpoint for commit messages