Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jcouchdb.db;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
Default implementation of the Server interface.

Author(s):
fforw at gmx dot de
 
 public class ServerImpl
     implements Server
 {
     private static final String CHARSET = "UTF-8";
 
     protected static Logger log = LoggerFactory.getLogger(ServerImpl.class);
 
 
     private AuthScope authScope;
 
     private Credentials credentials;
 
     private HttpContext context;
 
     private String serverURI;
 
     private volatile DefaultHttpClient httpClient;
 
     private int maxConnectionsPerRoute = 10;
 
     private int maxTotalConnections = 25;
 
     private volatile boolean shutdown;
     
     public void setMaxConnectionsPerRoute(int maxConnectionsPerRoute)
     {
         this. = maxConnectionsPerRoute;
     }
     
     public void setMaxTotalConnections(int maxTotalConnections)
     {
         this. = maxTotalConnections;
     }
     
     public ServerImpl(String host)
     {
         this(host);
     }
 
     public ServerImpl(String hostint port)
     {
         this. = "http://" + host + ":" + port;
     }
 
     private DefaultHttpClient getHttpClient()
     {
         if ( == null)
         {
             synchronized(this)
            {
                if ( == null)
                {
                    SchemeRegistry supportedSchemes = new SchemeRegistry();
                    SocketFactory sf = PlainSocketFactory.getSocketFactory();
                    supportedSchemes.register(new Scheme("http"sf, 80));
            
                    HttpParams params = new BasicHttpParams();
                    HttpProtocolParams.setVersion(params.);
                    HttpProtocolParams.setUseExpectContinue(paramsfalse);
                    HttpClientParams.setRedirecting(paramsfalse);
            
                    params.setParameter(.);
            
                     = new BasicHttpContext();
                     = new ThreadSafeClientConnManagerparamssupportedSchemes);
                     = new DefaultHttpClient(params);
                    if ( != null)
                    {
                        .getCredentialsProvider().setCredentials();
                    }
                }
            }
        }
        return ;
    }
    private final Response executeHttpRequestBase request ) throws ClientProtocolExceptionIOException 
    {
        HttpResponse res = getHttpClient().executerequest );
        return new Responseres );
    }
    private Response executePut(HttpPut put)
    {
        try
        {
            return execute(put);
        }
        catch (IOException e)
        {
            put.abort();
            throw ExceptionWrapper.wrap(e);
        }
    }

    
    
    public List<StringlistDatabases()
    {
        Response resp = null;
        try
        {
            resp = get("/_all_dbs");
            if (!resp.isOk())
            {
                throw new CouchDBException("Error listing databases: " + resp);
            }
            return resp.getContentAsList();
        }
        finally
        {
            if (resp != null)
            {
                resp.destroy();
            }
        }
    }

    
    public boolean createDatabase(String name)
    {
        Response resp = null;
        try
        {
            resp = put("/" + name + "/");
            if (resp.isOk())
            {
                return true;
            }
            else
            {
                if (resp.getCode() == 412 || resp.getCode() == 500)
                {
                    return false;
                }
                else
                {
                    throw new CouchDBException("Error creating database: " + resp);
                }
            }
        }
        finally
        {
            if (resp != null)
            {
                resp.destroy();
            }
        }
    }

    
    public void deleteDatabase(String name)
    {
        Response resp = null;
        try
        {
            resp = delete("/" + name + "/");
            if (!resp.isOk())
            {
                throw new CouchDBException("Cannot delete database " + name + ": " + resp);
            }
        }
        finally
        {
            if (resp != null)
            {
                resp.destroy();
            }
        }
    }

    
    public Response get(String uri)
    {
        if (.isDebugEnabled())
        {
            .debug("GET " + uri);
        }
        HttpGet get = new HttpGet + uri );
        
        try
        {
            return executeget );
        }
        catch (IOException e) {
            get.abort();
            throw ExceptionWrapper.wrap(e);
        }
    }

    
    public Response put(String uri)
    {
        return put(urinull);
    }

    
    public Response put(String uriString body)
    {
        if (.isDebugEnabled())
        {
            .debug("PUT " + uri + ", body = " + body);
        }
        HttpPut put = new HttpPut + uri );
        if (body != null) {
            try {
                StringEntity reqEntity = new StringEntitybody , "UTF-8");
                reqEntity.setContentType("application/json");
                reqEntity.setContentEncoding );
                put.setEntityreqEntity );
            }
            catch (UnsupportedEncodingException e) {
                throw ExceptionWrapper.wrap(e);
            }
        }
        return executePutput );        
    }

    
    public Response put(String uribyte[] bodyString contentType)
    {
        if (.isDebugEnabled())
        {
            .debug("PUT " + uri + ", body = " + body);
        }
        HttpPut put = new HttpPut( + uri);
        if (body != null)
        {
            ByteArrayEntity reqEntity = new ByteArrayEntity(body);
            reqEntity.setContentType(contentType);
            put.setEntity(reqEntity);
        }
        return executePut(put);
    }
    
    public Response put(String uriInputStream inputStreamString contentTypelong lengththrows CouchDBException
    {
        Assert.notNull(inputStream"inputStream can't be null");
        if (.isDebugEnabled())
        {
            .debug("PUT " + uri + ", inputStream = " + inputStream);
        }
        HttpPut put = new HttpPut( + uri);
        InputStreamEntity entity = new InputStreamEntityinputStreamlength);
        entity.setContentType(contentType);
        put.setEntity(entity);
        return executePut(put);
    }

    
    public Response post(String uriString body)
    {
        if (.isDebugEnabled())
        {
            .debug("POST " + uri + ", body = " + body);
        }
        HttpPost post = new HttpPost + uri );
        try
        {
            StringEntity reqEntity = new StringEntity(body"UTF-8");
            reqEntity.setContentType("application/json");
            reqEntity.setContentEncoding();
            post.setEntity(reqEntity);
            return execute(post);
        }
        catch (IOException e)
        {
            post.abort();
            throw ExceptionWrapper.wrap(e);
        }
    }

    
    public Response delete(String uri)
    {
        if (.isDebugEnabled())
        {
            .debug("DELETE " + uri);
        }
        HttpDelete delete = new HttpDelete + uri );
        try
        {
            return execute(delete);
        }
        catch (IOException e)
        {
            delete.abort();
            throw ExceptionWrapper.wrap(e);
        }
    }

    
    public void setCredentials(AuthScope authScopeCredentials credentials)
    {
        this.      = authScope;
        this.    = credentials;
         = null;
    }
    public void shutDown()
    {
        if ( != null)
        {
            .shutdown();
        }
         = null;
         = null;
         = true;
    }
    
    public boolean isShutdown()
    {
        return ;
    }

    
    public Map<String,Map<String,Object>> getStats(String filter)
    {
        String uri = "/_stats";
        
        if (filter != null)
        {
            uri += filter;
        }
        
        Response resp = get(uri);
        return resp.getContentAsMap();
    }
    public ReplicationInfo replicate(String sourceString targetboolean continuous)
    {
        Response response = null;
        try
        {
            Map<String,Objectmap = new HashMap<StringObject>();
            map.put("source"source);
            map.put("target"target);
            
            if (continuous)
            {
                map.put("continuous"true);
            }
            
            response = post("/_replicate", JSON.defaultJSON().forValue(map) );
            return response.getContentAsBean(ReplicationInfo.class);
        }
        finally
        {
            if (response != null)
            {
                response.destroy();
            }
        }
    }
    
    
Requests a list of uuids from the CouchDB server

Parameters:
count number of uuids to request
Returns:
    public List<StringgetUUIDs(int count)
    {
        Response response = null;
        try
        {
            response = get("/_uuids?count=" + count );
            Map content = response.getContentAsMap();
            return (List<String>)content.get("uuids");
        }
        finally
        {
            if (response != null)
            {
                response.destroy();
            }
        }
    }
New to GrepCode? Check out our FAQ X