Search in sources :

Example 11 with Attachment

use of com.enonic.xp.attachment.Attachment in project xp by enonic.

the class ImageHandlerTest method setupContent.

private void setupContent() throws Exception {
    final Attachment attachment = Attachment.create().name("enonic-logo.png").mimeType("image/png").label("source").build();
    final Content content = createContent("123456", "path/to/image-name.jpg", attachment);
    when(this.contentService.getById(eq(content.getId()))).thenReturn(content);
    when(this.contentService.getByPath(eq(content.getPath()))).thenReturn(content);
    final ByteSource imageBytes = ByteSource.wrap(new byte[0]);
    when(this.contentService.getBinary(isA(ContentId.class), isA(BinaryReference.class))).thenReturn(imageBytes);
    when(this.imageService.readImage(isA(ReadImageParams.class))).thenReturn(imageBytes);
}
Also used : ReadImageParams(com.enonic.xp.image.ReadImageParams) Content(com.enonic.xp.content.Content) ByteSource(com.google.common.io.ByteSource) Attachment(com.enonic.xp.attachment.Attachment) ContentId(com.enonic.xp.content.ContentId) BinaryReference(com.enonic.xp.util.BinaryReference)

Example 12 with Attachment

use of com.enonic.xp.attachment.Attachment in project xp by enonic.

the class AttachmentHandlerTest method setupMedia.

private void setupMedia() throws Exception {
    final Attachment attachment = Attachment.create().name("logo.png").mimeType("image/png").label("small").build();
    final Media content = createMedia("123456", "path/to/content", attachment);
    when(this.contentService.getById(eq(content.getId()))).thenReturn(content);
    when(this.contentService.getByPath(eq(content.getPath()))).thenReturn(content);
    when(this.contentService.getBinaryKey(eq(content.getId()), eq(content.getMediaAttachment().getBinaryReference()))).thenReturn("98765");
    this.mediaBytes = ByteSource.wrap(new byte[] { '0', '1', '2', '3', '4', '5', '6' });
    when(this.contentService.getBinary(isA(ContentId.class), isA(BinaryReference.class))).thenReturn(this.mediaBytes);
}
Also used : Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) ContentId(com.enonic.xp.content.ContentId) BinaryReference(com.enonic.xp.util.BinaryReference)

Example 13 with Attachment

use of com.enonic.xp.attachment.Attachment in project xp by enonic.

the class AttachmentHandlerTest method setup.

@BeforeEach
final void setup() throws Exception {
    this.contentService = mock(ContentService.class);
    this.handler = new AttachmentHandler(this.contentService);
    this.handler.activate(mock(PortalConfig.class, invocation -> invocation.getMethod().getDefaultValue()));
    this.request = new PortalRequest();
    this.request.setMethod(HttpMethod.GET);
    this.request.setBranch(ContentConstants.BRANCH_MASTER);
    this.request.setBaseUri("/site");
    this.request.setContentPath(ContentPath.from("/path/to/content"));
    this.request.setEndpointPath("/_/attachment/inline/123456/logo.png");
    setupMedia();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ContentConstants(com.enonic.xp.content.ContentConstants) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebResponse(com.enonic.xp.web.WebResponse) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Attachment(com.enonic.xp.attachment.Attachment) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) Permission(com.enonic.xp.security.acl.Permission) ContentId(com.enonic.xp.content.ContentId) HttpMethod(com.enonic.xp.web.HttpMethod) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) PropertyTree(com.enonic.xp.data.PropertyTree) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) BinaryReference(com.enonic.xp.util.BinaryReference) MediaType(com.google.common.net.MediaType) Media(com.enonic.xp.content.Media) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) ContentPath(com.enonic.xp.content.ContentPath) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) AccessControlList(com.enonic.xp.security.acl.AccessControlList) StandardCharsets(java.nio.charset.StandardCharsets) ContentNotFoundException(com.enonic.xp.content.ContentNotFoundException) PortalConfig(com.enonic.xp.portal.impl.PortalConfig) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) PrincipalKey(com.enonic.xp.security.PrincipalKey) Attachments(com.enonic.xp.attachment.Attachments) RoleKeys(com.enonic.xp.security.RoleKeys) HttpStatus(com.enonic.xp.web.HttpStatus) AccessControlEntry(com.enonic.xp.security.acl.AccessControlEntry) Mockito.mock(org.mockito.Mockito.mock) PortalConfig(com.enonic.xp.portal.impl.PortalConfig) ContentService(com.enonic.xp.content.ContentService) PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with Attachment

use of com.enonic.xp.attachment.Attachment in project xp by enonic.

the class ImageUrlBuilderTest method init.

@BeforeEach
public void init() {
    final PortalRequest portalRequest = new PortalRequest();
    portalRequest.setBranch(Branch.from("draft"));
    portalRequest.setApplicationKey(ApplicationKey.from("myapplication"));
    portalRequest.setBaseUri("/site");
    portalRequest.setContentPath(ContentPath.from("context/path"));
    this.imageUrlParams = new ImageUrlParams().portalRequest(portalRequest).scale("testScale");
    urlBuilder = new ImageUrlBuilder();
    urlBuilder.setParams(imageUrlParams);
    final Attachment attachment = Attachment.create().name("attachmentName").mimeType("attachmentMimeType").size(1).build();
    media = Mockito.mock(Media.class);
    final ContentId contentId = ContentId.from("testID");
    Mockito.when(media.getId()).thenReturn(contentId);
    Mockito.when(media.getName()).thenReturn(ContentName.from("testName"));
    Mockito.when(media.getType()).thenReturn(ContentTypeName.imageMedia());
    Mockito.when(media.getMediaAttachment()).thenReturn(attachment);
    final ContentService contentService = Mockito.mock(ContentService.class);
    Mockito.when(contentService.getByPath(Mockito.any())).thenReturn(media);
    Mockito.when(contentService.getById(Mockito.any())).thenReturn(media);
    Mockito.when(contentService.getBinaryKey(contentId, attachment.getBinaryReference())).thenReturn("binaryHash");
    urlBuilder.contentService = contentService;
}
Also used : Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) ContentId(com.enonic.xp.content.ContentId) ContentService(com.enonic.xp.content.ContentService) PortalRequest(com.enonic.xp.portal.PortalRequest) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with Attachment

use of com.enonic.xp.attachment.Attachment in project xp by enonic.

the class AttachmentUrlBuilder method resolveAttachment.

private Attachment resolveAttachment(final Content content) {
    final Attachments attachments = content.getAttachments();
    final Attachment attachment;
    if (this.params.getName() != null) {
        attachment = attachments.byName(this.params.getName());
        if (attachment == null) {
            throw new IllegalArgumentException("Could not find attachment with name [" + this.params.getName() + "] on content [" + content.getId() + "]");
        }
    } else {
        final String label = this.params.getLabel() != null ? this.params.getLabel() : "source";
        attachment = attachments.byLabel(label);
        if (attachment == null) {
            throw new IllegalArgumentException("Could not find attachment with label [" + label + "] on content [" + content.getId() + "]");
        }
    }
    return attachment;
}
Also used : Attachment(com.enonic.xp.attachment.Attachment) Attachments(com.enonic.xp.attachment.Attachments)

Aggregations

Attachment (com.enonic.xp.attachment.Attachment)31 Content (com.enonic.xp.content.Content)14 BinaryReference (com.enonic.xp.util.BinaryReference)13 ByteSource (com.google.common.io.ByteSource)12 Attachments (com.enonic.xp.attachment.Attachments)10 ContentId (com.enonic.xp.content.ContentId)10 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)6 Media (com.enonic.xp.content.Media)6 ReadImageParams (com.enonic.xp.image.ReadImageParams)6 Test (org.junit.jupiter.api.Test)5 PropertyTree (com.enonic.xp.data.PropertyTree)4 PortalResponse (com.enonic.xp.portal.PortalResponse)4 ServletRequestUrlHelper.contentDispositionAttachment (com.enonic.xp.web.servlet.ServletRequestUrlHelper.contentDispositionAttachment)4 MediaType (com.google.common.net.MediaType)4 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)3 ContentPath (com.enonic.xp.content.ContentPath)2 ContentService (com.enonic.xp.content.ContentService)2 UpdateMediaParams (com.enonic.xp.content.UpdateMediaParams)2 Thumbnail (com.enonic.xp.icon.Thumbnail)2 PortalRequest (com.enonic.xp.portal.PortalRequest)2