From 0519a57433ee4b47dbb0560eb49260fca37941c7 Mon Sep 17 00:00:00 2001 From: Ahmed Dajani Date: Sun, 25 May 2025 17:51:10 -0500 Subject: [PATCH] Support database authentication --- src/main/java/couchdb/CouchdbClient.java | 40 +++++++++++++++++++----- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/main/java/couchdb/CouchdbClient.java b/src/main/java/couchdb/CouchdbClient.java index 2aad458..67ffb83 100644 --- a/src/main/java/couchdb/CouchdbClient.java +++ b/src/main/java/couchdb/CouchdbClient.java @@ -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; @@ -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 @@ -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; } @@ -69,30 +73,50 @@ public CouchdbClient(List urls){ } private List 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 result = new ArrayList(); 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"); } }