/*
 * Copyright 2017 the original author or authors.
 *
 * 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 org.springframework.cloud.dataflow.server.controller.support;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.dataflow.server.config.MetricsProperties;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.hal.Jackson2HalModule;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * Store implementation returning metrics info from a collector application.
 * Implemented via hystrix command having a fallback to empty response.
 *
 * @author Janne Valkealahti
 *
 */
public class MetricStore {

	private static Log logger = LogFactory.getLog(MetricStore.class);
	private final RestTemplate restTemplate;
	private final MetricsProperties metricsProperties;
	private final static List<ApplicationsMetrics> EMPTY_RESPONSE = new ArrayList<ApplicationsMetrics>();
	private String collectorEndpoint;

	/**
	 * Instantiates a new metric store.
	 *
	 * @param metricsProperties the metrics properties
	 */
	public MetricStore(MetricsProperties metricsProperties) {
		this.metricsProperties = metricsProperties;
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.registerModule(new Jackson2HalModule());
		MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
		messageConverter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
		messageConverter.setObjectMapper(mapper);
		restTemplate = new RestTemplate(Arrays.asList(messageConverter));
		String baseURI = metricsProperties.getCollector().getUri();
		if(StringUtils.hasText(baseURI)){
			try {
				URI uri = new URI(baseURI);
				this.collectorEndpoint = UriComponentsBuilder.fromUri(uri).path("/collector/metrics/streams").build().toString();
			}
			catch (URISyntaxException e) {
				logger.warn("Could not parse collector URI, stream metrics monitoring will not be available");
			}
		}
	}

	@HystrixCommand(fallbackMethod = "defaultMetrics")
	public List<ApplicationsMetrics> getMetrics() {
		List<ApplicationsMetrics> metrics = null;
		if (StringUtils.hasText(this.collectorEndpoint)) {
			try {
				PagedResources<ApplicationsMetrics> response = restTemplate.exchange(this.collectorEndpoint,
						HttpMethod.GET, null, new ParameterizedTypeReference<PagedResources<ApplicationsMetrics>>() {
						}).getBody();
				metrics = new ArrayList<>(response.getContent());
			} catch (Exception e) {
				if (logger.isDebugEnabled()) {
					logger.debug("Error requesting metrics from url " +this.collectorEndpoint, e);
				}
				throw e;
			}
		} else {
			metrics = defaultMetrics();
		}
		return metrics;
	}

	public List<ApplicationsMetrics> defaultMetrics() {
		return EMPTY_RESPONSE;
	}
}

@@ -3120,16 +3120,88 @@
 ring();%0A
+%09%09%09%09logger.info(%22Metrics Collector URI = %5B%22 + collectorEndpoint + %22%5D%22);%0A
 %09%09%09%7D%0A%09%09%09
@@ -3335,16 +3335,73 @@
 );%0A%09%09%09%7D%0A
+%09%09%7D else %7B%0A%09%09%09logger.info(%22Metrics Collector URI = %5B%5D%22);%0A
 %09%09%7D%0A%09%7D%0A%0A

Add logging of Metrics Collector endpoint