private <T> T parse(Class<T> type, T instance) throws IOException { if (uc.getResponseCode()==304) return null; // special case handling for 304 unmodified, as the content will be "" InputStreamReader r = null; try { r = new InputStreamReader(wrapStream(uc.getInputStream()), "UTF-8"); String data = IOUtils.toString(r); if (type!=null) try { return MAPPER.readValue(data,type); } catch (JsonMappingException e) { throw (IOException)new IOException("Failed to deserialize "+data).initCause(e); } if (instance!=null) return MAPPER.readerForUpdating(instance).<T>readValue(data); return null; } finally { IOUtils.closeQuietly(r); } }
/** * Handle API error by either throwing it or by returning normally to retry. */ /*package*/ void handleApiError(IOException e) throws IOException { if (uc.getResponseCode() == 401) // Unauthorized == bad creds throw e; if ("0".equals(uc.getHeaderField("X-RateLimit-Remaining"))) { root.rateLimitHandler.onError(e,uc); } InputStream es = wrapStream(uc.getErrorStream()); try { if (es!=null) { if (e instanceof FileNotFoundException) { // pass through 404 Not Found to allow the caller to handle it intelligently throw (IOException) new FileNotFoundException(IOUtils.toString(es, "UTF-8")).initCause(e); } else throw (IOException) new IOException(IOUtils.toString(es, "UTF-8")).initCause(e); } else throw e; } finally { IOUtils.closeQuietly(es); } }
r = new InputStreamReader(wrapStream(uc.getInputStream()), "UTF-8"); String data = IOUtils.toString(r); if (type!=null)
r = new InputStreamReader(wrapStream(uc.getInputStream()), "UTF-8"); String data = IOUtils.toString(r); if (type!=null)
public InputStream asStream(String tailApiUrl) throws IOException { while (true) {// loop while API rate limit is hit setupConnection(root.getApiURL(tailApiUrl)); buildRequest(); try { return wrapStream(uc.getInputStream()); } catch (IOException e) { handleApiError(e); } } }
public InputStream asStream(String tailApiUrl) throws IOException { while (true) {// loop while API rate limit is hit setupConnection(root.getApiURL(tailApiUrl)); buildRequest(); try { return wrapStream(uc.getInputStream()); } catch (IOException e) { handleApiError(e); } finally { noteRateLimit(tailApiUrl); } } }
public InputStream asStream(String tailApiUrl) throws IOException { while (true) {// loop while API rate limit is hit setupConnection(root.getApiURL(tailApiUrl)); buildRequest(); try { return wrapStream(uc.getInputStream()); } catch (IOException e) { handleApiError(e); } finally { noteRateLimit(tailApiUrl); } } }
throw e; InputStream es = wrapStream(uc.getErrorStream()); if (es != null) { try {
throw e; InputStream es = wrapStream(uc.getErrorStream()); if (es != null) { try {