Example usage for com.squareup.okhttp.internal.tls OkHostnameVerifier INSTANCE

List of usage examples for com.squareup.okhttp.internal.tls OkHostnameVerifier INSTANCE

Introduction

In this page you can find the example usage for com.squareup.okhttp.internal.tls OkHostnameVerifier INSTANCE.

Prototype

OkHostnameVerifier INSTANCE

To view the source code for com.squareup.okhttp.internal.tls OkHostnameVerifier INSTANCE.

Click Source Link

Usage

From source file:com.granita.contacticloudsync.HttpClient.java

License:Open Source License

protected HttpClient(Logger log, Context context) {
    super();//from ww w  .  j  a v a  2  s . c  o  m
    this.log = (log != null) ? log : Constants.log;
    this.context = context;

    if (context != null) {
        // use MemorizingTrustManager to manage self-signed certificates
        MemorizingTrustManager mtm = new MemorizingTrustManager(context);
        setSslSocketFactory(new SSLSocketFactoryCompat(mtm));
        setHostnameVerifier(mtm.wrapHostnameVerifier(OkHostnameVerifier.INSTANCE));
    }

    // set timeouts
    setConnectTimeout(30, TimeUnit.SECONDS);
    setWriteTimeout(15, TimeUnit.SECONDS);
    setReadTimeout(45, TimeUnit.SECONDS);

    // add User-Agent to every request
    networkInterceptors().add(userAgentInterceptor);

    // add cookie store for non-persistent cookies (some services like Horde use cookies for session tracking)
    CookieManager cookies = new CookieManager();
    setCookieHandler(cookies);

    // enable verbose logs, if requested
    if (this.log.isTraceEnabled()) {
        HttpLoggingInterceptor logger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                BufferedReader reader = new BufferedReader(new StringReader(message));
                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        int len = line.length();
                        for (int pos = 0; pos < len; pos += MAX_LOG_LINE_LENGTH)
                            if (pos < len - MAX_LOG_LINE_LENGTH)
                                HttpClient.this.log
                                        .trace(line.substring(pos, pos + MAX_LOG_LINE_LENGTH) + "\\");
                            else
                                HttpClient.this.log.trace(line.substring(pos));
                    }
                } catch (IOException e) {
                    // for some reason, we couldn't split our message
                    HttpClient.this.log.trace(message);
                }
            }
        });
        logger.setLevel(HttpLoggingInterceptor.Level.BODY);
        interceptors().add(logger);
    }
}

From source file:com.tapchatapp.android.network.ssl.MemorizingHostnameVerifier.java

License:Apache License

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
@Override/*from   w w w.ja v a2 s  .  c  o  m*/
public boolean verify(final String hostname, final SSLSession session) {
    if (OkHostnameVerifier.INSTANCE.verify(hostname, session)) {
        return true;
    }

    final byte[] encodedCertificate = CertUtil.getEncodedCertificate(session);
    final String fingerprint = CertUtil.certHash(encodedCertificate, CertUtil.SHA1);
    final String base64Certificate = Base64.encodeToString(encodedCertificate, Base64.DEFAULT);

    if (getKnownCertificates(hostname).contains(base64Certificate)) {
        return true;
    }

    final Decision decision = createDecision();

    mHandler.post(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(mContext, VerifyHostnameActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(EXTRA_DECISION_ID, decision.id);
            intent.putExtra(EXTRA_HOSTNAME, hostname);
            intent.putExtra(EXTRA_FINGERPRINT, fingerprint);
            mContext.startActivity(intent);
        }
    });

    try {
        synchronized (decision) {
            decision.wait();
        }
    } catch (InterruptedException ignored) {
    }

    if (decision.allow) {
        addKnownCertificate(hostname, base64Certificate);
    }

    return decision.allow;
}

From source file:io.takari.aether.okhttp.OkHttpAetherClient.java

License:Open Source License

public OkHttpAetherClient(AetherClientConfig config) {
    this.config = config;

    // headers are modified during http auth handshake
    // make a copy to avoid cross-talk among client instances
    headers = new HashMap<String, String>();
    if (config.getHeaders() != null) {
        headers.putAll(config.getHeaders());
    }/*from ww  w .j  a  va  2s .c  om*/

    //
    // If the User-Agent has been overriden in the headers then we will use that
    //
    if (!headers.containsKey("User-Agent")) {
        headers.put("User-Agent", config.getUserAgent());
    }

    OkHttpClient httpClient = new OkHttpClient();
    httpClient.setProxy(getProxy(config.getProxy()));
    httpClient.setHostnameVerifier(OkHostnameVerifier.INSTANCE);
    httpClient.setAuthenticator(NOAUTH); // see #authenticate below
    httpClient.setConnectTimeout(config.getConnectionTimeout(), TimeUnit.MILLISECONDS);
    httpClient.setReadTimeout(config.getRequestTimeout(), TimeUnit.MILLISECONDS);
    httpClient.setSslSocketFactory(config.getSslSocketFactory());
    this.httpClient = httpClient;
}