/*
 * Copyright 2017 Google, Inc.
 *
 * 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.netflix.spinnaker.clouddriver.controllers;

import com.netflix.spinnaker.clouddriver.artifacts.ArtifactCredentialsRepository;
import com.netflix.spinnaker.clouddriver.artifacts.ArtifactDownloader;
import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/artifacts")
public class ArtifactController {
  private ArtifactCredentialsRepository artifactCredentialsRepository;
  private ArtifactDownloader artifactDownloader;

  @Autowired
  public ArtifactController(Optional<ArtifactCredentialsRepository> artifactCredentialsRepository,
      Optional<ArtifactDownloader> artifactDownloader) {
    this.artifactCredentialsRepository = artifactCredentialsRepository.orElse(null);
    this.artifactDownloader = artifactDownloader.orElse(null);
  }

  @RequestMapping(method = RequestMethod.GET, value = "/credentials")
  List<ArtifactCredentials> list() {
    if (artifactCredentialsRepository == null) {
      return new ArrayList<>();
    } else {
      return artifactCredentialsRepository.getAllCredentials();
    }
  }

  @RequestMapping(method = RequestMethod.GET, value = "/fetch")
  String fetch(@RequestParam("artifactAccount") String artifactAccount,
      @RequestParam("type") String type,
      @RequestParam("reference") String reference,
      @RequestParam(value = "version", required = false) String version) {
    if (artifactDownloader == null) {
      throw new IllegalStateException("Artifacts have not been enabled. Enable them using 'artifacts.enabled' in clouddriver");
    }

    Artifact artifact = Artifact.builder()
        .type(type)
        .artifactAccount(artifactAccount)
        .reference(reference)
        .version(version)
        .build();

    try {
      return IOUtils.toString(artifactDownloader.download(artifact));
    } catch (IOException e) {
      throw new RuntimeException("Failure fetching '" + artifact + "': " + e.getMessage(), e);
    }
  }
}

@@ -1126,20 +1126,19 @@
 ion.
-PathVariable
+RequestBody
 ;%0Aim
@@ -1264,69 +1264,8 @@
 od;%0A
-import org.springframework.web.bind.annotation.RequestParam;%0A
 impo
@@ -1327,36 +1327,92 @@
 er;%0A
-%0A
 import 
-java.io.IOException;
+org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;%0A
 %0Aimp
@@ -2289,24 +2289,110 @@
 %0A    %7D%0A  %7D%0A%0A
+  // PUT because we need to send a body, which GET does not allow for spring/retrofit%0A
   @RequestMa
@@ -2412,34 +2412,34 @@
 = RequestMethod.
-GE
+PU
 T, value = %22/fet
@@ -2452,617 +2452,266 @@
  Str
-ing fetch(@RequestParam(%22artifactAccount%22) String artifactAccount,%0A      @RequestParam(%22type%22) String type,%0A      @RequestParam(%22reference%22) String reference,%0A      @RequestParam(value = %22version%22, required = false) String version) %7B%0A    if (artifactDownloader == null) %7B%0A      throw new IllegalStateException(%22Artifacts have not been enabled. Enable them using 'artifacts.enabled' in clouddriver%22);%0A    %7D%0A%0A    Artifact artifact = Artifact.builder()%0A        .type(type)%0A        .artifactAccount(artifactAccount)%0A        .reference(reference)%0A        .version(version)%0A        .build();%0A%0A    try %7B%0A      return
+eamingResponseBody fetch(@RequestBody Artifact artifact) %7B%0A    if (artifactDownloader == null) %7B%0A      throw new IllegalStateException(%22Artifacts have not been enabled. Enable them using 'artifacts.enabled' in clouddriver%22);%0A    %7D%0A%0A    return outputStream -%3E
  IOU
@@ -2719,16 +2719,12 @@
 ils.
-toString
+copy
 (art
@@ -2761,141 +2761,24 @@
 act)
-);%0A    %7D catch (IOException e) %7B%0A      throw new RuntimeException(%22Failure fetching '%22 + artifact + %22': %22 + e.getMessage(), e);%0A    %7D
+, outputStream);
 %0A  %7D

change fetch verb type (#2492)