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.users.channels;
017
018import com.fasterxml.jackson.annotation.*;
019import java.net.URI;
020import java.util.Map;
021import java.util.Objects;
022
023/**
024 * Represents a Websocket channel.
025 */
026@JsonTypeInfo(use = JsonTypeInfo.Id.NONE, visible = true)
027public class Websocket extends Channel {
028        private URI uri;
029        private ContentType contentType;
030        private Map<String, ?> headers;
031
032        protected Websocket() {}
033
034        public Websocket(String uri) {
035                Objects.requireNonNull(uri, "Websocket URI is required");
036                if (!(uri.startsWith("ws://") || uri.startsWith("wss://"))) {
037                        throw new IllegalArgumentException("Invalid websocket URI protocol.");
038                }
039                this.uri = URI.create(uri);
040        }
041
042        public Websocket(String uri, ContentType contentType, Map<String, ?> headers) {
043                this(uri);
044                this.contentType = contentType;
045                this.headers = headers;
046        }
047
048        /**
049         * Full URI of the websocket.
050         *
051         * @return The websocket URI, or {@code null} if not set.
052         */
053        @JsonProperty("uri")
054        public URI getUri() {
055                return uri;
056        }
057
058        /**
059         * MIME type of the content sent over the websocket.
060         *
061         * @return The media content type as an enum, or {@code null} if unknown.
062         */
063        @JsonProperty("content-type")
064        public ContentType getContentType() {
065                return contentType;
066        }
067
068        /**
069         * Custom properties to add to the header.
070         *
071         * @return Header properties as a Map, or {@code null} if not set.
072         */
073        @JsonProperty("headers")
074        public Map<String, ?> getHeaders() {
075                return headers;
076        }
077
078        /**
079         * Represents the possible content types for a Websocket.
080         */
081        public enum ContentType {
082                /**
083                 * audio/l16 with bitrate of 8000.
084                 */
085                AUDIO_L16_8K("audio/l16;rate=8000"),
086
087                /**
088                 * audio/l16 with bitrate of 16000.
089                 */
090                AUDIO_L16_16K("audio/l16;rate=16000");
091
092                private final String value;
093
094                ContentType(String ser) {
095                        this.value = ser;
096                }
097
098                @JsonValue
099                @Override
100                public String toString() {
101                        return value;
102                }
103
104                @JsonCreator
105                public static ContentType fromString(String value) {
106                        if (value == null) return null;
107                        switch (value.toLowerCase()) {
108                                case ("audio/l16;rate=8000"): return AUDIO_L16_8K;
109                                case ("audio/l16;rate=16000"): return AUDIO_L16_16K;
110                                default: throw new IllegalArgumentException("Unknown content-type: "+value);
111                        }
112                }
113        }
114}