@Override public void parse() { if (Constants.MIMETYPE_PAYMENTREQUEST.equals(inputType)) { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); Io.copy(is, baos); parseAndHandlePaymentRequest(baos.toByteArray()); } catch (final IOException x) { log.info("i/o error while fetching payment request", x); error(R.string.input_parser_io_error, x.getMessage()); } catch (final PkiVerificationException x) { log.info("got unverifyable payment request", x); error(R.string.input_parser_unverifyable_paymentrequest, x.getMessage()); } catch (final PaymentRequestException x) { log.info("got invalid payment request", x); error(R.string.input_parser_invalid_paymentrequest, x.getMessage()); } finally { try { if (baos != null) baos.close(); } catch (IOException x) { x.printStackTrace(); } try { is.close(); } catch (IOException x) { x.printStackTrace(); } } } else { cannotClassify(inputType); } }
private static float requestDogeBtcConversion(int provider) { HttpURLConnection connection = null; Reader reader = null; URL providerUrl; switch (provider) { case 0: providerUrl = DOGEPOOL_URL; break; case 1: providerUrl = VIRCUREX_URL; break; default: providerUrl = DOGEPOOL_URL; break; } try { connection = (HttpURLConnection) providerUrl.openConnection(); connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS); connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS); connection.connect(); final int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { reader = new InputStreamReader( new BufferedInputStream(connection.getInputStream(), 1024), Constants.UTF_8); final StringBuilder content = new StringBuilder(); Io.copy(reader, content); try { float rate; switch (provider) { case 0: /*rate = Float.parseFloat( json.getJSONObject("return") .getJSONObject("markets") .getJSONObject("DOGE") .getString("lasttradeprice"));*/ // For later use. rate = Float.parseFloat(content.toString()); break; case 1: final JSONObject json = new JSONObject(content.toString()); rate = Float.parseFloat(json.getString("value")); break; default: return -1; } return rate; } catch (NumberFormatException e) { log.debug( "Couldn't get the current exchnage rate from provider " + String.valueOf(provider)); return -1; } } else { log.debug("http status " + responseCode + " when fetching " + providerUrl); } } catch (final Exception x) { log.debug("problem reading exchange rates", x); } finally { if (reader != null) { try { reader.close(); } catch (final IOException x) { // swallow } } if (connection != null) connection.disconnect(); } return -1; }
private static Map<String, ExchangeRate> requestExchangeRates( final URL url, float dogeBtcConversion, final String userAgent, final String source, final String... fields) { final long start = System.currentTimeMillis(); HttpURLConnection connection = null; Reader reader = null; try { connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); 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) { 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(); final long length = Io.copy(reader, content); final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); final JSONObject head = new JSONObject(content.toString()); for (final Iterator<String> i = head.keys(); i.hasNext(); ) { final String currencyCode = i.next(); if (!"timestamp".equals(currencyCode)) { final JSONObject o = head.getJSONObject(currencyCode); for (final String field : fields) { final String rate = o.optString(field, null); if (rate != null) { try { BigDecimal btcRate = new BigDecimal(GenericUtils.toNanoCoins(rate, 0)); BigInteger dogeRate = btcRate.multiply(BigDecimal.valueOf(dogeBtcConversion)).toBigInteger(); if (dogeRate.signum() > 0) { rates.put(currencyCode, new ExchangeRate(currencyCode, dogeRate, source)); break; } } catch (final ArithmeticException x) { log.warn( "problem fetching {} exchange rate from {} ({}): {}", currencyCode, url, contentEncoding, x.getMessage()); } } } } } log.info( "fetched exchange rates from {} ({}), {} chars, took {} ms", url, contentEncoding, length, System.currentTimeMillis() - start); return rates; } else { log.warn("http status {} when fetching {}", responseCode, url); } } catch (final Exception x) { log.warn("problem fetching exchange rates from " + url, x); } finally { if (reader != null) { try { reader.close(); } catch (final IOException x) { // swallow } } if (connection != null) connection.disconnect(); } return null; }