Skip to content

Commit

Permalink
Accept gzip encoded exchange rate feeds.
Browse files Browse the repository at this point in the history
  • Loading branch information
schildbach committed Mar 19, 2014
1 parent 3996d95 commit 0385064
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions wallet/src/de/schildbach/wallet/ExchangeRatesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigInteger;
Expand All @@ -30,6 +31,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.GZIPInputStream;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -256,12 +258,19 @@ private static Map<String, ExchangeRate> requestExchangeRates(final URL url, fin
connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS);
connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS);
connection.addRequestProperty("User-Agent", userAgent);
connection.addRequestProperty("Accept-Encoding", "gzip");
connection.connect();

final int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
{
reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024), Constants.UTF_8);
final String contentEncoding = connection.getContentEncoding();

InputStream is = new BufferedInputStream(connection.getInputStream(), 1024);
if ("gzip".equalsIgnoreCase(contentEncoding))
is = new GZIPInputStream(is);

reader = new InputStreamReader(is, Constants.UTF_8);
final StringBuilder content = new StringBuilder();
Io.copy(reader, content);

Expand Down Expand Up @@ -293,14 +302,14 @@ private static Map<String, ExchangeRate> requestExchangeRates(final URL url, fin
}
catch (final ArithmeticException x)
{
log.warn("problem fetching {} exchange rate from {}: {}", new Object[] { currencyCode, url, x.getMessage() });
log.warn("problem fetching {} exchange rate from {} ({}): {}", currencyCode, url, contentEncoding, x.getMessage());
}
}
}
}
}

log.info("fetched exchange rates from {}, took {} ms", url, (System.currentTimeMillis() - start));
log.info("fetched exchange rates from {} ({}), took {} ms", url, contentEncoding, (System.currentTimeMillis() - start));

return rates;
}
Expand Down

0 comments on commit 0385064

Please sign in to comment.