Search in sources :

Example 31 with BinaryReference

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();
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) ServletRequestUrlHelper.contentDispositionAttachment(com.enonic.xp.web.servlet.ServletRequestUrlHelper.contentDispositionAttachment) Attachment(com.enonic.xp.attachment.Attachment) BinaryReference(com.enonic.xp.util.BinaryReference)

Example 32 with BinaryReference

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());
}
Also used : BinaryReference(com.enonic.xp.util.BinaryReference) Test(org.junit.jupiter.api.Test) AbstractEqualsTest(com.enonic.xp.support.AbstractEqualsTest)

Example 33 with BinaryReference

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));
}
Also used : Context(com.enonic.xp.context.Context) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IdProviderKey(com.enonic.xp.security.IdProviderKey) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) DeleteRepositoryParams(com.enonic.xp.repository.DeleteRepositoryParams) Node(com.enonic.xp.node.Node) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Branch(com.enonic.xp.branch.Branch) NodeHelper(com.enonic.xp.repo.impl.node.NodeHelper) RepositoryId(com.enonic.xp.repository.RepositoryId) ImmutableList(com.google.common.collect.ImmutableList) CreateBranchParams(com.enonic.xp.repository.CreateBranchParams) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ContextAccessor(com.enonic.xp.context.ContextAccessor) ContextBuilder(com.enonic.xp.context.ContextBuilder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) SystemConstants(com.enonic.xp.security.SystemConstants) PropertyTree(com.enonic.xp.data.PropertyTree) BinaryReference(com.enonic.xp.util.BinaryReference) DeleteBranchParams(com.enonic.xp.repository.DeleteBranchParams) User(com.enonic.xp.security.User) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) NodePath(com.enonic.xp.node.NodePath) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AccessControlList(com.enonic.xp.security.acl.AccessControlList) CreateRepositoryParams(com.enonic.xp.repository.CreateRepositoryParams) UpdateRepositoryParams(com.enonic.xp.repository.UpdateRepositoryParams) Test(org.junit.jupiter.api.Test) PrincipalKey(com.enonic.xp.security.PrincipalKey) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RoleKeys(com.enonic.xp.security.RoleKeys) Context(com.enonic.xp.context.Context) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) AccessControlEntry(com.enonic.xp.security.acl.AccessControlEntry) Repository(com.enonic.xp.repository.Repository) PropertyTree(com.enonic.xp.data.PropertyTree) ByteSource(com.google.common.io.ByteSource) BinaryReference(com.enonic.xp.util.BinaryReference) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 34 with BinaryReference

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);
}
Also used : BinaryReference(com.enonic.xp.util.BinaryReference) AttachedBinary(com.enonic.xp.node.AttachedBinary)

Example 35 with BinaryReference

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());
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) BinaryReference(com.enonic.xp.util.BinaryReference) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) Test(org.junit.jupiter.api.Test)

Aggregations

BinaryReference (com.enonic.xp.util.BinaryReference)38 Test (org.junit.jupiter.api.Test)26 PropertyTree (com.enonic.xp.data.PropertyTree)25 Node (com.enonic.xp.node.Node)23 ByteSource (com.google.common.io.ByteSource)17 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)13 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)12 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)8 AttachedBinary (com.enonic.xp.node.AttachedBinary)7 NodePath (com.enonic.xp.node.NodePath)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Attachment (com.enonic.xp.attachment.Attachment)5 BinaryAttachment (com.enonic.xp.node.BinaryAttachment)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)4 NodeExportResult (com.enonic.xp.export.NodeExportResult)3 NodeBinaryReferenceException (com.enonic.xp.node.NodeBinaryReferenceException)3 NodeHelper (com.enonic.xp.repo.impl.node.NodeHelper)3 ByteStreams (com.google.common.io.ByteStreams)3