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.server;
20  
21  import java.security.Principal;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.servlet.http.Cookie;
32  import javax.servlet.http.HttpServletRequest;
33  
34  import org.eclipse.jetty.websocket.api.UpgradeRequest;
35  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
36  import org.eclipse.jetty.websocket.api.util.QuoteUtil;
37  
38  public class ServletWebSocketRequest extends UpgradeRequest
39  {
40      private Map<String, String> cookieMap;
41      private HttpServletRequest req;
42  
43      public ServletWebSocketRequest(HttpServletRequest request)
44      {
45          super(request.getRequestURI());
46          this.req = request;
47  
48          // Copy Request Line Details
49          setMethod(request.getMethod());
50          setHttpVersion(request.getProtocol());
51  
52          // Copy parameters
53          super.setParameterMap(request.getParameterMap());
54  
55          // Copy Cookies
56          cookieMap = new HashMap<String, String>();
57          for (Cookie cookie : request.getCookies())
58          {
59              cookieMap.put(cookie.getName(),cookie.getValue());
60          }
61  
62          // Copy Headers
63          Enumeration<String> headerNames = request.getHeaderNames();
64          while (headerNames.hasMoreElements())
65          {
66              String name = headerNames.nextElement();
67              Enumeration<String> valuesEnum = request.getHeaders(name);
68              List<String> values = new ArrayList<>();
69              while (valuesEnum.hasMoreElements())
70              {
71                  values.add(valuesEnum.nextElement());
72              }
73              setHeader(name,values);
74          }
75  
76          // Parse Sub Protocols
77          Enumeration<String> protocols = request.getHeaders("Sec-WebSocket-Protocol");
78          List<String> subProtocols = new ArrayList<>();
79          String protocol = null;
80          while ((protocol == null) && (protocols != null) && protocols.hasMoreElements())
81          {
82              String candidate = protocols.nextElement();
83              for (String p : parseProtocols(candidate))
84              {
85                  subProtocols.add(p);
86              }
87          }
88          setSubProtocols(subProtocols);
89  
90          // Parse Extension Configurations
91          Enumeration<String> e = request.getHeaders("Sec-WebSocket-Extensions");
92          while (e.hasMoreElements())
93          {
94              Iterator<String> extTokenIter = QuoteUtil.splitAt(e.nextElement(),",");
95              while (extTokenIter.hasNext())
96              {
97                  String extToken = extTokenIter.next();
98                  ExtensionConfig config = ExtensionConfig.parse(extToken);
99                  addExtensions(config);
100             }
101         }
102     }
103 
104     public Principal getPrincipal()
105     {
106         return req.getUserPrincipal();
107     }
108 
109     public StringBuffer getRequestURL()
110     {
111         return req.getRequestURL();
112     }
113 
114     public Map<String, Object> getServletAttributes()
115     {
116         Map<String, Object> attributes = new HashMap<String, Object>();
117 
118         for (String name : Collections.list(req.getAttributeNames()))
119         {
120             attributes.put(name,req.getAttribute(name));
121         }
122 
123         return attributes;
124     }
125 
126     public Map<String, List<String>> getServletParameters()
127     {
128         Map<String, List<String>> parameters = new HashMap<String, List<String>>();
129 
130         for (String name : Collections.list(req.getParameterNames()))
131         {
132             parameters.put(name,Collections.unmodifiableList(Arrays.asList(req.getParameterValues(name))));
133         }
134 
135         return parameters;
136     }
137 
138     /**
139      * Return the HttpSession if it exists.
140      * <p>
141      * Note: this is equivalent to {@link HttpServletRequest#getSession()} and will not create a new HttpSession.
142      */
143     @Override
144     public Object getSession()
145     {
146         return this.req.getSession();
147     }
148 
149     protected String[] parseProtocols(String protocol)
150     {
151         if (protocol == null)
152         {
153             return new String[]
154             { null };
155         }
156         protocol = protocol.trim();
157         if ((protocol == null) || (protocol.length() == 0))
158         {
159             return new String[]
160             { null };
161         }
162         String[] passed = protocol.split("\\s*,\\s*");
163         String[] protocols = new String[passed.length + 1];
164         System.arraycopy(passed,0,protocols,0,passed.length);
165         return protocols;
166     }
167 
168     public void setServletAttribute(String name, Object o)
169     {
170         this.req.setAttribute(name,o);
171     }
172 }