Search in sources :

Example 1 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class ScriptValueTranslator method handleValue.

private void handleValue(final PropertySet parent, final String name, final Object value) {
    if (value instanceof Instant) {
        parent.addInstant(name, (Instant) value);
    } else if (value instanceof GeoPoint) {
        parent.addGeoPoint(name, (GeoPoint) value);
    } else if (value instanceof Double) {
        parent.addDouble(name, (Double) value);
    } else if (value instanceof Float) {
        parent.addDouble(name, ((Float) value).doubleValue());
    } else if (value instanceof Integer) {
        parent.addLong(name, ((Integer) value).longValue());
    } else if (value instanceof Byte) {
        parent.addLong(name, ((Byte) value).longValue());
    } else if (value instanceof Long) {
        parent.addLong(name, ((Long) value));
    } else if (value instanceof Number) {
        parent.addDouble(name, ((Number) value).doubleValue());
    } else if (value instanceof Boolean) {
        parent.addBoolean(name, (Boolean) value);
    } else if (value instanceof LocalDateTime) {
        parent.addLocalDateTime(name, (LocalDateTime) value);
    } else if (value instanceof LocalDate) {
        parent.addLocalDate(name, (LocalDate) value);
    } else if (value instanceof LocalTime) {
        parent.addLocalTime(name, (LocalTime) value);
    } else if (value instanceof Date) {
        parent.addInstant(name, ((Date) value).toInstant());
    } else if (value instanceof Reference) {
        parent.addReference(name, (Reference) value);
    } else if (value instanceof BinaryReference) {
        parent.addBinaryReference(name, (BinaryReference) value);
    } else if (value instanceof Link) {
        parent.addLink(name, (Link) value);
    } else if (value instanceof BinaryAttachment) {
        final BinaryAttachment binaryAttachment = (BinaryAttachment) value;
        parent.addBinaryReference(name, binaryAttachment.getReference());
        if (includeBinaryAttachments) {
            this.binaryAttachmentsBuilder.add(new BinaryAttachment(binaryAttachment.getReference(), binaryAttachment.getByteSource()));
        }
    } else {
        parent.addString(name, value.toString());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) BinaryReference(com.enonic.xp.util.BinaryReference) Reference(com.enonic.xp.util.Reference) Instant(java.time.Instant) BinaryReference(com.enonic.xp.util.BinaryReference) LocalDate(java.time.LocalDate) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) Date(java.util.Date) LocalDate(java.time.LocalDate) GeoPoint(com.enonic.xp.util.GeoPoint) Link(com.enonic.xp.util.Link)

Example 2 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class ImageHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final Media content = getImage(this.contentId);
    final String contentName = content.getName().toString();
    final boolean contentNameEquals = contentName.equals(this.name);
    if (!(contentNameEquals || contentName.equals(Files.getNameWithoutExtension(this.name)))) {
        throw WebException.notFound(String.format("Image [%s] not found for content [%s]", this.name, this.contentId));
    }
    final Attachment attachment = content.getMediaAttachment();
    if (attachment == null) {
        throw WebException.notFound(String.format("Attachment [%s] not found", content.getName()));
    }
    final BinaryReference binaryReference = attachment.getBinaryReference();
    final ByteSource binary = this.contentService.getBinary(this.contentId, binaryReference);
    if (binary == null) {
        throw WebException.notFound(String.format("Binary [%s] not found for content [%s]", binaryReference, this.contentId));
    }
    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 String attachmentMimeType = attachment.getMimeType();
    final PortalResponse.Builder portalResponse = PortalResponse.create();
    if ("svgz".equals(attachment.getExtension())) {
        portalResponse.contentType(MediaType.SVG_UTF_8.withoutParameters());
        portalResponse.header("Content-Encoding", "gzip");
        portalResponse.body(binary);
    } else if (attachmentMimeType.equals("image/svg+xml") || attachmentMimeType.equals("image/gif")) {
        portalResponse.contentType(MediaType.parse(attachmentMimeType));
        portalResponse.body(binary);
    } else {
        final ImageOrientation imageOrientation = Objects.requireNonNullElseGet(content.getOrientation(), () -> Objects.requireNonNullElse(mediaInfoService.getImageOrientation(binary), ImageOrientation.TopLeft));
        final MediaType mimeType = contentNameEquals ? MediaType.parse(attachmentMimeType) : MediaTypes.instance().fromFile(this.name);
        portalResponse.contentType(mimeType);
        try {
            final ReadImageParams readImageParams = ReadImageParams.newImageParams().contentId(this.contentId).binaryReference(binaryReference).cropping(content.getCropping()).scaleParams(this.scaleParams).focalPoint(content.getFocalPoint()).filterParam(this.filterParam).backgroundColor(getBackgroundColor()).mimeType(mimeType.toString()).quality(getImageQuality()).orientation(imageOrientation).build();
            portalResponse.body(this.imageService.readImage(readImageParams));
        } catch (IllegalArgumentException e) {
            throw new WebException(HttpStatus.BAD_REQUEST, "Invalid parameters", e);
        } catch (ThrottlingException e) {
            throw new WebException(HttpStatus.TOO_MANY_REQUESTS, "Try again later", e);
        }
    }
    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))) {
            portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
        }
    }
    final Trace trace = Tracer.current();
    if (trace != null) {
        trace.put("contentPath", content.getPath());
        trace.put("type", "image");
    }
    return portalResponse.build();
}
Also used : WebException(com.enonic.xp.web.WebException) Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) BinaryReference(com.enonic.xp.util.BinaryReference) ImageOrientation(com.enonic.xp.media.ImageOrientation) ReadImageParams(com.enonic.xp.image.ReadImageParams) Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) ThrottlingException(com.enonic.xp.exception.ThrottlingException)

Example 3 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class ApplicationNodeTransformerTest method binary_reference_added.

@Test
public void binary_reference_added() throws Exception {
    final Application app = Mockito.mock(Application.class);
    Mockito.when(app.getKey()).thenReturn(ApplicationKey.from("myApp"));
    Mockito.when(app.getVersion()).thenReturn(Version.valueOf("1.0.0"));
    Mockito.when(app.getMaxSystemVersion()).thenReturn("1.0.0");
    Mockito.when(app.getMinSystemVersion()).thenReturn("1.0.0.");
    Mockito.when(app.getDisplayName()).thenReturn("displayName");
    final ByteSource appSource = ByteSource.wrap(ByteStreams.toByteArray(newBundle("myBundle", true).build()));
    final CreateNodeParams createNodeParams = ApplicationNodeTransformer.toCreateNodeParams(app, appSource);
    final PropertyTree data = createNodeParams.getData();
    final BinaryReference binaryReference = data.getBinaryReference(ApplicationNodeTransformer.APPLICATION_BINARY_REF);
    assertNotNull(binaryReference);
    final BinaryAttachment binaryAttachment = createNodeParams.getBinaryAttachments().get(binaryReference);
    assertEquals(appSource, binaryAttachment.getByteSource());
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) ByteSource(com.google.common.io.ByteSource) BinaryReference(com.enonic.xp.util.BinaryReference) Application(com.enonic.xp.app.Application) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) Test(org.junit.jupiter.api.Test)

Example 4 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class NodeImporter method addBinary.

private void addBinary(final VirtualFile nodeFile, final BinaryAttachments.Builder builder, final Property binaryRefProperty) {
    final BinaryReference binaryReference = binaryRefProperty.getBinaryReference();
    try {
        final VirtualFile binary = exportReader.getBinarySource(nodeFile, binaryReference.toString());
        builder.add(new BinaryAttachment(binaryReference, binary.getByteSource()));
        result.addBinary(binary.getPath().getPath(), binaryReference);
    } catch (Exception e) {
        result.addError("Error processing binary, skip", e);
    }
}
Also used : VirtualFile(com.enonic.xp.vfs.VirtualFile) BinaryReference(com.enonic.xp.util.BinaryReference) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) ImportNodeException(com.enonic.xp.export.ImportNodeException) XmlException(com.enonic.xp.xml.XmlException)

Example 5 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class NodeExportIntegrationTest method create_binary_files.

// Wait with this until decided how to handle versions. Only in dump, or in export too?
@Disabled
@Test
public void create_binary_files() throws Exception {
    final PropertyTree data = new PropertyTree();
    final BinaryReference binaryRef1 = BinaryReference.from("image1.jpg");
    final BinaryReference binaryRef2 = BinaryReference.from("image2.jpg");
    data.addBinaryReference("my-image-1", binaryRef1);
    data.addBinaryReference("my-image-2", binaryRef2);
    this.nodeService.create(CreateNodeParams.create().name("my-node").parent(NodePath.ROOT).data(data).attachBinary(binaryRef1, ByteSource.wrap("this-is-the-binary-data-for-image1".getBytes())).attachBinary(binaryRef2, ByteSource.wrap("this-is-the-binary-data-for-image2".getBytes())).build());
    final NodeExportResult result = NodeExporter.create().nodeService(this.nodeService).nodeExportWriter(new FileExportWriter()).sourceNodePath(NodePath.ROOT).targetDirectory(this.temporaryFolder.resolve("myExport")).build().execute();
    assertEquals(2, result.getExportedNodes().getSize());
    assertEquals(2, result.getExportedBinaries().size());
    assertFileExists("myExport/_/node.xml");
    assertFileExists("myExport/my-node/_/node.xml");
    assertFileExists("myExport/my-node/_/bin/image1.jpg");
    assertFileExists("myExport/my-node/_/bin/image2.jpg");
}
Also used : NodeExportResult(com.enonic.xp.export.NodeExportResult) PropertyTree(com.enonic.xp.data.PropertyTree) BinaryReference(com.enonic.xp.util.BinaryReference) FileExportWriter(com.enonic.xp.core.impl.export.writer.FileExportWriter) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

BinaryReference (com.enonic.xp.util.BinaryReference)37 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)16 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 BinaryAttachment (com.enonic.xp.node.BinaryAttachment)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 Attachment (com.enonic.xp.attachment.Attachment)4 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