/*
 * 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.echo.artifacts;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.echo.config.WebhookProperties;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class TemplateBasedArtifactExtractor implements WebhookArtifactExtractor {
  final private WebhookProperties webhookProperties;
  final private ObjectMapper objectMapper;

  @Autowired(required = false)
  public TemplateBasedArtifactExtractor(WebhookProperties webhookProperties, ObjectMapper objectMapper) {
    this.webhookProperties = webhookProperties;
    this.objectMapper = objectMapper;
  }

  @Override
  public List<Artifact> getArtifacts(String source, Map payload) {
    String templatePath = webhookProperties.getTemplatePathForSource(source);
    // empty template path is the identity translator;
    MessageArtifactTranslator translator = new MessageArtifactTranslator(templatePath);
    List<Artifact> result = new ArrayList<>();
    try {
      result = translator.parseArtifacts(objectMapper.writeValueAsString(payload));
      log.info("Webhook artifacts were processed: {}", result);
    } catch (Exception e) {
      log.error("Unable to translate artifacts: {}", payload, e);
    }

    return result;
  }

  @Override
  public boolean handles(String type, String source) {
    return webhookProperties != null && (type.equals("artifact") || type.equals("artifacts"));
  }
}

@@ -1027,16 +1027,43 @@
 til.Map;
+%0Aimport java.util.Optional;
 %0A%0A@Compo
@@ -1269,26 +1269,8 @@
 ired
-(required = false)
 %0A  p
@@ -1306,16 +1306,25 @@
 tractor(
+Optional%3C
 WebhookP
@@ -1324,32 +1324,33 @@
 ebhookProperties
+%3E
  webhookProperti
@@ -1424,24 +1424,37 @@
 okProperties
+.orElse(null)
 ;%0A    this.o
@@ -1561,24 +1561,100 @@
  payload) %7B%0A
+    if (webhookProperties == null) %7B%0A      return new ArrayList%3C%3E();%0A    %7D%0A%0A
     String t

fix disabled webhook startup (#215)