Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
Merge pull request #215 from bsinno/dev/addCouchdbPW/contrib
Browse files Browse the repository at this point in the history
add couchdb password / authentication support 
review-by:[email protected]
tested-by:[email protected]
  • Loading branch information
mcjaeger authored Sep 28, 2016
2 parents 5b82326 + f40c1af commit 8d5044e
Show file tree
Hide file tree
Showing 48 changed files with 222 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> httpClient, String dbName, String attachmentDbName, ComponentModerator moderator, ReleaseModerator releaseModerator) throws MalformedURLException {
DatabaseConnector db = new DatabaseConnector(httpClient, dbName);

// Create the repositories
vendorRepository = new VendorRepository(db);
Expand All @@ -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> 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> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> httpClient, String dbName) throws IOException {
connector = new LuceneAwareDatabaseConnector(httpClient, dbName);
connector.addView(luceneSearchView);
connector.setResultLimit(DatabaseSettings.LUCENE_SEARCH_LIMIT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> 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> httpClient, String dbName, String attachmentDbName, ProjectModerator moderator, ComponentDatabaseHandler componentDatabaseHandler) throws MalformedURLException {
DatabaseConnector db = new DatabaseConnector(httpClient, dbName);

// Create the repositories
repository = new ProjectRepository(db);
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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> httpClient, String dbName) throws IOException {
connector = new LuceneAwareDatabaseConnector(httpClient, dbName);
connector.addView(luceneSearchView);
connector.setResultLimit(DatabaseSettings.LUCENE_SEARCH_LIMIT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,20 +45,16 @@ 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> httpClient, String attachmentDbName) throws MalformedURLException {
db = new DatabaseConnector(httpClient, attachmentDbName);
attachmentConnector = new AttachmentConnector(httpClient, attachmentDbName, durationOf(30, TimeUnit.SECONDS));
repository = new AttachmentRepository(db);
}

public AttachmentConnector getAttachmentConnector(){
return attachmentConnector;
}

public DatabaseAddress getDatabaseAddress(){
return db.getAddress();
}

public AttachmentContent add(AttachmentContent attachmentContent){
repository.add(attachmentContent);
return attachmentContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]").setCreatedOn("2012-07-30").setFilename("a.txt").setContentType("text"));
// databaseConnector.add(new Attachment().setId("A2").setCreatedBy("[email protected]").setCreatedOn("2012-05-22").setFilename("b.jpg").setContentType("image"));
databaseConnector.add(new AttachmentContent().setId("A1").setFilename("a.txt").setContentType("text"));
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions backend/src/src-bdpimport/src/test/resources/couchdb.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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> 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> httpClient, String dbName, String attachmentDbName, ThriftClients thriftClients) throws IOException {
handler = new ComponentDatabaseHandler(httpClient, dbName, attachmentDbName, thriftClients);
searchHandler = new ComponentSearchHandler(httpClient, dbName);
attachmentHandler = new AttachmentHandler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 8d5044e

Please sign in to comment.