/** * Constructs a new instance of this class. * @param dbName The database name. * @param createDbIfNotExist To create a new database if it does not already exist. * @param protocol The protocol to use (i.e http or https) * @param host The database host address * @param port The database listening port * @param username The Username credential * @param password The Password credential */ public CouchDbClient(String dbName, boolean createDbIfNotExist, String protocol, String host, int port, String username, String password) { super(new CouchDbConfig(new CouchDbProperties(dbName, createDbIfNotExist, protocol, host, port, username, password))); }
CouchDbContext(CouchDbClientBase dbc, CouchDbProperties props) { this.dbc = dbc; if (props.isCreateDbIfNotExist()) { createDB(props.getDbName()); } else { serverVersion(); // pre warm up client } }
private PoolingHttpClientConnectionManager createConnectionManager( CouchDbProperties props, Registry<ConnectionSocketFactory> registry) { PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(registry); if (props.getMaxConnections() != 0) { ccm.setMaxTotal(props.getMaxConnections()); ccm.setDefaultMaxPerRoute(props.getMaxConnections()); } return ccm; }
private SchemeRegistry createRegistry(CouchDbProperties properties) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, CertificateException, IOException, UnrecoverableKeyException { SchemeRegistry registry = new SchemeRegistry(); if("https".equals(properties.getProtocol())) { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new MySSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); registry.register(new Scheme(properties.getProtocol(), sf, properties.getPort())); } else { registry.register(new Scheme(properties.getProtocol(), PlainSocketFactory.getSocketFactory(), properties.getPort())); } return registry; }
private void readProperties() { try { // required dbProperties = new CouchDbProperties(); dbProperties.setDbName(getProperty("couchdb.name", true)); dbProperties.setCreateDbIfNotExist(new Boolean(getProperty("couchdb.createdb.if-not-exist", true))); dbProperties.setProtocol(getProperty("couchdb.protocol", true)); dbProperties.setHost(getProperty("couchdb.host", true)); dbProperties.setPort(Integer.parseInt(getProperty("couchdb.port", true))); dbProperties.setUsername(getProperty("couchdb.username", true)); dbProperties.setPassword(getProperty("couchdb.password", true)); // optional dbProperties.setPath(getProperty("couchdb.path", false)); dbProperties.setSocketTimeout(getPropertyAsInt("couchdb.http.socket.timeout", false)); dbProperties.setConnectionTimeout(getPropertyAsInt("couchdb.http.connection.timeout", false)); dbProperties.setMaxConnections(getPropertyAsInt("couchdb.max.connections", false)); dbProperties.setProxyHost(getProperty("couchdb.proxy.host", false)); dbProperties.setProxyPort(getPropertyAsInt("couchdb.proxy.port", false)); } catch (Exception e) { throw new IllegalStateException(e); } properties = null; }
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, props.getSocketTimeout()); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, props.getConnectionTimeout()); final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(params,schemeRegistry); httpclient = new DefaultHttpClient(ccm, params); if(props.getProxyHost() != null) { HttpHost proxy = new HttpHost(props.getProxyHost(), props.getProxyPort()); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if(props.getUsername() != null && props.getPassword() != null) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(props.getHost(), props.getPort()), new UsernamePasswordCredentials(props.getUsername(), props.getPassword())); props.clearPassword();
public CouchDbConfig(CouchDbProperties dbProperties) { assertNotEmpty(dbProperties, "Properties"); assertNotEmpty(dbProperties.getDbName(), "Database"); assertNotEmpty(dbProperties.getProtocol(), "Protocol"); assertNotEmpty(dbProperties.getHost(), "Host"); assertNotEmpty(dbProperties.getPort(), "Port"); this.dbProperties = dbProperties; }
private Registry<ConnectionSocketFactory> createRegistry(CouchDbProperties props) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { RegistryBuilder<ConnectionSocketFactory> registry = RegistryBuilder .<ConnectionSocketFactory> create(); if("https".equals(props.getProtocol())) { SSLContext sslcontext = SSLContexts.custom() .loadTrustMaterial(null, new TrustStrategy(){ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); return registry.register("https", new SSLConnectionSocketFactory(sslcontext, new NoopHostnameVerifier())).build(); } else { return registry.register("http", PlainConnectionSocketFactory.INSTANCE).build(); } }
.setCharset(Consts.UTF_8).build()) .setDefaultRequestConfig(RequestConfig.custom() .setSocketTimeout(props.getSocketTimeout()) .setConnectTimeout(props.getConnectionTimeout()).build()); if (props.getProxyHost() != null) clientBuilder.setProxy(new HttpHost(props.getProxyHost(), props.getProxyPort())); if (props.getUsername() != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(props.getHost(), props.getPort()), new UsernamePasswordCredentials(props.getUsername(), props.getPassword())); clientBuilder.setDefaultCredentialsProvider(credsProvider); props.clearPassword();
CouchDbClientBase(CouchDbConfig config) { final CouchDbProperties props = config.getProperties(); this.httpClient = createHttpClient(props); this.gson = initGson(new GsonBuilder()); this.host = new HttpHost(props.getHost(), props.getPort(), props.getProtocol()); final String path = props.getPath() != null ? props.getPath() : ""; this.baseURI = buildUri().scheme(props.getProtocol()).host(props.getHost()).port(props.getPort()).path("/").path(path).build(); this.dbURI = buildUri(baseURI).path(props.getDbName()).path("/").build(); this.context = new CouchDbContext(this, props); this.design = new CouchDbDesign(this); }
/** * Constructs a new client instance. * @param dbName The database name. * @param createDbIfNotExist To create a new database if it does not already exist. * @param protocol The protocol to use (i.e http or https) * @param host The database host address * @param port The database listening port * @param username The Username credential * @param password The Password credential */ public CouchDbClientAndroid(String dbName, boolean createDbIfNotExist, String protocol, String host, int port, String username, String password) { super(new CouchDbConfig(new CouchDbProperties(dbName, createDbIfNotExist, protocol, host, port, username, password))); }
private static CouchDbProperties props(String dbname) { return new CouchDbProperties(dbname, true, CouchDbConfig.INSTANCE.getProtocol(), CouchDbConfig.INSTANCE.getHost(), CouchDbConfig.INSTANCE.getPort(), null, null); } }