From 69be2c31d532e1c4553496264603a26ddcf5d8b6 Mon Sep 17 00:00:00 2001 From: piyumaldk Date: Wed, 29 May 2024 15:40:58 +0530 Subject: [PATCH 1/4] Related to fixing https://github.com/wso2/api-manager/issues/2908 --- .../wso2/carbon/apimgt/impl/APIProviderImpl.java | 15 ++++++++++++++- .../persistence/RegistryPersistenceImpl.java | 10 +++++++--- .../RegistryPersistenceImplTestCase.java | 7 +++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 3025e4581cfe..c0771dce4210 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -5606,13 +5606,26 @@ public void setThumbnailToAPI(String apiId, ResourceFile resource, String organi try { org.wso2.carbon.apimgt.persistence.dto.ResourceFile iconResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile( resource.getContent(), resource.getContentType()); - apiPersistenceInstance.saveThumbnail(new Organization(organization), apiId, iconResourceFile); + InputStream content = iconResourceFile.getContent(); + content.mark(1); + if (content.read() == -1) { + // Thumbnail deletion from publisher originally handled through the same PUT call + // It was decided after discussion to fix the deletion (U2 update) through the same originally used PUT + apiPersistenceInstance.deleteThumbnail(new Organization(organization), apiId); + } + else { + // Content will be reset to re-read the stream from the beginning + content.reset(); + apiPersistenceInstance.saveThumbnail(new Organization(organization), apiId, iconResourceFile); + } } catch (ThumbnailPersistenceException e) { if (e.getErrorHandler() == ExceptionCodes.API_NOT_FOUND) { throw new APIMgtResourceNotFoundException(e); } else { throw new APIManagementException("Error while saving thumbnail ", e); } + } catch (IOException e) { + throw new RuntimeException(e); } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java index 9fc4d90ddfca..2ef8ae0f6a34 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java @@ -2874,8 +2874,9 @@ public void deleteThumbnail(Organization org, String apiId) throws ThumbnailPers RegistryHolder holder = getRegistry(tenantDomain); registry = holder.getRegistry(); isTenantFlowStarted = holder.isTenantFlowStarted(); - - GenericArtifact apiArtifact = getAPIArtifact(apiId, registry); + GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, + APIConstants.API_KEY); + GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId); if (apiArtifact == null) { throw new ThumbnailPersistenceException("API not found for id " + apiId, ExceptionCodes.API_NOT_FOUND); } @@ -2892,13 +2893,16 @@ public void deleteThumbnail(Organization org, String apiId) throws ThumbnailPers String oldThumbPath = artifactOldPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE; String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE; - + // Remove thumbnail from registry if (registry.resourceExists(thumbPath)) { registry.delete(thumbPath); } if (registry.resourceExists(oldThumbPath)) { registry.delete(oldThumbPath); } + // Remove thumbnail path from overview thumbnail of artifact's attributes + apiArtifact.setAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL, null); + apiArtifactManager.updateGenericArtifact(apiArtifact); } catch (RegistryException | APIPersistenceException e) { String msg = "Error while loading API icon of API " + apiId + " from the registry"; throw new ThumbnailPersistenceException(msg, e); diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/test/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImplTestCase.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/test/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImplTestCase.java index dafeff09407e..01c946a0011b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/test/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImplTestCase.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/test/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImplTestCase.java @@ -363,6 +363,13 @@ public void testThumbnailTasks() throws Exception { GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact(); String apiUUID = artifact.getId(); + PowerMockito.mockStatic(RegistryPersistenceUtil.class); + GenericArtifactManager manager = Mockito.mock(GenericArtifactManager.class); + PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)) + .thenReturn(manager); + Mockito.when(manager.getGenericArtifact(apiUUID)).thenReturn(artifact); + Mockito.doNothing().when(manager).updateGenericArtifact(artifact); + Organization org = new Organization(SUPER_TENANT_DOMAIN); APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact); From 2a84bbeec6412b1bde8ea5c3c68de764673169df Mon Sep 17 00:00:00 2001 From: piyumaldk Date: Wed, 29 May 2024 16:14:02 +0530 Subject: [PATCH 2/4] Addressed the requested changes --- .../main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index c0771dce4210..4ea5fb1c4b0f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -5625,7 +5625,7 @@ public void setThumbnailToAPI(String apiId, ResourceFile resource, String organi throw new APIManagementException("Error while saving thumbnail ", e); } } catch (IOException e) { - throw new RuntimeException(e); + throw new APIManagementException("Error while reading input stream ", e); } } From 091289cacf84b50f9e04e94ef967e6eaf66c75c9 Mon Sep 17 00:00:00 2001 From: piyumaldk Date: Wed, 29 May 2024 16:25:58 +0530 Subject: [PATCH 3/4] Fix formatting --- .../main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 4ea5fb1c4b0f..42fc5107e1d9 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -5612,8 +5612,7 @@ public void setThumbnailToAPI(String apiId, ResourceFile resource, String organi // Thumbnail deletion from publisher originally handled through the same PUT call // It was decided after discussion to fix the deletion (U2 update) through the same originally used PUT apiPersistenceInstance.deleteThumbnail(new Organization(organization), apiId); - } - else { + } else { // Content will be reset to re-read the stream from the beginning content.reset(); apiPersistenceInstance.saveThumbnail(new Organization(organization), apiId, iconResourceFile); From aa10ed1cd1d794583a5fe5fe7ed9efe82f9d132c Mon Sep 17 00:00:00 2001 From: piyumaldk Date: Thu, 30 May 2024 15:50:46 +0530 Subject: [PATCH 4/4] Fix issue and add test --- .../main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 1 + .../apimgt/persistence/RegistryPersistenceImplTestCase.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 42fc5107e1d9..7b1f23fb20fb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -205,6 +205,7 @@ import javax.xml.stream.XMLStreamException; import java.io.ByteArrayInputStream; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URI; diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/test/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImplTestCase.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/test/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImplTestCase.java index 01c946a0011b..a496e38d05ca 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/test/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImplTestCase.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/test/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImplTestCase.java @@ -392,7 +392,7 @@ public void testThumbnailTasks() throws Exception { apiPersistenceInstance.deleteThumbnail(org, apiUUID); Mockito.verify(registry, times(1)).delete(thumbPath); - + Mockito.verify(manager, times(1)).updateGenericArtifact(artifact); } @Test