View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  
30  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
31  import org.eclipse.jetty.websocket.api.util.QuoteUtil;
32  
33  public class UpgradeRequest
34  {
35      private URI requestURI;
36      private List<String> subProtocols = new ArrayList<>();
37      private List<ExtensionConfig> extensions = new ArrayList<>();
38      private List<HttpCookie> cookies = new ArrayList<>();
39      private Map<String, List<String>> headers = new HashMap<>();
40      private Map<String, String[]> parameters = new HashMap<>();
41      private Object session;
42      private String httpVersion;
43      private String method;
44      private String host;
45      private boolean secure = false;
46  
47      protected UpgradeRequest()
48      {
49          /* anonymous, no requestURI, upgrade request */
50      }
51  
52      public UpgradeRequest(String requestURI)
53      {
54          this(URI.create(requestURI));
55      }
56  
57      public UpgradeRequest(URI requestURI)
58      {
59          this();
60          setRequestURI(requestURI);
61      }
62  
63      public void addExtensions(ExtensionConfig... configs)
64      {
65          for (ExtensionConfig config : configs)
66          {
67              extensions.add(config);
68          }
69      }
70  
71      public void addExtensions(String... configs)
72      {
73          for (String config : configs)
74          {
75              extensions.add(ExtensionConfig.parse(config));
76          }
77      }
78  
79      public List<HttpCookie> getCookies()
80      {
81          return cookies;
82      }
83  
84      public List<ExtensionConfig> getExtensions()
85      {
86          return extensions;
87      }
88  
89      public String getHeader(String name)
90      {
91          List<String> values = headers.get(name.toLowerCase(Locale.ENGLISH));
92          // no value list
93          if (values == null)
94          {
95              return null;
96          }
97          int size = values.size();
98          // empty value list
99          if (size <= 0)
100         {
101             return null;
102         }
103         // simple return
104         if (size == 1)
105         {
106             return values.get(0);
107         }
108         // join it with commas
109         boolean needsDelim = false;
110         StringBuilder ret = new StringBuilder();
111         for (String value : values)
112         {
113             if (needsDelim)
114             {
115                 ret.append(", ");
116             }
117             QuoteUtil.quoteIfNeeded(ret,value,QuoteUtil.ABNF_REQUIRED_QUOTING);
118             needsDelim = true;
119         }
120         return ret.toString();
121     }
122 
123     public int getHeaderInt(String name)
124     {
125         List<String> values = headers.get(name.toLowerCase(Locale.ENGLISH));
126         // no value list
127         if (values == null)
128         {
129             return -1;
130         }
131         int size = values.size();
132         // empty value list
133         if (size <= 0)
134         {
135             return -1;
136         }
137         // simple return
138         if (size == 1)
139         {
140             return Integer.parseInt(values.get(0));
141         }
142         throw new NumberFormatException("Cannot convert multi-value header into int");
143     }
144 
145     public Map<String, List<String>> getHeaders()
146     {
147         return headers;
148     }
149 
150     public List<String> getHeaders(String name)
151     {
152         return headers.get(name);
153     }
154 
155     public String getHost()
156     {
157         return host;
158     }
159 
160     public String getHttpVersion()
161     {
162         return httpVersion;
163     }
164 
165     public String getMethod()
166     {
167         return method;
168     }
169 
170     public String getOrigin()
171     {
172         return getHeader("Origin");
173     }
174 
175     /**
176      * Returns a map of the query parameters of the request.
177      * 
178      * @return a unmodifiable map of query parameters of the request.
179      */
180     public Map<String, String[]> getParameterMap()
181     {
182         return Collections.unmodifiableMap(parameters);
183     }
184     
185     public String getProtocolVersion()
186     {
187         String version = getHeader("Sec-WebSocket-Version");
188         if (version == null)
189         {
190             return "13"; // Default
191         }
192         return version;
193     }
194 
195     public String getQueryString()
196     {
197         return requestURI.getQuery();
198     }
199 
200     public URI getRequestURI()
201     {
202         return requestURI;
203     }
204 
205     /**
206      * Access the Servlet HTTP Session (if present)
207      * <p>
208      * Note: Never present on a Client UpgradeRequest.
209      * 
210      * @return the Servlet HTTPSession on server side UpgradeRequests
211      */
212     public Object getSession()
213     {
214         return session;
215     }
216 
217     public List<String> getSubProtocols()
218     {
219         return subProtocols;
220     }
221 
222     public boolean hasSubProtocol(String test)
223     {
224         for (String protocol : subProtocols)
225         {
226             if (protocol.equalsIgnoreCase(test))
227             {
228                 return true;
229             }
230         }
231         return false;
232     }
233 
234     public boolean isOrigin(String test)
235     {
236         return test.equalsIgnoreCase(getOrigin());
237     }
238 
239     public boolean isSecure()
240     {
241         return secure;
242     }
243 
244     public void setCookies(List<HttpCookie> cookies)
245     {
246         this.cookies = cookies;
247     }
248 
249     public void setHeader(String name, List<String> values)
250     {
251         headers.put(name.toLowerCase(Locale.ENGLISH),values);
252     }
253 
254     public void setHeader(String name, String value)
255     {
256         List<String> values = new ArrayList<>();
257         values.add(value);
258         setHeader(name.toLowerCase(Locale.ENGLISH),values);
259     }
260 
261     public void setHttpVersion(String httpVersion)
262     {
263         this.httpVersion = httpVersion;
264     }
265 
266     public void setMethod(String method)
267     {
268         this.method = method;
269     }
270 
271     protected void setParameterMap(Map<String, String[]> parameters)
272     {
273         this.parameters.clear();
274         this.parameters.putAll(parameters);
275     }
276 
277     public void setRequestURI(URI uri)
278     {
279         this.requestURI = uri;
280         String scheme = uri.getScheme();
281         if ("ws".equalsIgnoreCase(scheme))
282         {
283             secure = false;
284         }
285         else if ("wss".equalsIgnoreCase(scheme))
286         {
287             secure = true;
288         }
289         else
290         {
291             throw new IllegalArgumentException("URI scheme must be 'ws' or 'wss'");
292         }
293         this.host = this.requestURI.getHost();
294         this.parameters.clear();
295     }
296 
297     public void setSession(Object session)
298     {
299         this.session = session;
300     }
301 
302     public void setSubProtocols(List<String> subProtocols)
303     {
304         this.subProtocols.clear();
305         if (subProtocols != null)
306         {
307             this.subProtocols.addAll(subProtocols);
308         }
309     }
310 
311     /**
312      * Set Sub Protocol request list.
313      * 
314      * @param protocols
315      *            the sub protocols desired
316      */
317     public void setSubProtocols(String... protocols)
318     {
319         this.subProtocols.clear();
320         for (String protocol : protocols)
321         {
322             this.subProtocols.add(protocol);
323         }
324     }
325 }