View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.servlet;
20  
21  import java.io.BufferedReader;
22  import java.io.IOException;
23  import java.io.UnsupportedEncodingException;
24  import java.net.InetSocketAddress;
25  import java.security.Principal;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.Enumeration;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Locale;
33  import java.util.Map;
34  import java.util.TreeMap;
35  
36  import javax.servlet.AsyncContext;
37  import javax.servlet.DispatcherType;
38  import javax.servlet.RequestDispatcher;
39  import javax.servlet.ServletContext;
40  import javax.servlet.ServletException;
41  import javax.servlet.ServletInputStream;
42  import javax.servlet.ServletRequest;
43  import javax.servlet.ServletResponse;
44  import javax.servlet.http.Cookie;
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletResponse;
47  import javax.servlet.http.HttpSession;
48  import javax.servlet.http.HttpUpgradeHandler;
49  import javax.servlet.http.Part;
50  
51  public class UpgradeHttpServletRequest implements HttpServletRequest
52  {
53      private static final String UNSUPPORTED_WITH_WEBSOCKET_UPGRADE = "Feature unsupported with a Upgraded to WebSocket HttpServletRequest";
54  
55      private HttpServletRequest request;
56      private final ServletContext context;
57      private final DispatcherType dispatcher;
58      private final String method;
59      private final String protocol;
60      private final String scheme;
61      private final boolean secure;
62      private final String requestURI;
63      private final StringBuffer requestURL;
64      private final String pathInfo;
65      private final String pathTranslated;
66      private final String servletPath;
67      private final String query;
68      private final String authType;
69      private final Cookie[] cookies;
70      private final String remoteUser;
71      private final Principal principal;
72  
73      private final Map<String, List<String>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
74      private final Map<String, String[]> parameters = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
75      private final Map<String, Object> attributes = new HashMap<>(2);
76      private final List<Locale> locales = new ArrayList<>(2);
77  
78      private HttpSession session;
79  
80      private final InetSocketAddress localAddress;
81      private final String localName;
82      private final InetSocketAddress remoteAddress;
83      private final String remoteName;
84      private final InetSocketAddress serverAddress;
85  
86      public UpgradeHttpServletRequest(HttpServletRequest httpRequest)
87      {
88          // The original request object must be held temporarily for the duration of the handshake
89          // in order to be able to implement methods such as isUserInRole() and setAttribute().
90          request = httpRequest;
91          context = httpRequest.getServletContext();
92          dispatcher = httpRequest.getDispatcherType();
93  
94          method = httpRequest.getMethod();
95          protocol = httpRequest.getProtocol();
96          scheme = httpRequest.getScheme();
97          secure = httpRequest.isSecure();
98          requestURI = httpRequest.getRequestURI();
99          requestURL = httpRequest.getRequestURL();
100         pathInfo = httpRequest.getPathInfo();
101         pathTranslated = httpRequest.getPathTranslated();
102         servletPath = httpRequest.getServletPath();
103         query = httpRequest.getQueryString();
104         authType = httpRequest.getAuthType();
105         cookies = request.getCookies();
106 
107         remoteUser = httpRequest.getRemoteUser();
108         principal = httpRequest.getUserPrincipal();
109 
110         Enumeration<String> headerNames = httpRequest.getHeaderNames();
111         while (headerNames.hasMoreElements())
112         {
113             String name = headerNames.nextElement();
114             headers.put(name, Collections.list(httpRequest.getHeaders(name)));
115         }
116 
117         parameters.putAll(request.getParameterMap());
118 
119         Enumeration<String> attributeNames = httpRequest.getAttributeNames();
120         while (attributeNames.hasMoreElements())
121         {
122             String name = attributeNames.nextElement();
123             attributes.put(name, httpRequest.getAttribute(name));
124         }
125 
126         localAddress = InetSocketAddress.createUnresolved(httpRequest.getLocalAddr(), httpRequest.getLocalPort());
127         localName = httpRequest.getLocalName();
128         remoteAddress = InetSocketAddress.createUnresolved(httpRequest.getRemoteAddr(), httpRequest.getRemotePort());
129         remoteName = httpRequest.getRemoteHost();
130         serverAddress = InetSocketAddress.createUnresolved(httpRequest.getServerName(), httpRequest.getServerPort());
131     }
132 
133     public HttpServletRequest getHttpServletRequest()
134     {
135         return request;
136     }
137 
138     @Override
139     public String getAuthType()
140     {
141         return authType;
142     }
143 
144     @Override
145     public Cookie[] getCookies()
146     {
147         return cookies;
148     }
149 
150     @Override
151     public String getHeader(String name)
152     {
153         List<String> values = headers.get(name);
154         if (values == null || values.isEmpty())
155             return null;
156         return values.get(0);
157     }
158 
159     @Override
160     public Enumeration<String> getHeaders(String name)
161     {
162         List<String> values = headers.get(name);
163         if (values == null)
164             return Collections.emptyEnumeration();
165         return Collections.enumeration(values);
166     }
167 
168     @Override
169     public Enumeration<String> getHeaderNames()
170     {
171         return Collections.enumeration(headers.keySet());
172     }
173 
174     public Map<String, List<String>> getHeaders()
175     {
176         return Collections.unmodifiableMap(headers);
177     }
178 
179     @Override
180     public long getDateHeader(String name)
181     {
182         // TODO
183         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
184     }
185 
186     @Override
187     public int getIntHeader(String name)
188     {
189         // TODO
190         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
191     }
192 
193     @Override
194     public String getMethod()
195     {
196         return method;
197     }
198 
199     @Override
200     public String getPathInfo()
201     {
202         return pathInfo;
203     }
204 
205     @Override
206     public String getPathTranslated()
207     {
208         return pathTranslated;
209     }
210 
211     @Override
212     public String getContextPath()
213     {
214         return context.getContextPath();
215     }
216 
217     @Override
218     public String getQueryString()
219     {
220         return query;
221     }
222 
223     @Override
224     public String getRemoteUser()
225     {
226         return remoteUser;
227     }
228 
229     @Override
230     public boolean isUserInRole(String role)
231     {
232         HttpServletRequest request = getHttpServletRequest();
233         if (request != null)
234             return request.isUserInRole(role);
235         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
236     }
237 
238     @Override
239     public Principal getUserPrincipal()
240     {
241         return principal;
242     }
243 
244     @Override
245     public String getRequestURI()
246     {
247         return requestURI;
248     }
249 
250     @Override
251     public StringBuffer getRequestURL()
252     {
253         return requestURL;
254     }
255 
256     @Override
257     public String getServletPath()
258     {
259         return servletPath;
260     }
261 
262     @Override
263     public HttpSession getSession(boolean create)
264     {
265         HttpServletRequest request = getHttpServletRequest();
266         if (request != null)
267             return session = request.getSession(create);
268         return session;
269     }
270 
271     @Override
272     public HttpSession getSession()
273     {
274         return getSession(true);
275     }
276 
277     @Override
278     public String getRequestedSessionId()
279     {
280         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
281     }
282 
283     @Override
284     public boolean isRequestedSessionIdValid()
285     {
286         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
287     }
288 
289     @Override
290     public boolean isRequestedSessionIdFromCookie()
291     {
292         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
293     }
294 
295     @Override
296     public boolean isRequestedSessionIdFromURL()
297     {
298         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
299     }
300 
301     @Override
302     public boolean isRequestedSessionIdFromUrl()
303     {
304         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
305     }
306 
307     @Override
308     public Object getAttribute(String name)
309     {
310         return attributes.get(name);
311     }
312 
313     @Override
314     public Enumeration<String> getAttributeNames()
315     {
316         return Collections.enumeration(attributes.keySet());
317     }
318 
319     public Map<String, Object> getAttributes()
320     {
321         return Collections.unmodifiableMap(attributes);
322     }
323 
324     @Override
325     public String getParameter(String name)
326     {
327         String[] values = parameters.get(name);
328         if (values == null || values.length == 0)
329             return null;
330         return values[0];
331     }
332 
333     @Override
334     public Enumeration<String> getParameterNames()
335     {
336         return Collections.enumeration(parameters.keySet());
337     }
338 
339     @Override
340     public String[] getParameterValues(String name)
341     {
342         return parameters.get(name);
343     }
344 
345     @Override
346     public Map<String, String[]> getParameterMap()
347     {
348         return parameters;
349     }
350 
351     @Override
352     public String getProtocol()
353     {
354         return protocol;
355     }
356 
357     @Override
358     public String getScheme()
359     {
360         return scheme;
361     }
362 
363     @Override
364     public String getServerName()
365     {
366         return serverAddress.getHostString();
367     }
368 
369     @Override
370     public int getServerPort()
371     {
372         return serverAddress.getPort();
373     }
374 
375     @Override
376     public String getRemoteAddr()
377     {
378         return remoteAddress.getHostString();
379     }
380 
381     @Override
382     public int getRemotePort()
383     {
384         return remoteAddress.getPort();
385     }
386 
387     @Override
388     public String getRemoteHost()
389     {
390         return remoteName;
391     }
392 
393     @Override
394     public void setAttribute(String name, Object value)
395     {
396         HttpServletRequest request = getHttpServletRequest();
397         if (request != null)
398             request.setAttribute(name, value);
399         attributes.put(name, value);
400     }
401 
402     @Override
403     public void removeAttribute(String name)
404     {
405         HttpServletRequest request = getHttpServletRequest();
406         if (request != null)
407             request.removeAttribute(name);
408         attributes.remove(name);
409     }
410 
411     @Override
412     public Locale getLocale()
413     {
414         if (locales.isEmpty())
415             return Locale.getDefault();
416         return locales.get(0);
417     }
418 
419     @Override
420     public Enumeration<Locale> getLocales()
421     {
422         return Collections.enumeration(locales);
423     }
424 
425     @Override
426     public boolean isSecure()
427     {
428         return secure;
429     }
430 
431     @Override
432     public String getRealPath(String path)
433     {
434         return context.getRealPath(path);
435     }
436 
437     @Override
438     public String getLocalName()
439     {
440         return localName;
441     }
442 
443     @Override
444     public String getLocalAddr()
445     {
446         return localAddress.getHostString();
447     }
448 
449     @Override
450     public int getLocalPort()
451     {
452         return localAddress.getPort();
453     }
454 
455     @Override
456     public ServletContext getServletContext()
457     {
458         return context;
459     }
460 
461     @Override
462     public DispatcherType getDispatcherType()
463     {
464         return dispatcher;
465     }
466 
467     @Override
468     public boolean authenticate(HttpServletResponse response) throws IOException, ServletException
469     {
470         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
471     }
472 
473     @Override
474     public String changeSessionId()
475     {
476         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
477     }
478 
479     @Override
480     public AsyncContext getAsyncContext()
481     {
482         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
483     }
484 
485     @Override
486     public String getCharacterEncoding()
487     {
488         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
489     }
490 
491     @Override
492     public int getContentLength()
493     {
494         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
495     }
496 
497     @Override
498     public long getContentLengthLong()
499     {
500         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
501     }
502 
503     @Override
504     public String getContentType()
505     {
506         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
507     }
508 
509     @Override
510     public ServletInputStream getInputStream() throws IOException
511     {
512         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
513     }
514 
515     @Override
516     public Part getPart(String name) throws IOException, ServletException
517     {
518         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
519     }
520 
521     @Override
522     public Collection<Part> getParts() throws IOException, ServletException
523     {
524         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
525     }
526 
527     @Override
528     public BufferedReader getReader() throws IOException
529     {
530         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
531     }
532 
533     @Override
534     public RequestDispatcher getRequestDispatcher(String path)
535     {
536         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
537     }
538 
539     @Override
540     public boolean isAsyncStarted()
541     {
542         return false;
543     }
544 
545     @Override
546     public boolean isAsyncSupported()
547     {
548         return false;
549     }
550 
551     @Override
552     public void login(String username, String password) throws ServletException
553     {
554         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
555     }
556 
557     @Override
558     public void logout() throws ServletException
559     {
560         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
561     }
562 
563     @Override
564     public void setCharacterEncoding(String enc) throws UnsupportedEncodingException
565     {
566         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
567     }
568 
569     @Override
570     public AsyncContext startAsync() throws IllegalStateException
571     {
572         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
573     }
574 
575     @Override
576     public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException
577     {
578         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
579     }
580 
581     @Override
582     public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException
583     {
584         throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
585     }
586 
587     public void complete()
588     {
589         request = null;
590     }
591 }