mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-15 15:50:07 +00:00
104 lines
5.8 KiB
HTML
104 lines
5.8 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>OkHttpConnector.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">GitHub API for Java</a> > <a href="index.source.html" class="el_package">org.kohsuke.github.extras</a> > <span class="el_source">OkHttpConnector.java</span></div><h1>OkHttpConnector.java</h1><pre class="source lang-java linenums">package org.kohsuke.github.extras;
|
|
|
|
import com.squareup.okhttp.CacheControl;
|
|
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 java.util.concurrent.TimeUnit;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
|
/**
|
|
* {@link HttpConnector} for {@link OkHttpClient}.
|
|
* <p>
|
|
* 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
|
|
* @deprecated This class depends on an unsupported version of OkHttp. Switch to
|
|
* {@link org.kohsuke.github.extras.okhttp3.OkHttpConnector}.
|
|
* @see org.kohsuke.github.extras.okhttp3.OkHttpConnector
|
|
*/
|
|
@Deprecated
|
|
public class OkHttpConnector implements HttpConnector {
|
|
private static final String HEADER_NAME = "Cache-Control";
|
|
private final OkUrlFactory urlFactory;
|
|
|
|
private final String maxAgeHeaderValue;
|
|
|
|
/**
|
|
* Instantiates a new Ok http connector.
|
|
*
|
|
* @param urlFactory
|
|
* the url factory
|
|
*/
|
|
public OkHttpConnector(OkUrlFactory urlFactory) {
|
|
<span class="fc" id="L47"> this(urlFactory, 0);</span>
|
|
<span class="fc" id="L48"> }</span>
|
|
|
|
/**
|
|
* package private for tests to be able to change max-age for cache.
|
|
*
|
|
* @param urlFactory
|
|
* @param cacheMaxAge
|
|
*/
|
|
<span class="fc" id="L56"> OkHttpConnector(OkUrlFactory urlFactory, int cacheMaxAge) {</span>
|
|
<span class="fc" id="L57"> urlFactory.client().setSslSocketFactory(TlsSocketFactory());</span>
|
|
<span class="fc" id="L58"> urlFactory.client().setConnectionSpecs(TlsConnectionSpecs());</span>
|
|
<span class="fc" id="L59"> this.urlFactory = urlFactory;</span>
|
|
|
|
<span class="pc bpc" id="L61" title="1 of 6 branches missed."> if (cacheMaxAge >= 0 && urlFactory.client() != null && urlFactory.client().getCache() != null) {</span>
|
|
<span class="fc" id="L62"> maxAgeHeaderValue = new CacheControl.Builder().maxAge(cacheMaxAge, TimeUnit.SECONDS).build().toString();</span>
|
|
} else {
|
|
<span class="fc" id="L64"> maxAgeHeaderValue = null;</span>
|
|
}
|
|
<span class="fc" id="L66"> }</span>
|
|
|
|
public HttpURLConnection connect(URL url) throws IOException {
|
|
<span class="fc" id="L69"> HttpURLConnection urlConnection = urlFactory.open(url);</span>
|
|
<span class="fc bfc" id="L70" title="All 2 branches covered."> if (maxAgeHeaderValue != null) {</span>
|
|
// 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.
|
|
<span class="fc" id="L77"> urlConnection.setRequestProperty(HEADER_NAME, maxAgeHeaderValue);</span>
|
|
}
|
|
|
|
<span class="fc" id="L80"> return urlConnection;</span>
|
|
}
|
|
|
|
/** Returns TLSv1.2 only SSL Socket Factory. */
|
|
private SSLSocketFactory TlsSocketFactory() {
|
|
SSLContext sc;
|
|
try {
|
|
<span class="fc" id="L87"> sc = SSLContext.getInstance("TLSv1.2");</span>
|
|
<span class="nc" id="L88"> } catch (NoSuchAlgorithmException e) {</span>
|
|
<span class="nc" id="L89"> throw new RuntimeException(e.getMessage(), e);</span>
|
|
<span class="fc" id="L90"> }</span>
|
|
try {
|
|
<span class="fc" id="L92"> sc.init(null, null, null);</span>
|
|
<span class="fc" id="L93"> return sc.getSocketFactory();</span>
|
|
<span class="nc" id="L94"> } catch (KeyManagementException e) {</span>
|
|
<span class="nc" id="L95"> throw new RuntimeException(e.getMessage(), e);</span>
|
|
}
|
|
}
|
|
|
|
/** Returns connection spec with TLS v1.2 in it */
|
|
private List<ConnectionSpec> TlsConnectionSpecs() {
|
|
<span class="fc" id="L101"> return Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);</span>
|
|
}
|
|
}
|
|
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html> |