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.JsonProperty;
019import com.fasterxml.jackson.annotation.JsonTypeInfo;
020import java.net.URI;
021import java.util.Objects;
022
023/**
024 * Represents a Session Initiation Protocol (SIP) channel.
025 */
026@JsonTypeInfo(use = JsonTypeInfo.Id.NONE, visible = true)
027public class Sip extends Channel {
028        private URI uri;
029        private String username, password;
030
031        protected Sip() {}
032
033        public Sip(String uri) {
034                Objects.requireNonNull(uri, "SIP URI is required");
035                if (!(uri.startsWith("sip:") || uri.startsWith("sips:"))) {
036                        throw new IllegalArgumentException("Invalid protocol for SIP URI.");
037                }
038                this.uri = URI.create(uri);
039        }
040
041        public Sip(String uri, String username, String password) {
042                this(uri);
043                this.username = username;
044                if ((this.password = password) != null && !password.isEmpty() && username == null) {
045                        throw new IllegalArgumentException("Username should be provided along with password.");
046                }
047        }
048
049        /**
050         * Full SIP URI, including number, domain and (optionally) whether TLS is used.
051         *
052         * @return The SIP URI, or {@code null} if not specified.
053         */
054        @JsonProperty("uri")
055        public URI getUri() {
056                return uri;
057        }
058
059        /**
060         * SIP username.
061         *
062         * @return The username, or {@code null} if not specified.
063         */
064        @JsonProperty("username")
065        public String getUsername() {
066                return username;
067        }
068
069        /**
070         * SIP user password.
071         *
072         * @return The password, or {@code null} if not specified.
073         */
074        @JsonProperty("password")
075        public String getPassword() {
076                return password;
077        }
078}