public abstract class CouchDbClientBase
extends java.lang.Object
CouchDbClient,
CouchDbClientAndroid| Modifier and Type | Field and Description |
|---|---|
private java.net.URI |
baseURI |
private CouchDbContext |
context |
private java.net.URI |
dbURI |
private CouchDbDesign |
design |
private com.google.gson.Gson |
gson |
(package private) org.apache.http.HttpHost |
host |
(package private) org.apache.http.client.HttpClient |
httpClient |
(package private) static org.apache.commons.logging.Log |
log |
| Constructor and Description |
|---|
CouchDbClientBase() |
CouchDbClientBase(CouchDbConfig config) |
| Modifier and Type | Method and Description |
|---|---|
void |
batch(java.lang.Object object)
Saves a document with batch=ok query param.
|
java.util.List<Response> |
bulk(java.util.List<?> objects,
boolean allOrNothing)
Performs a Bulk Documents insert request.
|
Changes |
changes()
Provides access to Change Notifications API.
|
boolean |
contains(java.lang.String id)
Checks if a document exist in the database.
|
CouchDbContext |
context()
Provides access to DB server APIs.
|
(package private) abstract org.apache.http.protocol.HttpContext |
createContext() |
(package private) abstract org.apache.http.client.HttpClient |
createHttpClient(CouchDbProperties properties) |
(package private) Response |
delete(java.net.URI uri)
Performs a HTTP DELETE request.
|
CouchDbDesign |
design()
Provides access to CouchDB Design Documents.
|
org.apache.http.HttpResponse |
executeRequest(org.apache.http.client.methods.HttpRequestBase request)
Executes a HTTP request.
|
<T> T |
find(java.lang.Class<T> classType,
java.lang.String id)
Finds an Object of the specified type.
|
<T> T |
find(java.lang.Class<T> classType,
java.lang.String id,
Params params)
Finds an Object of the specified type.
|
<T> T |
find(java.lang.Class<T> classType,
java.lang.String id,
java.lang.String rev)
Finds an Object of the specified type.
|
java.io.InputStream |
find(java.lang.String id)
Finds a document and return the result as
InputStream. |
java.io.InputStream |
find(java.lang.String id,
java.lang.String rev)
Finds a document given id and revision and returns the result as
InputStream. |
<T> T |
findAny(java.lang.Class<T> classType,
java.lang.String uri)
This method finds any document given a URI.
|
(package private) java.io.InputStream |
get(org.apache.http.client.methods.HttpGet httpGet)
Performs a HTTP GET request.
|
(package private) java.io.InputStream |
get(java.net.URI uri)
Performs a HTTP GET request.
|
(package private) <T> T |
get(java.net.URI uri,
java.lang.Class<T> classType)
Performs a HTTP GET request.
|
java.net.URI |
getBaseUri() |
java.net.URI |
getDBUri() |
com.google.gson.Gson |
getGson() |
private Response |
getResponse(org.apache.http.HttpResponse response) |
private java.util.List<Response> |
getResponseList(org.apache.http.HttpResponse response) |
(package private) org.apache.http.HttpResponse |
head(java.net.URI uri)
Performs a HTTP HEAD request.
|
private com.google.gson.Gson |
initGson(com.google.gson.GsonBuilder gsonBuilder)
Builds
Gson and registers any required serializer/deserializer. |
java.lang.String |
invokeUpdateHandler(java.lang.String updateHandlerUri,
java.lang.String docId,
Params params)
Invokes an Update Handler.
|
java.lang.String |
invokeUpdateHandler(java.lang.String updateHandlerUri,
java.lang.String docId,
java.lang.String query)
Invokes an Update Handler.
|
Response |
post(java.lang.Object object)
Saves an object in the database using HTTP POST request.
|
(package private) org.apache.http.HttpResponse |
post(java.net.URI uri,
java.lang.String json)
Performs a HTTP POST request.
|
(package private) Response |
put(java.net.URI uri,
java.io.InputStream instream,
java.lang.String contentType)
Performs a HTTP PUT request, saves an attachment.
|
(package private) Response |
put(java.net.URI uri,
java.lang.Object object,
boolean newEntity)
Performs a HTTP PUT request, saves or updates a document.
|
Response |
remove(java.lang.Object object)
Removes a document from the database.
|
Response |
remove(java.lang.String id,
java.lang.String rev)
Removes a document from the database given both a document
_id and _rev values. |
Replication |
replication()
Provides access to CouchDB replication APIs.
|
Replicator |
replicator()
Provides access to the replicator database.
|
Response |
save(java.lang.Object object)
Saves an object in the database, using HTTP PUT request.
|
Response |
saveAttachment(java.io.InputStream in,
java.lang.String name,
java.lang.String contentType)
Saves an attachment to a new document with a generated UUID as the document id.
|
Response |
saveAttachment(java.io.InputStream in,
java.lang.String name,
java.lang.String contentType,
java.lang.String docId,
java.lang.String docRev)
Saves an attachment to an existing document given both a document id
and revision, or save to a new document given only the id, and rev as
null. |
private void |
setEntity(org.apache.http.client.methods.HttpEntityEnclosingRequestBase httpRequest,
java.lang.String json)
Sets a JSON String as a request entity.
|
void |
setGsonBuilder(com.google.gson.GsonBuilder gsonBuilder)
Sets a
GsonBuilder to create Gson instance. |
(package private) abstract void |
shutdown()
Shuts down the connection manager used by this client instance.
|
void |
syncDesignDocsWithDb()
Synchronize all design documents with the database.
|
Response |
update(java.lang.Object object)
Updates an object in the database, the object must have the correct
_id and _rev values. |
(package private) void |
validate(org.apache.http.HttpResponse response)
Validates a HTTP response; on error cases logs status and throws relevant exceptions.
|
View |
view(java.lang.String viewId)
Provides access to CouchDB View APIs.
|
static final org.apache.commons.logging.Log log
private java.net.URI baseURI
private java.net.URI dbURI
private com.google.gson.Gson gson
private CouchDbContext context
private CouchDbDesign design
final org.apache.http.client.HttpClient httpClient
final org.apache.http.HttpHost host
CouchDbClientBase()
CouchDbClientBase(CouchDbConfig config)
abstract org.apache.http.client.HttpClient createHttpClient(CouchDbProperties properties)
HttpClient instance for HTTP request execution.abstract org.apache.http.protocol.HttpContext createContext()
HttpContext instance for HTTP request execution.abstract void shutdown()
public CouchDbContext context()
CouchDbContextpublic CouchDbDesign design()
CouchDbDesignpublic Replication replication()
Replicationpublic Replicator replicator()
Replicatorpublic <T> T find(java.lang.Class<T> classType,
java.lang.String id)
T - Object type.classType - The class of type T.id - The document id.NoDocumentException - If the document is not found in the database.public <T> T find(java.lang.Class<T> classType,
java.lang.String id,
Params params)
T - Object type.classType - The class of type T.id - The document id.params - Extra parameters to append.NoDocumentException - If the document is not found in the database.public <T> T find(java.lang.Class<T> classType,
java.lang.String id,
java.lang.String rev)
T - Object type.classType - The class of type T.id - The document _id field.rev - The document _rev field.NoDocumentException - If the document is not found in the database.public <T> T findAny(java.lang.Class<T> classType,
java.lang.String uri)
The URI must be URI-encoded.
classType - The class of type T.uri - The URI as string.public java.io.InputStream find(java.lang.String id)
InputStream.
Note: The stream must be closed after use to release the connection.
id - The document _id field.InputStreamNoDocumentException - If the document is not found in the database.find(String, String)public java.io.InputStream find(java.lang.String id,
java.lang.String rev)
InputStream.
Note: The stream must be closed after use to release the connection.
id - The document _id field.rev - The document _rev field.InputStreamNoDocumentException - If the document is not found in the database.public boolean contains(java.lang.String id)
id - The document _id field.public Response save(java.lang.Object object)
If the object doesn't have an _id value, the code will assign a UUID as the document id.
object - The object to saveResponseDocumentConflictException - If a conflict is detected during the save.public Response post(java.lang.Object object)
The database will be responsible for generating the document id.
object - The object to saveResponsepublic void batch(java.lang.Object object)
object - The object to save.public Response update(java.lang.Object object)
_id and _rev values.object - The object to updateResponseDocumentConflictException - If a conflict is detected during the update.public Response remove(java.lang.Object object)
The object must have the correct _id and _rev values.
object - The document to remove as object.ResponseNoDocumentException - If the document is not found in the database.public Response remove(java.lang.String id, java.lang.String rev)
_id and _rev values.id - The document _id field.rev - The document _rev field.ResponseNoDocumentException - If the document is not found in the database.public java.util.List<Response> bulk(java.util.List<?> objects, boolean allOrNothing)
objects - The List of objects.allOrNothing - Indicates whether the request has all-or-nothing semantics.List<Response> Containing the resulted entries.public Response saveAttachment(java.io.InputStream in, java.lang.String name, java.lang.String contentType)
To retrieve an attachment, see find(String).
instream - The InputStream holding the binary data.name - The attachment name.contentType - The attachment "Content-Type".Responsepublic Response saveAttachment(java.io.InputStream in, java.lang.String name, java.lang.String contentType, java.lang.String docId, java.lang.String docRev)
null.
To retrieve an attachment, see find(String).
instream - The InputStream holding the binary data.name - The attachment name.contentType - The attachment "Content-Type".docId - The document id to save the attachment under, or null to save under a new document.docRev - The document revision to save the attachment under, or null when saving to a new document.ResponseDocumentConflictExceptionpublic java.lang.String invokeUpdateHandler(java.lang.String updateHandlerUri,
java.lang.String docId,
java.lang.String query)
String query = "field=foo&value=bar";
String output = dbClient.invokeUpdateHandler("designDoc/update1", "docId", query);
updateHandlerUri - The Update Handler URI, in the format: designDoc/update1docId - The document id to update.query - The query string parameters, e.g, field=field1&value=value1public java.lang.String invokeUpdateHandler(java.lang.String updateHandlerUri,
java.lang.String docId,
Params params)
Use this method in particular when the docId contain special characters such as slashes (/).
Params params = new Params()
.addParam("field", "foo")
.addParam("value", "bar");
String output = dbClient.invokeUpdateHandler("designDoc/update1", "docId", params);
updateHandlerUri - The Update Handler URI, in the format: designDoc/update1docId - The document id to update.query - The query parameters as Params.public org.apache.http.HttpResponse executeRequest(org.apache.http.client.methods.HttpRequestBase request)
Note: The response must be closed after use to release the connection.
request - The HTTP request to execute.HttpResponsepublic void syncDesignDocsWithDb()
public void setGsonBuilder(com.google.gson.GsonBuilder gsonBuilder)
GsonBuilder to create Gson instance.
Useful for registering custom serializers/deserializers, such as JodaTime classes.
public java.net.URI getBaseUri()
public java.net.URI getDBUri()
public com.google.gson.Gson getGson()
java.io.InputStream get(org.apache.http.client.methods.HttpGet httpGet)
InputStreamjava.io.InputStream get(java.net.URI uri)
InputStream<T> T get(java.net.URI uri,
java.lang.Class<T> classType)
org.apache.http.HttpResponse head(java.net.URI uri)
HttpResponseResponse put(java.net.URI uri, java.lang.Object object, boolean newEntity)
ResponseResponse put(java.net.URI uri, java.io.InputStream instream, java.lang.String contentType)
Responseorg.apache.http.HttpResponse post(java.net.URI uri,
java.lang.String json)
HttpResponsevoid validate(org.apache.http.HttpResponse response)
throws java.io.IOException
response - The HTTP response.java.io.IOExceptionprivate Response getResponse(org.apache.http.HttpResponse response) throws CouchDbException
response - The HttpResponseResponseCouchDbExceptionprivate java.util.List<Response> getResponseList(org.apache.http.HttpResponse response) throws CouchDbException
response - The HttpResponseResponseCouchDbExceptionprivate void setEntity(org.apache.http.client.methods.HttpEntityEnclosingRequestBase httpRequest,
java.lang.String json)
httpRequest - The request to set entity.json - The JSON String to set.private com.google.gson.Gson initGson(com.google.gson.GsonBuilder gsonBuilder)
Gson and registers any required serializer/deserializer.Gson instance