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