diff --git a/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ComponentDatabaseHandler.java b/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ComponentDatabaseHandler.java index 2f6158b42..b71639a33 100644 --- a/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ComponentDatabaseHandler.java +++ b/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ComponentDatabaseHandler.java @@ -34,11 +34,13 @@ import org.apache.log4j.Logger; import org.apache.thrift.TException; import org.ektorp.DocumentOperationResult; +import org.ektorp.http.HttpClient; import org.jetbrains.annotations.NotNull; import java.net.MalformedURLException; import java.util.*; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import java.util.stream.Collectors; import static com.google.common.base.Strings.isNullOrEmpty; @@ -82,8 +84,8 @@ public class ComponentDatabaseHandler { private final ReleaseModerator releaseModerator; - public ComponentDatabaseHandler(String url, String dbName, String attachmentDbName, ComponentModerator moderator, ReleaseModerator releaseModerator) throws MalformedURLException { - DatabaseConnector db = new DatabaseConnector(url, dbName); + public ComponentDatabaseHandler(Supplier httpClient, String dbName, String attachmentDbName, ComponentModerator moderator, ReleaseModerator releaseModerator) throws MalformedURLException { + DatabaseConnector db = new DatabaseConnector(httpClient, dbName); // Create the repositories vendorRepository = new VendorRepository(db); @@ -96,16 +98,16 @@ public ComponentDatabaseHandler(String url, String dbName, String attachmentDbNa this.releaseModerator = releaseModerator; // Create the attachment connector - attachmentConnector = new AttachmentConnector(url, attachmentDbName, durationOf(30, TimeUnit.SECONDS)); + attachmentConnector = new AttachmentConnector(httpClient, attachmentDbName, durationOf(30, TimeUnit.SECONDS)); } - public ComponentDatabaseHandler(String url, String dbName, String attachmentDbName) throws MalformedURLException { - this(url, dbName, attachmentDbName, new ComponentModerator(), new ReleaseModerator()); + public ComponentDatabaseHandler(Supplier httpClient, String dbName, String attachmentDbName) throws MalformedURLException { + this(httpClient, dbName, attachmentDbName, new ComponentModerator(), new ReleaseModerator()); } - public ComponentDatabaseHandler(String url, String dbName, String attachmentDbName, ThriftClients thriftClients) throws MalformedURLException { - this(url, dbName, attachmentDbName, new ComponentModerator(thriftClients), new ReleaseModerator(thriftClients)); + public ComponentDatabaseHandler(Supplier httpClient, String dbName, String attachmentDbName, ThriftClients thriftClients) throws MalformedURLException { + this(httpClient, dbName, attachmentDbName, new ComponentModerator(thriftClients), new ReleaseModerator(thriftClients)); } private void autosetReleaseClearingState(Release releaseAfter, Release releaseBefore) { diff --git a/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ComponentSearchHandler.java b/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ComponentSearchHandler.java index b60672c35..aba5db29f 100644 --- a/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ComponentSearchHandler.java +++ b/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ComponentSearchHandler.java @@ -13,11 +13,13 @@ import com.siemens.sw360.datahandler.couchdb.lucene.LuceneSearchView; import com.siemens.sw360.datahandler.thrift.components.Component; import org.apache.log4j.Logger; +import org.ektorp.http.HttpClient; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; /** * Class for accessing the Lucene connector on the CouchDB database @@ -73,8 +75,8 @@ public class ComponentSearchHandler { private final LuceneAwareDatabaseConnector connector; - public ComponentSearchHandler(String url, String dbName) throws IOException { - connector = new LuceneAwareDatabaseConnector(url, dbName); + public ComponentSearchHandler(Supplier httpClient, String dbName) throws IOException { + connector = new LuceneAwareDatabaseConnector(httpClient, dbName); connector.addView(luceneSearchView); connector.setResultLimit(DatabaseSettings.LUCENE_SEARCH_LIMIT); } diff --git a/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ProjectDatabaseHandler.java b/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ProjectDatabaseHandler.java index 9d4b07fe5..201082b8e 100644 --- a/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ProjectDatabaseHandler.java +++ b/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ProjectDatabaseHandler.java @@ -28,10 +28,12 @@ import com.siemens.sw360.datahandler.thrift.users.User; import com.siemens.sw360.datahandler.thrift.vulnerabilities.ProjectVulnerabilityRating; import org.apache.log4j.Logger; +import org.ektorp.http.HttpClient; import java.net.MalformedURLException; import java.util.*; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import static com.siemens.sw360.datahandler.common.CommonUtils.isInProgressOrPending; import static com.siemens.sw360.datahandler.common.CommonUtils.nullToEmptyList; @@ -57,13 +59,13 @@ public class ProjectDatabaseHandler { private final AttachmentConnector attachmentConnector; private final ComponentDatabaseHandler componentDatabaseHandler; - public ProjectDatabaseHandler(String url, String dbName, String attachmentDbName) throws MalformedURLException { - this(url, dbName, attachmentDbName, new ProjectModerator(), new ComponentDatabaseHandler(url,dbName,attachmentDbName)); + public ProjectDatabaseHandler(Supplier httpClient, String dbName, String attachmentDbName) throws MalformedURLException { + this(httpClient, dbName, attachmentDbName, new ProjectModerator(), new ComponentDatabaseHandler(httpClient,dbName,attachmentDbName)); } @VisibleForTesting - public ProjectDatabaseHandler(String url, String dbName, String attachmentDbName, ProjectModerator moderator, ComponentDatabaseHandler componentDatabaseHandler) throws MalformedURLException { - DatabaseConnector db = new DatabaseConnector(url, dbName); + public ProjectDatabaseHandler(Supplier httpClient, String dbName, String attachmentDbName, ProjectModerator moderator, ComponentDatabaseHandler componentDatabaseHandler) throws MalformedURLException { + DatabaseConnector db = new DatabaseConnector(httpClient, dbName); // Create the repositories repository = new ProjectRepository(db); @@ -73,7 +75,7 @@ public ProjectDatabaseHandler(String url, String dbName, String attachmentDbName this.moderator = moderator; // Create the attachment connector - attachmentConnector = new AttachmentConnector(url, attachmentDbName, Duration.durationOf(30, TimeUnit.SECONDS)); + attachmentConnector = new AttachmentConnector(httpClient, attachmentDbName, Duration.durationOf(30, TimeUnit.SECONDS)); this.componentDatabaseHandler = componentDatabaseHandler; } diff --git a/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ProjectSearchHandler.java b/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ProjectSearchHandler.java index 1af2eeaad..f09a4d98b 100644 --- a/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ProjectSearchHandler.java +++ b/backend/src-common/src/main/java/com/siemens/sw360/datahandler/db/ProjectSearchHandler.java @@ -14,11 +14,13 @@ import com.siemens.sw360.datahandler.thrift.projects.Project; import com.siemens.sw360.datahandler.thrift.users.User; import org.apache.log4j.Logger; + import org.ektorp.http.HttpClient; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Set; + import java.util.function.Supplier; public class ProjectSearchHandler { @@ -68,8 +70,8 @@ public class ProjectSearchHandler { private final LuceneAwareDatabaseConnector connector; - public ProjectSearchHandler(String url, String dbName) throws IOException { - connector = new LuceneAwareDatabaseConnector(url, dbName); + public ProjectSearchHandler(Supplier httpClient, String dbName) throws IOException { + connector = new LuceneAwareDatabaseConnector(httpClient, dbName); connector.addView(luceneSearchView); connector.setResultLimit(DatabaseSettings.LUCENE_SEARCH_LIMIT); } diff --git a/backend/src/src-attachments/src/main/java/com/siemens/sw360/attachments/AttachmentHandler.java b/backend/src/src-attachments/src/main/java/com/siemens/sw360/attachments/AttachmentHandler.java index 268ff4b59..c79476da6 100644 --- a/backend/src/src-attachments/src/main/java/com/siemens/sw360/attachments/AttachmentHandler.java +++ b/backend/src/src-attachments/src/main/java/com/siemens/sw360/attachments/AttachmentHandler.java @@ -48,20 +48,11 @@ public class AttachmentHandler implements AttachmentService.Iface { private final AttachmentRepository repository; private final AttachmentConnector attachmentConnector; - private final DatabaseAddress address; - public AttachmentHandler() throws MalformedURLException { - DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_ATTACHMENTS); - attachmentConnector = new AttachmentConnector(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_ATTACHMENTS, durationOf(30, TimeUnit.SECONDS)); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_ATTACHMENTS); + attachmentConnector = new AttachmentConnector(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_ATTACHMENTS, durationOf(30, TimeUnit.SECONDS)); repository = new AttachmentRepository(databaseConnector); - - address = databaseConnector.getAddress(); - } - - @Override - public DatabaseAddress getDatabaseAddress() throws TException { - return address; } @Override diff --git a/backend/src/src-attachments/src/main/java/com/siemens/sw360/attachments/db/AttachmentDatabaseHandler.java b/backend/src/src-attachments/src/main/java/com/siemens/sw360/attachments/db/AttachmentDatabaseHandler.java index 9a7ec92d6..bcb729ac4 100644 --- a/backend/src/src-attachments/src/main/java/com/siemens/sw360/attachments/db/AttachmentDatabaseHandler.java +++ b/backend/src/src-attachments/src/main/java/com/siemens/sw360/attachments/db/AttachmentDatabaseHandler.java @@ -19,11 +19,13 @@ import org.apache.log4j.Logger; import org.apache.thrift.TException; import org.ektorp.DocumentOperationResult; +import org.ektorp.http.HttpClient; import java.net.MalformedURLException; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import java.util.stream.Collectors; import static com.siemens.sw360.datahandler.common.Duration.durationOf; @@ -43,9 +45,9 @@ public class AttachmentDatabaseHandler { private static final Logger log = Logger.getLogger(AttachmentDatabaseHandler.class); - public AttachmentDatabaseHandler(String url, String dbName, String attachmentDbName) throws MalformedURLException { - db = new DatabaseConnector(url, attachmentDbName); - attachmentConnector = new AttachmentConnector(url, attachmentDbName, durationOf(30, TimeUnit.SECONDS)); + public AttachmentDatabaseHandler(Supplier httpClient, String attachmentDbName) throws MalformedURLException { + db = new DatabaseConnector(httpClient, attachmentDbName); + attachmentConnector = new AttachmentConnector(httpClient, attachmentDbName, durationOf(30, TimeUnit.SECONDS)); repository = new AttachmentRepository(db); } @@ -53,10 +55,6 @@ public AttachmentConnector getAttachmentConnector(){ return attachmentConnector; } - public DatabaseAddress getDatabaseAddress(){ - return db.getAddress(); - } - public AttachmentContent add(AttachmentContent attachmentContent){ repository.add(attachmentContent); return attachmentContent; diff --git a/backend/src/src-attachments/src/test/java/com/siemens/sw360/attachments/AttachmentHandlerTest.java b/backend/src/src-attachments/src/test/java/com/siemens/sw360/attachments/AttachmentHandlerTest.java index 5dd2c20e9..803ca6ad0 100644 --- a/backend/src/src-attachments/src/test/java/com/siemens/sw360/attachments/AttachmentHandlerTest.java +++ b/backend/src/src-attachments/src/test/java/com/siemens/sw360/attachments/AttachmentHandlerTest.java @@ -45,9 +45,9 @@ public class AttachmentHandlerTest { @Before public void setUp() throws Exception { // Create the database - TestUtils.createDatabase(url, dbName); + TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); - DatabaseConnector databaseConnector = new DatabaseConnector(url, dbName); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); // databaseConnector.add(new Attachment().setId("A1").setCreatedBy("user1@test.com").setCreatedOn("2012-07-30").setFilename("a.txt").setContentType("text")); // databaseConnector.add(new Attachment().setId("A2").setCreatedBy("user2@test.com").setCreatedOn("2012-05-22").setFilename("b.jpg").setContentType("image")); databaseConnector.add(new AttachmentContent().setId("A1").setFilename("a.txt").setContentType("text")); @@ -59,14 +59,7 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { // Delete the database - TestUtils.deleteDatabase(url, dbName); - } - - @Test - public void testGetDatabaseAddress() throws Exception { - DatabaseAddress address = handler.getDatabaseAddress(); - assertEquals(url, address.getUrl()); - assertEquals(dbName, address.getDbName()); + TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } @Test diff --git a/backend/src/src-attachments/src/test/java/com/siemens/sw360/attachments/TestAttachmentClient.java b/backend/src/src-attachments/src/test/java/com/siemens/sw360/attachments/TestAttachmentClient.java index 3cb5ba9b3..ac5af3ce4 100644 --- a/backend/src/src-attachments/src/test/java/com/siemens/sw360/attachments/TestAttachmentClient.java +++ b/backend/src/src-attachments/src/test/java/com/siemens/sw360/attachments/TestAttachmentClient.java @@ -23,13 +23,14 @@ */ public class TestAttachmentClient { - public static void main(String[] args) throws TException, IOException { - THttpClient thriftClient = new THttpClient("http://127.0.0.1:8080/attachmentservice/thrift"); - TProtocol protocol = new TCompactProtocol(thriftClient); - AttachmentService.Iface client = new AttachmentService.Client(protocol); - - System.out.println(client.getDatabaseAddress().toString()); - + public static void main(String[] args) { + try { + THttpClient thriftClient = new THttpClient("http://127.0.0.1:8080/attachmentservice/thrift"); + TProtocol protocol = new TCompactProtocol(thriftClient); + AttachmentService.Iface client = new AttachmentService.Client(protocol); + } catch (Exception e) { + assert(false); + } } } diff --git a/backend/src/src-bdpimport/src/test/java/com/bosch/osmi/sw360/bdp/datasink/thrift/ThriftApiSimpleTest.java b/backend/src/src-bdpimport/src/test/java/com/bosch/osmi/sw360/bdp/datasink/thrift/ThriftApiSimpleTest.java index 6a3e80a62..7b46a0a71 100644 --- a/backend/src/src-bdpimport/src/test/java/com/bosch/osmi/sw360/bdp/datasink/thrift/ThriftApiSimpleTest.java +++ b/backend/src/src-bdpimport/src/test/java/com/bosch/osmi/sw360/bdp/datasink/thrift/ThriftApiSimpleTest.java @@ -25,21 +25,20 @@ public class ThriftApiSimpleTest { - private static final String url = DatabaseSettings.COUCH_DB_URL; private static final String dbName = DatabaseSettings.COUCH_DB_DATABASE; private ThriftApi thriftApi; @Before public void setUp() throws Exception { - TestUtils.createDatabase(url, dbName); + TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); thriftApi = new ThriftApiSimple(); } @After public void tearDown() throws Exception { - TestUtils.deleteDatabase(url, dbName); + TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } @Test diff --git a/backend/src/src-bdpimport/src/test/resources/couchdb.properties b/backend/src/src-bdpimport/src/test/resources/couchdb.properties deleted file mode 100644 index 0e0c5e3a7..000000000 --- a/backend/src/src-bdpimport/src/test/resources/couchdb.properties +++ /dev/null @@ -1,16 +0,0 @@ -# -# Copyright Siemens AG, 2013-2015. Part of the SW360 Portal Project. -# -# Copying and distribution of this file, with or without modification, -# are permitted in any medium without royalty provided the copyright -# notice and this notice are preserved. This file is offered as-is, -# without any warranty. -# - -# N.B this is the build property file, copied from module build-configuration - -couchdb.url = http://localhost:5984 -couchdb.database = sw360_test_db -couchdb.usersdb = sw360_test_users -couchdb.attachments = sw360_test_attachments -couchdb.fossologyKeys = sw360_test_fossologykeys diff --git a/backend/src/src-components/src/main/java/com/siemens/sw360/components/ComponentHandler.java b/backend/src/src-components/src/main/java/com/siemens/sw360/components/ComponentHandler.java index 3ddba9008..dacf5831b 100644 --- a/backend/src/src-components/src/main/java/com/siemens/sw360/components/ComponentHandler.java +++ b/backend/src/src-components/src/main/java/com/siemens/sw360/components/ComponentHandler.java @@ -20,11 +20,13 @@ import com.siemens.sw360.datahandler.thrift.components.*; import com.siemens.sw360.datahandler.thrift.users.User; import org.apache.thrift.TException; +import org.ektorp.http.HttpClient; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; import static com.siemens.sw360.datahandler.common.SW360Assert.*; @@ -41,23 +43,23 @@ public class ComponentHandler implements ComponentService.Iface { private final AttachmentHandler attachmentHandler; public ComponentHandler() throws IOException { - this(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); + this(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); } - ComponentHandler(String dbUrl, String dbName, String attachmentDbName) throws IOException { - handler = new ComponentDatabaseHandler(dbUrl, dbName, attachmentDbName); - searchHandler = new ComponentSearchHandler(dbUrl, dbName); + ComponentHandler(Supplier httpClient, String dbName, String attachmentDbName) throws IOException { + handler = new ComponentDatabaseHandler(httpClient, dbName, attachmentDbName); + searchHandler = new ComponentSearchHandler(httpClient, dbName); attachmentHandler = new AttachmentHandler(); } // TODO use dependency injection instead of this constructors mess public ComponentHandler(ThriftClients thriftClients) throws IOException { - this(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS, thriftClients); + this(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS, thriftClients); } - ComponentHandler(String dbUrl, String dbName, String attachmentDbName, ThriftClients thriftClients) throws IOException { - handler = new ComponentDatabaseHandler(dbUrl, dbName, attachmentDbName, thriftClients); - searchHandler = new ComponentSearchHandler(dbUrl, dbName); + ComponentHandler(Supplier httpClient, String dbName, String attachmentDbName, ThriftClients thriftClients) throws IOException { + handler = new ComponentDatabaseHandler(httpClient, dbName, attachmentDbName, thriftClients); + searchHandler = new ComponentSearchHandler(httpClient, dbName); attachmentHandler = new AttachmentHandler(); } diff --git a/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ComponentDatabaseHandlerTest.java b/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ComponentDatabaseHandlerTest.java index 9bf67e7c6..6852470ab 100644 --- a/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ComponentDatabaseHandlerTest.java +++ b/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ComponentDatabaseHandlerTest.java @@ -47,7 +47,6 @@ @RunWith(MockitoJUnitRunner.class) public class ComponentDatabaseHandlerTest { - private static final String url = DatabaseSettings.COUCH_DB_URL; private static final String dbName = DatabaseSettings.COUCH_DB_DATABASE; private static final String attachmentsDbName = DatabaseSettings.COUCH_DB_ATTACHMENTS; @@ -115,10 +114,10 @@ public void setUp() throws Exception { releases.add(release2c); // Create the database - TestUtils.createDatabase(url, dbName); + TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); // Prepare the database - DatabaseConnector databaseConnector = new DatabaseConnector(url, dbName); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); for (Vendor vendor : vendors.values()) { databaseConnector.add(vendor); @@ -134,12 +133,12 @@ public void setUp() throws Exception { releaseMap= ThriftUtils.getIdMap(releases); // Prepare the handler - handler = new ComponentDatabaseHandler(url, dbName, attachmentsDbName, moderator, releaseModerator); + handler = new ComponentDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), dbName, attachmentsDbName, moderator, releaseModerator); } @After public void tearDown() throws Exception { - TestUtils.deleteDatabase(url, dbName); + TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } @Test diff --git a/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ComponentSearchHandlerTest.java b/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ComponentSearchHandlerTest.java index 86ff5bab3..4120d67f0 100644 --- a/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ComponentSearchHandlerTest.java +++ b/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ComponentSearchHandlerTest.java @@ -73,22 +73,22 @@ public void setUp() throws Exception { components.add(component3); // Create the database - TestUtils.createDatabase(url, dbName); + TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); // Prepare the database - DatabaseConnector databaseConnector = new DatabaseConnector(url, dbName); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); for (Component component : components) { databaseConnector.add(component); } // Prepare the handler - searchHandler = new ComponentSearchHandler(url, dbName); + searchHandler = new ComponentSearchHandler(DatabaseSettings.getConfiguredHttpClient(), dbName); } @After public void tearDown() throws Exception { - TestUtils.deleteDatabase(url, dbName); + TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } @Test diff --git a/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ProjectDatabaseHandlerTest.java b/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ProjectDatabaseHandlerTest.java index 6ab26f352..cf22cbc89 100644 --- a/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ProjectDatabaseHandlerTest.java +++ b/backend/src/src-components/src/test/java/com/siemens/sw360/components/db/ProjectDatabaseHandlerTest.java @@ -31,6 +31,7 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import javax.xml.crypto.Data; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -88,10 +89,10 @@ public void setUp() throws Exception { projects.add(project4); // Create the database - TestUtils.createDatabase(url, dbName); + TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); // Prepare the database - DatabaseConnector databaseConnector = new DatabaseConnector(url, dbName); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); for (Vendor vendor : vendors) { databaseConnector.add(vendor); @@ -103,13 +104,13 @@ public void setUp() throws Exception { databaseConnector.add(project); } - ComponentDatabaseHandler componentHandler = new ComponentDatabaseHandler(url, dbName, attachmentsDbName); - handler = new ProjectDatabaseHandler(url, dbName, attachmentsDbName, moderator, componentHandler); + ComponentDatabaseHandler componentHandler = new ComponentDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), dbName, attachmentsDbName); + handler = new ProjectDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), dbName, attachmentsDbName, moderator, componentHandler); } @After public void tearDown() throws Exception { - TestUtils.deleteDatabase(url, dbName); + TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } @Test diff --git a/backend/src/src-cvesearch/src/main/java/com/bosch/osmi/sw360/cvesearch/datasink/VulnerabilityConnector.java b/backend/src/src-cvesearch/src/main/java/com/bosch/osmi/sw360/cvesearch/datasink/VulnerabilityConnector.java index a7e0f349d..1d7aeeddb 100644 --- a/backend/src/src-cvesearch/src/main/java/com/bosch/osmi/sw360/cvesearch/datasink/VulnerabilityConnector.java +++ b/backend/src/src-cvesearch/src/main/java/com/bosch/osmi/sw360/cvesearch/datasink/VulnerabilityConnector.java @@ -39,9 +39,9 @@ public class VulnerabilityConnector { VendorRepository vendorRepository; public VulnerabilityConnector() throws IOException{ - vulnerabilityDatabaseHandler = new VulnerabilityDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_VM); + vulnerabilityDatabaseHandler = new VulnerabilityDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_VM); - DatabaseConnector db = new DatabaseConnector(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE); + DatabaseConnector db = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE); vendorRepository = new VendorRepository(db); releaseRepository = new ReleaseRepository(db, vendorRepository); componentRepository = new ComponentRepository(db, releaseRepository, vendorRepository); diff --git a/backend/src/src-fossology/src/main/java/com/siemens/sw360/fossology/config/FossologyConfig.java b/backend/src/src-fossology/src/main/java/com/siemens/sw360/fossology/config/FossologyConfig.java index ff871c305..dd00ed60e 100644 --- a/backend/src/src-fossology/src/main/java/com/siemens/sw360/fossology/config/FossologyConfig.java +++ b/backend/src/src-fossology/src/main/java/com/siemens/sw360/fossology/config/FossologyConfig.java @@ -32,13 +32,13 @@ public class FossologyConfig { @Bean public FossologyFingerPrintRepository fossologyFingerPrintRepository() throws MalformedURLException { - DatabaseConnector fossologyFingerPrintDatabaseConnector = new DatabaseConnector(COUCH_DB_URL, COUCH_DB_FOSSOLOGY); + DatabaseConnector fossologyFingerPrintDatabaseConnector = new DatabaseConnector(getConfiguredHttpClient(), COUCH_DB_FOSSOLOGY); return new FossologyFingerPrintRepository(FossologyHostFingerPrint.class, fossologyFingerPrintDatabaseConnector); } @Bean public AttachmentConnector attachmentConnector() throws MalformedURLException { - return new AttachmentConnector(COUCH_DB_URL, COUCH_DB_ATTACHMENTS, downloadTimeout); + return new AttachmentConnector(getConfiguredHttpClient(), COUCH_DB_ATTACHMENTS, downloadTimeout); } @Bean diff --git a/backend/src/src-licenseinfo/src/main/java/com/siemens/sw360/licenseinfo/LicenseInfoHandler.java b/backend/src/src-licenseinfo/src/main/java/com/siemens/sw360/licenseinfo/LicenseInfoHandler.java index be1050c2c..17424079b 100644 --- a/backend/src/src-licenseinfo/src/main/java/com/siemens/sw360/licenseinfo/LicenseInfoHandler.java +++ b/backend/src/src-licenseinfo/src/main/java/com/siemens/sw360/licenseinfo/LicenseInfoHandler.java @@ -66,9 +66,9 @@ public class LicenseInfoHandler implements LicenseInfoService.Iface { private final ComponentDatabaseHandler componentDatabaseHandler; public LicenseInfoHandler() throws MalformedURLException { - this(new AttachmentDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS), - new ProjectDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS), - new ComponentDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS)); + this(new AttachmentDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_ATTACHMENTS), + new ProjectDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS), + new ComponentDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS)); } @VisibleForTesting diff --git a/backend/src/src-licenses/src/main/java/com/siemens/sw360/licenses/LicenseHandler.java b/backend/src/src-licenses/src/main/java/com/siemens/sw360/licenses/LicenseHandler.java index 956b42410..bc6277ade 100644 --- a/backend/src/src-licenses/src/main/java/com/siemens/sw360/licenses/LicenseHandler.java +++ b/backend/src/src-licenses/src/main/java/com/siemens/sw360/licenses/LicenseHandler.java @@ -35,7 +35,7 @@ public class LicenseHandler implements LicenseService.Iface { LicenseDatabaseHandler handler; LicenseHandler() throws MalformedURLException { - handler = new LicenseDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE); + handler = new LicenseDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE); } ///////////////////// diff --git a/backend/src/src-licenses/src/main/java/com/siemens/sw360/licenses/db/LicenseDatabaseHandler.java b/backend/src/src-licenses/src/main/java/com/siemens/sw360/licenses/db/LicenseDatabaseHandler.java index e7dec661c..6b6564224 100644 --- a/backend/src/src-licenses/src/main/java/com/siemens/sw360/licenses/db/LicenseDatabaseHandler.java +++ b/backend/src/src-licenses/src/main/java/com/siemens/sw360/licenses/db/LicenseDatabaseHandler.java @@ -27,12 +27,14 @@ import com.siemens.sw360.datahandler.thrift.users.User; import com.siemens.sw360.datahandler.thrift.users.UserGroup; import org.ektorp.DocumentOperationResult; +import org.ektorp.http.HttpClient; import org.jetbrains.annotations.NotNull; import org.apache.log4j.Logger; import java.net.MalformedURLException; import java.util.*; +import java.util.function.Supplier; import java.util.stream.Collectors; import static com.siemens.sw360.datahandler.common.CommonUtils.isInProgressOrPending; @@ -68,9 +70,9 @@ public class LicenseDatabaseHandler { private final Logger log = Logger.getLogger(LicenseDatabaseHandler.class); - public LicenseDatabaseHandler(String url, String dbName) throws MalformedURLException { + public LicenseDatabaseHandler(Supplier httpClient, String dbName) throws MalformedURLException { // Create the connector - db = new DatabaseConnector(url, dbName); + db = new DatabaseConnector(httpClient, dbName); // Create the repository licenseRepository = new LicenseRepository(db); diff --git a/backend/src/src-licenses/src/test/java/com/siemens/sw360/licenses/LicenseHandlerTest.java b/backend/src/src-licenses/src/test/java/com/siemens/sw360/licenses/LicenseHandlerTest.java index f5fb4d1c1..9e564d683 100644 --- a/backend/src/src-licenses/src/test/java/com/siemens/sw360/licenses/LicenseHandlerTest.java +++ b/backend/src/src-licenses/src/test/java/com/siemens/sw360/licenses/LicenseHandlerTest.java @@ -56,7 +56,7 @@ public static Todo getById(String id, Collection todos) { @Before public void setUp() throws Exception { // Create the database - TestUtils.createDatabase(url, dbName); + TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); // Create all test entries createTestEntries(); @@ -71,7 +71,7 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { // Delete the database - TestUtils.deleteDatabase(url, dbName); + TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } @Test @@ -182,7 +182,7 @@ public void createTestEntries() throws MalformedURLException { obligations.put("O3", new Obligation().setId("O3").setName("Documentation that represent additional requirements in case of modifications (for example notice file with author's name)")); obligations.put("O4", new Obligation().setId("O4").setName("Apache Copyleft effect")); - DatabaseConnector db = new DatabaseConnector(url, dbName); + DatabaseConnector db = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); // Add obligations to database for (Obligation obligation : obligations.values()) { diff --git a/backend/src/src-moderation/src/main/java/com/siemens/sw360/moderation/ModerationHandler.java b/backend/src/src-moderation/src/main/java/com/siemens/sw360/moderation/ModerationHandler.java index f1d360570..7ca910644 100644 --- a/backend/src/src-moderation/src/main/java/com/siemens/sw360/moderation/ModerationHandler.java +++ b/backend/src/src-moderation/src/main/java/com/siemens/sw360/moderation/ModerationHandler.java @@ -42,7 +42,7 @@ public class ModerationHandler implements ModerationService.Iface { /*private final DocumentDatabaseHandler documentHandler;*/ public ModerationHandler() throws MalformedURLException { - handler = new ModerationDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); + handler = new ModerationDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); /*documentHandler = new DocumentDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE);*/ } diff --git a/backend/src/src-moderation/src/main/java/com/siemens/sw360/moderation/db/ModerationDatabaseHandler.java b/backend/src/src-moderation/src/main/java/com/siemens/sw360/moderation/db/ModerationDatabaseHandler.java index 694da28fd..5c82e9ad1 100644 --- a/backend/src/src-moderation/src/main/java/com/siemens/sw360/moderation/db/ModerationDatabaseHandler.java +++ b/backend/src/src-moderation/src/main/java/com/siemens/sw360/moderation/db/ModerationDatabaseHandler.java @@ -34,10 +34,12 @@ import com.siemens.sw360.mail.MailUtil; import org.apache.log4j.Logger; import org.apache.thrift.TException; +import org.ektorp.http.HttpClient; import java.net.MalformedURLException; import java.util.*; +import java.util.function.Supplier; import java.util.stream.Collectors; @@ -62,15 +64,15 @@ public class ModerationDatabaseHandler { private final ComponentDatabaseHandler componentDatabaseHandler; private final DatabaseConnector db; - public ModerationDatabaseHandler(String url, String dbName, String attachmentDbName) throws MalformedURLException { - db = new DatabaseConnector(url, dbName); + public ModerationDatabaseHandler(Supplier httpClient, String dbName, String attachmentDbName) throws MalformedURLException { + db = new DatabaseConnector(httpClient, dbName); // Create the repository repository = new ModerationRequestRepository(db); - licenseDatabaseHandler = new LicenseDatabaseHandler(url,dbName); - projectDatabaseHandler = new ProjectDatabaseHandler(url,dbName,attachmentDbName); - componentDatabaseHandler = new ComponentDatabaseHandler(url,dbName,attachmentDbName); + licenseDatabaseHandler = new LicenseDatabaseHandler(httpClient, dbName); + projectDatabaseHandler = new ProjectDatabaseHandler(httpClient, dbName, attachmentDbName); + componentDatabaseHandler = new ComponentDatabaseHandler(httpClient, dbName, attachmentDbName); } public List getRequestsByModerator(String moderator) { diff --git a/backend/src/src-moderation/src/test/java/com/siemens/sw360/moderation/testutil/DatabaseTestSetup.java b/backend/src/src-moderation/src/test/java/com/siemens/sw360/moderation/testutil/DatabaseTestSetup.java index fbd085548..774c86e26 100644 --- a/backend/src/src-moderation/src/test/java/com/siemens/sw360/moderation/testutil/DatabaseTestSetup.java +++ b/backend/src/src-moderation/src/test/java/com/siemens/sw360/moderation/testutil/DatabaseTestSetup.java @@ -9,6 +9,7 @@ package com.siemens.sw360.moderation.testutil; +import com.siemens.sw360.datahandler.common.DatabaseSettings; import com.siemens.sw360.datahandler.couchdb.DatabaseConnector; import com.siemens.sw360.datahandler.thrift.moderation.DocumentType; import com.siemens.sw360.datahandler.thrift.moderation.ModerationRequest; @@ -24,9 +25,7 @@ public class DatabaseTestSetup { public static void main(String[] args) throws MalformedURLException { - //DatabaseTestSetup dbSetup = new DatabaseTestSetup("http://localhost:5984", "sw360db"); - - DatabaseConnector db = new DatabaseConnector("http://localhost:5984", "sw360db"); + DatabaseConnector db = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE); Project project = new Project().setName("Test Project"); project.addToModerators("user1"); diff --git a/backend/src/src-projects/src/main/java/com/siemens/sw360/projects/ProjectHandler.java b/backend/src/src-projects/src/main/java/com/siemens/sw360/projects/ProjectHandler.java index 3e598a35b..6316877b9 100644 --- a/backend/src/src-projects/src/main/java/com/siemens/sw360/projects/ProjectHandler.java +++ b/backend/src/src-projects/src/main/java/com/siemens/sw360/projects/ProjectHandler.java @@ -38,12 +38,10 @@ public class ProjectHandler implements ProjectService.Iface { private final ProjectDatabaseHandler handler; private final ProjectSearchHandler searchHandler; - private final AttachmentHandler attachmentHandler; ProjectHandler() throws MalformedURLException, IOException { - handler = new ProjectDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); - searchHandler = new ProjectSearchHandler(DatabaseSettings.COUCH_DB_URL,DatabaseSettings.COUCH_DB_DATABASE); - attachmentHandler = new AttachmentHandler(); + handler = new ProjectDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); + searchHandler = new ProjectSearchHandler(DatabaseSettings.getConfiguredHttpClient(),DatabaseSettings.COUCH_DB_DATABASE); } ///////////////////// diff --git a/backend/src/src-projects/src/test/java/com/siemens/sw360/projects/ProjectHandlerTest.java b/backend/src/src-projects/src/test/java/com/siemens/sw360/projects/ProjectHandlerTest.java index ae6268430..f66c38e39 100644 --- a/backend/src/src-projects/src/test/java/com/siemens/sw360/projects/ProjectHandlerTest.java +++ b/backend/src/src-projects/src/test/java/com/siemens/sw360/projects/ProjectHandlerTest.java @@ -37,7 +37,6 @@ public class ProjectHandlerTest { - private static final String url = DatabaseSettings.COUCH_DB_URL; private static final String dbName = DatabaseSettings.COUCH_DB_DATABASE; private static final String attachmentDbName = DatabaseSettings.COUCH_DB_ATTACHMENTS; @@ -57,10 +56,10 @@ public void setUp() throws Exception { projects.add(new Project().setId("P3").setName("Project3").setBusinessUnit("AB CD EF").setCreatedBy("user3")); // Create the database - TestUtils.createDatabase(url, dbName); + TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); // Prepare the database - DatabaseConnector databaseConnector = new DatabaseConnector(url, dbName); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); for (Project project : projects) { databaseConnector.add(project); } @@ -72,7 +71,7 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { // Delete the database - TestUtils.deleteDatabase(url, dbName); + TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } @Test @@ -216,7 +215,7 @@ public void testUpdateProject1_1() throws Exception { public void testUpdateProject2_1() throws Exception { ProjectModerator moderator = Mockito.mock(ProjectModerator.class); - ProjectDatabaseHandler handler = new ProjectDatabaseHandler(url, dbName, attachmentDbName, moderator, new ComponentDatabaseHandler(url, dbName, attachmentDbName)); + ProjectDatabaseHandler handler = new ProjectDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), dbName, attachmentDbName, moderator, new ComponentDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), dbName, attachmentDbName)); Project project2 = handler.getProjectById("P2", user1); project2.setName("Project2new"); diff --git a/backend/src/src-search/src/main/java/com/siemens/sw360/search/db/DatabaseSearchHandler.java b/backend/src/src-search/src/main/java/com/siemens/sw360/search/db/DatabaseSearchHandler.java index dac19cb4f..41550d678 100644 --- a/backend/src/src-search/src/main/java/com/siemens/sw360/search/db/DatabaseSearchHandler.java +++ b/backend/src/src-search/src/main/java/com/siemens/sw360/search/db/DatabaseSearchHandler.java @@ -66,8 +66,8 @@ public class DatabaseSearchHandler { public DatabaseSearchHandler(String url, String dbName) throws IOException { // Create the database connector and add the search view to couchDB - projectRepository = new ProjectRepository(new DatabaseConnector(url, dbName)); - connector = new LuceneAwareDatabaseConnector(url, dbName); + projectRepository = new ProjectRepository(new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName)); + connector = new LuceneAwareDatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); connector.addView(luceneSearchView); connector.setResultLimit(DatabaseSettings.LUCENE_SEARCH_LIMIT); } diff --git a/backend/src/src-users/src/main/java/com/siemens/sw360/users/UserHandler.java b/backend/src/src-users/src/main/java/com/siemens/sw360/users/UserHandler.java index 5c3cf145d..31dd6e3f4 100644 --- a/backend/src/src-users/src/main/java/com/siemens/sw360/users/UserHandler.java +++ b/backend/src/src-users/src/main/java/com/siemens/sw360/users/UserHandler.java @@ -34,7 +34,7 @@ public class UserHandler implements UserService.Iface { UserDatabaseHandler db; public UserHandler() throws MalformedURLException { - db = new UserDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_USERS); + db = new UserDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_USERS); } @Override diff --git a/backend/src/src-users/src/main/java/com/siemens/sw360/users/db/UserDatabaseHandler.java b/backend/src/src-users/src/main/java/com/siemens/sw360/users/db/UserDatabaseHandler.java index c68738473..565afb6bc 100644 --- a/backend/src/src-users/src/main/java/com/siemens/sw360/users/db/UserDatabaseHandler.java +++ b/backend/src/src-users/src/main/java/com/siemens/sw360/users/db/UserDatabaseHandler.java @@ -14,9 +14,11 @@ import com.siemens.sw360.datahandler.thrift.users.User; import com.siemens.sw360.mail.MailConstants; import com.siemens.sw360.mail.MailUtil; +import org.ektorp.http.HttpClient; import java.net.MalformedURLException; import java.util.List; +import java.util.function.Supplier; import static com.siemens.sw360.datahandler.permissions.PermissionUtils.makePermission; @@ -33,9 +35,9 @@ public class UserDatabaseHandler { private DatabaseConnector db; private UserRepository repository; - public UserDatabaseHandler(String url, String dbName) throws MalformedURLException { + public UserDatabaseHandler(Supplier httpClient, String dbName) throws MalformedURLException { // Create the connector - db = new DatabaseConnector(url, dbName); + db = new DatabaseConnector(httpClient, dbName); repository = new UserRepository(db); } diff --git a/backend/src/src-vendors/src/main/java/com/siemens/sw360/vendors/VendorHandler.java b/backend/src/src-vendors/src/main/java/com/siemens/sw360/vendors/VendorHandler.java index b6b124557..6413f7843 100644 --- a/backend/src/src-vendors/src/main/java/com/siemens/sw360/vendors/VendorHandler.java +++ b/backend/src/src-vendors/src/main/java/com/siemens/sw360/vendors/VendorHandler.java @@ -38,7 +38,7 @@ public class VendorHandler implements VendorService.Iface { private final VendorSearch vendorSearch; public VendorHandler() throws IOException { - DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE); vendorRepository = new VendorRepository(databaseConnector); vendorSearch = new VendorSearch(databaseConnector); // Remove release id from component } diff --git a/backend/src/src-vendors/src/test/java/com/siemens/sw360/vendors/TestVendorClient.java b/backend/src/src-vendors/src/test/java/com/siemens/sw360/vendors/TestVendorClient.java index d7bd85d03..802520dd8 100644 --- a/backend/src/src-vendors/src/test/java/com/siemens/sw360/vendors/TestVendorClient.java +++ b/backend/src/src-vendors/src/test/java/com/siemens/sw360/vendors/TestVendorClient.java @@ -8,6 +8,7 @@ */ package com.siemens.sw360.vendors; +import com.siemens.sw360.datahandler.common.DatabaseSettings; import com.siemens.sw360.datahandler.couchdb.DatabaseConnector; import com.siemens.sw360.datahandler.thrift.vendors.Vendor; import com.siemens.sw360.datahandler.thrift.vendors.VendorService; @@ -29,7 +30,7 @@ public class TestVendorClient { @SuppressWarnings("unused") public static void InitDatabase() throws MalformedURLException { - DatabaseConnector databaseConnector = new DatabaseConnector("http://localhost:5984", "sw360db"); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE); databaseConnector.add(new Vendor().setShortname("Microsoft").setFullname("Microsoft Corporation").setUrl("http://www.microsoft.com")); databaseConnector.add(new Vendor().setShortname("Apache").setFullname("The Apache Software Foundation").setUrl("http://www.apache.org")); diff --git a/backend/src/src-vendors/src/test/java/com/siemens/sw360/vendors/VendorHandlerTest.java b/backend/src/src-vendors/src/test/java/com/siemens/sw360/vendors/VendorHandlerTest.java index 749f883d2..d8c239647 100644 --- a/backend/src/src-vendors/src/test/java/com/siemens/sw360/vendors/VendorHandlerTest.java +++ b/backend/src/src-vendors/src/test/java/com/siemens/sw360/vendors/VendorHandlerTest.java @@ -26,7 +26,6 @@ public class VendorHandlerTest { - private static final String url = DatabaseSettings.COUCH_DB_URL; private static final String dbName = DatabaseSettings.COUCH_DB_DATABASE; private VendorHandler vendorHandler; @@ -36,10 +35,10 @@ public class VendorHandlerTest { public void setUp() throws Exception { // Create the database - TestUtils.createDatabase(url, dbName); + TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); // Prepare the database - DatabaseConnector databaseConnector = new DatabaseConnector(url, dbName); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); vendorList = new ArrayList<>(); vendorList.add(new Vendor().setShortname("Microsoft").setFullname("Microsoft Corporation").setUrl("http://www.microsoft.com")); vendorList.add(new Vendor().setShortname("Apache").setFullname("The Apache Software Foundation").setUrl("http://www.apache.org")); @@ -55,7 +54,7 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { // Delete the database - TestUtils.deleteDatabase(url, dbName); + TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } diff --git a/backend/src/src-vulnerabilities/src/main/java/com/siemens/sw360/vulnerabilities/VulnerabilityHandler.java b/backend/src/src-vulnerabilities/src/main/java/com/siemens/sw360/vulnerabilities/VulnerabilityHandler.java index 35c5225f4..dd55475e8 100644 --- a/backend/src/src-vulnerabilities/src/main/java/com/siemens/sw360/vulnerabilities/VulnerabilityHandler.java +++ b/backend/src/src-vulnerabilities/src/main/java/com/siemens/sw360/vulnerabilities/VulnerabilityHandler.java @@ -54,9 +54,9 @@ public class VulnerabilityHandler implements VulnerabilityService.Iface { private final ProjectDatabaseHandler projectDatabaseHandler; public VulnerabilityHandler() throws IOException, SW360Exception { - dbHandler = new VulnerabilityDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_VM); - compHandler = new ComponentDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); - projectDatabaseHandler = new ProjectDatabaseHandler(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); + dbHandler = new VulnerabilityDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_VM); + compHandler = new ComponentDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); + projectDatabaseHandler = new ProjectDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS); } @Override diff --git a/backend/src/src-vulnerabilities/src/main/java/com/siemens/sw360/vulnerabilities/db/VulnerabilityDatabaseHandler.java b/backend/src/src-vulnerabilities/src/main/java/com/siemens/sw360/vulnerabilities/db/VulnerabilityDatabaseHandler.java index dce990af0..c44f4e315 100644 --- a/backend/src/src-vulnerabilities/src/main/java/com/siemens/sw360/vulnerabilities/db/VulnerabilityDatabaseHandler.java +++ b/backend/src/src-vulnerabilities/src/main/java/com/siemens/sw360/vulnerabilities/db/VulnerabilityDatabaseHandler.java @@ -18,9 +18,11 @@ import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.apache.thrift.TBase; +import org.ektorp.http.HttpClient; import java.net.MalformedURLException; import java.util.*; +import java.util.function.Supplier; /** * Class for accessing the CouchDB database @@ -38,9 +40,9 @@ public class VulnerabilityDatabaseHandler { private VulnerabilityRepository vulRepo; private VulnerabilityRelationRepository relationRepo; - public VulnerabilityDatabaseHandler(String url, String dbName) throws MalformedURLException { + public VulnerabilityDatabaseHandler(Supplier httpClient, String dbName) throws MalformedURLException { // Create the connector - db = new DatabaseConnector(url, dbName); + db = new DatabaseConnector(httpClient, dbName); vulRepo = new VulnerabilityRepository(db); relationRepo = new VulnerabilityRelationRepository(db); } diff --git a/backend/utils/src/main/java/com/siemens/sw360/attachments/db/RemoteAttachmentDownloader.java b/backend/utils/src/main/java/com/siemens/sw360/attachments/db/RemoteAttachmentDownloader.java index b50ed5e0c..f26383837 100644 --- a/backend/utils/src/main/java/com/siemens/sw360/attachments/db/RemoteAttachmentDownloader.java +++ b/backend/utils/src/main/java/com/siemens/sw360/attachments/db/RemoteAttachmentDownloader.java @@ -16,12 +16,14 @@ import com.siemens.sw360.datahandler.thrift.SW360Exception; import com.siemens.sw360.datahandler.thrift.attachments.AttachmentContent; import org.apache.log4j.Logger; +import org.ektorp.http.HttpClient; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import static com.siemens.sw360.datahandler.common.CommonUtils.closeQuietly; import static com.siemens.sw360.datahandler.common.Duration.durationOf; @@ -37,16 +39,13 @@ public class RemoteAttachmentDownloader { private static final Logger log = getLogger(RemoteAttachmentDownloader.class); public static void main(String[] args) throws MalformedURLException { - String couchDbUrl = DatabaseSettings.COUCH_DB_URL; - String dbAttachments = DatabaseSettings.COUCH_DB_ATTACHMENTS; - Duration downloadTimeout = durationOf(30, TimeUnit.SECONDS); - retrieveRemoteAttachments(couchDbUrl, dbAttachments, downloadTimeout); + retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_ATTACHMENTS, downloadTimeout); } - public static int retrieveRemoteAttachments(String couchDbUrl, String dbAttachments, Duration downloadTimeout) throws MalformedURLException { - AttachmentConnector attachmentConnector = new AttachmentConnector(couchDbUrl, dbAttachments, downloadTimeout); - AttachmentRepository attachmentRepository = new AttachmentRepository(new DatabaseConnector(couchDbUrl, dbAttachments)); + public static int retrieveRemoteAttachments(Supplier httpClient, String dbAttachments, Duration downloadTimeout) throws MalformedURLException { + AttachmentConnector attachmentConnector = new AttachmentConnector(httpClient, dbAttachments, downloadTimeout); + AttachmentRepository attachmentRepository = new AttachmentRepository(new DatabaseConnector(httpClient, dbAttachments)); List remoteAttachments = attachmentRepository.getOnlyRemoteAttachments(); log.info(format("we have %d remote attachments to retrieve", remoteAttachments.size())); diff --git a/backend/utils/src/main/resources/couchdb.properties b/backend/utils/src/main/resources/couchdb.properties deleted file mode 100644 index 20a051e70..000000000 --- a/backend/utils/src/main/resources/couchdb.properties +++ /dev/null @@ -1,11 +0,0 @@ -# -# Copyright Siemens AG, 2013-2015. Part of the SW360 Portal Project. -# -# Copying and distribution of this file, with or without modification, -# are permitted in any medium without royalty provided the copyright -# notice and this notice are preserved. This file is offered as-is, -# without any warranty. -# -couchdb.url=http://localhost:5984 -couchdb.database=sw360db -couchdb.attachments=sw360attachments diff --git a/backend/utils/src/test/java/com/siemens/sw360/attachments/db/RemoteAttachmentDownloaderTest.java b/backend/utils/src/test/java/com/siemens/sw360/attachments/db/RemoteAttachmentDownloaderTest.java index 0d263ef3b..6106ae296 100644 --- a/backend/utils/src/test/java/com/siemens/sw360/attachments/db/RemoteAttachmentDownloaderTest.java +++ b/backend/utils/src/test/java/com/siemens/sw360/attachments/db/RemoteAttachmentDownloaderTest.java @@ -58,13 +58,13 @@ public class RemoteAttachmentDownloaderTest { @BeforeClass public static void setUpClass() throws Exception { assertTestString(dbName); - deleteDatabase(url, dbName); + deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } @Before public void setUp() throws Exception { - DatabaseConnector databaseConnector = new DatabaseConnector(url, dbName); - attachmentConnector = new AttachmentConnector(url, dbName, downloadTimeout); + DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName); + attachmentConnector = new AttachmentConnector(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout); repository = new AttachmentRepository(databaseConnector); garbage = new ArrayList<>(); @@ -81,11 +81,11 @@ public void tearDown() throws Exception { public void testIntegration() throws Exception { AttachmentContent attachmentContent = saveRemoteAttachment(url); - assertThat(retrieveRemoteAttachments(url, dbName, downloadTimeout), is(1)); + assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(1)); assertThat(attachmentConnector.getAttachmentStream(attachmentContent), hasLength(greaterThan(0l))); - assertThat(retrieveRemoteAttachments(url, dbName, downloadTimeout), is(0)); + assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(0)); } @Test @@ -101,7 +101,7 @@ public void testWithBlackHole() throws Exception { Future future = executor.submit(new Callable() { @Override public Integer call() throws Exception { - return retrieveRemoteAttachments(url, dbName, downloadTimeout); + return retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout); } }); @@ -126,13 +126,13 @@ public void testWithBrokenURL() throws Exception { AttachmentContent attachmentGood = saveRemoteAttachment(url); assertThat(repository.getOnlyRemoteAttachments(), hasSize(2)); - assertThat(retrieveRemoteAttachments(url, dbName, downloadTimeout), is(1)); + assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(1)); assertThat(repository.getOnlyRemoteAttachments(), hasSize(1)); assertThat(attachmentConnector.getAttachmentStream(attachmentGood), hasLength(greaterThan(0l))); assertThat(repository.getOnlyRemoteAttachments(), hasSize(1)); - assertThat(retrieveRemoteAttachments(url, dbName, downloadTimeout), is(0)); + assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(0)); assertThat(repository.getOnlyRemoteAttachments(), hasSize(1)); try { diff --git a/frontend/sw360-portlet/src/main/java/com/siemens/sw360/portal/common/AttachmentPortletUtils.java b/frontend/sw360-portlet/src/main/java/com/siemens/sw360/portal/common/AttachmentPortletUtils.java index ae99bbcc5..40c4a40df 100644 --- a/frontend/sw360-portlet/src/main/java/com/siemens/sw360/portal/common/AttachmentPortletUtils.java +++ b/frontend/sw360-portlet/src/main/java/com/siemens/sw360/portal/common/AttachmentPortletUtils.java @@ -71,12 +71,7 @@ public AttachmentPortletUtils(ThriftClients thriftClients) { private synchronized void makeConnector() throws TException { if (connector == null) { try { - AttachmentService.Iface client = thriftClients.makeAttachmentClient(); - DatabaseAddress address = client.getDatabaseAddress(); - connector = new AttachmentStreamConnector(address, downloadTimeout); - } catch (TException e) { - log.error("Could not get database address from attachment client...", e); - throw e; + connector = new AttachmentStreamConnector(downloadTimeout); } catch (MalformedURLException e) { log.error("Invalid database address received...", e); throw new TException(e); diff --git a/libraries/importers/component-importer/src/test/java/com/siemens/sw360/importer/ComponentAndAttachmentAwareDBTest.java b/libraries/importers/component-importer/src/test/java/com/siemens/sw360/importer/ComponentAndAttachmentAwareDBTest.java index 88033bc80..2e5c77df8 100644 --- a/libraries/importers/component-importer/src/test/java/com/siemens/sw360/importer/ComponentAndAttachmentAwareDBTest.java +++ b/libraries/importers/component-importer/src/test/java/com/siemens/sw360/importer/ComponentAndAttachmentAwareDBTest.java @@ -51,7 +51,7 @@ public class ComponentAndAttachmentAwareDBTest { protected User user; protected static DatabaseConnector getDBConnector(String couchDbDatabase) throws MalformedURLException { - return new DatabaseConnector(DatabaseSettings.COUCH_DB_URL, couchDbDatabase); + return new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), couchDbDatabase); } @@ -76,8 +76,8 @@ protected static FluentIterable getCompAttachmentC } protected void deleteDatabases() throws MalformedURLException { - deleteDatabase(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_ATTACHMENTS); - deleteDatabase(DatabaseSettings.COUCH_DB_URL, DatabaseSettings.COUCH_DB_DATABASE); + deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_ATTACHMENTS); + deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE); } protected static ThriftClients getThriftClients() throws TException, IOException { assertTestDbNames(); diff --git a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/common/DatabaseSettings.java b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/common/DatabaseSettings.java index f1c929196..3c8f93226 100644 --- a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/common/DatabaseSettings.java +++ b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/common/DatabaseSettings.java @@ -8,7 +8,13 @@ */ package com.siemens.sw360.datahandler.common; +import java.net.MalformedURLException; +import java.util.Optional; import java.util.Properties; +import java.util.function.Supplier; + +import org.ektorp.http.HttpClient; +import org.ektorp.http.StdHttpClient; /** * Constants for the database address @@ -29,11 +35,16 @@ public class DatabaseSettings { public static final int LUCENE_SEARCH_LIMIT; + private static final String COUCH_DB_USERNAME; + private static final String COUCH_DB_PASSWORD; + static { Properties props = CommonUtils.loadProperties(DatabaseSettings.class, PROPERTIES_FILE_PATH); COUCH_DB_URL = props.getProperty("couchdb.url", "http://localhost:5984"); COUCH_DB_DATABASE = props.getProperty("couchdb.database", "sw360db"); + COUCH_DB_USERNAME = props.getProperty("couchdb.user", ""); + COUCH_DB_PASSWORD = props.getProperty("couchdb.password", ""); COUCH_DB_USERS = props.getProperty("couchdb.usersdb", "sw360users"); COUCH_DB_ATTACHMENTS = props.getProperty("couchdb.attachments", "sw360attachments"); COUCH_DB_FOSSOLOGY = props.getProperty("couchdb.fossologyKeys", "sw360fossologyKeys"); @@ -42,6 +53,17 @@ public class DatabaseSettings { LUCENE_SEARCH_LIMIT = Integer.parseInt(props.getProperty("lucenesearch.limit", "25")); } + public static Supplier getConfiguredHttpClient() throws MalformedURLException { + StdHttpClient.Builder httpClientBuilder = new StdHttpClient.Builder().url(COUCH_DB_URL); + if(! "".equals(COUCH_DB_USERNAME)) { + httpClientBuilder.username(COUCH_DB_USERNAME); + } + if (! "".equals(COUCH_DB_PASSWORD)) { + httpClientBuilder.password(COUCH_DB_PASSWORD); + } + return httpClientBuilder::build; + } + private DatabaseSettings() { // Utility class with only static functions diff --git a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/AttachmentConnector.java b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/AttachmentConnector.java index 37cd2acf0..94ff301bd 100644 --- a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/AttachmentConnector.java +++ b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/AttachmentConnector.java @@ -19,6 +19,7 @@ import java.net.MalformedURLException; import java.util.Collection; import java.util.Set; +import java.util.function.Supplier; import java.util.stream.Collectors; import static com.google.common.base.Strings.isNullOrEmpty; @@ -28,6 +29,7 @@ import static org.apache.commons.codec.digest.DigestUtils.sha1Hex; import org.apache.log4j.Logger; +import org.ektorp.http.HttpClient; /** * Ektorp connector for uploading attachments @@ -46,8 +48,8 @@ public AttachmentConnector(DatabaseConnector connector, Duration downloadTimeout /** * @todo remove this mess of constructors and use dependency injection */ - public AttachmentConnector(String url, String dbName, Duration downloadTimeout) throws MalformedURLException { - this(new DatabaseConnector(url, dbName), downloadTimeout); + public AttachmentConnector(Supplier httpClient, String dbName, Duration downloadTimeout) throws MalformedURLException { + this(new DatabaseConnector(httpClient.get(), dbName), downloadTimeout); } /** diff --git a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/AttachmentStreamConnector.java b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/AttachmentStreamConnector.java index a487a2800..f49d252e2 100644 --- a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/AttachmentStreamConnector.java +++ b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/AttachmentStreamConnector.java @@ -10,6 +10,7 @@ import com.siemens.sw360.datahandler.common.CommonUtils; import com.siemens.sw360.datahandler.common.ConcatClosingInputStream; +import com.siemens.sw360.datahandler.common.DatabaseSettings; import com.siemens.sw360.datahandler.common.Duration; import com.siemens.sw360.datahandler.thrift.SW360Exception; import com.siemens.sw360.datahandler.thrift.attachments.AttachmentContent; @@ -39,13 +40,12 @@ public class AttachmentStreamConnector { private final Duration downloadTimeout; /** - * @param address Thrift object representing the database address * @param downloadTimeout timeout for downloading remote attachments * @throws java.net.MalformedURLException if the given database address not a valid url. * @todo remove this mess of constructors and use dependency injection */ - public AttachmentStreamConnector(DatabaseAddress address, Duration downloadTimeout) throws MalformedURLException { - this(new DatabaseConnector(address.getUrl(), address.getDbName()), downloadTimeout); + public AttachmentStreamConnector(Duration downloadTimeout) throws MalformedURLException { + this(new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient().get(), DatabaseSettings.COUCH_DB_ATTACHMENTS), downloadTimeout); } public AttachmentStreamConnector(DatabaseConnector connector, Duration downloadTimeout) { diff --git a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/DatabaseConnector.java b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/DatabaseConnector.java index 25564e2ae..05f132b2a 100644 --- a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/DatabaseConnector.java +++ b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/DatabaseConnector.java @@ -13,11 +13,13 @@ import com.siemens.sw360.datahandler.thrift.attachments.DatabaseAddress; import org.apache.log4j.Logger; import org.ektorp.*; +import org.ektorp.http.HttpClient; import org.ektorp.impl.StdCouchDbConnector; import org.ektorp.util.Documents; import java.net.MalformedURLException; import java.util.*; +import java.util.function.Supplier; /** * Database Connector to a CouchDB database @@ -34,22 +36,32 @@ public class DatabaseConnector extends StdCouchDbConnector { /** * Create a connection to the database * - * @param url URL of CouchDB server + * @param httpClient HttpClient with authentication of CouchDB server * @param dbName name of the database on the CouchDB server */ - public DatabaseConnector(String url, String dbName) throws MalformedURLException { - this(url, dbName, new MapperFactory()); + public DatabaseConnector(HttpClient httpClient, String dbName) throws MalformedURLException { + this(httpClient, dbName, new MapperFactory()); } /** * Create a connection to the database * - * @param url URL of CouchDB server + * @param httpClient Supplier with authentication of CouchDB server + * @param dbName name of the database on the CouchDB server + */ + public DatabaseConnector(Supplier httpClient, String dbName) throws MalformedURLException { + this(httpClient.get(), dbName, new MapperFactory()); + } + + /** + * Create a connection to the database + * + * @param httpClient HttpClient with authentication of CouchDB server * @param dbName name of the database on the CouchDB server * @param mapperFactory Specific mapper factory to use for serialization */ - public DatabaseConnector(String url, String dbName, MapperFactory mapperFactory) throws MalformedURLException { - this(dbName, new DatabaseInstance(url), mapperFactory); + public DatabaseConnector(HttpClient httpClient, String dbName, MapperFactory mapperFactory) throws MalformedURLException { + this(dbName, new DatabaseInstance(httpClient), mapperFactory); } private DatabaseConnector(String dbName, DatabaseInstance instance, MapperFactory mapperFactory) throws MalformedURLException { @@ -170,10 +182,6 @@ public boolean deleteById(String id) { return false; } - public DatabaseAddress getAddress() { - return new DatabaseAddress(instance.getUrl(), dbName); - } - public String getDbName() { return dbName; } diff --git a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/DatabaseInstance.java b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/DatabaseInstance.java index bcf3c104b..1af6042eb 100644 --- a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/DatabaseInstance.java +++ b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/DatabaseInstance.java @@ -8,6 +8,7 @@ */ package com.siemens.sw360.datahandler.couchdb; +import org.ektorp.http.HttpClient; import org.ektorp.http.StdHttpClient; import org.ektorp.impl.StdCouchDbInstance; @@ -18,17 +19,14 @@ */ public class DatabaseInstance extends StdCouchDbInstance { - private final String url; - /** * Builds a CouchDB instance using ektorp * - * @param url URL of the CouchDB instance + * @param httpClient HttpClient with authentication of the CouchDB instance * @throws MalformedURLException */ - public DatabaseInstance(String url) throws MalformedURLException { - super(new StdHttpClient.Builder().url(url).build()); - this.url = url; + public DatabaseInstance(HttpClient httpClient) throws MalformedURLException { + super(httpClient); DatabaseInstanceTracker.track(this); } @@ -42,8 +40,4 @@ public void createDatabase(String dbName) { public void destroy() { getConnection().shutdown(); } - - public String getUrl() { - return url; - } } diff --git a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/lucene/LuceneAwareDatabaseConnector.java b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/lucene/LuceneAwareDatabaseConnector.java index 09026d2d7..902804eb5 100644 --- a/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/lucene/LuceneAwareDatabaseConnector.java +++ b/libraries/lib-datahandler/src/main/java/com/siemens/sw360/datahandler/couchdb/lucene/LuceneAwareDatabaseConnector.java @@ -23,9 +23,11 @@ import com.siemens.sw360.datahandler.thrift.users.User; import org.apache.log4j.Logger; import org.ektorp.DbAccessException; +import org.ektorp.http.HttpClient; import java.io.IOException; import java.util.*; +import java.util.function.Supplier; import static com.google.common.base.Strings.isNullOrEmpty; import static com.siemens.sw360.datahandler.common.ThriftEnumUtils.enumByString; @@ -53,8 +55,8 @@ public class LuceneAwareDatabaseConnector extends LuceneAwareCouchDbConnector { /** * URL/DbName constructor */ - public LuceneAwareDatabaseConnector(String url, String dbName) throws IOException { - this(new DatabaseConnector(url, dbName)); + public LuceneAwareDatabaseConnector(Supplier httpClient, String dbName) throws IOException { + this(new DatabaseConnector(httpClient, dbName)); } /** diff --git a/libraries/lib-datahandler/src/main/thrift/attachments.thrift b/libraries/lib-datahandler/src/main/thrift/attachments.thrift index fa0a7ae95..7884c7e8b 100644 --- a/libraries/lib-datahandler/src/main/thrift/attachments.thrift +++ b/libraries/lib-datahandler/src/main/thrift/attachments.thrift @@ -89,11 +89,6 @@ struct DatabaseAddress { service AttachmentService { - /** - * Returns the URL and database name where to upload the attachment - * */ - DatabaseAddress getDatabaseAddress(); - /** * Add attachmentContent (= the actual attachment object) object to database, * return attachmentContent as written to database if successful diff --git a/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/TestUtils.java b/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/TestUtils.java index 46fbaefd3..c69a216f5 100644 --- a/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/TestUtils.java +++ b/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/TestUtils.java @@ -18,6 +18,7 @@ import com.siemens.sw360.datahandler.thrift.users.UserGroup; import org.apache.thrift.TBase; import org.apache.thrift.TFieldIdEnum; +import org.ektorp.http.HttpClient; import org.hamcrest.*; import org.hamcrest.collection.IsEmptyCollection; import org.mockito.invocation.InvocationOnMock; @@ -29,6 +30,7 @@ import java.net.SocketException; import java.net.URL; import java.util.*; +import java.util.function.Supplier; import static com.google.common.base.Predicates.instanceOf; import static com.google.common.base.Predicates.not; @@ -69,7 +71,7 @@ public static void assertTestDbNames() { public static void deleteAllDatabases() throws MalformedURLException { for (String dbName : dbNames) { - deleteDatabase(DatabaseSettings.COUCH_DB_URL, dbName); + deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName); } } @@ -109,20 +111,24 @@ public static User getAdminUser(Class caller) { return user; } - public static void deleteDatabase(String url, String dbName) throws MalformedURLException { + public static void deleteDatabase(Supplier httpClient, String dbName) throws MalformedURLException { assertTestString(dbName); - DatabaseInstance instance = new DatabaseInstance(url); + DatabaseInstance instance = new DatabaseInstance(httpClient.get()); if (instance.checkIfDbExists(dbName)) instance.deleteDatabase(dbName); DatabaseInstanceTracker.destroy(); } - public static void createDatabase(String url, String dbName) throws MalformedURLException { + public static void createDatabase(Supplier httpClient, String dbName) throws MalformedURLException { + createDatabase(httpClient.get(), dbName); + } + + public static void createDatabase(HttpClient httpClient, String dbName) throws MalformedURLException { assertTestString(dbName); - DatabaseInstance instance = new DatabaseInstance(url); + DatabaseInstance instance = new DatabaseInstance(httpClient); if (instance.checkIfDbExists(dbName)) instance.deleteDatabase(dbName); diff --git a/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/couchdb/DatabaseConnectorTest.java b/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/couchdb/DatabaseConnectorTest.java index 58a856376..b39667b92 100644 --- a/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/couchdb/DatabaseConnectorTest.java +++ b/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/couchdb/DatabaseConnectorTest.java @@ -61,7 +61,7 @@ public void setUp() throws Exception { id = object.getId(); rev = object.getRevision(); // Now create the actual database connector - connector = new DatabaseConnector(COUCH_DB_URL, COUCH_DB_DATABASE, factory); + connector = new DatabaseConnector(DatabaseTestProperties.getConfiguredHttpClient(), COUCH_DB_DATABASE, factory); } @After diff --git a/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/couchdb/DatabaseTestProperties.java b/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/couchdb/DatabaseTestProperties.java index 934373d59..0a1ef912f 100644 --- a/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/couchdb/DatabaseTestProperties.java +++ b/libraries/lib-datahandler/src/test/java/com/siemens/sw360/datahandler/couchdb/DatabaseTestProperties.java @@ -11,7 +11,11 @@ import com.siemens.sw360.datahandler.common.CommonUtils; import org.apache.log4j.Logger; +import org.ektorp.http.HttpClient; +import org.ektorp.http.StdHttpClient; +import java.net.MalformedURLException; +import java.util.Optional; import java.util.Properties; /** @@ -26,10 +30,24 @@ public class DatabaseTestProperties { public static final String COUCH_DB_URL; public static final String COUCH_DB_DATABASE; + private static final Optional COUCH_DB_USERNAME; + private static final Optional COUCH_DB_PASSWORD; + static { Properties props = CommonUtils.loadProperties(DatabaseTestProperties.class, PROPERTIES_FILE_PATH); COUCH_DB_URL = props.getProperty("couch_db_url", "http://localhost:5984"); COUCH_DB_DATABASE = props.getProperty("couch_db_database", "datahandlertestdb"); + COUCH_DB_USERNAME = Optional.ofNullable(props.getProperty("couchdb.username", null)); + COUCH_DB_PASSWORD = Optional.ofNullable(props.getProperty("couchdb.password", null)); + } + + public static HttpClient getConfiguredHttpClient() throws MalformedURLException { + StdHttpClient.Builder httpClientBuilder = new StdHttpClient.Builder().url(COUCH_DB_URL); + if(COUCH_DB_USERNAME.isPresent() && COUCH_DB_PASSWORD.isPresent()) { + httpClientBuilder.username(COUCH_DB_USERNAME.get()); + httpClientBuilder.password(COUCH_DB_PASSWORD.get()); + } + return httpClientBuilder.build(); } }