001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.messages.internal;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.JsonableBaseObject;
020import java.net.URI;
021import java.util.Collection;
022import java.util.Objects;
023import java.util.stream.Collectors;
024import java.util.stream.Stream;
025
026/**
027 * Container object for audio, video, image and file message payload contents.
028 * This class also validates the fields.
029 */
030public class MessagePayload extends JsonableBaseObject {
031        protected URI url;
032        protected String caption, name;
033
034        public MessagePayload(String url) {
035                this.url = URI.create(Objects.requireNonNull(url, "URL is required."));
036        }
037
038        public MessagePayload(String url, String caption) {
039                this(url);
040                if ((this.caption = caption) != null && caption.isEmpty()) {
041                        throw new IllegalArgumentException("Caption cannot be blank.");
042                }
043        }
044
045        public MessagePayload(String url, String caption, String name) {
046                this(url, caption);
047                this.name = name;
048        }
049
050        @JsonProperty("url")
051        public URI getUrl() {
052                return url;
053        }
054
055        @JsonProperty("caption")
056        public String getCaption() {
057                return caption;
058        }
059
060        @JsonProperty("name")
061        public String getName() {
062                return name;
063        }
064
065        public static void validateExtension(String path, String... allowed) {
066                int lastDot = path.lastIndexOf('.');
067                if (lastDot < 1) return;
068                String ext = path.substring(lastDot+1);
069                Collection<String> extensions = Stream.of(allowed)
070                                .map(s -> s.startsWith(".") ? s.substring(1) : s)
071                                .collect(Collectors.toSet());
072                if (!extensions.contains(ext)) {
073                        throw new IllegalArgumentException("Invalid extension: '"+ext+"'. Should be one of "+extensions+'.');
074                }
075        }
076
077        public void validateUrlExtension(String... allowed) {
078                validateExtension(url.getPath(), allowed);
079        }
080
081        public void validateCaptionLength(int max) {
082                if (caption == null) return;
083                if (caption.length() > max) {
084                        throw new IllegalArgumentException("Caption must be less than "+max+" characters.");
085                }
086        }
087
088        public void validateUrlLength(int min, int max) {
089                if (url == null) return;
090                int length = getUrl().toString().length();
091                if (length < min) {
092                        throw new IllegalArgumentException("URL must be longer than "+min+" characters.");
093                }
094                if (length > max) {
095                        throw new IllegalArgumentException("URL must be less than "+max+" characters.");
096                }
097        }
098}