Package org.lightcouch
Class CouchDbClientBase
java.lang.Object
org.lightcouch.CouchDbClientBase
- Direct Known Subclasses:
CouchDbClient
,CouchDbClientAndroid
Contains a client Public API implementation.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate URI
private CouchDbContext
private URI
private CouchDbDesign
private com.google.gson.Gson
(package private) final org.apache.http.HttpHost
(package private) final org.apache.http.client.HttpClient
(package private) static final org.apache.commons.logging.Log
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Saves a document with batch=ok query param.Performs bulk documents create and update request.changes()
Provides access to Change Notifications API.boolean
Checks if a document exist in the database.context()
Provides access to DB server APIs.(package private) abstract org.apache.http.protocol.HttpContext
(package private) abstract org.apache.http.client.HttpClient
createHttpClient
(CouchDbProperties properties) (package private) Response
Performs a HTTP DELETE request.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
Finds an Object of the specified type.<T> T
Finds an Object of the specified type.<T> T
Finds an Object of the specified type.Finds a document and return the result asInputStream
.Finds a document given id and revision and returns the result asInputStream
.<T> T
This method finds any document given a URI.<T> List
<T> Find documents using a declarative JSON querying syntax.(package private) InputStream
Performs a HTTP GET request.(package private) <T> T
Performs a HTTP GET request.(package private) InputStream
get
(org.apache.http.client.methods.HttpGet httpGet) Performs a HTTP GET request.getDBUri()
com.google.gson.Gson
getGson()
private Response
getResponse
(org.apache.http.HttpResponse response) getResponseList
(org.apache.http.HttpResponse response) (package private) org.apache.http.HttpResponse
Performs a HTTP HEAD request.private com.google.gson.Gson
initGson
(com.google.gson.GsonBuilder gsonBuilder) BuildsGson
and registers any required serializer/deserializer.invokeUpdateHandler
(String updateHandlerUri, String docId, Params params) Invokes an Update Handler.Saves an object in the database using HTTP POST request.(package private) org.apache.http.HttpResponse
Performs a HTTP POST request.(package private) Response
put
(URI uri, InputStream instream, String contentType) Performs a HTTP PUT request, saves an attachment.(package private) Response
Performs a HTTP PUT request, saves or updates a document.Removes a document from the database.Removes a document from the database given both a document_id
and_rev
values.Provides access to CouchDB replication APIs.Provides access to the replicator database.Saves an object in the database, using HTTP PUT request.saveAttachment
(InputStream in, String name, String contentType) Saves an attachment to a new document with a generated UUID as the document id.saveAttachment
(InputStream in, String name, String contentType, String docId, 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 asnull
.private void
Sets a JSON String as a request entity.void
setGsonBuilder
(com.google.gson.GsonBuilder gsonBuilder) Sets aGsonBuilder
to createGson
instance.(package private) abstract void
shutdown()
Shuts down the connection manager used by this client instance.void
Synchronize all design documents with the database.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.Provides access to CouchDB View APIs.
-
Field Details
-
log
static final org.apache.commons.logging.Log log -
baseURI
-
dbURI
-
gson
private com.google.gson.Gson gson -
context
-
design
-
httpClient
final org.apache.http.client.HttpClient httpClient -
host
final org.apache.http.HttpHost host
-
-
Constructor Details
-
CouchDbClientBase
CouchDbClientBase() -
CouchDbClientBase
CouchDbClientBase(CouchDbConfig config)
-
-
Method Details
-
createHttpClient
- Returns:
HttpClient
instance for HTTP request execution.
-
createContext
abstract org.apache.http.protocol.HttpContext createContext()- Returns:
HttpContext
instance for HTTP request execution.
-
shutdown
abstract void shutdown()Shuts down the connection manager used by this client instance. -
context
Provides access to DB server APIs.- Returns:
CouchDbContext
-
design
Provides access to CouchDB Design Documents.- Returns:
CouchDbDesign
-
view
Provides access to CouchDB View APIs.- Parameters:
viewId
- The view id.- Returns:
View
-
replication
Provides access to CouchDB replication APIs.- Returns:
Replication
-
replicator
Provides access to the replicator database.- Returns:
Replicator
-
changes
Provides access to Change Notifications API.- Returns:
Changes
-
find
Finds an Object of the specified type.- Type Parameters:
T
- Object type.- Parameters:
classType
- The class of type T.id
- The document id.- Returns:
- An object of type T.
- Throws:
NoDocumentException
- If the document is not found in the database.
-
find
Finds an Object of the specified type.- Type Parameters:
T
- Object type.- Parameters:
classType
- The class of type T.id
- The document id.params
- Extra parameters to append.- Returns:
- An object of type T.
- Throws:
NoDocumentException
- If the document is not found in the database.
-
find
Finds an Object of the specified type.- Type Parameters:
T
- Object type.- Parameters:
classType
- The class of type T.id
- The document _id field.rev
- The document _rev field.- Returns:
- An object of type T.
- Throws:
NoDocumentException
- If the document is not found in the database.
-
findAny
This method finds any document given a URI.The URI must be URI-encoded.
- Type Parameters:
T
- The class type.- Parameters:
classType
- The class of type T.uri
- The URI as string.- Returns:
- An object of type T.
-
find
Finds a document and return the result asInputStream
.Note: The stream must be closed after use to release the connection.
- Parameters:
id
- The document _id field.- Returns:
- The result as
InputStream
- Throws:
NoDocumentException
- If the document is not found in the database.- See Also:
-
find
Finds a document given id and revision and returns the result asInputStream
.Note: The stream must be closed after use to release the connection.
- Parameters:
id
- The document _id field.rev
- The document _rev field.- Returns:
- The result as
InputStream
- Throws:
NoDocumentException
- If the document is not found in the database.
-
findDocs
Find documents using a declarative JSON querying syntax.- Type Parameters:
T
- The class type.- Parameters:
jsonQuery
- The JSON query string.classOfT
- The class of type T.- Returns:
- The result of the query as a
List<T>
- Throws:
CouchDbException
- If the query failed to execute or the request is invalid.
-
contains
Checks if a document exist in the database.- Parameters:
id
- The document _id field.- Returns:
- true If the document is found, false otherwise.
-
save
Saves an object in the database, using HTTP PUT request.If the object doesn't have an
_id
value, the code will assign aUUID
as the document id.- Parameters:
object
- The object to save- Returns:
Response
- Throws:
DocumentConflictException
- If a conflict is detected during the save.
-
post
Saves an object in the database using HTTP POST request.The database will be responsible for generating the document id.
- Parameters:
object
- The object to save- Returns:
Response
-
batch
Saves a document with batch=ok query param.- Parameters:
object
- The object to save.
-
update
Updates an object in the database, the object must have the correct_id
and_rev
values.- Parameters:
object
- The object to update- Returns:
Response
- Throws:
DocumentConflictException
- If a conflict is detected during the update.
-
remove
Removes a document from the database.The object must have the correct
_id
and_rev
values.- Parameters:
object
- The document to remove as object.- Returns:
Response
- Throws:
NoDocumentException
- If the document is not found in the database.
-
remove
Removes a document from the database given both a document_id
and_rev
values.- Parameters:
id
- The document _id field.rev
- The document _rev field.- Returns:
Response
- Throws:
NoDocumentException
- If the document is not found in the database.
-
bulk
Performs bulk documents create and update request.- Parameters:
objects
- TheList
of documents objects.newEdits
- If false, prevents the database from assigning documents new revision IDs.- Returns:
List<Response>
Containing the resulted entries.
-
saveAttachment
Saves an attachment to a new document with a generated UUID as the document id.To retrieve an attachment, see
find(String)
.- Parameters:
in
- TheInputStream
holding the binary data.name
- The attachment name.contentType
- The attachment "Content-Type".- Returns:
Response
-
saveAttachment
public Response saveAttachment(InputStream in, String name, String contentType, String docId, 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 asnull
.To retrieve an attachment, see
find(String)
.- Parameters:
in
- TheInputStream
holding the binary data.name
- The attachment name.contentType
- The attachment "Content-Type".docId
- The document id to save the attachment under, ornull
to save under a new document.docRev
- The document revision to save the attachment under, ornull
when saving to a new document.- Returns:
Response
-
invokeUpdateHandler
Invokes an Update Handler.Params params = new Params() .addParam("field", "foo") .addParam("value", "bar"); String output = dbClient.invokeUpdateHandler("designDoc/update1", "docId", params);
- Parameters:
updateHandlerUri
- The Update Handler URI, in the format:designDoc/update1
docId
- The document id to update.params
- The query parameters asParams
.- Returns:
- The output of the request.
-
executeRequest
public org.apache.http.HttpResponse executeRequest(org.apache.http.client.methods.HttpRequestBase request) Executes a HTTP request.Note: The response must be closed after use to release the connection.
- Parameters:
request
- The HTTP request to execute.- Returns:
HttpResponse
-
syncDesignDocsWithDb
public void syncDesignDocsWithDb()Synchronize all design documents with the database. -
setGsonBuilder
public void setGsonBuilder(com.google.gson.GsonBuilder gsonBuilder) Sets aGsonBuilder
to createGson
instance.Useful for registering custom serializers/deserializers, such as JodaTime classes.
- Parameters:
gsonBuilder
- TheGsonBuilder
-
getBaseUri
- Returns:
- The base URI.
-
getDBUri
- Returns:
- The database URI.
-
getGson
public com.google.gson.Gson getGson()- Returns:
- The Gson instance.
-
get
Performs a HTTP GET request.- Returns:
InputStream
-
get
Performs a HTTP GET request.- Returns:
InputStream
-
get
Performs a HTTP GET request.- Returns:
- An object of type T
-
head
Performs a HTTP HEAD request.- Returns:
HttpResponse
-
put
Performs a HTTP PUT request, saves or updates a document.- Returns:
Response
-
put
Performs a HTTP PUT request, saves an attachment.- Returns:
Response
-
post
Performs a HTTP POST request.- Returns:
HttpResponse
-
delete
Performs a HTTP DELETE request.- Returns:
Response
-
validate
Validates a HTTP response; on error cases logs status and throws relevant exceptions.- Parameters:
response
- The HTTP response.- Throws:
IOException
-
getResponse
- Parameters:
response
- TheHttpResponse
- Returns:
Response
- Throws:
CouchDbException
-
getResponseList
private List<Response> getResponseList(org.apache.http.HttpResponse response) throws CouchDbException - Parameters:
response
- TheHttpResponse
- Returns:
Response
- Throws:
CouchDbException
-
setEntity
private void setEntity(org.apache.http.client.methods.HttpEntityEnclosingRequestBase httpRequest, String json) Sets a JSON String as a request entity.- Parameters:
httpRequest
- The request to set entity.json
- The JSON String to set.
-
initGson
private com.google.gson.Gson initGson(com.google.gson.GsonBuilder gsonBuilder) BuildsGson
and registers any required serializer/deserializer.- Returns:
Gson
instance
-