/** * Performs a HTTP GET request. * @return {@link InputStream} */ InputStream get(URI uri) { HttpGet get = new HttpGet(uri); get.addHeader("Accept", "application/json"); return get(get); }
/** * @return {@link CouchDbInfo} Containing the DB server info. */ public CouchDbInfo info() { return dbc.get(buildUri(dbc.getDBUri()).build(), CouchDbInfo.class); }
/** * 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); } }
/** * @return All Server databases. */ public List<String> getAllDbs() { InputStream instream = null; try { Type typeOfList = new TypeToken<List<String>>() {}.getType(); instream = dbc.get(buildUri(dbc.getBaseUri()).path("_all_dbs").build()); Reader reader = new InputStreamReader(instream, Charsets.UTF_8); return dbc.getGson().fromJson(reader, typeOfList); } finally { close(instream); } }
/** * @return DB Server version. */ public String serverVersion() { InputStream instream = null; try { instream = dbc.get(buildUri(dbc.getBaseUri()).build()); Reader reader = new InputStreamReader(instream, Charsets.UTF_8); return getAsString(new JsonParser().parse(reader).getAsJsonObject(), "version"); } finally { close(instream); } }
/** * Gets a design document from the database. * @param id The document id * @return {@link DesignDocument} */ public DesignDocument getFromDb(String id) { assertNotEmpty(id, "id"); final URI uri = buildUri(dbc.getDBUri()).path(id).build(); return dbc.get(uri, DesignDocument.class); }
/** * Requests CouchDB creates a new database; if one doesn't exist. * @param dbName The Database name */ public void createDB(String dbName) { assertNotEmpty(dbName, "dbName"); InputStream getresp = null; HttpResponse putresp = null; final URI uri = buildUri(dbc.getBaseUri()).path(dbName).build(); try { getresp = dbc.get(uri); } catch (NoDocumentException e) { // db doesn't exist final HttpPut put = new HttpPut(uri); putresp = dbc.executeRequest(put); log.info(String.format("Created Database: '%s'", dbName)); } finally { close(getresp); close(putresp); } }
/** * Finds all documents in the replicator database. * @return a list of {@link ReplicatorDocument} */ public List<ReplicatorDocument> findAll() { InputStream instream = null; try { final URI uri = buildUri(dbURI).path("_all_docs").query("include_docs", "true").build(); final Reader reader = new InputStreamReader(instream = dbc.get(uri), Charsets.UTF_8); final JsonArray jsonArray = new JsonParser().parse(reader) .getAsJsonObject().getAsJsonArray("rows"); final List<ReplicatorDocument> list = new ArrayList<ReplicatorDocument>(); for (JsonElement jsonElem : jsonArray) { JsonElement elem = jsonElem.getAsJsonObject().get("doc"); if(!getAsString(elem.getAsJsonObject(), "_id").startsWith("_design")) { // skip design docs ReplicatorDocument rd = dbc.getGson().fromJson(elem, ReplicatorDocument.class); list.add(rd); } } return list; } finally { close(instream); } }
/** * This method finds any document given a URI. * <p>The URI must be URI-encoded. * @param <T> The class type. * @param classType The class of type T. * @param uri The URI as string. * @return An object of type T. */ public <T> T findAny(Class<T> classType, String uri) { assertNotEmpty(classType, "Class"); assertNotEmpty(uri, "uri"); return get(URI.create(uri), classType); }
/** * Finds a document and return the result as {@link InputStream}. * <p><b>Note</b>: The stream must be closed after use to release the connection. * @param id The document _id field. * @return The result as {@link InputStream} * @throws NoDocumentException If the document is not found in the database. * @see #find(String, String) */ public InputStream find(String id) { assertNotEmpty(id, "id"); return get(buildUri(getDBUri()).path(id).build()); }
/** * Requests Change notifications of feed type continuous. * <p>Feed notifications are accessed in an <i>iterator</i> style. * @return {@link Changes} */ public Changes continuousChanges() { final URI uri = uriBuilder.query("feed", "continuous").build(); httpGet = new HttpGet(uri); final InputStream in = dbc.get(httpGet); final InputStreamReader is = new InputStreamReader(in, Charsets.UTF_8); setReader(new BufferedReader(is)); return this; }
/** * Gets a design document from the database. * @param id The document id * @param rev The document revision * @return {@link DesignDocument} */ public DesignDocument getFromDb(String id, String rev) { assertNotEmpty(id, "id"); assertNotEmpty(id, "rev"); final URI uri = buildUri(dbc.getDBUri()).path(id).query("rev", rev).build(); return dbc.get(uri, DesignDocument.class); }
/** * Requests Change notifications of feed type normal. * @return {@link ChangesResult} */ public ChangesResult getChanges() { final URI uri = uriBuilder.query("feed", "normal").build(); return dbc.get(uri, ChangesResult.class); }
/** * Finds an Object of the specified type. * @param <T> Object type. * @param classType The class of type T. * @param id The document id. * @return An object of type T. * @throws NoDocumentException If the document is not found in the database. */ public <T> T find(Class<T> classType, String id) { assertNotEmpty(classType, "Class"); assertNotEmpty(id, "id"); final URI uri = buildUri(getDBUri()).pathEncoded(id).build(); return get(uri, classType); }
/** * Queries a view as an {@link InputStream} * <p>The stream should be properly closed after usage, as to avoid connection leaks. * @return The result as an {@link InputStream}. */ public InputStream queryForStream() { URI uri = uriBuilder.build(); if(allDocsKeys != null) { // bulk docs return getStream(dbc.post(uri, allDocsKeys)); } return dbc.get(uri); }
/** * Finds a document given id and revision and returns the result as {@link InputStream}. * <p><b>Note</b>: The stream must be closed after use to release the connection. * @param id The document _id field. * @param rev The document _rev field. * @return The result as {@link InputStream} * @throws NoDocumentException If the document is not found in the database. */ public InputStream find(String id, String rev) { assertNotEmpty(id, "id"); assertNotEmpty(rev, "rev"); final URI uri = buildUri(getDBUri()).path(id).query("rev", rev).build(); return get(uri); }
/** * Finds a document in the replicator database. * @return {@link ReplicatorDocument} */ public ReplicatorDocument find() { assertNotEmpty(replicatorDoc.getId(), "Doc id"); final URI uri = buildUri(dbURI).path(replicatorDoc.getId()).query("rev", replicatorDoc.getRevision()).build(); return dbc.get(uri, ReplicatorDocument.class); }
/** * Finds an Object of the specified type. * @param <T> Object type. * @param classType The class of type T. * @param id The document id. * @param params Extra parameters to append. * @return An object of type T. * @throws NoDocumentException If the document is not found in the database. */ public <T> T find(Class<T> classType, String id, Params params) { assertNotEmpty(classType, "Class"); assertNotEmpty(id, "id"); final URI uri = buildUri(getDBUri()).pathEncoded(id).query(params).build(); return get(uri, classType); }
/** * Finds an Object of the specified type. * @param <T> Object type. * @param classType The class of type T. * @param id The document _id field. * @param rev The document _rev field. * @return An object of type T. * @throws NoDocumentException If the document is not found in the database. */ public <T> T find(Class<T> classType, String id, String rev) { assertNotEmpty(classType, "Class"); assertNotEmpty(id, "id"); assertNotEmpty(id, "rev"); final URI uri = buildUri(getDBUri()).pathEncoded(id).query("rev", rev).build(); return get(uri, classType); }