Skip to content

Commit

Permalink
Merge pull request wso2#12456 from piyumaldk/master
Browse files Browse the repository at this point in the history
[master] Related to fixing wso2/api-manager#2908
  • Loading branch information
piyumaldk authored Jun 3, 2024
2 parents 47b05ad + aa10ed1 commit 6aac513
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,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;
Expand Down Expand Up @@ -5607,13 +5608,25 @@ 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 APIManagementException("Error while reading input stream ", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2873,8 +2873,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);
}
Expand All @@ -2891,13 +2892,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -385,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
Expand Down

0 comments on commit 6aac513

Please sign in to comment.