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