use of com.enonic.xp.util.BinaryReference in project xp by enonic.
the class AbstractAttachmentHandlerWorker method execute.
@Override
public PortalResponse execute() throws Exception {
if (request.getMethod() == HttpMethod.OPTIONS) {
// it will be handled by default OPTIONS handler in BaseWebHandler
return PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
}
final T content = cast(getContent(this.id));
final Attachment attachment = resolveAttachment(content, this.name);
final BinaryReference binaryReference = attachment.getBinaryReference();
final ByteSource binary = getBinary(this.id, binaryReference);
final MediaType attachmentMimeType = MediaType.parse(attachment.getMimeType());
final boolean isSvgz = "svgz".equals(attachment.getExtension());
final boolean isGif = attachmentMimeType.is(MediaType.GIF);
final PortalResponse.Builder portalResponse = PortalResponse.create();
final MediaType contentType;
if (isSvgz) {
contentType = SVG_MEDIA_TYPE;
portalResponse.header("Content-Encoding", "gzip");
} else if (isGif) {
contentType = MediaType.GIF;
} else if (shouldConvert(content, this.name)) {
contentType = MediaTypes.instance().fromFile(this.name);
} else {
contentType = attachmentMimeType;
}
if (contentType.is(SVG_MEDIA_TYPE)) {
if (!nullToEmpty(contentSecurityPolicySvg).isBlank()) {
portalResponse.header("Content-Security-Policy", contentSecurityPolicySvg);
}
} else {
if (!nullToEmpty(contentSecurityPolicy).isBlank()) {
portalResponse.header("Content-Security-Policy", contentSecurityPolicy);
}
}
if (!nullToEmpty(this.fingerprint).isBlank()) {
final boolean isPublic = content.getPermissions().isAllowedFor(RoleKeys.EVERYONE, Permission.READ) && ContentConstants.BRANCH_MASTER.equals(request.getBranch());
final String cacheControlHeaderConfig = isPublic ? publicCacheControlHeaderConfig : privateCacheControlHeaderConfig;
if (!nullToEmpty(cacheControlHeaderConfig).isBlank() && this.fingerprint.equals(resolveHash(content, binaryReference))) {
portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
}
}
if (download) {
portalResponse.header("Content-Disposition", contentDispositionAttachment(attachment.getName()));
}
final ByteSource body;
if (isGif || isSvgz || attachmentMimeType.is(SVG_MEDIA_TYPE)) {
body = binary;
} else {
body = transform(content, binaryReference, binary, contentType);
}
addTrace(content);
writeResponseContent(portalResponse, contentType, body);
return portalResponse.build();
}
use of com.enonic.xp.util.BinaryReference in project xp by enonic.
the class PropertyTreeTest method setBinaryReference.
@Test
public void setBinaryReference() {
PropertyTree tree = new PropertyTree();
BinaryReference binaryReference1 = BinaryReference.from("ref1");
tree.setBinaryReference("binaryRef1", binaryReference1);
assertEquals(1, tree.getTotalSize());
assertEquals(binaryReference1, tree.getBinaryReference("binaryRef1"));
BinaryReference binaryReference2 = BinaryReference.from("ref2");
tree.setBinaryReference(PropertyPath.from("binaryRef2"), binaryReference2);
assertEquals(2, tree.getTotalSize());
assertEquals(binaryReference2, tree.getBinaryReference(PropertyPath.from("binaryRef2")));
BinaryReference binaryReference3 = BinaryReference.from("ref3");
tree.setBinaryReference("binaryRef3", 0, binaryReference3);
assertEquals(3, tree.getTotalSize());
assertEquals(binaryReference3, tree.getBinaryReference("binaryRef3", 0));
assertEquals("ref3", tree.getBinaryReferences("binaryRef3").iterator().next().toString());
}
use of com.enonic.xp.util.BinaryReference in project xp by enonic.
the class RepositoryServiceImplTest method update_attachment.
@Test
void update_attachment() throws Exception {
final String repoId = "repo-with-attachment";
doCreateRepo(repoId);
final BinaryReference binaryRef = BinaryReference.from("image1.jpg");
ByteSource binarySource = ByteSource.wrap("this-is-the-binary-data-for-image1".getBytes());
Context mockCurrentContext = ContextBuilder.create().branch("master").repositoryId(repoId).authInfo(REPO_TEST_DEFAULT_USER_AUTHINFO).build();
PropertyTree data = new PropertyTree();
data.setBinaryReference("someIcon", binaryRef);
mockCurrentContext.runWith(() -> repositoryService.updateRepository(UpdateRepositoryParams.create().repositoryId(RepositoryId.from(repoId)).editor(edit -> {
edit.data = data;
edit.binaryAttachments = ImmutableList.of(new BinaryAttachment(binaryRef, binarySource));
}).build()));
createAdminContext().runWith(() -> {
repositoryService.invalidateAll();
});
ByteSource persistedAttachment = mockCurrentContext.callWith(() -> repositoryService.getBinary(RepositoryId.from(repoId), BinaryReference.from("image1.jpg")));
assertTrue(binarySource.contentEquals(persistedAttachment));
}
use of com.enonic.xp.util.BinaryReference in project xp by enonic.
the class AbstractGetBinaryCommand method getByPropertyPath.
ByteSource getByPropertyPath(final Node node) {
final BinaryReference binaryReference = node.data().getBinaryReference(this.propertyPath);
if (binaryReference == null) {
return null;
}
final AttachedBinary attachedBinary = node.getAttachedBinaries().getByBinaryReference(binaryReference);
return doGetByteSource(attachedBinary);
}
use of com.enonic.xp.util.BinaryReference in project xp by enonic.
the class UpdateNodeCommandTest method keep_existing_binaries_also_when_new_property_but_equal_BinaryReference.
@Test
public void keep_existing_binaries_also_when_new_property_but_equal_BinaryReference() throws Exception {
final PropertyTree data = new PropertyTree();
final BinaryReference binaryRef = BinaryReference.from("my-car.jpg");
data.setBinaryReference("my-image", binaryRef);
final CreateNodeParams params = CreateNodeParams.create().parent(NodePath.ROOT).name("my-node").data(data).attachBinary(binaryRef, ByteSource.wrap("my-car-image-source".getBytes())).build();
final Node node = createNode(params);
final UpdateNodeParams updateNodeParams = UpdateNodeParams.create().editor(toBeEdited -> {
toBeEdited.data.removeProperties("my-image");
toBeEdited.data.setBinaryReference("my-image", binaryRef);
}).id(node.id()).build();
final Node updatedNode = updateNode(updateNodeParams);
assertEquals(1, updatedNode.getAttachedBinaries().getSize());
}
Aggregations