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());
}
}
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();
}
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());
}
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);
}
}
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");
}
Aggregations