package org.kohsuke.github.extras;

import com.squareup.okhttp.ConnectionSpec;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;

import org.kohsuke.github.HttpConnector;

import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.URL;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import java.util.Arrays;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

/**
 * {@link HttpConnector} for {@link OkHttpClient}.
 *
 * Unlike {@link #DEFAULT}, OkHttp does response caching.
 * Making a conditional request against GitHubAPI and receiving a 304
 * response does not count against the rate limit.
 * See http://developer.github.com/v3/#conditional-requests
 *
 * @author Roberto Tyley
 * @author Kohsuke Kawaguchi
 */
public class OkHttpConnector implements HttpConnector {
    private final OkUrlFactory urlFactory;

    private final String maxAgeHeaderValue;

    public OkHttpConnector(OkUrlFactory urlFactory) {
        this(urlFactory, 0);
    }

    /**
     * package private for tests to be able to change max-age for cache.
     * @param urlFactory
     * @param cacheMaxAge
     */
    OkHttpConnector(OkUrlFactory urlFactory, int cacheMaxAge) {
        urlFactory.client().setSslSocketFactory(TlsSocketFactory());
        urlFactory.client().setConnectionSpecs(TlsConnectionSpecs());
        this.urlFactory = urlFactory;

        if (cacheMaxAge >= 0 && urlFactory.client() != null) {
            maxAgeHeaderValue = "max-age=" + cacheMaxAge;
        } else {
            maxAgeHeaderValue = null;
        }
    }


    public HttpURLConnection connect(URL url) throws IOException {
        HttpURLConnection urlConnection = urlFactory.open(url);
        // Cache can be added after client is created so we have to check it for each call
        if (maxAgeHeaderValue != null && urlFactory.client().getCache() != null) {
            // By default OkHttp honors max-age, meaning it will use local cache
            // without checking the network within that time frame.
            // However, that can result in stale data being returned during that time so
            // we force network-based checking no matter how often the query is made.
            // OkHttp still automatically does ETag checking and returns cached data when
            // GitHub reports 304, but those do not count against rate limit.
            urlConnection.setRequestProperty("Cache-Control", maxAgeHeaderValue);
        }

        return urlConnection;
    }

    /** Returns TLSv1.2 only SSL Socket Factory. */
    private SSLSocketFactory TlsSocketFactory() {
        SSLContext sc;
        try {
            sc = SSLContext.getInstance("TLSv1.2");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        try {
            sc.init(null, null, null);
            return sc.getSocketFactory();
        } catch (KeyManagementException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /** Returns connection spec with TLS v1.2 in it */
    private List<ConnectionSpec> TlsConnectionSpecs() {
        return Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);
    }
}

@@ -29,16 +29,57 @@
 xtras;%0A%0A
+import com.squareup.okhttp.CacheControl;%0A
 import c
@@ -113,16 +113,16 @@
 onSpec;%0A
-
 import c
@@ -464,16 +464,54 @@
 il.List;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -991,16 +991,79 @@
 ector %7B%0A
+    private static final String HEADER_NAME = %22Cache-Control%22;%0A
     priv
@@ -1678,70 +1678,224 @@
 null
-) %7B%0A            maxAgeHeaderValue = %22max-age=%22 + cacheMaxAge
+ && urlFactory.client().getCache() != null) %7B%0A            maxAgeHeaderValue =  new CacheControl.Builder()%0A                .maxAge(cacheMaxAge, TimeUnit.SECONDS)%0A                .build()%0A                .toString()
 ;%0A
+%0A
     
@@ -2106,162 +2106,29 @@
     
-// Cache can be added after client is created so we have to check it for each call%0A        if (maxAgeHeaderValue != null && urlFactory.client().getCache()
+if (maxAgeHeaderValue
  != 
@@ -2672,31 +2672,27 @@
 roperty(
-%22Cache-Control%22
+HEADER_NAME
 , maxAge

Use CacheControl and only check for cache in constructor