Package org.lightcouch
Class CouchDbClientBase
- java.lang.Object
-
- org.lightcouch.CouchDbClientBase
-
- Direct Known Subclasses:
CouchDbClient,CouchDbClientAndroid
public abstract class CouchDbClientBase extends java.lang.ObjectContains a client Public API implementation.- See Also:
CouchDbClient,CouchDbClientAndroid
-
-
Field Summary
Fields Modifier and Type Field Description private java.net.URIbaseURIprivate CouchDbContextcontextprivate java.net.URIdbURIprivate CouchDbDesigndesignprivate com.google.gson.Gsongson(package private) org.apache.http.HttpHosthost(package private) org.apache.http.client.HttpClienthttpClient(package private) static org.apache.commons.logging.Loglog
-
Constructor Summary
Constructors Constructor Description CouchDbClientBase()CouchDbClientBase(CouchDbConfig config)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidbatch(java.lang.Object object)Saves a document with batch=ok query param.java.util.List<Response>bulk(java.util.List<?> objects, boolean newEdits)Performs bulk documents create and update request.Changeschanges()Provides access to Change Notifications API.booleancontains(java.lang.String id)Checks if a document exist in the database.CouchDbContextcontext()Provides access to DB server APIs.(package private) abstract org.apache.http.protocol.HttpContextcreateContext()(package private) abstract org.apache.http.client.HttpClientcreateHttpClient(CouchDbProperties properties)(package private) Responsedelete(java.net.URI uri)Performs a HTTP DELETE request.CouchDbDesigndesign()Provides access to CouchDB Design Documents.org.apache.http.HttpResponseexecuteRequest(org.apache.http.client.methods.HttpRequestBase request)Executes a HTTP request.<T> Tfind(java.lang.Class<T> classType, java.lang.String id)Finds an Object of the specified type.<T> Tfind(java.lang.Class<T> classType, java.lang.String id, java.lang.String rev)Finds an Object of the specified type.<T> Tfind(java.lang.Class<T> classType, java.lang.String id, Params params)Finds an Object of the specified type.java.io.InputStreamfind(java.lang.String id)Finds a document and return the result asInputStream.java.io.InputStreamfind(java.lang.String id, java.lang.String rev)Finds a document given id and revision and returns the result asInputStream.<T> TfindAny(java.lang.Class<T> classType, java.lang.String uri)This method finds any document given a URI.<T> java.util.List<T>findDocs(java.lang.String jsonQuery, java.lang.Class<T> classOfT)Find documents using a declarative JSON querying syntax.(package private) java.io.InputStreamget(java.net.URI uri)Performs a HTTP GET request.(package private) <T> Tget(java.net.URI uri, java.lang.Class<T> classType)Performs a HTTP GET request.(package private) java.io.InputStreamget(org.apache.http.client.methods.HttpGet httpGet)Performs a HTTP GET request.java.net.URIgetBaseUri()java.net.URIgetDBUri()com.google.gson.GsongetGson()private ResponsegetResponse(org.apache.http.HttpResponse response)private java.util.List<Response>getResponseList(org.apache.http.HttpResponse response)(package private) org.apache.http.HttpResponsehead(java.net.URI uri)Performs a HTTP HEAD request.private com.google.gson.GsoninitGson(com.google.gson.GsonBuilder gsonBuilder)BuildsGsonand registers any required serializer/deserializer.java.lang.StringinvokeUpdateHandler(java.lang.String updateHandlerUri, java.lang.String docId, Params params)Invokes an Update Handler.Responsepost(java.lang.Object object)Saves an object in the database using HTTP POST request.(package private) org.apache.http.HttpResponsepost(java.net.URI uri, java.lang.String json)Performs a HTTP POST request.(package private) Responseput(java.net.URI uri, java.io.InputStream instream, java.lang.String contentType)Performs a HTTP PUT request, saves an attachment.(package private) Responseput(java.net.URI uri, java.lang.Object object, boolean newEntity)Performs a HTTP PUT request, saves or updates a document.Responseremove(java.lang.Object object)Removes a document from the database.Responseremove(java.lang.String id, java.lang.String rev)Removes a document from the database given both a document_idand_revvalues.Replicationreplication()Provides access to CouchDB replication APIs.Replicatorreplicator()Provides access to the replicator database.Responsesave(java.lang.Object object)Saves an object in the database, using HTTP PUT request.ResponsesaveAttachment(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.ResponsesaveAttachment(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 asnull.private voidsetEntity(org.apache.http.client.methods.HttpEntityEnclosingRequestBase httpRequest, java.lang.String json)Sets a JSON String as a request entity.voidsetGsonBuilder(com.google.gson.GsonBuilder gsonBuilder)Sets aGsonBuilderto createGsoninstance.(package private) abstract voidshutdown()Shuts down the connection manager used by this client instance.voidsyncDesignDocsWithDb()Synchronize all design documents with the database.Responseupdate(java.lang.Object object)Updates an object in the database, the object must have the correct_idand_revvalues.(package private) voidvalidate(org.apache.http.HttpResponse response)Validates a HTTP response; on error cases logs status and throws relevant exceptions.Viewview(java.lang.String viewId)Provides access to CouchDB View APIs.
-
-
-
Field Detail
-
log
static final org.apache.commons.logging.Log log
-
baseURI
private java.net.URI baseURI
-
dbURI
private java.net.URI dbURI
-
gson
private com.google.gson.Gson gson
-
context
private CouchDbContext context
-
design
private CouchDbDesign design
-
httpClient
final org.apache.http.client.HttpClient httpClient
-
host
final org.apache.http.HttpHost host
-
-
Constructor Detail
-
CouchDbClientBase
CouchDbClientBase()
-
CouchDbClientBase
CouchDbClientBase(CouchDbConfig config)
-
-
Method Detail
-
createHttpClient
abstract org.apache.http.client.HttpClient createHttpClient(CouchDbProperties properties)
- Returns:
HttpClientinstance for HTTP request execution.
-
createContext
abstract org.apache.http.protocol.HttpContext createContext()
- Returns:
HttpContextinstance for HTTP request execution.
-
shutdown
abstract void shutdown()
Shuts down the connection manager used by this client instance.
-
context
public CouchDbContext context()
Provides access to DB server APIs.- Returns:
CouchDbContext
-
design
public CouchDbDesign design()
Provides access to CouchDB Design Documents.- Returns:
CouchDbDesign
-
view
public View view(java.lang.String viewId)
Provides access to CouchDB View APIs.- Parameters:
viewId- The view id.- Returns:
View
-
replication
public Replication replication()
Provides access to CouchDB replication APIs.- Returns:
Replication
-
replicator
public Replicator replicator()
Provides access to the replicator database.- Returns:
Replicator
-
find
public <T> T find(java.lang.Class<T> classType, java.lang.String id)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
public <T> T find(java.lang.Class<T> classType, java.lang.String id, Params params)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
public <T> T find(java.lang.Class<T> classType, java.lang.String id, java.lang.String rev)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
public <T> T findAny(java.lang.Class<T> classType, java.lang.String uri)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
public java.io.InputStream find(java.lang.String id)
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(String, String)
-
find
public java.io.InputStream find(java.lang.String id, java.lang.String rev)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
public <T> java.util.List<T> findDocs(java.lang.String jsonQuery, java.lang.Class<T> classOfT)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
public boolean contains(java.lang.String id)
Checks if a document exist in the database.- Parameters:
id- The document _id field.- Returns:
- true If the document is found, false otherwise.
-
save
public Response save(java.lang.Object object)
Saves an object in the database, using HTTP PUT request.If the object doesn't have an
_idvalue, the code will assign aUUIDas the document id.- Parameters:
object- The object to save- Returns:
Response- Throws:
DocumentConflictException- If a conflict is detected during the save.
-
post
public Response post(java.lang.Object object)
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
public void batch(java.lang.Object object)
Saves a document with batch=ok query param.- Parameters:
object- The object to save.
-
update
public Response update(java.lang.Object object)
Updates an object in the database, the object must have the correct_idand_revvalues.- Parameters:
object- The object to update- Returns:
Response- Throws:
DocumentConflictException- If a conflict is detected during the update.
-
remove
public Response remove(java.lang.Object object)
Removes a document from the database.The object must have the correct
_idand_revvalues.- Parameters:
object- The document to remove as object.- Returns:
Response- Throws:
NoDocumentException- If the document is not found in the database.
-
remove
public Response remove(java.lang.String id, java.lang.String rev)
Removes a document from the database given both a document_idand_revvalues.- 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
public java.util.List<Response> bulk(java.util.List<?> objects, boolean newEdits)
Performs bulk documents create and update request.- Parameters:
objects- TheListof documents objects.newEdits- If false, prevents the database from assigning documents new revision IDs.- Returns:
List<Response>Containing the resulted entries.
-
saveAttachment
public 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.To retrieve an attachment, see
find(String).- Parameters:
in- TheInputStreamholding the binary data.name- The attachment name.contentType- The attachment "Content-Type".- Returns:
Response
-
saveAttachment
public 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 asnull.To retrieve an attachment, see
find(String).- Parameters:
in- TheInputStreamholding the binary data.name- The attachment name.contentType- The attachment "Content-Type".docId- The document id to save the attachment under, ornullto save under a new document.docRev- The document revision to save the attachment under, ornullwhen saving to a new document.- Returns:
Response
-
invokeUpdateHandler
public java.lang.String invokeUpdateHandler(java.lang.String updateHandlerUri, java.lang.String docId, Params params)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/update1docId- 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 aGsonBuilderto createGsoninstance.Useful for registering custom serializers/deserializers, such as JodaTime classes.
- Parameters:
gsonBuilder- TheGsonBuilder
-
getBaseUri
public java.net.URI getBaseUri()
- Returns:
- The base URI.
-
getDBUri
public java.net.URI getDBUri()
- Returns:
- The database URI.
-
getGson
public com.google.gson.Gson getGson()
- Returns:
- The Gson instance.
-
get
java.io.InputStream get(org.apache.http.client.methods.HttpGet httpGet)
Performs a HTTP GET request.- Returns:
InputStream
-
get
java.io.InputStream get(java.net.URI uri)
Performs a HTTP GET request.- Returns:
InputStream
-
get
<T> T get(java.net.URI uri, java.lang.Class<T> classType)Performs a HTTP GET request.- Returns:
- An object of type T
-
head
org.apache.http.HttpResponse head(java.net.URI uri)
Performs a HTTP HEAD request.- Returns:
HttpResponse
-
put
Response put(java.net.URI uri, java.lang.Object object, boolean newEntity)
Performs a HTTP PUT request, saves or updates a document.- Returns:
Response
-
put
Response put(java.net.URI uri, java.io.InputStream instream, java.lang.String contentType)
Performs a HTTP PUT request, saves an attachment.- Returns:
Response
-
post
org.apache.http.HttpResponse post(java.net.URI uri, java.lang.String json)Performs a HTTP POST request.- Returns:
HttpResponse
-
validate
void validate(org.apache.http.HttpResponse response) throws java.io.IOExceptionValidates a HTTP response; on error cases logs status and throws relevant exceptions.- Parameters:
response- The HTTP response.- Throws:
java.io.IOException
-
getResponse
private Response getResponse(org.apache.http.HttpResponse response) throws CouchDbException
- Parameters:
response- TheHttpResponse- Returns:
Response- Throws:
CouchDbException
-
getResponseList
private java.util.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, java.lang.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)
BuildsGsonand registers any required serializer/deserializer.- Returns:
Gsoninstance
-
-