package org.springframework.social.common.api.impl;

import java.io.IOException;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.social.botFramework.api.BotFrameworkOperations;
import org.springframework.social.botFramework.api.impl.BotFrameworkTemplate;
import org.springframework.social.common.api.ConnectorClient;
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.skypeBot.api.SkypeBotOperations;
import org.springframework.social.skypeBot.api.impl.SkypeBotTemplate;
import org.springframework.social.support.ClientHttpRequestFactorySelector;
import org.springframework.web.client.RestTemplate;

/**
 * @author Anton Leliuk
 */
public class ConnectorClientTemplate extends AbstractOAuth2ApiBinding implements ConnectorClient {

    private String skypeUrl;
    private String apiVersion;

    private SkypeBotOperations skypeBotOperations;

    private BotFrameworkOperations botFrameworkOperations;

    private final LoggingClientHttpRequestInterceptor loggingInterceptor = new LoggingClientHttpRequestInterceptor();

    public ConnectorClientTemplate(String accessToken, String skypeUrl, String apiVersion) {
        super(accessToken);
        this.skypeUrl = skypeUrl;
        this.apiVersion = apiVersion;
        initialize();
    }

    @Override
    public SkypeBotOperations getSkypeBotOperations() {
        return skypeBotOperations;
    }

    @Override
    public BotFrameworkOperations getBotFrameworkOperations() {
        return botFrameworkOperations;
    }

    // private helpers
    private void initialize() {
        // Wrap the request factory with a BufferingClientHttpRequestFactory so that the error handler can do repeat reads on the response.getBody()
        super.setRequestFactory(ClientHttpRequestFactorySelector.bufferRequests(getRestTemplate().getRequestFactory()));
        initSubApis();
    }

    private void initSubApis() {
        this.skypeBotOperations = new SkypeBotTemplate(getRestTemplate(), skypeUrl, apiVersion);
        this.botFrameworkOperations = new BotFrameworkTemplate(getRestTemplate(), skypeUrl, apiVersion);
    }

    @Override
    protected void configureRestTemplate(RestTemplate restTemplate) {
        restTemplate.getInterceptors().add(loggingInterceptor);
    }

    private class LoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            StringBuilder sb = new StringBuilder(System.lineSeparator())
                    .append("-------------------").append(System.lineSeparator())
                    .append("URL = ").append(request.getURI()).append(System.lineSeparator())
                    .append("Headers = ").append(request.getHeaders()).append(System.lineSeparator())
                    .append("Body = ").append(new String(body)).append(System.lineSeparator())
                    .append("-------------------");
            System.out.println(sb);
            return execution.execute(request, body);
        }
    }
}

@@ -1172,127 +1172,8 @@
 s;%0A%0A
-    private final LoggingClientHttpRequestInterceptor loggingInterceptor = new LoggingClientHttpRequestInterceptor();%0A%0A
     
@@ -2354,15 +2354,36 @@
 add(
-l
+new L
 ogging
+ClientHttpRequest
 Inte
@@ -2389,16 +2389,18 @@
 erceptor
+()
 );%0A    %7D

Test signin card