1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.handler.mediasource.ngdm.impl.metadata;
21
22 import java.util.Objects;
23
24 import org.apache.commons.lang3.builder.ToStringBuilder;
25 import org.apache.commons.lang3.builder.ToStringStyle;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.json.JsonMapper;
31
32 import io.wcm.handler.media.Dimension;
33 import io.wcm.wcm.commons.contenttype.ContentType;
34
35
36
37
38 public final class NextGenDynamicMediaMetadata {
39
40 private final String mimeType;
41 private final Dimension dimension;
42
43 private static final JsonMapper OBJECT_MAPPER = new JsonMapper();
44
45 NextGenDynamicMediaMetadata(@Nullable String mimeType, long width, long height) {
46 this.mimeType = mimeType;
47 if (width > 0 && height > 0) {
48 this.dimension = new Dimension(width, height);
49 }
50 else {
51 this.dimension = null;
52 }
53 }
54
55
56
57
58 public @NotNull String getMimeType() {
59 return Objects.toString(mimeType, ContentType.OCTET_STREAM);
60 }
61
62
63
64
65 public @Nullable Dimension getDimension() {
66 return dimension;
67 }
68
69 boolean isValid() {
70 return mimeType != null;
71 }
72
73 @Override
74 public String toString() {
75 return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
76 }
77
78
79
80
81
82
83
84 @SuppressWarnings("null")
85 public static @NotNull NextGenDynamicMediaMetadata fromJson(@NotNull String jsonResponse) throws JsonProcessingException {
86 MetadataResponse response = OBJECT_MAPPER.readValue(jsonResponse, MetadataResponse.class);
87
88 String mimeType = null;
89 if (response.repositoryMetadata != null) {
90 mimeType = response.repositoryMetadata.dcFormat;
91 }
92
93 long width = 0;
94 long height = 0;
95 if (response.assetMetadata != null) {
96 width = response.assetMetadata.tiffImageWidth;
97 height = response.assetMetadata.tiffImageLength;
98 }
99
100 return new NextGenDynamicMediaMetadata(mimeType, width, height);
101 }
102
103 }