Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/main/java/couchdb/CouchdbClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package couchdb;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -47,6 +49,8 @@ public class CouchdbClient extends DB{

// Default configuration
private static final String DEFAULT_DATABASE_NAME = "usertable";
private static final String DEFAULT_DATABASE_USER = "admin";
private static final String DEFAULT_DATABASE_PASSWORD = "password";
private static final int DEFAULT_COUCHDB_PORT_NUMBER = 5984;
private static final String PROTOCOL = "http";
// Database connector
Expand All @@ -56,7 +60,7 @@ public class CouchdbClient extends DB{
private static final int UPDATE_CONFLICT = -2;
private static final int DOC_NOT_FOUND = -3;
private static final int JSON_PARSING_FAULT = -4;

public CouchdbClient(){
this.dbConnector = null;
}
Expand All @@ -69,30 +73,50 @@ public CouchdbClient(List<URL> urls){
}

private List<URL> getUrlsForHosts() throws DBException{
String user = getProperties().getProperty("user");
if(user == null) {
user = DEFAULT_DATABASE_USER;
}
if(user.isEmpty()) {
throw new IllegalArgumentException("user is required");
}

String password = getProperties().getProperty("password");
if(password == null) {
password = DEFAULT_DATABASE_PASSWORD;
}
if(password.isEmpty()) {
throw new IllegalArgumentException("password is required");
}

List<URL> result = new ArrayList<URL>();
String hosts = getProperties().getProperty("hosts");
String[] differentHosts = hosts.split(",");
for(String host: differentHosts){
URL url = this.getUrlForHost(host);
URL url = this.getUrlForHost(host, user, password);
result.add(url);
}
return result;
}

private URL getUrlForHost(String host) throws DBException{
private URL getUrlForHost(String host, String user, String password) throws DBException{
String[] hostAndPort = host.split(":");
try{
int portNumber;
if(hostAndPort.length == 1){
return new URL(PROTOCOL, host, DEFAULT_COUCHDB_PORT_NUMBER, "");
}
else{
int portNumber = Integer.parseInt(hostAndPort[1]);
return new URL(PROTOCOL, hostAndPort[0], portNumber, "");
portNumber = DEFAULT_COUCHDB_PORT_NUMBER;
} else {
portNumber = Integer.parseInt(hostAndPort[1]);
}

URI couchdb_uri = new URI(PROTOCOL, user + ":" + password, hostAndPort[0], portNumber, null, null, null);
return couchdb_uri.toURL();
} catch(MalformedURLException exc){
throw new DBException("Invalid host specified");
} catch(NumberFormatException exc){
throw new DBException("Invalid port number specified");
} catch (URISyntaxException e) {
throw new DBException("Invalid URI");
}
}

Expand Down