View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2024 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Metadata for Next Gen Dynamic Media asset fetched from the HTTP API.
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     * @return Mime type
57     */
58    public @NotNull String getMimeType() {
59      return Objects.toString(mimeType, ContentType.OCTET_STREAM);
60    }
61  
62    /**
63     * @return Image Dimension or null if no image or dimension not available
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     * Converts JSON response from NGDM API to metadata object.
80     * @param jsonResponse JSON response
81     * @return Metadata object
82     * @throws JsonProcessingException If JSON parsing fails
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 }