View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.api;
20  
21  import java.net.HttpCookie;
22  import java.net.URI;
23  import java.security.Principal;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
32  import org.eclipse.jetty.websocket.api.util.QuoteUtil;
33  
34  public class UpgradeRequest
35  {
36      private URI requestURI;
37      private List<String> subProtocols = new ArrayList<>(1);
38      private List<ExtensionConfig> extensions = new ArrayList<>(1);
39      private List<HttpCookie> cookies = new ArrayList<>(1);
40      private Map<String, List<String>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
41      private Map<String, List<String>> parameters = new HashMap<>(1);
42      private Object session;
43      private String httpVersion;
44      private String method;
45      private String host;
46      private boolean secure;
47  
48      protected UpgradeRequest()
49      {
50          /* anonymous, no requestURI, upgrade request */
51      }
52  
53      public UpgradeRequest(String requestURI)
54      {
55          this(URI.create(requestURI));
56      }
57  
58      public UpgradeRequest(URI requestURI)
59      {
60          setRequestURI(requestURI);
61      }
62  
63      public void addExtensions(ExtensionConfig... configs)
64      {
65          Collections.addAll(extensions, configs);
66      }
67  
68      public void addExtensions(String... configs)
69      {
70          for (String config : configs)
71          {
72              extensions.add(ExtensionConfig.parse(config));
73          }
74      }
75  
76      public void clearHeaders()
77      {
78          headers.clear();
79      }
80  
81      public List<HttpCookie> getCookies()
82      {
83          return cookies;
84      }
85  
86      public List<ExtensionConfig> getExtensions()
87      {
88          return extensions;
89      }
90  
91      public String getHeader(String name)
92      {
93          List<String> values = headers.get(name);
94          // no value list
95          if (values == null)
96          {
97              return null;
98          }
99          int size = values.size();
100         // empty value list
101         if (size <= 0)
102         {
103             return null;
104         }
105         // simple return
106         if (size == 1)
107         {
108             return values.get(0);
109         }
110         // join it with commas
111         boolean needsDelim = false;
112         StringBuilder ret = new StringBuilder();
113         for (String value : values)
114         {
115             if (needsDelim)
116             {
117                 ret.append(", ");
118             }
119             QuoteUtil.quoteIfNeeded(ret,value,QuoteUtil.ABNF_REQUIRED_QUOTING);
120             needsDelim = true;
121         }
122         return ret.toString();
123     }
124 
125     public int getHeaderInt(String name)
126     {
127         List<String> values = headers.get(name);
128         // no value list
129         if (values == null)
130         {
131             return -1;
132         }
133         int size = values.size();
134         // empty value list
135         if (size <= 0)
136         {
137             return -1;
138         }
139         // simple return
140         if (size == 1)
141         {
142             return Integer.parseInt(values.get(0));
143         }
144         throw new NumberFormatException("Cannot convert multi-value header into int");
145     }
146 
147     public Map<String, List<String>> getHeaders()
148     {
149         return headers;
150     }
151 
152     public List<String> getHeaders(String name)
153     {
154         return headers.get(name);
155     }
156 
157     public String getHost()
158     {
159         return host;
160     }
161 
162     public String getHttpVersion()
163     {
164         return httpVersion;
165     }
166 
167     public String getMethod()
168     {
169         return method;
170     }
171 
172     public String getOrigin()
173     {
174         return getHeader("Origin");
175     }
176 
177     /**
178      * Returns a map of the query parameters of the request.
179      * 
180      * @return a unmodifiable map of query parameters of the request.
181      */
182     public Map<String, List<String>> getParameterMap()
183     {
184         return Collections.unmodifiableMap(parameters);
185     }
186 
187     public String getProtocolVersion()
188     {
189         String version = getHeader("Sec-WebSocket-Version");
190         if (version == null)
191         {
192             return "13"; // Default
193         }
194         return version;
195     }
196 
197     public String getQueryString()
198     {
199         return requestURI.getQuery();
200     }
201 
202     public URI getRequestURI()
203     {
204         return requestURI;
205     }
206 
207     /**
208      * Access the Servlet HTTP Session (if present)
209      * <p>
210      * Note: Never present on a Client UpgradeRequest.
211      * 
212      * @return the Servlet HTTPSession on server side UpgradeRequests
213      */
214     public Object getSession()
215     {
216         return session;
217     }
218 
219     public List<String> getSubProtocols()
220     {
221         return subProtocols;
222     }
223 
224     /**
225      * Get the User Principal for this request.
226      * <p>
227      * Only applicable when using UpgradeRequest from server side.
228      * 
229      * @return the user principal
230      */
231     public Principal getUserPrincipal()
232     {
233         // Server side should override to implement
234         return null;
235     }
236 
237     public boolean hasSubProtocol(String test)
238     {
239         for (String protocol : subProtocols)
240         {
241             if (protocol.equalsIgnoreCase(test))
242             {
243                 return true;
244             }
245         }
246         return false;
247     }
248 
249     public boolean isOrigin(String test)
250     {
251         return test.equalsIgnoreCase(getOrigin());
252     }
253 
254     public boolean isSecure()
255     {
256         return secure;
257     }
258 
259     public void setCookies(List<HttpCookie> cookies)
260     {
261         this.cookies.clear();
262         if (cookies != null && !cookies.isEmpty())
263         {
264             this.cookies.addAll(cookies);
265         }
266     }
267     
268     public void setExtensions(List<ExtensionConfig> configs)
269     {
270         this.extensions.clear();
271         if (configs != null)
272         {
273             this.extensions.addAll(configs);
274         }
275     }
276 
277     public void setHeader(String name, List<String> values)
278     {
279         headers.put(name,values);
280     }
281 
282     public void setHeader(String name, String value)
283     {
284         List<String> values = new ArrayList<>();
285         values.add(value);
286         setHeader(name,values);
287     }
288 
289     public void setHeaders(Map<String, List<String>> headers)
290     {
291         clearHeaders();
292 
293         for (Map.Entry<String, List<String>> entry : headers.entrySet())
294         {
295             String name = entry.getKey();
296             List<String> values = entry.getValue();
297             setHeader(name,values);
298         }
299     }
300 
301     public void setHttpVersion(String httpVersion)
302     {
303         this.httpVersion = httpVersion;
304     }
305 
306     public void setMethod(String method)
307     {
308         this.method = method;
309     }
310 
311     protected void setParameterMap(Map<String, List<String>> parameters)
312     {
313         this.parameters.clear();
314         this.parameters.putAll(parameters);
315     }
316 
317     public void setRequestURI(URI uri)
318     {
319         this.requestURI = uri;
320         String scheme = uri.getScheme();
321         if ("ws".equalsIgnoreCase(scheme))
322         {
323             secure = false;
324         }
325         else if ("wss".equalsIgnoreCase(scheme))
326         {
327             secure = true;
328         }
329         else
330         {
331             throw new IllegalArgumentException("URI scheme must be 'ws' or 'wss'");
332         }
333         this.host = this.requestURI.getHost();
334         this.parameters.clear();
335     }
336 
337     public void setSession(Object session)
338     {
339         this.session = session;
340     }
341 
342     public void setSubProtocols(List<String> subProtocols)
343     {
344         this.subProtocols.clear();
345         if (subProtocols != null)
346         {
347             this.subProtocols.addAll(subProtocols);
348         }
349     }
350 
351     /**
352      * Set Sub Protocol request list.
353      * 
354      * @param protocols
355      *            the sub protocols desired
356      */
357     public void setSubProtocols(String... protocols)
358     {
359         subProtocols.clear();
360         Collections.addAll(subProtocols, protocols);
361     }
362 }