1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.handler.media;
21
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.function.Function;
26
27 import org.apache.commons.lang3.builder.ToStringBuilder;
28 import org.apache.commons.lang3.builder.ToStringStyle;
29 import org.jdom2.Element;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 import org.osgi.annotation.versioning.ProviderType;
33
34 import com.fasterxml.jackson.annotation.JsonIgnore;
35 import com.fasterxml.jackson.annotation.JsonInclude;
36 import com.fasterxml.jackson.annotation.JsonInclude.Include;
37
38 import io.wcm.handler.commons.dom.HtmlElement;
39 import io.wcm.handler.commons.dom.Span;
40 import io.wcm.handler.media.imagemap.ImageMapArea;
41 import io.wcm.handler.media.spi.MediaSource;
42
43
44
45
46 @ProviderType
47 @JsonInclude(Include.NON_EMPTY)
48 public final class Media {
49
50 private final @NotNull MediaSource mediaSource;
51 private @NotNull MediaRequest mediaRequest;
52 private HtmlElement element;
53 private Function<Media, HtmlElement> elementBuilder;
54 private String url;
55 private Asset asset;
56 private Collection<Rendition> renditions;
57 private CropDimension cropDimension;
58 private Integer rotation;
59 private List<ImageMapArea> map;
60 private MediaInvalidReason mediaInvalidReason;
61 private String mediaInvalidReasonCustomMessage;
62 private String markup;
63
64
65
66
67
68 public Media(@NotNull MediaSource mediaSource, @NotNull MediaRequest mediaRequest) {
69 this.mediaSource = mediaSource;
70 this.mediaRequest = mediaRequest;
71 }
72
73
74
75
76 @JsonIgnore
77 public @NotNull MediaSource getMediaSource() {
78 return this.mediaSource;
79 }
80
81
82
83
84 @JsonIgnore
85 public @NotNull MediaRequest getMediaRequest() {
86 return this.mediaRequest;
87 }
88
89
90
91
92 public void setMediaRequest(@NotNull MediaRequest mediaRequest) {
93 this.mediaRequest = mediaRequest;
94 }
95
96
97
98
99 @JsonIgnore
100 public @Nullable HtmlElement getElement() {
101 if (this.element == null && this.elementBuilder != null) {
102 this.element = this.elementBuilder.apply(this);
103 this.elementBuilder = null;
104 }
105 return this.element;
106 }
107
108
109
110
111 @JsonIgnore
112 public @Nullable String getMarkup() {
113 HtmlElement el = getElement();
114 if (markup == null && el != null) {
115 if (el instanceof Span) {
116
117 StringBuilder result = new StringBuilder();
118 for (Element child : el.getChildren()) {
119 result.append(child.toString());
120 }
121 markup = result.toString();
122 }
123 else {
124 markup = el.toString();
125 }
126 }
127 return markup;
128 }
129
130
131
132
133 public void setElementBuilder(@NotNull Function<Media, HtmlElement> value) {
134 this.elementBuilder = value;
135 this.markup = null;
136 }
137
138
139
140
141 public @Nullable String getUrl() {
142 return this.url;
143 }
144
145
146
147
148 public void setUrl(@Nullable String value) {
149 this.url = value;
150 }
151
152
153
154
155
156 public @Nullable Asset getAsset() {
157 return this.asset;
158 }
159
160
161
162
163
164 public void setAsset(@Nullable Asset asset) {
165 this.asset = asset;
166 }
167
168
169
170
171
172 @JsonIgnore
173 public @Nullable Rendition getRendition() {
174 if (this.renditions == null || this.renditions.isEmpty()) {
175 return null;
176 }
177 return this.renditions.iterator().next();
178 }
179
180
181
182
183
184 public @NotNull Collection<Rendition> getRenditions() {
185 if (this.renditions == null) {
186 return Collections.emptyList();
187 }
188 else {
189 return this.renditions;
190 }
191 }
192
193
194
195
196
197 public void setRenditions(@Nullable Collection<Rendition> renditions) {
198 this.renditions = renditions;
199 }
200
201
202
203
204 @JsonIgnore
205 public @Nullable CropDimension getCropDimension() {
206 return this.cropDimension;
207 }
208
209
210
211
212 public void setCropDimension(@Nullable CropDimension cropDimension) {
213 this.cropDimension = cropDimension;
214 }
215
216
217
218
219 @JsonIgnore
220 public @Nullable Integer getRotation() {
221 return this.rotation;
222 }
223
224
225
226
227 public void setRotation(@Nullable Integer rotation) {
228 this.rotation = rotation;
229 }
230
231
232
233
234 @JsonIgnore
235 public @Nullable List<ImageMapArea> getMap() {
236 return this.map;
237 }
238
239
240
241
242 public void setMap(@Nullable List<ImageMapArea> map) {
243 this.map = map;
244 }
245
246
247
248
249 public boolean isValid() {
250 return (mediaInvalidReason == null);
251 }
252
253
254
255
256 @JsonIgnore
257 public @Nullable MediaInvalidReason getMediaInvalidReason() {
258 return this.mediaInvalidReason;
259 }
260
261
262
263
264 public void setMediaInvalidReason(@Nullable MediaInvalidReason mediaInvalidReason) {
265 this.mediaInvalidReason = mediaInvalidReason;
266 }
267
268
269
270
271
272 public @Nullable String getMediaInvalidReasonCustomMessage() {
273 return this.mediaInvalidReasonCustomMessage;
274 }
275
276
277
278
279
280 public void setMediaInvalidReasonCustomMessage(@Nullable String mediaInvalidReasonCustomMessage) {
281 this.mediaInvalidReasonCustomMessage = mediaInvalidReasonCustomMessage;
282 }
283
284 @Override
285 public String toString() {
286 ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
287 if (isValid()) {
288 sb.append("url", getUrl());
289 }
290 else {
291 sb.append("mediaInvalidReason", this.mediaInvalidReason);
292 }
293 sb.append("mediaSource", mediaSource.getId());
294 if (asset != null) {
295 sb.append("asset", asset.getPath());
296 }
297 if (renditions != null) {
298 sb.append("renditions", renditions);
299 }
300 if (cropDimension != null) {
301 sb.append("cropDimension", cropDimension);
302 }
303 if (rotation != null) {
304 sb.append("rotation", rotation);
305 }
306 if (map != null) {
307 sb.append("map", map);
308 }
309 sb.append("mediaRequest", mediaRequest);
310 return sb.build();
311 }
312
313 }