use of com.turbomanage.httpclient.HttpResponse in project iosched by google.
the class RemoteConferenceDataFetcher method fetchConferenceDataIfNewer.
/**
* Fetches data from the remote server.
*
* @param refTimestamp The timestamp of the data to use as a reference; if the remote data is
* not newer than this timestamp, no data will be downloaded and this method
* will return null.
* @return The data downloaded, or null if there is no data to download
* @throws IOException if an error occurred during download.
*/
public String[] fetchConferenceDataIfNewer(String refTimestamp) throws IOException {
if (TextUtils.isEmpty(mManifestUrl)) {
LOGW(TAG, "Manifest URL is empty (remote sync disabled!).");
return null;
}
BasicHttpClient httpClient = new BasicHttpClient();
httpClient.setRequestLogger(mQuietLogger);
IOUtils.authorizeHttpClient(mContext, httpClient);
// warning in the log, because it might mean unnecessary data is being downloaded.
if (!TextUtils.isEmpty(refTimestamp)) {
if (TimeUtils.isValidFormatForIfModifiedSinceHeader(refTimestamp)) {
httpClient.addHeader("If-Modified-Since", refTimestamp);
} else {
LOGW(TAG, "Could not set If-Modified-Since HTTP header. Potentially downloading " + "unnecessary data. Invalid format of refTimestamp argument: " + refTimestamp);
}
}
HttpResponse response = httpClient.get(mManifestUrl, null);
if (response == null) {
LOGE(TAG, "Request for manifest returned null response.");
throw new IOException("Request for data manifest returned null response.");
}
int status = response.getStatus();
if (status == HttpURLConnection.HTTP_OK) {
LOGD(TAG, "Server returned HTTP_OK, so new data is available.");
mServerTimestamp = getLastModified(response);
LOGD(TAG, "Server timestamp for new data is: " + mServerTimestamp);
String body = response.getBodyAsString();
if (TextUtils.isEmpty(body)) {
LOGE(TAG, "Request for manifest returned empty data.");
throw new IOException("Error fetching conference data manifest: no data.");
}
LOGD(TAG, "Manifest " + mManifestUrl + " read, contents: " + body);
mBytesDownloaded += body.getBytes().length;
return processManifest(body);
} else if (status == HttpURLConnection.HTTP_NOT_MODIFIED) {
// data on the server is not newer than our data
LOGD(TAG, "HTTP_NOT_MODIFIED: data has not changed since " + refTimestamp);
return null;
} else {
LOGE(TAG, "Error fetching conference data: HTTP status " + status + " and manifest " + mManifestUrl);
throw new IOException("Error fetching conference data: HTTP status " + status);
}
}
use of com.turbomanage.httpclient.HttpResponse in project iosched by google.
the class ConferenceDataHandler method processMapOverlayFiles.
/**
* Synchronise the map overlay files either from the local assets (if available) or from a remote url.
*
* @param collection Set of tiles containing a local filename and remote url.
* @throws IOException
*/
private void processMapOverlayFiles(Collection<Tile> collection, boolean downloadAllowed) throws IOException, SVGParseException {
// clear the tile cache on disk if any tiles have been updated
boolean shouldClearCache = false;
// keep track of used files, unused files are removed
ArrayList<String> usedTiles = new ArrayList<>();
for (Tile tile : collection) {
final String filename = tile.filename;
final String url = tile.url;
usedTiles.add(filename);
if (!MapUtils.hasTile(mContext, filename)) {
shouldClearCache = true;
// copy or download the tile if it is not stored yet
if (MapUtils.hasTileAsset(mContext, filename)) {
// file already exists as an asset, copy it
MapUtils.copyTileAsset(mContext, filename);
} else if (downloadAllowed && !TextUtils.isEmpty(url)) {
try {
// download the file only if downloads are allowed and url is not empty
File tileFile = MapUtils.getTileFile(mContext, filename);
BasicHttpClient httpClient = new BasicHttpClient();
httpClient.setRequestLogger(mQuietLogger);
IOUtils.authorizeHttpClient(mContext, httpClient);
HttpResponse httpResponse = httpClient.get(url, null);
IOUtils.writeToFile(httpResponse.getBody(), tileFile);
// ensure the file is valid SVG
InputStream is = new FileInputStream(tileFile);
SVG svg = new SVGBuilder().readFromInputStream(is).build();
is.close();
} catch (IOException ex) {
LOGE(TAG, "FAILED downloading map overlay tile " + url + ": " + ex.getMessage(), ex);
} catch (SVGParseException ex) {
LOGE(TAG, "FAILED parsing map overlay tile " + url + ": " + ex.getMessage(), ex);
}
} else {
LOGD(TAG, "Skipping download of map overlay tile" + " (since downloadsAllowed=false)");
}
}
}
if (shouldClearCache) {
MapUtils.clearDiskCache(mContext);
}
MapUtils.removeUnusedTiles(mContext, usedTiles);
}
use of com.turbomanage.httpclient.HttpResponse in project iosched by google.
the class RemoteConferenceDataFetcher method fetchFile.
/**
* Fetches a file from the cache/network, from an absolute or relative URL. If the file is
* available in our cache, we read it from there; if not, we will download it from the network
* and cache it.
*
* @param url The URL to fetch the file from. The URL may be absolute or relative; if relative,
* it will be considered to be relative to the manifest URL.
* @return The contents of the file.
* @throws IOException If an error occurs.
*/
private String fetchFile(String url) throws IOException {
// If this is a relative url, consider it relative to the manifest URL
if (!url.contains("://")) {
if (TextUtils.isEmpty(mManifestUrl) || !mManifestUrl.contains("/")) {
LOGE(TAG, "Could not build relative URL based on manifest URL.");
return null;
}
int i = mManifestUrl.lastIndexOf('/');
url = mManifestUrl.substring(0, i) + "/" + url;
}
LOGD(TAG, "Attempting to fetch: " + sanitizeUrl(url));
// Check if we have it in our cache first
String body;
try {
body = loadFromCache(url);
if (!TextUtils.isEmpty(body)) {
// cache hit
mBytesReadFromCache += body.getBytes().length;
mCacheFilesToKeep.add(getCacheKey(url));
return body;
}
} catch (IOException ex) {
ex.printStackTrace();
LOGE(TAG, "IOException getting file from cache.");
// proceed anyway to attempt to download it from the network
}
BasicHttpClient client = new BasicHttpClient();
IOUtils.authorizeHttpClient(mContext, client);
client.setRequestLogger(mQuietLogger);
// We don't have the file on cache, so download it
LOGD(TAG, "Cache miss. Downloading from network: " + sanitizeUrl(url));
HttpResponse response = client.get(url, null);
if (response == null) {
throw new IOException("Request for URL " + sanitizeUrl(url) + " returned null response.");
}
LOGD(TAG, "HTTP response " + response.getStatus());
if (response.getStatus() == HttpURLConnection.HTTP_OK) {
body = response.getBodyAsString();
if (TextUtils.isEmpty(body)) {
throw new IOException("Got empty response when attempting to fetch " + sanitizeUrl(url) + url);
}
LOGD(TAG, "Successfully downloaded from network: " + sanitizeUrl(url));
mBytesDownloaded += body.getBytes().length;
writeToCache(url, body);
mCacheFilesToKeep.add(getCacheKey(url));
return body;
} else {
LOGE(TAG, "Failed to fetch from network: " + sanitizeUrl(url));
throw new IOException("Request for URL " + sanitizeUrl(url) + " failed with HTTP error " + response.getStatus());
}
}
Aggregations