com.fourspaces.couchdb
Class Session

java.lang.Object
  extended by com.fourspaces.couchdb.Session

public class Session
extends Object

The Session is the main connection to the CouchDB instance. However, you'll only use the Session to obtain a reference to a CouchDB Database. All of the main work happens at the Database level.

It uses the Apache's HttpClient library for all communication with the server. This is a little more robust than the standard URLConnection.

Ex usage:
Session session = new Session(host,port); Database db = session.getDatabase("dbname");

Author:
mbreese, brennanjubb - HTTP-Auth username/pass

Field Summary
protected  String host
           
protected  org.apache.http.client.HttpClient httpClient
           
protected  CouchResponse lastResponse
           
protected  org.apache.commons.logging.Log log
           
protected  String pass
           
protected  int port
           
protected  boolean secure
           
protected  String user
           
protected  boolean usesAuth
           
 
Constructor Summary
Session(String host, int port)
          Main constructor for obtaining a Session.
Session(String host, int port, boolean secure)
          Optional constructor that indicates an HTTPS connection should be used.
Session(String host, int port, String user, String pass)
          Constructor for obtaining a Session with an HTTP-AUTH username/password This isn't supported by CouchDB - you need a proxy in front to use this
Session(String host, int port, String user, String pass, boolean usesAuth, boolean secure)
          Constructor for obtaining a Session with an HTTP-AUTH username/password and (optionally) a secure connection This isn't supported by CouchDB - you need a proxy in front to use this
 
Method Summary
protected  String buildUrl(String url)
          For a given url (such as /_all_dbs/), build the database connection url
protected  String buildUrl(String url, org.apache.http.NameValuePair[] params)
           
protected  String buildUrl(String url, String queryString)
           
 Database createDatabase(String name)
          Creates a new database (if the name doesn't already exist)
 boolean deleteDatabase(Database db)
          Deletes a database from the CouchDB server
 boolean deleteDatabase(String name)
          Deletes a database (by name) from the CouchDB server.
protected  String encodeParameter(String paramValue)
           
 Database getDatabase(String name)
          Loads a database instance from the server
 List<String> getDatabaseNames()
          Retrieves a list of all database names from the server
 String getHost()
          Read-only
 CouchResponse getLastResponse()
          Returns the last response for this given session - useful for debugging purposes
 int getPort()
          read-only
protected  CouchResponse http(org.apache.http.client.methods.HttpRequestBase req)
          Method that actually performs the GET/PUT/POST/DELETE calls.
 boolean isSecure()
          Is this a secured connection (set in constructor)
 void setConnectionTimeout(int milliseconds)
           
 void setSocketTimeout(int milliseconds)
           
 void setUserAgent(String ua)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected org.apache.commons.logging.Log log

host

protected final String host

port

protected final int port

user

protected final String user

pass

protected final String pass

secure

protected final boolean secure

usesAuth

protected final boolean usesAuth

lastResponse

protected CouchResponse lastResponse

httpClient

protected org.apache.http.client.HttpClient httpClient
Constructor Detail

Session

public Session(String host,
               int port,
               String user,
               String pass,
               boolean usesAuth,
               boolean secure)
Constructor for obtaining a Session with an HTTP-AUTH username/password and (optionally) a secure connection This isn't supported by CouchDB - you need a proxy in front to use this

Parameters:
host - - hostname
port - - port to use
user - - username
pass - - password
secure - - use an SSL connection?

Session

public Session(String host,
               int port,
               String user,
               String pass)
Constructor for obtaining a Session with an HTTP-AUTH username/password This isn't supported by CouchDB - you need a proxy in front to use this

Parameters:
host -
port -
user - - username
pass - - password

Session

public Session(String host,
               int port)
Main constructor for obtaining a Session.

Parameters:
host -
port -

Session

public Session(String host,
               int port,
               boolean secure)
Optional constructor that indicates an HTTPS connection should be used. This isn't supported by CouchDB - you need a proxy in front to use this

Parameters:
host -
port -
secure -
Method Detail

getHost

public String getHost()
Read-only

Returns:
the host name

getPort

public int getPort()
read-only

Returns:
the port

isSecure

public boolean isSecure()
Is this a secured connection (set in constructor)

Returns:

getDatabaseNames

public List<String> getDatabaseNames()
Retrieves a list of all database names from the server

Returns:

getDatabase

public Database getDatabase(String name)
Loads a database instance from the server

Parameters:
name -
Returns:
the database (or null if it doesn't exist)

createDatabase

public Database createDatabase(String name)
Creates a new database (if the name doesn't already exist)

Parameters:
name -
Returns:
the new database (or null if there was an error)

deleteDatabase

public boolean deleteDatabase(String name)
Deletes a database (by name) from the CouchDB server.

Parameters:
name -
Returns:
true = successful, false = an error occurred (likely the database named didn't exist)

deleteDatabase

public boolean deleteDatabase(Database db)
Deletes a database from the CouchDB server

Parameters:
db -
Returns:
was successful

buildUrl

protected String buildUrl(String url)
For a given url (such as /_all_dbs/), build the database connection url

Parameters:
url -
Returns:
the absolute URL (hostname/port/etc)

buildUrl

protected String buildUrl(String url,
                          String queryString)

buildUrl

protected String buildUrl(String url,
                          org.apache.http.NameValuePair[] params)

http

protected CouchResponse http(org.apache.http.client.methods.HttpRequestBase req)
Method that actually performs the GET/PUT/POST/DELETE calls. Executes the given HttpMethod on the HttpClient object (one HttpClient per Session).

This returns a CouchResponse, which can be used to get the status of the call (isOk), and any headers / body that was sent back.

Parameters:
method -
Returns:
the CouchResponse (status / error / json document)

getLastResponse

public CouchResponse getLastResponse()
Returns the last response for this given session - useful for debugging purposes

Returns:

setUserAgent

public void setUserAgent(String ua)

setConnectionTimeout

public void setConnectionTimeout(int milliseconds)

setSocketTimeout

public void setSocketTimeout(int milliseconds)

encodeParameter

protected String encodeParameter(String paramValue)


Copyright © 2012. All Rights Reserved.