001/*
002 * Copyright (c) 2011-2018 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.voice.ncco;
023
024import com.fasterxml.jackson.annotation.JsonInclude;
025import com.fasterxml.jackson.annotation.JsonProperty;
026
027import java.util.HashMap;
028import java.util.Map;
029
030/**
031 * Represents a web socket endpoint used in a {@link ConnectAction}
032 */
033@JsonInclude(value = JsonInclude.Include.NON_NULL)
034public class WebSocketEndpoint implements Endpoint {
035    private static final String TYPE = "websocket";
036
037    private String uri;
038    private String contentType;
039    private Map<String, String> headers;
040
041    private WebSocketEndpoint(Builder builder) {
042        this.uri = builder.uri;
043        this.contentType = builder.contentType;
044        this.headers = builder.headers;
045    }
046
047    public String getUri() {
048        return uri;
049    }
050
051    @JsonProperty("content-type")
052    public String getContentType() {
053        return contentType;
054    }
055
056    public Map<String, String> getHeaders() {
057        return headers;
058    }
059
060    @Override
061    public String getType() {
062        return TYPE;
063    }
064
065    public static Builder builder(String uri, String contentType) {
066        return new Builder(uri, contentType);
067    }
068
069    public static class Builder {
070        private String uri;
071        private String contentType;
072        private Map<String, String> headers;
073
074        public Builder(String uri, String contentType) {
075            this.uri = uri;
076            this.contentType = contentType;
077        }
078
079        public Builder uri(String uri) {
080            this.uri = uri;
081            return this;
082        }
083
084        public Builder contentType(String contentType) {
085            this.contentType = contentType;
086            return this;
087        }
088
089        public Builder headers(Map<String, String> headers) {
090            this.headers = headers;
091            return this;
092        }
093
094        public Builder headers(String... entries) {
095            // TODO: Replace with Map.of when we target Java 9
096            if (entries.length % 2 != 0) {
097                throw new IllegalArgumentException("Entries must be key, value and every key must have a value.");
098            }
099
100            Map<String, String> headers = new HashMap<>();
101            for (int i = 0; i < entries.length - 1; i += 2) {
102                headers.put(entries[i], entries[i + 1]);
103            }
104
105            return headers(headers);
106        }
107
108        public WebSocketEndpoint build() {
109            return new WebSocketEndpoint(this);
110        }
111    }
112}