Skip to content

Commit

Permalink
Merge pull request #6277 from hieupham007/timob-17798-3_4_1
Browse files Browse the repository at this point in the history
timob-17798: take care of other types of ResponseCache
  • Loading branch information
ingo committed Oct 27, 2014
2 parents 41077d4 + 72e9619 commit e2234fe
Showing 1 changed file with 64 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,44 +182,80 @@ public void abort()
}
}

/**
* Check whether the content from uri has been cached. This method is optimized for
* TiResponseCache. For other kinds of ResponseCache, eg. HttpResponseCache, it only
* checks whether the system's default response cache is set.
* @param uri
* @return true if the content from uri is cached; false otherwise.
*/
public static boolean peek(URI uri)
{
TiResponseCache rc = (TiResponseCache) TiResponseCache.getDefault();
if (rc == null) return false;
if (rc.cacheDir == null) return false;

String hash = DigestUtils.shaHex(uri.toString());
File hFile = new File(rc.cacheDir, hash + HEADER_SUFFIX);
File bFile = new File(rc.cacheDir, hash + BODY_SUFFIX);
if (!bFile.exists() || !hFile.exists()) return false;
return true;
ResponseCache rcc = TiResponseCache.getDefault();

if (rcc instanceof TiResponseCache) {
// The default response cache is set by Titanium
TiResponseCache rc = (TiResponseCache) rcc;
if (rc.cacheDir == null) {
return false;
}
String hash = DigestUtils.shaHex(uri.toString());
File hFile = new File(rc.cacheDir, hash + HEADER_SUFFIX);
File bFile = new File(rc.cacheDir, hash + BODY_SUFFIX);
if (!bFile.exists() || !hFile.exists()) {
return false;
}
return true;

} else if (rcc != null) {
// The default response cache is set by other modules/sdks
return true;
}

return false;
}

/**
* Get the cached content for uri. It works for all kinds of ResponseCache.
* @param uri
* @return an InputStream of the cached content
*/
public static InputStream openCachedStream(URI uri)
{
TiResponseCache rc = (TiResponseCache) TiResponseCache.getDefault();
if (rc == null) {
return null;
}
ResponseCache rcc = TiResponseCache.getDefault();

if (rc.cacheDir == null) {
return null;
}

String hash = DigestUtils.shaHex(uri.toString());
File hFile = new File(rc.cacheDir, hash + HEADER_SUFFIX);
File bFile = new File(rc.cacheDir, hash + BODY_SUFFIX);
if (rcc instanceof TiResponseCache) {
// The default response cache is set by Titanium
TiResponseCache rc = (TiResponseCache) rcc;
if (rc.cacheDir == null) {
return null;
}
String hash = DigestUtils.shaHex(uri.toString());
File hFile = new File(rc.cacheDir, hash + HEADER_SUFFIX);
File bFile = new File(rc.cacheDir, hash + BODY_SUFFIX);
if (!bFile.exists() || !hFile.exists()) {
return null;
}
try {
return new FileInputStream(bFile);
} catch (FileNotFoundException e) {
// Fallback to URL download.
return null;
}

if (!bFile.exists() || !hFile.exists()) {
return null;
} else if (rcc != null) {
// The default response cache is set by other modules/sdks
try {
URLConnection urlc = uri.toURL().openConnection();
urlc.setRequestProperty("Cache-Control", "only-if-cached");
return urlc.getInputStream();
} catch (Exception e) {
// Not cached. Fallback to URL download.
return null;
}
}

try {
return new FileInputStream(bFile);
} catch (FileNotFoundException e) {
// Fallback to URL download?
return null;
}
return null;
}

public static void addCompleteListener(URI uri, CompleteListener listener)
Expand Down

0 comments on commit e2234fe

Please sign in to comment.