1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket.api;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
29 import org.eclipse.jetty.websocket.api.util.QuoteUtil;
30
31 public class UpgradeResponse
32 {
33 public static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol";
34 private int statusCode;
35 private String statusReason;
36 private Map<String, List<String>> headers = new HashMap<>();
37 private List<ExtensionConfig> extensions = new ArrayList<>();
38 private boolean success = false;
39
40 public void addHeader(String name, String value)
41 {
42 String key = name.toLowerCase();
43 List<String> values = headers.get(key);
44 if (values == null)
45 {
46 values = new ArrayList<>();
47 }
48 values.add(value);
49 headers.put(key,values);
50 }
51
52
53
54
55
56
57 public String getAcceptedSubProtocol()
58 {
59 return getHeader(SEC_WEBSOCKET_PROTOCOL);
60 }
61
62
63
64
65
66
67 public List<ExtensionConfig> getExtensions()
68 {
69 return extensions;
70 }
71
72 public String getHeader(String name)
73 {
74 List<String> values = getHeaders(name);
75
76 if (values == null)
77 {
78 return null;
79 }
80 int size = values.size();
81
82 if (size <= 0)
83 {
84 return null;
85 }
86
87 if (size == 1)
88 {
89 return values.get(0);
90 }
91
92 boolean needsDelim = false;
93 StringBuilder ret = new StringBuilder();
94 for (String value : values)
95 {
96 if (needsDelim)
97 {
98 ret.append(", ");
99 }
100 QuoteUtil.quoteIfNeeded(ret,value,QuoteUtil.ABNF_REQUIRED_QUOTING);
101 needsDelim = true;
102 }
103 return ret.toString();
104 }
105
106 public Set<String> getHeaderNames()
107 {
108 return headers.keySet();
109 }
110
111 public Map<String, List<String>> getHeaders()
112 {
113 return headers;
114 }
115
116 public List<String> getHeaders(String name)
117 {
118 return headers.get(name.toLowerCase());
119 }
120
121 public int getStatusCode()
122 {
123 return statusCode;
124 }
125
126 public String getStatusReason()
127 {
128 return statusReason;
129 }
130
131 public boolean isSuccess()
132 {
133 return success;
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147 public void sendForbidden(String message) throws IOException
148 {
149 throw new UnsupportedOperationException("Not supported");
150 }
151
152
153
154
155
156
157
158 public void setAcceptedSubProtocol(String protocol)
159 {
160 setHeader(SEC_WEBSOCKET_PROTOCOL,protocol);
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178 public void setExtensions(List<ExtensionConfig> extensions)
179 {
180 this.extensions.clear();
181 if (extensions != null)
182 {
183 this.extensions.addAll(extensions);
184 }
185 }
186
187 public void setHeader(String name, String value)
188 {
189 List<String> values = new ArrayList<>();
190 values.add(value);
191 headers.put(name.toLowerCase(),values);
192 }
193
194 public void setStatusCode(int statusCode)
195 {
196 this.statusCode = statusCode;
197 }
198
199 public void setStatusReason(String statusReason)
200 {
201 this.statusReason = statusReason;
202 }
203
204 public void setSuccess(boolean success)
205 {
206 this.success = success;
207 }
208 }