/*
 * Copyright (C) 2014 Stefan Niederhauser (nidin@gmx.ch)
 *
 * 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 guru.nidi.ramltester.spring;

import guru.nidi.ramltester.core.RamlReport;
import guru.nidi.ramltester.core.RamlTester;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

/**
 *
 */
public class RamlRestTemplate extends RestTemplate implements RamlTestRequestInterceptor.ReportStore {
    private final ClientHttpRequestFactory originalRequestFactory;
    private final RamlTestRequestInterceptor interceptor;
    private RamlReport lastReport;

    public RamlRestTemplate(RamlTester tester, String baseUri, ClientHttpRequestFactory requestFactory) {
        originalRequestFactory = requestFactory;
        interceptor = new RamlTestRequestInterceptor(this, tester, baseUri);
        setRequestFactory(new InterceptingClientHttpRequestFactory(requestFactory, Collections.<ClientHttpRequestInterceptor>singletonList(interceptor)));
    }

    public RamlRestTemplate(RamlTester tester, String baseUri, RestTemplate restTemplate) {
        this(tester, baseUri, restTemplate.getRequestFactory());
        init(restTemplate);
    }

    public RamlRestTemplate(RamlTester tester, String baseUri, RamlRestTemplate restTemplate) {
        this(tester, baseUri, restTemplate.originalRequestFactory);
        init(restTemplate);
    }

    private void init(RestTemplate restTemplate) {
        setErrorHandler(restTemplate.getErrorHandler());
        setMessageConverters(restTemplate.getMessageConverters());
    }

    public RamlRestTemplate assumingBaseUri(String baseUri) {
        return new RamlRestTemplate(interceptor.getTester(), baseUri, this);
    }

    public RamlReport getLastReport() {
        return lastReport;
    }

    @Override
    public void storeReport(RamlReport report) {
        this.lastReport = report;
    }
}

@@ -1293,16 +1293,41 @@
 private 
+static final ThreadLocal%3C
 RamlRepo
@@ -1328,16 +1328,17 @@
 mlReport
+%3E
  lastRep
@@ -1340,16 +1340,38 @@
 stReport
+ = new ThreadLocal%3C%3E()
 ;%0A%0A    p
@@ -2539,24 +2539,30 @@
 n lastReport
+.get()
 ;%0A    %7D%0A%0A   
@@ -2648,17 +2648,20 @@
 port
- = 
+.set(
 report
+)
 ;%0A  

make RamlRestTemplate thead safe