Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 store get resource from property path should use the id object, not the entity #892

Merged
merged 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public Resource getResource(S entity, PropertyPath propertyPath) {
return null;

BlobId blobId = null;
if (placementService.canConvert(entity.getClass(), BlobId.class)) {
blobId = placementService.convert(entity, BlobId.class);
if (placementService.canConvert(property.getContentIdType(entity).getClass(), BlobId.class)) {
blobId = placementService.convert(property.getContentId(entity), BlobId.class);

if (blobId != null) {
return this.getResourceInternal(blobId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public class AzureStorageIT {
entity = repo.save(entity);

store.setContent(entity, new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Spring Content World!</html>".getBytes()));
});

It("should be able to store new content", () -> {
Expand All @@ -299,7 +299,7 @@ public class AzureStorageIT {

//rendition
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
assertThat(IOUtils.contentEquals(new ByteArrayInputStream("Hello Spring Content World!".getBytes()), content), is(true));
assertThat(IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Spring Content World!</html>".getBytes()), content), is(true));
} catch (IOException ioe) {}
});

Expand All @@ -312,13 +312,13 @@ public class AzureStorageIT {
//rendition
assertThat(entity.getRenditionId(), is(notNullValue()));
assertThat(entity.getRenditionId().trim().length(), greaterThan(0));
Assert.assertEquals(entity.getRenditionLen(), 27L);
Assert.assertEquals(entity.getRenditionLen(), 40L);
});

Context("when content is updated", () -> {
BeforeEach(() ->{
store.setContent(entity, new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Updated Spring Content World!</html>".getBytes()));
entity = repo.save(entity);
});

Expand All @@ -333,7 +333,7 @@ public class AzureStorageIT {
//rendition
matches = false;
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
matches = IOUtils.contentEquals(new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()), content);
matches = IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Updated Spring Content World!</html>".getBytes()), content);
assertThat(matches, is(true));
}
});
Expand All @@ -342,7 +342,7 @@ public class AzureStorageIT {
Context("when content is updated with shorter content", () -> {
BeforeEach(() -> {
store.setContent(entity, new ByteArrayInputStream("Hello Spring World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Spring World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Spring World!</html>".getBytes()));
entity = repo.save(entity);
});
It("should store only the new content", () -> {
Expand All @@ -356,7 +356,7 @@ public class AzureStorageIT {
//rendition
matches = false;
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
matches = IOUtils.contentEquals(new ByteArrayInputStream("Hello Spring World!".getBytes()), content);
matches = IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Spring World!</html>".getBytes()), content);
assertThat(matches, is(true));
}
});
Expand Down
14 changes: 7 additions & 7 deletions spring-content-fs/src/test/java/it/store/FilesystemStoreIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public class FilesystemStoreIT {
entity = repo.save(entity);

store.setContent(entity, new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Spring Content World!</html>".getBytes()));
});

It("should be able to store new content", () -> {
Expand All @@ -281,7 +281,7 @@ public class FilesystemStoreIT {

//rendition
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
assertThat(IOUtils.contentEquals(new ByteArrayInputStream("Hello Spring Content World!".getBytes()), content), is(true));
assertThat(IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Spring Content World!</html>".getBytes()), content), is(true));
} catch (IOException ioe) {}
});

Expand All @@ -294,13 +294,13 @@ public class FilesystemStoreIT {
//rendition
assertThat(entity.getRenditionId(), is(notNullValue()));
assertThat(entity.getRenditionId().trim().length(), greaterThan(0));
Assert.assertEquals(entity.getRenditionLen(), 27L);
Assert.assertEquals(entity.getRenditionLen(), 40L);
});

Context("when content is updated", () -> {
BeforeEach(() ->{
store.setContent(entity, new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Updated Spring Content World!</html>".getBytes()));
entity = repo.save(entity);
});

Expand All @@ -315,7 +315,7 @@ public class FilesystemStoreIT {
//rendition
matches = false;
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
matches = IOUtils.contentEquals(new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()), content);
matches = IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Updated Spring Content World!</html>".getBytes()), content);
assertThat(matches, is(true));
}
});
Expand All @@ -324,7 +324,7 @@ public class FilesystemStoreIT {
Context("when content is updated with shorter content", () -> {
BeforeEach(() -> {
store.setContent(entity, new ByteArrayInputStream("Hello Spring World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Spring World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Spring World!</html>".getBytes()));
entity = repo.save(entity);
});
It("should store only the new content", () -> {
Expand All @@ -338,7 +338,7 @@ public class FilesystemStoreIT {
//rendition
matches = false;
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
matches = IOUtils.contentEquals(new ByteArrayInputStream("Hello Spring World!".getBytes()), content);
matches = IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Spring World!</html>".getBytes()), content);
assertThat(matches, is(true));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public Resource getResource(S entity, PropertyPath propertyPath) {
return null;

BlobId blobId = null;
if (placementService.canConvert(entity.getClass(), BlobId.class)) {
blobId = placementService.convert(entity, BlobId.class);
if (placementService.canConvert(property.getContentIdType(entity).getClass(), BlobId.class)) {
blobId = placementService.convert(property.getContentId(entity), BlobId.class);

if (blobId != null) {
return this.getResourceInternal(blobId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public class GCPStorageIT {
entity = repo.save(entity);

store.setContent(entity, new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Spring Content World!</html>".getBytes()));
});

It("should be able to store new content", () -> {
Expand All @@ -293,7 +293,7 @@ public class GCPStorageIT {

//rendition
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
assertThat(IOUtils.contentEquals(new ByteArrayInputStream("Hello Spring Content World!".getBytes()), content), is(true));
assertThat(IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Spring Content World!</html>".getBytes()), content), is(true));
} catch (IOException ioe) {}
});

Expand All @@ -306,13 +306,13 @@ public class GCPStorageIT {
//rendition
assertThat(entity.getRenditionId(), is(notNullValue()));
assertThat(entity.getRenditionId().trim().length(), greaterThan(0));
Assert.assertEquals(entity.getRenditionLen(), 27L);
Assert.assertEquals(entity.getRenditionLen(), 40L);
});

Context("when content is updated", () -> {
BeforeEach(() ->{
store.setContent(entity, new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Updated Spring Content World!</html>".getBytes()));
entity = repo.save(entity);
});

Expand All @@ -327,7 +327,7 @@ public class GCPStorageIT {
//rendition
matches = false;
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
matches = IOUtils.contentEquals(new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()), content);
matches = IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Updated Spring Content World!</html>".getBytes()), content);
assertThat(matches, is(true));
}
});
Expand All @@ -336,7 +336,7 @@ public class GCPStorageIT {
Context("when content is updated with shorter content", () -> {
BeforeEach(() -> {
store.setContent(entity, new ByteArrayInputStream("Hello Spring World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("Hello Spring World!".getBytes()));
store.setContent(entity, PropertyPath.from("rendition"), new ByteArrayInputStream("<html>Hello Spring World!</html>".getBytes()));
entity = repo.save(entity);
});
It("should store only the new content", () -> {
Expand All @@ -350,7 +350,7 @@ public class GCPStorageIT {
//rendition
matches = false;
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
matches = IOUtils.contentEquals(new ByteArrayInputStream("Hello Spring World!".getBytes()), content);
matches = IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Spring World!</html>".getBytes()), content);
assertThat(matches, is(true));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class ContentStoreIT {
claim = claimRepo.save(claim);

claimFormStore.setContent(claim, PropertyPath.from("claimForm.content"), new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
claimFormStore.setContent(claim, PropertyPath.from("claimForm.rendition"), new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
claimFormStore.setContent(claim, PropertyPath.from("claimForm.rendition"), new ByteArrayInputStream("<html>Hello Spring Content World!</html>".getBytes()));
});

It("should be able to store new content", () -> {
Expand All @@ -117,7 +117,7 @@ public class ContentStoreIT {
// rendition
doInTransaction(ptm, () -> {
try (InputStream content = claimFormStore.getContent(claim, PropertyPath.from("claimForm.rendition"))) {
assertThat(IOUtils.contentEquals(new ByteArrayInputStream("Hello Spring Content World!".getBytes()), content), is(true));
assertThat(IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Spring Content World!</html>".getBytes()), content), is(true));
} catch (IOException ioe) {}
return null;
});
Expand All @@ -133,13 +133,13 @@ public class ContentStoreIT {
// renditoin
Assert.assertThat(claim.getClaimForm().getRenditionId(), is(notNullValue()));
Assert.assertThat(claim.getClaimForm().getRenditionId().trim().length(), greaterThan(0));
Assert.assertEquals(claim.getClaimForm().getContentLength(), 27L);
Assert.assertEquals(claim.getClaimForm().getRenditionLen(), 40L);
});

Context("when content is updated", () -> {
BeforeEach(() ->{
claimFormStore.setContent(claim, PropertyPath.from("claimForm.content"), new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
claimFormStore.setContent(claim, PropertyPath.from("claimForm.rendition"), new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
claimFormStore.setContent(claim, PropertyPath.from("claimForm.rendition"), new ByteArrayInputStream("<html>Hello Updated Spring Content World!</html>".getBytes()));
claim = claimRepo.save(claim);
});

Expand All @@ -159,7 +159,7 @@ public class ContentStoreIT {
doInTransaction(ptm, () -> {
boolean matches = false;
try (InputStream content = claimFormStore.getContent(claim, PropertyPath.from("claimForm.rendition"))) {
matches = IOUtils.contentEquals(new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()), content);
matches = IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Updated Spring Content World!</html>".getBytes()), content);
assertThat(matches, is(true));
} catch (IOException e) {
}
Expand All @@ -171,7 +171,7 @@ public class ContentStoreIT {
Context("when content is updated with shorter content", () -> {
BeforeEach(() -> {
claimFormStore.setContent(claim, PropertyPath.from("claimForm.content"), new ByteArrayInputStream("Hello Spring World!".getBytes()));
claimFormStore.setContent(claim, PropertyPath.from("claimForm.rendition"), new ByteArrayInputStream("Hello Spring World!".getBytes()));
claimFormStore.setContent(claim, PropertyPath.from("claimForm.rendition"), new ByteArrayInputStream("<html>Hello Spring World!</html>".getBytes()));
claim = claimRepo.save(claim);
});
It("should store only the new content", () -> {
Expand All @@ -190,7 +190,7 @@ public class ContentStoreIT {
doInTransaction(ptm, () -> {
boolean matches = false;
try (InputStream content = claimFormStore.getContent(claim, PropertyPath.from("claimForm.rendition"))) {
matches = IOUtils.contentEquals(new ByteArrayInputStream("Hello Spring World!".getBytes()), content);
matches = IOUtils.contentEquals(new ByteArrayInputStream("<html>Hello Spring World!</html>".getBytes()), content);
assertThat(matches, is(true));
} catch (IOException e) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public Resource getResource(S entity, PropertyPath propertyPath) {
return null;

S3ObjectId s3ObjectId = null;
if (placementService.canConvert(entity.getClass(), S3ObjectId.class)) {
s3ObjectId = placementService.convert(entity, S3ObjectId.class);
if (placementService.canConvert(property.getContentIdType(entity).getClass(), S3ObjectId.class)) {
s3ObjectId = placementService.convert(property.getContentId(entity), S3ObjectId.class);

if (s3ObjectId != null) {
return this.getResourceInternal(s3ObjectId);
Expand Down Expand Up @@ -413,7 +413,7 @@ public S unsetContent(S entity, PropertyPath propertyPath) {
if (entity == null)
return entity;

deleteIfExists(entity);
deleteIfExists(entity, propertyPath);

// reset content fields
property.setContentId(entity, null, new org.springframework.content.commons.mappingcontext.Condition() {
Expand Down Expand Up @@ -461,4 +461,18 @@ private void deleteIfExists(S entity) {
}
}
}

private void deleteIfExists(S entity, PropertyPath path) {

Resource resource = this.getResource(entity, path);
if (resource != null && resource.exists() && resource instanceof DeletableResource) {

try {
((DeletableResource)resource).delete();
} catch (Exception e) {
logger.error(format("Unexpected error unsetting content for entity %s", entity));
throw new StoreAccessException(format("Unsetting content for entity %s", entity), e);
}
}
}
}
Loading