Example usage for com.squareup.okhttp Cache Cache

List of usage examples for com.squareup.okhttp Cache Cache

Introduction

In this page you can find the example usage for com.squareup.okhttp Cache Cache.

Prototype

public Cache(File directory, long maxSize) 

Source Link

Usage

From source file:org.jboss.set.aphrodite.repository.services.github.AbstractGithubService.java

License:Apache License

public static boolean commonGithubInit(RepositoryConfig config) {
    cacheDir = System.getProperty("cacheDir");
    cacheName = System.getProperty("cacheName");

    try {//from   ww  w  .j a  va 2 s  . c o  m
        if (cacheDir == null || cacheName == null) {
            // no cache specified
            github = GitHub.connect(config.getUsername(), config.getPassword());
        } else {
            // use cache
            cacheFile = new File(cacheDir, cacheName);
            cacheSize = System.getProperty("cacheSize");
            if (cacheSize == null) {
                cache = new Cache(cacheFile, DEFAULT_CACHE_SIZE * 1024 * 1024); // default 20MB cache
            } else {
                int size = DEFAULT_CACHE_SIZE;
                try {
                    size = Integer.valueOf(cacheSize);
                } catch (NumberFormatException e) {
                    Utils.logWarnMessage(LOG, cacheSize + " is not a valid cache size. Use default size 20MB.");
                }
                cache = new Cache(cacheFile, size * 1024 * 1024); // default 20MB cache
            }

            // oauthAccessToken here, if you use text password, call .withPassword()
            github = new GitHubBuilder().withOAuthToken(config.getPassword(), config.getUsername())
                    .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache))))
                    .build();

        }
        user = github.getUser(config.getUsername());
        return github.isCredentialValid();
    } catch (IOException e) {
        Utils.logException(LOG, "Authentication failed for username: " + config.getUsername(), e);
    }
    return false;
}

From source file:org.jboss.set.aphrodite.repository.services.github.GithubPullRequestHomeService.java

License:Apache License

public boolean init(RepositoryConfig config) {
    boolean parentInitiated = super.init(config);
    if (!parentInitiated) {
        return false;
    }/*from ww w  .ja va 2 s .  com*/

    // Cache
    cacheDir = System.getProperty("cacheDir");
    cacheName = System.getProperty("cacheName");

    try {
        if (cacheDir == null || cacheName == null) {
            // no cache specified
            github = GitHub.connect(config.getUsername(), config.getPassword());
        } else {
            // use cache
            cacheFile = new File(cacheDir, cacheName);
            cacheSize = System.getProperty("cacheSize");
            if (cacheSize == null) {
                cache = new Cache(cacheFile, DEFAULT_CACHE_SIZE * 1024 * 1024); // default 20MB cache
            } else {
                int size = DEFAULT_CACHE_SIZE;
                try {
                    size = Integer.valueOf(cacheSize);
                } catch (NumberFormatException e) {
                    Utils.logWarnMessage(LOG, cacheSize + " is not a valid cache size. Use default size 20MB.");
                }
                cache = new Cache(cacheFile, size * 1024 * 1024); // default 20MB cache
            }

            // oauthAccessToken here, if you use text password, call .withPassword()
            github = new GitHubBuilder().withOAuthToken(config.getPassword(), config.getUsername())
                    .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache))))
                    .build();

        }
        return github.isCredentialValid();
    } catch (IOException e) {
        Utils.logException(LOG, "Authentication failed for RepositoryService: " + this.getClass().getName(), e);
    }
    return false;
}

From source file:org.jboss.set.aphrodite.repository.services.github.GitHubRepositoryService.java

License:Open Source License

@Override
public boolean init(RepositoryConfig config) {
    boolean parentInitiated = super.init(config);
    if (!parentInitiated)
        return false;

    // Cache//from  ww  w  . j  av a  2s . c  o m
    cacheDir = System.getProperty("cacheDir");
    cacheName = System.getProperty("cacheName");

    try {
        if (cacheDir == null || cacheName == null) {
            // no cache specified
            github = GitHub.connect(config.getUsername(), config.getPassword());
        } else {
            // use cache
            cacheFile = new File(cacheDir, cacheName);
            cacheSize = System.getProperty("cacheSize");
            if (cacheSize == null) {
                cache = new Cache(cacheFile, DEFAULT_CACHE_SIZE * 1024 * 1024); // default 20MB cache
            } else {
                int size = DEFAULT_CACHE_SIZE;
                try {
                    size = Integer.valueOf(cacheSize);
                } catch (NumberFormatException e) {
                    Utils.logWarnMessage(LOG, cacheSize + " is not a valid cache size. Use default size 20MB.");
                }
                cache = new Cache(cacheFile, size * 1024 * 1024); // default 20MB cache
            }

            // oauthAccessToken here, if you use text password, call .withPassword()
            github = new GitHubBuilder().withOAuthToken(config.getPassword(), config.getUsername())
                    .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache))))
                    .build();

        }
    } catch (IOException e) {
        Utils.logException(LOG, "Authentication failed for RepositoryService: " + this.getClass().getName(), e);
        return false;
    }
    return true;
}

From source file:org.tomahawk.libtomahawk.infosystem.hatchet.HatchetInfoPlugin.java

License:Open Source License

public HatchetInfoPlugin() {
    RequestInterceptor requestInterceptor = new RequestInterceptor() {
        @Override/*  ww w. j  a  va2s.  c o  m*/
        public void intercept(RequestFacade request) {
            if (!TomahawkUtils.isNetworkAvailable()) {
                int maxStale = 60 * 60 * 24 * 7; // tolerate 1-week stale
                request.addHeader("Cache-Control", "public, max-stale=" + maxStale);
            }
        }
    };
    OkHttpClient okHttpClient = new OkHttpClient();
    File cacheDir = new File(TomahawkApp.getContext().getCacheDir(), "responseCache");
    try {
        Cache cache = new Cache(cacheDir, 1024 * 1024 * 20);
        okHttpClient.setCache(cache);
    } catch (IOException e) {
        Log.e(TAG, "<init>: " + e.getClass() + ": " + e.getLocalizedMessage());
    }
    RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.BASIC)
            .setEndpoint(HATCHET_BASE_URL).setConverter(new JacksonConverter(InfoSystemUtils.getObjectMapper()))
            .setRequestInterceptor(requestInterceptor).setClient(new OkClient(okHttpClient)).build();
    mHatchet = restAdapter.create(Hatchet.class);
}

From source file:ph.devcon.android.base.module.APIModule.java

License:Apache License

@Provides
public RestAdapter provideRestAdapter() {
    int SIZE_OF_CACHE = 1024;
    OkHttpClient ok = new OkHttpClient();
    ok.setReadTimeout(30, TimeUnit.SECONDS);
    ok.setConnectTimeout(30, TimeUnit.SECONDS);
    try {/*from ww  w.  j a  va  2s. co m*/
        Cache responseCache = new Cache(DevConApplication.getInstance().getCacheDir(), SIZE_OF_CACHE);
        ok.setCache(responseCache);
    } catch (Exception e) {
        Log.d("OkHttp", "Unable to set http cache", e);
    }
    Executor executor = Executors.newCachedThreadPool();
    return new RestAdapter.Builder().setExecutors(executor, executor).setClient(new OkClient(ok))
            .setEndpoint(DevConApplication.API_ENDPOINT).setRequestInterceptor(new ApiRequestInterceptor())
            //                .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new AndroidLog("RETROFIT"))
            .build();
}

From source file:retrofit.MagnetRestAdapter.java

License:Apache License

@Override
public void onInit(Context context, Map<String, String> map, ApiCallback<Boolean> callback) {
    this.applicationContext = context;
    if (null != map && StringUtil.isNotEmpty(map.get("clientId"))
            && StringUtil.isNotEmpty(map.get("clientSecret"))) {
        isAuthRequired = true;/*from   w  w  w  .  ja  va 2s . c  om*/
    }

    // Set cache
    if (null == client.getCache()) {
        client.setCache(
                new Cache(new File(applicationContext.getCacheDir(), applicationContext.getPackageName()),
                        DEFAULT_CACHE_SIZE));
    }

    ConnectivityManager.getInstance(applicationContext).registerListener(requestManager);

    if (null != callback) {
        callback.success(true);
    }
}

From source file:travel.izi.sdk.IZITravel.java

License:Apache License

/**
 * Set HTTP response cache.//from  ww  w . j a  va2  s.  com
 *
 * @param cacheDirectory Cache directory.
 * @param cacheSize      Cache size in bytes.
 * @throws java.io.IOException
 */
public void setCache(File cacheDirectory, int cacheSize) throws IOException {
    mCache = new Cache(cacheDirectory, cacheSize);
    mOkHttpClient = null;
    mRestAdapter = null;
}