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.voice;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.JsonableBaseObject;
020import com.vonage.client.users.channels.Sip;
021import static com.vonage.client.voice.SipHeader.USER_TO_USER;
022import java.util.Collections;
023import java.util.LinkedHashMap;
024import java.util.Map;
025
026/**
027 * Endpoint for connecting to a SIP URI.
028 */
029public class SipEndpoint extends JsonableBaseObject implements Endpoint {
030    private String uri;
031    private Map<String, ?> headers;
032    private Map<SipHeader, String> standardHeaders;
033
034    protected SipEndpoint() {
035    }
036
037    /**
038     * Creates a new SIP endpoint request.
039     *
040     * @param uri (REQUIRED) SIP URI to connect to.
041     */
042    public SipEndpoint(String uri) {
043        this(uri, null, null);
044    }
045
046    /**
047     * Creates a new SIP endpoint request.
048     *
049     * @param uri (REQUIRED) SIP URI to connect to.
050     * @param userToUserHeader (OPTIONAL) The User-to-User header, as per RFC 7433.
051     * @since 8.9.0
052     */
053    public SipEndpoint(String uri, String userToUserHeader) {
054        this(uri, null, userToUserHeader);
055    }
056
057    /**
058     * Creates a new SIP endpoint request.
059     *
060     * @param uri (REQUIRED) URI of the websocket to connect to.
061     * @param headers (OPTIONAL) Custom headers to include.
062     * @since 8.9.0
063     */
064    public SipEndpoint(String uri, Map<String, ?> headers) {
065        this(uri, headers, null);
066    }
067
068    /**
069     * Creates a new SIP endpoint request.
070     *
071     * @param uri (REQUIRED) SIP URI to connect to.
072     * @param headers (OPTIONAL) Custom headers to include.
073     * @param userToUserHeader (OPTIONAL) The User-to-User header, as per RFC 7433.
074     * @since 8.9.0
075     */
076    public SipEndpoint(String uri, Map<String, ?> headers, String userToUserHeader) {
077        this.uri = uri;
078        this.headers = headers;
079        if (userToUserHeader != null) {
080            standardHeaders = Collections.singletonMap(USER_TO_USER, userToUserHeader);
081        }
082    }
083
084    /**
085     * SIP URI to connect to.
086     *
087     * @return The SIP URI as a String.
088     */
089    @JsonProperty("uri")
090    public String getUri() {
091        return uri;
092    }
093
094    @Override
095    public String getType() {
096        return EndpointType.SIP.toString();
097    }
098
099    /**
100     * Defines custom headers to be sent as part of the SIP INVITE request.
101     * All keys will be prepended with the {@code X-} prefix.
102     *
103     * @return The custom headers as a Map, or {@code null} if unspecified.
104     * @since 8.9.0
105     */
106    @JsonProperty("headers")
107    public Map<String, ?> getHeaders() {
108        return headers;
109    }
110
111    /**
112     * Headers that are RFC standards, i.e. not prepended with {@code X-}.
113     *
114     * @return The standard headers, or {@code null} if unspecified.
115     * @since 8.9.0
116     */
117    @JsonProperty("standard_headers")
118    public Map<SipHeader, String> getStandardHeaders() {
119        return standardHeaders;
120    }
121
122    @Override
123    public String toLog() {
124        return uri;
125    }
126}