/** * Closes a REST response. */ private static void closeResponse(HttpConnection response) throws Exception { InputStream responseStream = response.responseAsInputStream(); IOUtils.closeQuietly(responseStream); }
/** * <p> * Return response body data from server as a byte array. * </p> * <p> * <b>Important:</b> you must call <code>execute()</code> before calling this method. * </p> * * @return Byte array of response body data from server, if any * @throws IOException if there was a problem reading data from the server */ public byte[] responseAsBytes() throws IOException { InputStream is = responseAsInputStream(); try { return IOUtils.toByteArray(is); } finally { Utils.close(is); disconnect(); } }
/** * <p>Execute the HttpConnection request and return the InputStream if there were no errors.</p> * <p>The stream <b>must</b> be closed after use.</p> * * @param connection the request HttpConnection * @return InputStream from the HttpConnection response * @throws CouchDbException for HTTP error codes or if there was an IOException */ public InputStream executeToInputStream(HttpConnection connection) throws CouchDbException { try { return execute(connection).responseAsInputStream(); } catch (IOException ioe) { throw new CouchDbException("Error retrieving server response", ioe); } }
@Override public boolean call(HttpConnection connection) throws IOException { if (sessionHasStarted(connection.responseAsInputStream())) { return storeCookiesFromResponse(connection.getConnection()); } else { // If the session did not start, consume the error stream to avoid // leaking connections. Utils.consumeAndCloseStream(connection.getConnection().getErrorStream ()); return false; } } }
/** * Returns a JSON entity as a JSON object. */ private static JsonObject getJSONEntityAsJsonObject(HttpConnection connection, URI uri) { // Optimized: Avoids the use of a temporary string. Charset charset = getPlainTextEntityCharset(connection); InputStream stream; try { stream = connection.responseAsInputStream(); } catch (IOException e) { throw new RuntimeException("Error starting to read from " + uri, e); } Reader reader = new InputStreamReader(new BufferedInputStream(stream), charset); return JSON_PARSER.parse(reader).getAsJsonObject(); }
/** * Reads an attachment from the database. * * The stream must be closed after usage, otherwise http connection leaks will occur. * * @param uri the attachment URI * @return the attachment in the form of an {@code InputStream}. */ private InputStream getAttachment(URI uri) { HttpConnection connection = Http.GET(uri); couchDbClient.execute(connection); try { return connection.responseAsInputStream(); } catch (IOException e) { throw new CouchDbException("Error retrieving response input stream.", e); } }
static JsonObject executeRequestWithResponseAsJson(ViewQueryParameters parameters, HttpConnection request) throws IOException { CloudantClient client = parameters.getClient(); InputStream response = client.executeRequest(request).responseAsInputStream(); return CouchDbUtil.getResponse(response, JsonObject.class, client.getGson()); } }
inputStream = connection.execute().responseAsInputStream(); } catch (IOException ioe) { cause = ioe;
public static List<String> getRemoteRevisionIDs(URI uri, CouchConfig config) throws Exception{ HttpConnection connection = Http.GET(uri); connection.requestInterceptors.addAll(config.getRequestInterceptors()); connection.responseInterceptors.addAll(config.getResponseInterceptors()); InputStream in = connection.execute().responseAsInputStream(); Map<String, Object> m = JSONUtils.fromJson(new InputStreamReader(in)); List<Object> revsInfo = (List<Object>)m.get("_revs_info"); List<String> revisions = new ArrayList<String>(revsInfo.size()); for(Object rev : revsInfo){ revisions.add(((Map<String, String>)rev).get("rev")); } return revisions; }
/** * Outputs a plain-text entity to a character accumulator. * * @param destination An Appendable (such as a StringBuilder, a Writer, or a PrintStream). * @throws RuntimeException if there is an exception reading the entity * @throws IOException if there is an exception writing to the destination */ private static void pipePlainTextEntity(Appendable destination, HttpConnection connection, URI uri) throws IOException { Charset charset = getPlainTextEntityCharset(connection); InputStream stream; try { stream = connection.responseAsInputStream(); } catch (IOException e) { throw new RuntimeException("Error starting to read from " + uri, e); } Reader reader = new InputStreamReader(new BufferedInputStream(stream), charset); try { pipeEntityContentAsChars(destination, reader, uri); } finally { try { reader.close(); } catch (IOException e) { // Ignore errors. We were only reading from a socket. } } }
responseStream = connection.responseAsInputStream(); List<Response> bulkResponses = getResponseList(responseStream, getGson(), DeserializationTypes.LC_RESPONSES);
@TestTemplate @RequiresCloudant public void testCookieAuthWithoutRetry() throws IOException { CookieInterceptor interceptor = new CookieInterceptor(CloudantClientHelper.COUCH_USERNAME, CloudantClientHelper.COUCH_PASSWORD, clientResource.get().getBaseUri().toString()); HttpConnection conn = new HttpConnection("POST", dbResource.get().getDBUri().toURL(), "application/json"); conn.responseInterceptors.add(interceptor); conn.requestInterceptors.add(interceptor); ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes()); // nothing read from stream assertEquals(data.getBytes().length, bis.available()); conn.setRequestBody(bis); HttpConnection responseConn = conn.execute(); // stream was read to end assertEquals(0, bis.available()); assertEquals(2, responseConn.getConnection().getResponseCode() / 100); //check the json Gson gson = new Gson(); InputStream is = responseConn.responseAsInputStream(); try { JsonObject response = gson.fromJson(new InputStreamReader(is), JsonObject.class); assertTrue(response.has("ok")); assertTrue(response.get("ok").getAsBoolean()); assertTrue(response.has("id")); assertTrue(response.has("rev")); } finally { is.close(); } }
InputStream is = responseConn.responseAsInputStream(); try { JsonObject response = gson.fromJson(new InputStreamReader(is), JsonObject.class);