Skip to content

Commit

Permalink
Fix to properly delete attachment content
Browse files Browse the repository at this point in the history
Change Persistor.deleteAttachmentContent to take the gridFsId rather
than the attachment id. This matches the signature for
retrieveAttachmentMetadata and the difference is probably why the bug
wasn't originally spotted.

PersistenceBean.deleteAttachmentContent now actually does what it says
it does.

AssetServiceLayer change to match the new semantics of
deleteAttachmentContent.

RepositoryContext change to check for orphaned attachment content when
cleaning repository between tests.
  • Loading branch information
Azquelt committed Jan 30, 2017
1 parent e5431a6 commit b0d3548
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import com.ibm.ws.lars.rest.model.Attachment;
import com.ibm.ws.lars.rest.model.AttachmentList;
import com.ibm.ws.lars.testutils.FatUtils;
import com.mongodb.gridfs.GridFS;

/**
* Context used by testcases to perform HTTP operations against one LARS server. Tests should use
Expand Down Expand Up @@ -385,6 +386,7 @@ private void cleanRepo() throws InvalidJsonAssetException, IOException {
AssetList assets = doGetAllAssets();
assertEquals("Assets remaining in the repository", 0, assets.size());
assertEquals("Attachments remaining in the repository", 0, FatUtils.getMongoDB().getCollection("attachments").count());
assertEquals("Files remaining in repository", 0, new GridFS(FatUtils.getMongoDB()).getFileList().size());
}

void deleteAllAssets() throws IOException, InvalidJsonAssetException {
Expand Down
11 changes: 9 additions & 2 deletions server/src/main/java/com/ibm/ws/lars/rest/AssetServiceLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,15 @@ public Attachment createAttachmentNoContent(String assetId, String name, Attachm
}

public void deleteAttachment(String attachmentId) {
persistenceBean.deleteAttachmentMetadata(attachmentId);
persistenceBean.deleteAttachmentContent(attachmentId);
try {
Attachment attachment = persistenceBean.retrieveAttachmentMetadata(attachmentId);
if (attachment.getGridFSId() != null) {
persistenceBean.deleteAttachmentContent(attachment.getGridFSId());
}
persistenceBean.deleteAttachmentMetadata(attachmentId);
} catch (NonExistentArtefactException ex) {
// Do nothing if attachment does not exist
}
}

public Attachment retrieveAttachmentMetadata(String assetId, String attachmentId, UriInfo uriInfo) throws NonExistentArtefactException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ public Attachment retrieveAttachmentMetadata(String attachmentId) throws NonExis
}

@Override
public void deleteAttachmentContent(String attachmentId) {
gridFS.remove(attachmentId);
public void deleteAttachmentContent(String gridFsId) {
gridFS.remove(gridFsId);
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/java/com/ibm/ws/lars/rest/Persistor.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ public interface Persistor {
public Attachment retrieveAttachmentMetadata(String attachmentId) throws NonExistentArtefactException;

/**
* Deletes the content of the specified attachment. Caller should also delete the attachment
* metadata.
* Deletes the attachment content associated with the given gridFsId. Caller should also delete
* the attachment metadata.
*/
public void deleteAttachmentContent(String attachmentId);
public void deleteAttachmentContent(String gridFsId);

/**
* Deletes the metadata for the specified attachment. Callers should have already deleted
Expand Down

0 comments on commit b0d3548

Please sign in to comment.