001/* 002 * Copyright 2020 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.ncco; 017 018import com.fasterxml.jackson.annotation.JsonInclude; 019import com.fasterxml.jackson.annotation.JsonProperty; 020 021import java.util.HashMap; 022import java.util.Map; 023 024/** 025 * Represents a web socket endpoint used in a {@link ConnectAction} 026 */ 027@JsonInclude(value = JsonInclude.Include.NON_NULL) 028public class WebSocketEndpoint implements Endpoint { 029 private static final String TYPE = "websocket"; 030 031 private String uri; 032 private String contentType; 033 private Map<String, String> headers; 034 035 private WebSocketEndpoint(Builder builder) { 036 this.uri = builder.uri; 037 this.contentType = builder.contentType; 038 this.headers = builder.headers; 039 } 040 041 public String getUri() { 042 return uri; 043 } 044 045 @JsonProperty("content-type") 046 public String getContentType() { 047 return contentType; 048 } 049 050 public Map<String, String> getHeaders() { 051 return headers; 052 } 053 054 @Override 055 public String getType() { 056 return TYPE; 057 } 058 059 public static Builder builder(String uri, String contentType) { 060 return new Builder(uri, contentType); 061 } 062 063 public static class Builder { 064 private String uri; 065 private String contentType; 066 private Map<String, String> headers; 067 068 public Builder(String uri, String contentType) { 069 this.uri = uri; 070 this.contentType = contentType; 071 } 072 073 public Builder uri(String uri) { 074 this.uri = uri; 075 return this; 076 } 077 078 public Builder contentType(String contentType) { 079 this.contentType = contentType; 080 return this; 081 } 082 083 public Builder headers(Map<String, String> headers) { 084 this.headers = headers; 085 return this; 086 } 087 088 public Builder headers(String... entries) { 089 // TODO: Replace with Map.of when we target Java 9 090 if (entries.length % 2 != 0) { 091 throw new IllegalArgumentException("Entries must be key, value and every key must have a value."); 092 } 093 094 Map<String, String> headers = new HashMap<>(); 095 for (int i = 0; i < entries.length - 1; i += 2) { 096 headers.put(entries[i], entries[i + 1]); 097 } 098 099 return headers(headers); 100 } 101 102 public WebSocketEndpoint build() { 103 return new WebSocketEndpoint(this); 104 } 105 } 106}