/*
 * Copyright (c) 2002-2015 mgm technology partners GmbH
 *
 * 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.mgmtp.perfload.core.client.web.okhttp;

import java.net.CookieHandler;
import java.net.InetAddress;

import javax.inject.Provider;
import javax.net.SocketFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;

import com.google.inject.Inject;
import com.mgmtp.perfload.core.client.web.net.LocalAddressSocketFactory;
import com.squareup.okhttp.OkHttpClient;

/**
 * A JSR 330 provider for {@link OkHttpClient} instances.
 *
 * @author rnaegele
 */
public class OkHttpClientProvider implements Provider<OkHttpClient> {

	private SSLSocketFactory sslSocketFactory;
	private HostnameVerifier hostnameVerifier;
	private final CookieHandler cookieHandler;
	private final Provider<InetAddress> localAddressProvider;

	/**
	 * Sets an optinal {@link HostnameVerifier}.
	 *
	 * @param hostnameVerifier
	 *            the hostname verifier
	 */
	@Inject(optional = true)
	public void setHostnameVerifier(final HostnameVerifier hostnameVerifier) {
		this.hostnameVerifier = hostnameVerifier;
	}

	/**
	 * Sets an optinal {@link SSLSocketFactory}.
	 *
	 * @param sslSocketFactory
	 *            the ssl socket factory
	 */
	@Inject(optional = true)
	public void setSslSocketFactory(final SSLSocketFactory sslSocketFactory) {
		this.sslSocketFactory = sslSocketFactory;
	}

	/**
	 * @param cookieHandler
	 *            the cookie handler
	 * @param localAddressProvider
	 *            the local address provider
	 */
	@Inject
	protected OkHttpClientProvider(final CookieHandler cookieHandler, final Provider<InetAddress> localAddressProvider) {
		this.cookieHandler = cookieHandler;
		this.localAddressProvider = localAddressProvider;
	}

	/**
	 * Creates a new OkHttpClient instance.
	 * 
	 * @return the OkHttpClient
	 */
	@Override
	public OkHttpClient get() {
		OkHttpClient client = new OkHttpClient();
		client.setFollowRedirects(true);
		client.setFollowSslRedirects(true);
		if (sslSocketFactory != null) {
			client.setSslSocketFactory(sslSocketFactory);
		}
		if (hostnameVerifier != null) {
			client.setHostnameVerifier(hostnameVerifier);
		}
		client.setCookieHandler(cookieHandler);
		client.setSocketFactory(new LocalAddressSocketFactory(SocketFactory.getDefault(), localAddressProvider));
		return client;
	}
}

@@ -970,24 +970,63 @@
 ketFactory;%0A
+import com.squareup.okhttp.Dispatcher;%0A
 import com.s
@@ -1054,16 +1054,16 @@
 Client;%0A
-
 %0A/**%0A * 
@@ -1299,24 +1299,57 @@
 meVerifier;%0A
+%09private Dispatcher dispatcher;%0A%0A
 %09private fin
@@ -1987,24 +1987,294 @@
 actory;%0A%09%7D%0A%0A
+%09/**%0A%09 * Set an optional %7B@link Dispatcher%7D for better control of asynchronous requests.%0A%09 *%0A%09 * @param dispatcher%0A%09 *            the dispatcher%0A%09 */%0A%09@Inject(optional = true)%0A%09public void setDispatcher(final Dispatcher dispatcher) %7B%0A%09%09this.dispatcher = dispatcher;%0A%09%7D%0A%0A
 %09/**%0A%09 * @pa
@@ -2675,17 +2675,16 @@
 nce.%0A%09 *
- 
 %0A%09 * @re
@@ -2867,16 +2867,166 @@
 (true);%0A
+%09%09client.setCookieHandler(cookieHandler);%0A%09%09client.setSocketFactory(new LocalAddressSocketFactory(SocketFactory.getDefault(), localAddressProvider));%0A
 %09%09if (ss
@@ -3197,156 +3197,75 @@
 %7D%0A%09%09
-client.setCookieHandler(cookieHandler);%0A%09%09client.setSocketFactory(new LocalAddressSocketFactory(SocketFactory.getDefault(), localAddressProvid
+if (dispatcher != null) %7B%0A%09%09%09client.setDispatcher(dispatch
 er)
-)
 ;%0A
+%09%09%7D%0A
 %09%09re

Allow a custom Dispatcher to be injected