Search in sources :

Example 6 with SecureCacheResponse

use of java.net.SecureCacheResponse in project okhttp by square.

the class JavaApiConverter method createJavaCacheResponse.

/**
   * Creates a {@link java.net.CacheResponse} of the correct (sub)type using information gathered
   * from the supplied {@link Response}.
   */
public static CacheResponse createJavaCacheResponse(final Response response) {
    final Headers headers = withSyntheticHeaders(response);
    final ResponseBody body = response.body();
    if (response.request().isHttps()) {
        final Handshake handshake = response.handshake();
        return new SecureCacheResponse() {

            @Override
            public String getCipherSuite() {
                return handshake != null ? handshake.cipherSuite().javaName() : null;
            }

            @Override
            public List<Certificate> getLocalCertificateChain() {
                if (handshake == null)
                    return null;
                // Java requires null, not an empty list here.
                List<Certificate> certificates = handshake.localCertificates();
                return certificates.size() > 0 ? certificates : null;
            }

            @Override
            public List<Certificate> getServerCertificateChain() throws SSLPeerUnverifiedException {
                if (handshake == null)
                    return null;
                // Java requires null, not an empty list here.
                List<Certificate> certificates = handshake.peerCertificates();
                return certificates.size() > 0 ? certificates : null;
            }

            @Override
            public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
                if (handshake == null)
                    return null;
                return handshake.peerPrincipal();
            }

            @Override
            public Principal getLocalPrincipal() {
                if (handshake == null)
                    return null;
                return handshake.localPrincipal();
            }

            @Override
            public Map<String, List<String>> getHeaders() throws IOException {
                // Java requires that the entry with a null key be the status line.
                return JavaNetHeaders.toMultimap(headers, StatusLine.get(response).toString());
            }

            @Override
            public InputStream getBody() throws IOException {
                if (body == null)
                    return null;
                return body.byteStream();
            }
        };
    } else {
        return new CacheResponse() {

            @Override
            public Map<String, List<String>> getHeaders() throws IOException {
                // Java requires that the entry with a null key be the status line.
                return JavaNetHeaders.toMultimap(headers, StatusLine.get(response).toString());
            }

            @Override
            public InputStream getBody() throws IOException {
                if (body == null)
                    return null;
                return body.byteStream();
            }
        };
    }
}
Also used : CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) HttpHeaders(okhttp3.internal.http.HttpHeaders) Headers(okhttp3.Headers) JavaNetHeaders(okhttp3.internal.JavaNetHeaders) List(java.util.List) ResponseBody(okhttp3.ResponseBody) Handshake(okhttp3.Handshake) Certificate(java.security.cert.Certificate)

Example 7 with SecureCacheResponse

use of java.net.SecureCacheResponse in project XobotOS by xamarin.

the class HttpsURLConnectionImpl method getLocalCertificates.

@Override
public Certificate[] getLocalCertificates() {
    SecureCacheResponse cacheResponse = delegate.getCacheResponse();
    if (cacheResponse != null) {
        List<Certificate> result = cacheResponse.getLocalCertificateChain();
        return result != null ? result.toArray(new Certificate[result.size()]) : null;
    }
    checkConnected();
    return delegate.getSSLSocket().getSession().getLocalCertificates();
}
Also used : SecureCacheResponse(java.net.SecureCacheResponse) Certificate(java.security.cert.Certificate)

Example 8 with SecureCacheResponse

use of java.net.SecureCacheResponse in project XobotOS by xamarin.

the class HttpsURLConnectionImpl method getCipherSuite.

@Override
public String getCipherSuite() {
    SecureCacheResponse cacheResponse = delegate.getCacheResponse();
    if (cacheResponse != null) {
        return cacheResponse.getCipherSuite();
    }
    checkConnected();
    return delegate.getSSLSocket().getSession().getCipherSuite();
}
Also used : SecureCacheResponse(java.net.SecureCacheResponse)

Example 9 with SecureCacheResponse

use of java.net.SecureCacheResponse in project XobotOS by xamarin.

the class HttpsURLConnectionImpl method getLocalPrincipal.

@Override
public Principal getLocalPrincipal() {
    SecureCacheResponse cacheResponse = delegate.getCacheResponse();
    if (cacheResponse != null) {
        return cacheResponse.getLocalPrincipal();
    }
    checkConnected();
    return delegate.getSSLSocket().getSession().getLocalPrincipal();
}
Also used : SecureCacheResponse(java.net.SecureCacheResponse)

Example 10 with SecureCacheResponse

use of java.net.SecureCacheResponse in project robovm by robovm.

the class HttpsURLConnectionImpl method getLocalPrincipal.

@Override
public Principal getLocalPrincipal() {
    SecureCacheResponse cacheResponse = delegate.getSecureCacheResponse();
    if (cacheResponse != null) {
        return cacheResponse.getLocalPrincipal();
    }
    SSLSocket sslSocket = getSslSocket();
    if (sslSocket != null) {
        return sslSocket.getSession().getLocalPrincipal();
    }
    return null;
}
Also used : SecureCacheResponse(java.net.SecureCacheResponse) SSLSocket(javax.net.ssl.SSLSocket)

Aggregations

SecureCacheResponse (java.net.SecureCacheResponse)27 SSLSocket (javax.net.ssl.SSLSocket)15 Certificate (java.security.cert.Certificate)11 CacheResponse (java.net.CacheResponse)6 List (java.util.List)5 Request (okhttp3.Request)5 Response (okhttp3.Response)5 Handshake (okhttp3.Handshake)4 ResponseBody (okhttp3.ResponseBody)4 Test (org.junit.Test)4 Headers (okhttp3.Headers)3 URI (java.net.URI)2 JavaNetHeaders (okhttp3.internal.JavaNetHeaders)2 HttpHeaders (okhttp3.internal.http.HttpHeaders)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Proxy (java.net.Proxy)1 ProxySelector (java.net.ProxySelector)1 Principal (java.security.Principal)1 X509Certificate (java.security.cert.X509Certificate)1 LinkedHashMap (java.util.LinkedHashMap)1