/*
 * 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.config.ArtifactCredentials;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

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

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

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

@@ -785,68 +785,237 @@
 cts.
-config.ArtifactCredentials;%0Aimport lombok.extern.slf4j.Slf4j
+ArtifactDownloader;%0Aimport com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;%0Aimport com.netflix.spinnaker.kork.artifacts.model.Artifact;%0Aimport lombok.extern.slf4j.Slf4j;%0Aimport org.apache.commons.io.IOUtils
 ;%0Aim
@@ -1075,16 +1075,77 @@
 owired;%0A
+import org.springframework.web.bind.annotation.PathVariable;%0A
 import o
@@ -1261,16 +1261,77 @@
 Method;%0A
+import org.springframework.web.bind.annotation.RequestParam;%0A
 import o
@@ -1386,16 +1386,44 @@
 oller;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -1650,16 +1650,65 @@
 ository;
+%0A  private ArtifactDownloader artifactDownloader;
 %0A%0A  @Aut
@@ -1811,16 +1811,71 @@
 pository
+,%0A      Optional%3CArtifactDownloader%3E artifactDownloader
 ) %7B%0A    
@@ -1955,16 +1955,79 @@
 (null);%0A
+    this.artifactDownloader = artifactDownloader.orElse(null);%0A
   %7D%0A%0A  @
@@ -2297,11 +2297,784 @@
    %7D%0A  %7D
+%0A%0A  @RequestMapping(method = RequestMethod.GET, value = %22/fetch%22)%0A  String fetch(@RequestParam(%22artifactAccount%22) String artifactAccount,%0A      @RequestParam(%22type%22) String type,%0A      @RequestParam(%22reference%22) String reference) %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        .build();%0A%0A    try %7B%0A      return IOUtils.toString(artifactDownloader.download(artifact));%0A    %7D catch (IOException e) %7B%0A      throw new RuntimeException(%22Failure fetching '%22 + artifact + %22': %22 + e.getMessage(), e);%0A    %7D%0A  %7D
 %0A%7D%0A

add artifact download endpoint (#2282)