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.inline;
21
22 import org.apache.commons.io.FilenameUtils;
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.sling.api.adapter.Adaptable;
25 import org.apache.sling.api.adapter.SlingAdaptable;
26 import org.apache.sling.api.resource.Resource;
27 import org.apache.sling.api.resource.ValueMap;
28 import org.jetbrains.annotations.NotNull;
29
30 import io.wcm.handler.media.Asset;
31 import io.wcm.handler.media.Dimension;
32 import io.wcm.handler.media.Media;
33 import io.wcm.handler.media.MediaArgs;
34 import io.wcm.handler.media.MediaFileType;
35 import io.wcm.handler.media.Rendition;
36 import io.wcm.handler.media.UriTemplate;
37 import io.wcm.handler.media.UriTemplateType;
38 import io.wcm.handler.media.spi.MediaHandlerConfig;
39 import io.wcm.wcm.commons.util.AemObjectReflectionToStringBuilder;
40
41
42
43
44 class InlineAsset extends SlingAdaptable implements Asset {
45
46 private final Adaptable adaptable;
47 private final Resource resource;
48 private final Media media;
49 private final MediaArgs defaultMediaArgs;
50 private final MediaHandlerConfig mediaHandlerConfig;
51 private final String fileName;
52
53
54
55
56
57
58 InlineAsset(Resource resource, Media media, MediaHandlerConfig mediaHandlerConfig,
59 String fileName, Adaptable adaptable) {
60 this.resource = resource;
61 this.media = media;
62 this.mediaHandlerConfig = mediaHandlerConfig;
63 this.defaultMediaArgs = media.getMediaRequest().getMediaArgs();
64 this.fileName = fileName;
65 this.adaptable = adaptable;
66 }
67
68 @Override
69 public String getTitle() {
70 return this.fileName;
71 }
72
73 @Override
74 public String getAltText() {
75 if (defaultMediaArgs.isDecorative()) {
76 return "";
77 }
78 else {
79 return defaultMediaArgs.getAltText();
80 }
81 }
82
83 @Override
84 public String getDescription() {
85
86 return null;
87 }
88
89 @Override
90 public @NotNull String getPath() {
91 return this.resource.getPath();
92 }
93
94 @Override
95 public @NotNull ValueMap getProperties() {
96 return this.resource.getValueMap();
97 }
98
99 @Override
100 public Rendition getDefaultRendition() {
101 return getRendition(this.defaultMediaArgs);
102 }
103
104 @Override
105 public Rendition getRendition(@NotNull MediaArgs mediaArgs) {
106 Rendition rendition = getInlineRendition(mediaArgs);
107
108
109 if (StringUtils.isEmpty(rendition.getUrl())) {
110 rendition = null;
111 }
112
113 return rendition;
114 }
115
116 @Override
117 public Rendition getImageRendition(@NotNull MediaArgs mediaArgs) {
118 Rendition rendition = getRendition(mediaArgs);
119 if (rendition != null && rendition.isImage()) {
120 return rendition;
121 }
122 else {
123 return null;
124 }
125 }
126
127 @Override
128 public Rendition getDownloadRendition(@NotNull MediaArgs mediaArgs) {
129 Rendition rendition = getRendition(mediaArgs);
130 if (rendition != null && rendition.isDownload()) {
131 return rendition;
132 }
133 else {
134 return null;
135 }
136 }
137
138
139
140
141
142
143 private Rendition getInlineRendition(MediaArgs mediaArgs) {
144 return new InlineRendition(this.resource, this.media, mediaArgs, this.mediaHandlerConfig,
145 this.fileName, this.adaptable);
146 }
147
148 @Override
149 @SuppressWarnings({ "unchecked", "null" })
150 public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
151 if (type == Resource.class) {
152 return (AdapterType)this.resource;
153 }
154 return super.adaptTo(type);
155 }
156
157 @Override
158 public @NotNull UriTemplate getUriTemplate(@NotNull UriTemplateType type) {
159 String extension = FilenameUtils.getExtension(fileName);
160 if (!MediaFileType.isImage(extension) || MediaFileType.isVectorImage(extension)) {
161 throw new UnsupportedOperationException("Unable to build URI template for this asset type: " + getPath());
162 }
163 Rendition originalRendition = getInlineRendition(new MediaArgs());
164 Dimension dimension = new Dimension(originalRendition.getWidth(), originalRendition.getHeight());
165 return new InlineUriTemplate(type, dimension, this.resource, fileName,
166 null, null, defaultMediaArgs, adaptable);
167 }
168
169 @Override
170 public String toString() {
171 return new AemObjectReflectionToStringBuilder(this,
172 io.wcm.wcm.commons.util.ToStringStyle.SHORT_PREFIX_OMIT_NULL_STYLE).build();
173 }
174
175 }