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

When content is unset all storage now attempts to reset content length to its type's default value #1554

Merged
merged 2 commits into from
Aug 11, 2023
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 @@ -449,7 +449,11 @@ public boolean matches(Field field) {
return true;
}
});
BeanUtils.setFieldWithAnnotation(entity, ContentLength.class, 0);

Class<?> contentLenType = BeanUtils.getFieldWithAnnotationType(entity, ContentLength.class);
if (contentLenType != null) {
BeanUtils.setFieldWithAnnotation(entity, ContentLength.class, BeanUtils.getDefaultValueForType(contentLenType));
}

return entity;
}
Expand Down Expand Up @@ -495,30 +499,30 @@ public S unsetContent(S entity, PropertyPath propertyPath, UnsetContentParams pa
// reset content fields
if (resource != null) {
property.setContentId(entity, null, new org.springframework.content.commons.mappingcontext.Condition() {
@Override
public boolean matches(TypeDescriptor descriptor) {
for (Annotation annotation : descriptor.getAnnotations()) {
if ("jakarta.persistence.Id".equals(
annotation.annotationType().getCanonicalName())
|| "org.springframework.data.annotation.Id"
.equals(annotation.annotationType()
.getCanonicalName())) {
return false;
@Override
public boolean matches(TypeDescriptor descriptor) {
for (Annotation annotation : descriptor.getAnnotations()) {
if ("jakarta.persistence.Id".equals(
annotation.annotationType().getCanonicalName())
|| "org.springframework.data.annotation.Id"
.equals(annotation.annotationType()
.getCanonicalName())) {
return false;
}
}
return true;
}
return true;
}
});
});

property.setContentLength(entity, 0);
property.setContentLength(entity, BeanUtils.getDefaultValueForType(property.getContentLengthType().getType()));
}

return entity;
}

private String absolutify(String bucket, String location) {
String locationToUse = null;
Assert.state(location.startsWith("azure-blob://") == false);
Assert.state(location.startsWith("azure-blob://") == false, "resource location must start with azure-blob://");
if (location.startsWith("/")) {
locationToUse = location.substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public class AzureStorageIT {
try (InputStream expected = new ByteArrayInputStream("Hello Spring Content World!".getBytes())) {
try (InputStream actual = genericResource.getInputStream()) {
matches = IOUtils.contentEquals(expected, actual);
assertThat(matches, Matchers.is(true));
assertThat(matches, is(true));
}
}
});
Expand Down Expand Up @@ -317,7 +317,7 @@ public class AzureStorageIT {
// content
assertThat(entity.getContentId(), is(notNullValue()));
assertThat(entity.getContentId().trim().length(), greaterThan(0));
Assert.assertEquals(entity.getContentLen(), 27L);
Assert.assertEquals(entity.getContentLen(), Long.valueOf(27L));

//rendition
assertThat(entity.getRenditionId(), is(notNullValue()));
Expand Down Expand Up @@ -407,21 +407,21 @@ public class AzureStorageIT {
It("should have no content", () -> {
//content
try (InputStream content = store.getContent(entity)) {
assertThat(content, is(Matchers.nullValue()));
assertThat(content, is(nullValue()));
}

assertThat(entity.getContentId(), is(Matchers.nullValue()));
Assert.assertEquals(entity.getContentLen(), 0);
assertThat(entity.getContentId(), is(nullValue()));
assertThat(entity.getContentLen(), is(nullValue()));

BlobContainerClient c = builder.buildClient().getBlobContainerClient("azure-test-bucket");
assertThat(c.getBlobClient(resourceLocation).getBlockBlobClient().exists(), is(false));

//rendition
try (InputStream content = store.getContent(entity, PropertyPath.from("rendition"))) {
assertThat(content, is(Matchers.nullValue()));
assertThat(content, is(nullValue()));
}

assertThat(entity.getRenditionId(), is(Matchers.nullValue()));
assertThat(entity.getRenditionId(), is(nullValue()));
Assert.assertEquals(entity.getRenditionLen(), 0);
});
});
Expand All @@ -436,11 +436,11 @@ public class AzureStorageIT {
It("should have no content", () -> {
//content
try (InputStream content = store.getContent(entity)) {
assertThat(content, is(Matchers.nullValue()));
assertThat(content, is(nullValue()));
}

assertThat(entity.getContentId(), is(Matchers.nullValue()));
Assert.assertEquals(entity.getContentLen(), 0);
assertThat(entity.getContentId(), is(nullValue()));
assertThat(entity.getContentLen(), is(nullValue()));

BlobContainerClient c = builder.buildClient().getBlobContainerClient("azure-test-bucket");
assertThat(c.getBlobClient(resourceLocation).getBlockBlobClient().exists(), is(true));
Expand Down Expand Up @@ -604,7 +604,7 @@ public static class TestEntity {
private String contentId;

@ContentLength
private long contentLen;
private Long contentLen;

@ContentId
private String renditionId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ContentProperty {
private String contentIdPropertyPath;
private TypeDescriptor contentIdType;
private String contentLengthPropertyPath;
private TypeDescriptor contentLengthType;
private String mimeTypePropertyPath;
private String originalFileNamePropertyPath;

Expand Down Expand Up @@ -107,6 +108,15 @@ public void setContentLength(Object entity, Object value) {
wrapper.setPropertyValue(contentLengthPropertyPath, value);
}

public TypeDescriptor getContentLengthType() {
Assert.notNull(this.contentLengthType, "content length property type must be set");
return this.contentLengthType;
}

public void setContentLengthType(TypeDescriptor descriptor) {
this.contentLengthType = descriptor;
}

public Object getMimeType(Object entity) {
if (mimeTypePropertyPath == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public boolean visitClassEnd(String path, Class<?> klazz) {
}
if (property.getContentLengthPropertyPath() != null) {
contentProperty.setContentLengthPropertyPath(property.getContentLengthPropertyPath());
contentProperty.setContentLengthType(property.getContentLengthType());
}
if (property.getMimeTypePropertyPath() != null) {
contentProperty.setMimeTypePropertyPath(property.getMimeTypePropertyPath());
Expand Down Expand Up @@ -137,6 +138,7 @@ public boolean visitField(String path, Class<?> klazz, Field f) {
classProperties.put(propertyName, property);
}
updateContentProperty(property::setContentLengthPropertyPath, fullyQualify(path, f.getName(), this.getContentPropertySeparator()));
property.setContentLengthType(TypeDescriptor.valueOf(f.getType()));
}
} else if (f.isAnnotationPresent(MimeType.class)) {
LOGGER.trace(String.format("%s.%s is @MimeType", f.getDeclaringClass().getCanonicalName(), f.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,23 @@ public static void setFieldWithAnnotationConditionally(Object domainObj,
}
}
}

public static Object getDefaultValueForType(Class<?> fieldType) {
if (fieldType == int.class || fieldType == double.class) {
return 0;
} else if (fieldType == boolean.class) {
return false;
} else if (fieldType == char.class) {
return '\u0000';
} else if (fieldType == byte.class) {
return (byte) 0;
} else if (fieldType == short.class) {
return (short) 0;
} else if (fieldType == long.class) {
return 0L;
} else if (fieldType == float.class) {
return 0.0f;
}
return null; // For non-primitive types
}
}
Loading
Loading