View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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         this.cookies.addAll(cookies);
263     }
264 
265     public void setExtensions(List<ExtensionConfig> configs)
266     {
267         this.extensions.clear();
268         if (configs != null)
269         {
270             this.extensions.addAll(configs);
271         }
272     }
273 
274     public void setHeader(String name, List<String> values)
275     {
276         headers.put(name,values);
277     }
278 
279     public void setHeader(String name, String value)
280     {
281         List<String> values = new ArrayList<>();
282         values.add(value);
283         setHeader(name,values);
284     }
285 
286     public void setHeaders(Map<String, List<String>> headers)
287     {
288         clearHeaders();
289 
290         for (Map.Entry<String, List<String>> entry : headers.entrySet())
291         {
292             String name = entry.getKey();
293             List<String> values = entry.getValue();
294             setHeader(name,values);
295         }
296     }
297 
298     public void setHttpVersion(String httpVersion)
299     {
300         this.httpVersion = httpVersion;
301     }
302 
303     public void setMethod(String method)
304     {
305         this.method = method;
306     }
307 
308     protected void setParameterMap(Map<String, List<String>> parameters)
309     {
310         this.parameters.clear();
311         this.parameters.putAll(parameters);
312     }
313 
314     public void setRequestURI(URI uri)
315     {
316         this.requestURI = uri;
317         String scheme = uri.getScheme();
318         if ("ws".equalsIgnoreCase(scheme))
319         {
320             secure = false;
321         }
322         else if ("wss".equalsIgnoreCase(scheme))
323         {
324             secure = true;
325         }
326         else
327         {
328             throw new IllegalArgumentException("URI scheme must be 'ws' or 'wss'");
329         }
330         this.host = this.requestURI.getHost();
331         this.parameters.clear();
332     }
333 
334     public void setSession(Object session)
335     {
336         this.session = session;
337     }
338 
339     public void setSubProtocols(List<String> subProtocols)
340     {
341         this.subProtocols.clear();
342         if (subProtocols != null)
343         {
344             this.subProtocols.addAll(subProtocols);
345         }
346     }
347 
348     /**
349      * Set Sub Protocol request list.
350      * 
351      * @param protocols
352      *            the sub protocols desired
353      */
354     public void setSubProtocols(String... protocols)
355     {
356         subProtocols.clear();
357         Collections.addAll(subProtocols, protocols);
358     }
359 }