/** * @return {@link InputStream} of {@link HttpResponse} */ public static InputStream getStream(HttpResponse response) { try { return response.getEntity().getContent(); } catch (Exception e) { throw new CouchDbException("Error reading response. ", e); } }
e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace();
e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace();
/** * Executes a HTTP request. * <p><b>Note</b>: The response must be closed after use to release the connection. * @param request The HTTP request to execute. * @return {@link HttpResponse} */ public HttpResponse executeRequest(HttpRequestBase request) { try { return httpClient.execute(host, request, createContext()); } catch (IOException e) { request.abort(); throw new CouchDbException("Error executing request. ", e); } }
/** * Validates a HTTP response; on error cases logs status and throws relevant exceptions. * @param response The HTTP response. */ void validate(HttpResponse response) throws IOException { final int code = response.getStatusLine().getStatusCode(); if(code == 200 || code == 201 || code == 202) { // success (ok | created | accepted) return; } String reason = response.getStatusLine().getReasonPhrase(); switch (code) { case HttpStatus.SC_NOT_FOUND: { throw new NoDocumentException(reason); } case HttpStatus.SC_CONFLICT: { throw new DocumentConflictException(reason); } default: { // other errors: 400 | 401 | 500 etc. throw new CouchDbException(reason += EntityUtils.toString(response.getEntity())); } } }
/** * Performs a HTTP GET request. * @return An object of type T */ <T> T get(URI uri, Class<T> classType) { InputStream in = null; try { in = get(uri); return getGson().fromJson(new InputStreamReader(in, "UTF-8"), classType); } catch (UnsupportedEncodingException e) { throw new CouchDbException(e); } finally { close(in); } }
throw new CouchDbException(e);
action = json.get(ACTION).getAsString(); } catch (Exception e) { throw new CouchDbException("could not parse the given param!", e);
/** * Reads and sets the next feed in the stream. */ private boolean readNextRow() { boolean hasNext = false; try { if(!stop) { String row = ""; do { row = getReader().readLine(); } while(row.length() == 0); if(!row.startsWith("{\"last_seq\":")) { setNextRow(gson.fromJson(row, Row.class)); hasNext = true; } } } catch (Exception e) { terminate(); throw new CouchDbException("Error reading continuous stream.", e); } if(!hasNext) terminate(); return hasNext; }