From 9452b2b89a42215399617711933894fec8c5d5b7 Mon Sep 17 00:00:00 2001 From: Gaurav Mishra Date: Sun, 13 Oct 2024 02:08:04 +0530 Subject: [PATCH] fix(cloudant): fix attachment creation While creating attachment with repository.add(), there are additional steps needed to create the attachment in DB. Not used by API. Signed-off-by: Gaurav Mishra --- .../db/AttachmentDatabaseHandler.java | 4 +- .../datahandler/db/DatabaseHandlerUtil.java | 8 ++- .../db/ProjectDatabaseHandler.java | 7 ++- .../components/db/BulkDeleteUtilTest.java | 8 +-- .../fossology/config/FossologyRestConfig.java | 20 ++++-- .../licenses/db/LicenseDatabaseHandler.java | 7 ++- .../sw360/licenses/LicenseHandlerTest.java | 3 +- .../db/ModerationDatabaseHandler.java | 61 +++++++++++++++---- .../testutil/DatabaseTestSetup.java | 3 +- .../db/RemoteAttachmentDownloaderTest.java | 2 +- .../sw360/vendors/VendorDatabaseHandler.java | 7 ++- .../sw360/vendors/TestVendorClient.java | 7 +-- .../DatabaseConnectorCloudant.java | 25 +++++++- .../DatabaseRepositoryCloudantClient.java | 3 +- .../importer/ComponentImportUtilsTest.java | 6 +- .../client/rest/OAuthClientController.java | 8 ++- .../OAuthClientRepositoryTest.java | 3 +- .../project/ProjectController.java | 9 +-- 18 files changed, 142 insertions(+), 49 deletions(-) diff --git a/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/AttachmentDatabaseHandler.java b/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/AttachmentDatabaseHandler.java index 43c02e551a..6b0f33a861 100644 --- a/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/AttachmentDatabaseHandler.java +++ b/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/AttachmentDatabaseHandler.java @@ -67,7 +67,7 @@ public AttachmentConnector getAttachmentConnector(){ return attachmentConnector; } - public AttachmentContent add(AttachmentContent attachmentContent){ + public AttachmentContent add(AttachmentContent attachmentContent) throws SW360Exception { attachmentContentRepository.add(attachmentContent); return attachmentContent; } @@ -138,7 +138,7 @@ public void deleteAttachmentUsagesByUsageDataTypes(Source usedBy, Set Runnable prepareChangeLogRunnable(T newDocVersi referenceDocLogList.stream().forEach(referenceDocLog -> { referenceDocLog.setDocumentId(changeLogParentId); changelog.debug(convertObjectToJson(referenceDocLog)); - changeLogRepository.add(referenceDocLog); + try { + changeLogRepository.add(referenceDocLog); + } catch (SW360Exception e) { + log.error("Error occurred while adding change log", e); + } }); } catch (Exception exp) { - log.error("Error occured while creating Change Logs", exp); + log.error("Error occurred while creating Change Logs", exp); } }; } diff --git a/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java b/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java index 33ab04e61f..85805d74d5 100644 --- a/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java +++ b/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java @@ -1137,7 +1137,12 @@ public List getProjectVulnerabilityRatingByProjectId public RequestStatus updateProjectVulnerabilityRating(ProjectVulnerabilityRating link) { if( ! link.isSetId()){ link.setId(SW360Constants.PROJECT_VULNERABILITY_RATING_ID_PREFIX + link.getProjectId()); - pvrRepository.add(link); + try { + pvrRepository.add(link); + } catch (SW360Exception e) { + log.error("Unable to update project vulnerability rating.", e); + return RequestStatus.FAILURE; + } } else { pvrRepository.update(link); } diff --git a/backend/components/src/test/java/org/eclipse/sw360/components/db/BulkDeleteUtilTest.java b/backend/components/src/test/java/org/eclipse/sw360/components/db/BulkDeleteUtilTest.java index 9c52b53aa9..c35a71357c 100644 --- a/backend/components/src/test/java/org/eclipse/sw360/components/db/BulkDeleteUtilTest.java +++ b/backend/components/src/test/java/org/eclipse/sw360/components/db/BulkDeleteUtilTest.java @@ -1138,7 +1138,7 @@ public void checkDeletedReleaseListInLoop(int loopCount, List deletedRe assertTrue(CommonUtils.isNullOrEmptyMap(relationShip_c1)); } - private void createTestRecords001() { + private void createTestRecords001() throws SW360Exception { List components = new ArrayList(); Component component_dr_A = new Component().setId(COMPONENT_ID_A).setName("DR_A").setDescription("DR Component A").setCreatedBy(USER_EMAIL1).setMainLicenseIds(new HashSet<>(Arrays.asList("lic1"))).setCreatedOn("2022-07-20"); @@ -1208,7 +1208,7 @@ private void createTestRecords001() { } - private void createTestRecords002() { + private void createTestRecords002() throws SW360Exception { List components = new ArrayList(); Component component_dr_A = new Component().setId(COMPONENT_ID_A).setName("DR_A").setDescription("DR Component A").setCreatedBy(USER_EMAIL1).setMainLicenseIds(new HashSet<>(Arrays.asList("lic1"))).setCreatedOn("2022-07-20"); @@ -1300,7 +1300,7 @@ private void createTestRecords002() { } } - private void createTestRecords002(int maxLink, int depth, List releaseIdList, List componentIdList) { + private void createTestRecords002(int maxLink, int depth, List releaseIdList, List componentIdList) throws SW360Exception { //create root node String componentId = String.format("dr_%08x", treeNodeCreateReleaseCounter); treeNodeCreateReleaseCounter++; @@ -1323,7 +1323,7 @@ private void createTestRecords002(int maxLink, int depth, List releaseId createReleaseTree(releaseId, 0, releaseIdList, componentIdList); } - private void createReleaseTree(String parentId, int level, List outReleaseIdList, List< String> outComponentIdList) { + private void createReleaseTree(String parentId, int level, List outReleaseIdList, List< String> outComponentIdList) throws SW360Exception { //create a compoent String componentId = String.format("dr_%08x", treeNodeCreateReleaseCounter); treeNodeCreateReleaseCounter++; diff --git a/backend/fossology/src/main/java/org/eclipse/sw360/fossology/config/FossologyRestConfig.java b/backend/fossology/src/main/java/org/eclipse/sw360/fossology/config/FossologyRestConfig.java index 8bb6e9b3ca..c1789394b4 100644 --- a/backend/fossology/src/main/java/org/eclipse/sw360/fossology/config/FossologyRestConfig.java +++ b/backend/fossology/src/main/java/org/eclipse/sw360/fossology/config/FossologyRestConfig.java @@ -15,6 +15,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -42,7 +43,7 @@ public class FossologyRestConfig { private boolean outdated; @Autowired - public FossologyRestConfig(ConfigContainerRepository repository) { + public FossologyRestConfig(ConfigContainerRepository repository) throws SW360Exception { this.repository = repository; // eager loading (or initial insert) get(); @@ -67,7 +68,12 @@ public String getFolderId() { } private String getFirstValue(String key) { - return get().getConfigKeyToValues().getOrDefault(key, new HashSet<>()).stream().findFirst().orElse(null); + try { + return get().getConfigKeyToValues().getOrDefault(key, new HashSet<>()).stream().findFirst().orElse(null); + } catch (SW360Exception e) { + log.error(e); + return null; + } } public ConfigContainer update(ConfigContainer newConfig) { @@ -97,7 +103,13 @@ public ConfigContainer update(ConfigContainer newConfig) { throw new IllegalStateException("The new FOSSology REST configuration does not contain a valid folder id."); } - ConfigContainer current = get(); + ConfigContainer current; + try { + current = get(); + } catch (SW360Exception e) { + log.error(e); + throw new IllegalStateException("Unable to get container config."); + } current.setConfigKeyToValues(newConfig.getConfigKeyToValues()); repository.update(current); @@ -108,7 +120,7 @@ public ConfigContainer update(ConfigContainer newConfig) { return current; } - public ConfigContainer get() { + public ConfigContainer get() throws SW360Exception { if (config == null || outdated) { try { config = repository.getByConfigFor(ConfigFor.FOSSOLOGY_REST); diff --git a/backend/licenses-core/src/main/java/org/eclipse/sw360/licenses/db/LicenseDatabaseHandler.java b/backend/licenses-core/src/main/java/org/eclipse/sw360/licenses/db/LicenseDatabaseHandler.java index 925abe14f8..6b173cbac5 100644 --- a/backend/licenses-core/src/main/java/org/eclipse/sw360/licenses/db/LicenseDatabaseHandler.java +++ b/backend/licenses-core/src/main/java/org/eclipse/sw360/licenses/db/LicenseDatabaseHandler.java @@ -981,7 +981,12 @@ public RequestStatus addOrUpdateCustomProperties(CustomProperties customProperti if(customProperties.isSetId()){ customPropertiesRepository.update(customProperties); } else { - customPropertiesRepository.add(customProperties); + try { + customPropertiesRepository.add(customProperties); + } catch (SW360Exception e) { + log.error("Unable to add or update custom license property.", e); + return RequestStatus.FAILURE; + } } return RequestStatus.SUCCESS; } diff --git a/backend/licenses/src/test/java/org/eclipse/sw360/licenses/LicenseHandlerTest.java b/backend/licenses/src/test/java/org/eclipse/sw360/licenses/LicenseHandlerTest.java index a2fdfd2423..cc4080ef53 100644 --- a/backend/licenses/src/test/java/org/eclipse/sw360/licenses/LicenseHandlerTest.java +++ b/backend/licenses/src/test/java/org/eclipse/sw360/licenses/LicenseHandlerTest.java @@ -21,7 +21,6 @@ import org.junit.Before; import org.junit.Test; -import java.net.MalformedURLException; import java.util.*; import static org.junit.Assert.*; @@ -167,7 +166,7 @@ public void testUpdateLicenseIdNotMatch() throws Exception { handler.updateLicense(invalidLicense, user, user); } - public void createTestEntries() throws MalformedURLException { + public void createTestEntries() throws SW360Exception { // List of test objects licenses = new HashMap<>(); obligs = new HashMap<>(); diff --git a/backend/moderation/src/main/java/org/eclipse/sw360/moderation/db/ModerationDatabaseHandler.java b/backend/moderation/src/main/java/org/eclipse/sw360/moderation/db/ModerationDatabaseHandler.java index b3954722c4..d56318caa8 100644 --- a/backend/moderation/src/main/java/org/eclipse/sw360/moderation/db/ModerationDatabaseHandler.java +++ b/backend/moderation/src/main/java/org/eclipse/sw360/moderation/db/ModerationDatabaseHandler.java @@ -181,7 +181,7 @@ public List getRequestByDocumentId(String documentId) { return requests; } - public String createClearingRequest(ClearingRequest request, User user) { + public String createClearingRequest(ClearingRequest request, User user) throws SW360Exception { request.setTimestamp(System.currentTimeMillis()); long id = 1; synchronized (ModerationDatabaseHandler.class) { @@ -441,7 +441,12 @@ public RequestStatus createRequest(Component component, User user, Boolean isDel if(component.isSetComponentType()) { request.setComponentType(component.getComponentType()); } - addOrUpdate(request, user); + try { + addOrUpdate(request, user); + } catch (SW360Exception e) { + log.error("Unable to update request.", e); + return RequestStatus.FAILURE; + } return RequestStatus.SENT_TO_MODERATOR; } @@ -476,7 +481,12 @@ public RequestStatus createRequest(Release release, User user, Boolean isDeleteR } catch (SW360Exception e) { log.error("Could not retrieve parent component type of release with ID=" + release.getId()); } - addOrUpdate(request, user); + try { + addOrUpdate(request, user); + } catch (SW360Exception e) { + log.error("Unable to update request.", e); + return RequestStatus.FAILURE; + } return RequestStatus.SENT_TO_MODERATOR; } @@ -551,7 +561,12 @@ public RequestStatus createRequest(Project project, User user, Boolean isDeleteR // Fill the request ModerationRequestGenerator generator = new ProjectModerationRequestGenerator(); request = generator.setAdditionsAndDeletions(request, project, dbproject); - addOrUpdate(request, user); + try { + addOrUpdate(request, user); + } catch (SW360Exception e) { + log.error("Unable to create request.", e); + return RequestStatus.FAILURE; + } return RequestStatus.SENT_TO_MODERATOR; } @@ -617,7 +632,12 @@ public RequestStatus createRequest(License license, User user) { // Fill the request ModerationRequestGenerator generator = new LicenseModerationRequestGenerator(); request = generator.setAdditionsAndDeletions(request, license, dblicense); - addOrUpdate(request, user); + try { + addOrUpdate(request, user); + } catch (SW360Exception e) { + log.error("Unable to create request.", e); + return RequestStatus.FAILURE; + } return RequestStatus.SENT_TO_MODERATOR; } @@ -633,7 +653,11 @@ public void createRequest(User user) { // Set the object request.setUser(user); - addOrUpdate(request, user); + try { + addOrUpdate(request, user); + } catch (SW360Exception e) { + log.error("Unable to create request.", e); + } } private Set getSPDXDocumentModerators(String department, SPDXDocument dbSpdx) { @@ -685,7 +709,12 @@ public RequestStatus createRequest(SPDXDocument spdx, User user, Boolean isDelet // Fill the request ModerationRequestGenerator generator = new SpdxDocumentModerationRequestGenerator(); request = generator.setAdditionsAndDeletions(request, spdx, dbSpdx); - addOrUpdate(request, user); + try { + addOrUpdate(request, user); + } catch (SW360Exception e) { + log.error("Unable to create request.", e); + return RequestStatus.FAILURE; + } return RequestStatus.SENT_TO_MODERATOR; } @@ -706,7 +735,12 @@ public RequestStatus createRequest(DocumentCreationInformation documentCreationI // Fill the request ModerationRequestGenerator generator = new SpdxDocumentCreationInfoModerationRequestGenerator(); request = generator.setAdditionsAndDeletions(request, documentCreationInfo, dbDocumentCreationInfo); - addOrUpdate(request, user); + try { + addOrUpdate(request, user); + } catch (SW360Exception e) { + log.error("Unable to create request.", e); + return RequestStatus.FAILURE; + } return RequestStatus.SENT_TO_MODERATOR; } @@ -727,7 +761,12 @@ public RequestStatus createRequest(PackageInformation packageInfo, User user, Bo // Fill the request ModerationRequestGenerator generator = new SpdxPackageInfoModerationRequestGenerator(); request = generator.setAdditionsAndDeletions(request, packageInfo, dbPackageInfo); - addOrUpdate(request, user); + try { + addOrUpdate(request, user); + } catch (SW360Exception e) { + log.error("Unable to create request.", e); + return RequestStatus.FAILURE; + } return RequestStatus.SENT_TO_MODERATOR; } @@ -846,10 +885,10 @@ private List getAllSW360Users() { return sw360users; } - public void addOrUpdate(ModerationRequest request, User user) { + public void addOrUpdate(ModerationRequest request, User user) throws SW360Exception { addOrUpdate(request, user.getEmail()); } - public void addOrUpdate(ModerationRequest request, String userEmail) { + public void addOrUpdate(ModerationRequest request, String userEmail) throws SW360Exception { if (request.isSetId()) { repository.update(request); sendMailNotificationsForUpdatedRequest(request, userEmail); diff --git a/backend/moderation/src/test/java/org/eclipse/sw360/moderation/testutil/DatabaseTestSetup.java b/backend/moderation/src/test/java/org/eclipse/sw360/moderation/testutil/DatabaseTestSetup.java index 0511aabec1..73826c5fe9 100644 --- a/backend/moderation/src/test/java/org/eclipse/sw360/moderation/testutil/DatabaseTestSetup.java +++ b/backend/moderation/src/test/java/org/eclipse/sw360/moderation/testutil/DatabaseTestSetup.java @@ -12,6 +12,7 @@ import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant; import org.eclipse.sw360.datahandler.common.DatabaseSettingsTest; +import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.datahandler.thrift.moderation.DocumentType; import org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest; import org.eclipse.sw360.datahandler.thrift.projects.Project; @@ -24,7 +25,7 @@ public class DatabaseTestSetup { - public static void main(String[] args) throws MalformedURLException { + public static void main(String[] args) throws MalformedURLException, SW360Exception { DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(DatabaseSettingsTest.getConfiguredClient(), DatabaseSettingsTest.COUCH_DB_DATABASE); diff --git a/backend/utils/src/test/java/org/eclipse/sw360/attachments/db/RemoteAttachmentDownloaderTest.java b/backend/utils/src/test/java/org/eclipse/sw360/attachments/db/RemoteAttachmentDownloaderTest.java index 71845d4c17..a1c143b0ab 100644 --- a/backend/utils/src/test/java/org/eclipse/sw360/attachments/db/RemoteAttachmentDownloaderTest.java +++ b/backend/utils/src/test/java/org/eclipse/sw360/attachments/db/RemoteAttachmentDownloaderTest.java @@ -167,7 +167,7 @@ public void testWithBrokenURL() throws Exception { } } - private AttachmentContent saveRemoteAttachment(String remoteUrl) { + private AttachmentContent saveRemoteAttachment(String remoteUrl) throws SW360Exception { AttachmentContent attachmentContent = new AttachmentContent() .setFilename("testfile") .setContentType("text") diff --git a/backend/vendors/src/main/java/org/eclipse/sw360/vendors/VendorDatabaseHandler.java b/backend/vendors/src/main/java/org/eclipse/sw360/vendors/VendorDatabaseHandler.java index aa252860a9..871760cfed 100644 --- a/backend/vendors/src/main/java/org/eclipse/sw360/vendors/VendorDatabaseHandler.java +++ b/backend/vendors/src/main/java/org/eclipse/sw360/vendors/VendorDatabaseHandler.java @@ -80,7 +80,12 @@ public AddDocumentRequestSummary addVendor(Vendor vendor) { log.error("Error creating the vendor: " + e.why); return new AddDocumentRequestSummary().setRequestStatus(AddDocumentRequestStatus.FAILURE).setMessage(e.why); } - repository.add(vendor); + try { + repository.add(vendor); + } catch (SW360Exception e) { + log.error("Error adding vendor", e); + return new AddDocumentRequestSummary().setRequestStatus(AddDocumentRequestStatus.FAILURE).setMessage(e.why); + } return new AddDocumentRequestSummary().setRequestStatus(AddDocumentRequestStatus.SUCCESS).setId(vendor.getId()); } diff --git a/backend/vendors/src/test/java/org/eclipse/sw360/vendors/TestVendorClient.java b/backend/vendors/src/test/java/org/eclipse/sw360/vendors/TestVendorClient.java index b261a4e7fa..2c67aad7e6 100644 --- a/backend/vendors/src/test/java/org/eclipse/sw360/vendors/TestVendorClient.java +++ b/backend/vendors/src/test/java/org/eclipse/sw360/vendors/TestVendorClient.java @@ -11,6 +11,7 @@ import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant; import org.eclipse.sw360.datahandler.common.DatabaseSettingsTest; +import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.datahandler.thrift.vendors.Vendor; import org.eclipse.sw360.datahandler.thrift.vendors.VendorService; import org.apache.thrift.TException; @@ -18,8 +19,6 @@ import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.THttpClient; -import java.io.IOException; -import java.net.MalformedURLException; import java.util.List; /** @@ -30,7 +29,7 @@ public class TestVendorClient { @SuppressWarnings("unused") - public static void InitDatabase() throws MalformedURLException { + public static void InitDatabase() throws SW360Exception { DatabaseConnectorCloudant databaseConnector = new DatabaseConnectorCloudant(DatabaseSettingsTest.getConfiguredClient(), DatabaseSettingsTest.COUCH_DB_DATABASE); databaseConnector.add(new Vendor().setShortname("Microsoft").setFullname("Microsoft Corporation").setUrl("http://www.microsoft.com")); @@ -38,7 +37,7 @@ public static void InitDatabase() throws MalformedURLException { databaseConnector.add(new Vendor().setShortname("Oracle").setFullname("Oracle Corporation Inc").setUrl("http://www.oracle.com")); } - public static void main(String[] args) throws TException, IOException { + public static void main(String[] args) throws TException { THttpClient thriftClient = new THttpClient("http://127.0.0.1:8080/vendorservice/thrift"); TProtocol protocol = new TCompactProtocol(thriftClient); VendorService.Iface client = new VendorService.Client(protocol); diff --git a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/cloudantclient/DatabaseConnectorCloudant.java b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/cloudantclient/DatabaseConnectorCloudant.java index 925bf2791c..78a124bc81 100644 --- a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/cloudantclient/DatabaseConnectorCloudant.java +++ b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/cloudantclient/DatabaseConnectorCloudant.java @@ -27,9 +27,11 @@ import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent; import org.jetbrains.annotations.NotNull; +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -500,18 +502,37 @@ private boolean deleteDocumentWithOption(DeleteDocumentOptions deleteOption) { return success; } - public boolean add(T doc) { + public boolean add(T doc) throws SW360Exception { Document document = this.getDocumentFromPojo(doc); if (document.getId() != null && this.contains(document.getId())) { // Cannot add same document again. Must update. return false; } + + DocumentResult resp; + if (doc instanceof AttachmentContent content) { + if (content.getFilename() == null) { + throw new SW360Exception("Attachment filename cannot be null."); + } + PostDocumentOptions postDocOption = new PostDocumentOptions.Builder() + .db(this.dbName) + .document(document) + .build(); + + DocumentResult createResp = this.instance.getClient().postDocument(postDocOption).execute().getResult(); + InputStream in = new ByteArrayInputStream(content.getFilename() + .getBytes(StandardCharsets.UTF_8)); + createAttachment(createResp.getId(), content.getFilename(), in, content.getContentType()); + Document updatedDoc = this.getDocument(createResp.getId()); + updateIdAndRev(doc, updatedDoc.getId(), updatedDoc.getRev()); + return true; + } PostDocumentOptions postDocOption = new PostDocumentOptions.Builder() .db(this.dbName) .document(document) .build(); - DocumentResult resp = this.instance.getClient().postDocument(postDocOption).execute().getResult(); + resp = this.instance.getClient().postDocument(postDocOption).execute().getResult(); updateIdAndRev(doc, resp.getId(), resp.getRev()); return resp.isOk(); } diff --git a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/cloudantclient/DatabaseRepositoryCloudantClient.java b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/cloudantclient/DatabaseRepositoryCloudantClient.java index a18fc9880f..990ee9fa69 100644 --- a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/cloudantclient/DatabaseRepositoryCloudantClient.java +++ b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/cloudantclient/DatabaseRepositoryCloudantClient.java @@ -23,6 +23,7 @@ import org.apache.logging.log4j.Logger; import org.apache.thrift.TBase; import org.apache.thrift.TFieldIdEnum; +import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.datahandler.thrift.Source; import org.eclipse.sw360.datahandler.thrift.attachments.Attachment; @@ -347,7 +348,7 @@ public PostViewOptions buildRequest(PostViewOptions.Builder viewQuery, Collectio .build(); } - public boolean add(T doc) { + public boolean add(T doc) throws SW360Exception { return connector.add(doc); } diff --git a/libraries/importers/src/test/java/org/eclipse/sw360/importer/ComponentImportUtilsTest.java b/libraries/importers/src/test/java/org/eclipse/sw360/importer/ComponentImportUtilsTest.java index e397f7dfb5..7bc550a3b4 100644 --- a/libraries/importers/src/test/java/org/eclipse/sw360/importer/ComponentImportUtilsTest.java +++ b/libraries/importers/src/test/java/org/eclipse/sw360/importer/ComponentImportUtilsTest.java @@ -75,9 +75,11 @@ public void testImportOnEmptyDb() throws Exception { final String attachmentContentId = getCreatedAttachmentContentId(); final AttachmentContent overwriter = new AttachmentContent().setId(OVERRIDING_ID) - .setOnlyRemote(true).setRemoteUrl(REMOTE_URL).setType(TYPE_ATTACHMENT); + .setOnlyRemote(true).setRemoteUrl(REMOTE_URL).setType(TYPE_ATTACHMENT) + .setFilename(fileName).setContentType("text/plain"); final AttachmentContent addition = new AttachmentContent().setId(ADDITIONAL_ID) - .setOnlyRemote(true).setRemoteUrl(REMOTE_URL).setType(TYPE_ATTACHMENT); + .setOnlyRemote(true).setRemoteUrl(REMOTE_URL).setType(TYPE_ATTACHMENT) + .setFilename(fileName).setContentType("text/plain"); attachmentContentRepository.add(overwriter); attachmentContentRepository.add(addition); diff --git a/rest/authorization-server/src/main/java/org/eclipse/sw360/rest/authserver/client/rest/OAuthClientController.java b/rest/authorization-server/src/main/java/org/eclipse/sw360/rest/authserver/client/rest/OAuthClientController.java index 1646c80ac5..9521f35366 100644 --- a/rest/authorization-server/src/main/java/org/eclipse/sw360/rest/authserver/client/rest/OAuthClientController.java +++ b/rest/authorization-server/src/main/java/org/eclipse/sw360/rest/authserver/client/rest/OAuthClientController.java @@ -17,6 +17,7 @@ import java.util.stream.Stream; import org.apache.commons.lang.StringUtils; +import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.rest.authserver.client.persistence.OAuthClientEntity; import org.eclipse.sw360.rest.authserver.client.persistence.OAuthClientRepository; import org.eclipse.sw360.rest.authserver.security.Sw360GrantedAuthority; @@ -88,7 +89,12 @@ public ResponseEntity createOrUpdateClient(@RequestBody OAuthClientResource c clientEntity = new OAuthClientEntity(); // store entity to get a new id - repo.add(clientEntity); + try { + repo.add(clientEntity); + } catch (SW360Exception e) { + return new ResponseEntity("Unable to add client " + clientResource.getClientId(), + HttpStatus.INTERNAL_SERVER_ERROR); + } clientEntity.setClientId(clientEntity.getId()); clientEntity.setClientSecret(passwordEncoder.encode(UUID.randomUUID().toString())); diff --git a/rest/authorization-server/src/test/java/org/eclipse/sw360/rest/authserver/client/persistence/OAuthClientRepositoryTest.java b/rest/authorization-server/src/test/java/org/eclipse/sw360/rest/authserver/client/persistence/OAuthClientRepositoryTest.java index 303d9a7cd6..479e77b69f 100644 --- a/rest/authorization-server/src/test/java/org/eclipse/sw360/rest/authserver/client/persistence/OAuthClientRepositoryTest.java +++ b/rest/authorization-server/src/test/java/org/eclipse/sw360/rest/authserver/client/persistence/OAuthClientRepositoryTest.java @@ -9,6 +9,7 @@ */ package org.eclipse.sw360.rest.authserver.client.persistence; +import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.rest.authserver.IntegrationTestBase; import org.junit.Before; @@ -50,7 +51,7 @@ public void testNoop() { } // @Test - public void testInsertNewClient() { + public void testInsertNewClient() throws SW360Exception { // given: String clientId = "foo"; OAuthClientEntity client = new OAuthClientEntity(); diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java index 8387d3bd81..35d4413084 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java @@ -14,7 +14,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; @@ -47,6 +46,7 @@ import org.eclipse.sw360.datahandler.common.SW360Utils; import org.eclipse.sw360.datahandler.common.ThriftEnumUtils; import org.eclipse.sw360.datahandler.couchdb.lucene.NouveauLuceneAwareDatabaseConnector; +import org.eclipse.sw360.datahandler.permissions.PermissionUtils; import org.eclipse.sw360.datahandler.resourcelists.PaginationParameterException; import org.eclipse.sw360.datahandler.resourcelists.PaginationResult; import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException; @@ -64,12 +64,10 @@ import org.eclipse.sw360.datahandler.thrift.Source; import org.eclipse.sw360.datahandler.thrift.attachments.Attachment; import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent; -import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentService; import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentType; import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentUsage; import org.eclipse.sw360.datahandler.thrift.attachments.UsageData; import org.eclipse.sw360.datahandler.thrift.components.ClearingState; -import org.eclipse.sw360.datahandler.thrift.components.Component; import org.eclipse.sw360.datahandler.thrift.components.Release; import org.eclipse.sw360.datahandler.thrift.components.ReleaseClearingStateSummary; import org.eclipse.sw360.datahandler.thrift.components.ReleaseLink; @@ -88,7 +86,6 @@ import org.eclipse.sw360.datahandler.thrift.projects.ProjectLink; import org.eclipse.sw360.datahandler.thrift.projects.ProjectProjectRelationship; import org.eclipse.sw360.datahandler.thrift.projects.ProjectRelationship; -import org.eclipse.sw360.datahandler.thrift.projects.ProjectService; import org.eclipse.sw360.datahandler.thrift.projects.ProjectDTO; import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest; import org.eclipse.sw360.datahandler.thrift.users.User; @@ -110,8 +107,6 @@ import org.eclipse.sw360.rest.resourceserver.release.ReleaseController; import org.eclipse.sw360.rest.resourceserver.release.Sw360ReleaseService; import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService; -import org.eclipse.sw360.rest.resourceserver.user.UserController; -import org.eclipse.sw360.rest.resourceserver.vendor.VendorController; import org.eclipse.sw360.rest.resourceserver.vulnerability.Sw360VulnerabilityService; import org.eclipse.sw360.rest.resourceserver.vulnerability.VulnerabilityController; import org.jetbrains.annotations.NotNull; @@ -131,7 +126,6 @@ import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.util.FileCopyUtils; -import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -164,7 +158,6 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.TreeMap; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate;