1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.server;
15
16 import java.io.BufferedReader;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.io.UnsupportedEncodingException;
21 import java.net.InetAddress;
22 import java.nio.ByteBuffer;
23 import java.security.Principal;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.EventListener;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.Map;
33
34 import javax.servlet.RequestDispatcher;
35 import javax.servlet.ServletContext;
36 import javax.servlet.ServletInputStream;
37 import javax.servlet.ServletRequest;
38 import javax.servlet.ServletRequestAttributeEvent;
39 import javax.servlet.ServletRequestAttributeListener;
40 import javax.servlet.ServletResponse;
41 import javax.servlet.http.Cookie;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpSession;
44
45 import org.eclipse.jetty.continuation.Continuation;
46 import org.eclipse.jetty.continuation.ContinuationListener;
47 import org.eclipse.jetty.http.HttpCookie;
48 import org.eclipse.jetty.http.HttpFields;
49 import org.eclipse.jetty.http.HttpHeaders;
50 import org.eclipse.jetty.http.HttpMethods;
51 import org.eclipse.jetty.http.HttpParser;
52 import org.eclipse.jetty.http.HttpURI;
53 import org.eclipse.jetty.http.HttpVersions;
54 import org.eclipse.jetty.http.MimeTypes;
55 import org.eclipse.jetty.io.Buffer;
56 import org.eclipse.jetty.io.BufferUtil;
57 import org.eclipse.jetty.io.EndPoint;
58 import org.eclipse.jetty.io.nio.DirectNIOBuffer;
59 import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
60 import org.eclipse.jetty.io.nio.NIOBuffer;
61 import org.eclipse.jetty.server.handler.ContextHandler;
62 import org.eclipse.jetty.server.handler.ContextHandler.Context;
63 import org.eclipse.jetty.util.Attributes;
64 import org.eclipse.jetty.util.AttributesMap;
65 import org.eclipse.jetty.util.LazyList;
66 import org.eclipse.jetty.util.MultiMap;
67 import org.eclipse.jetty.util.StringUtil;
68 import org.eclipse.jetty.util.URIUtil;
69 import org.eclipse.jetty.util.UrlEncoded;
70 import org.eclipse.jetty.util.log.Log;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public class Request implements HttpServletRequest
104 {
105 private static final String __ASYNC_FWD="org.eclipse.asyncfwd";
106 private static final Collection __defaultLocale = Collections.singleton(Locale.getDefault());
107 private static final int __NONE=0, _STREAM=1, __READER=2;
108
109
110 public static Request getRequest(HttpServletRequest request)
111 {
112 if (request instanceof Request)
113 return (Request) request;
114
115 return HttpConnection.getCurrentConnection().getRequest();
116 }
117 protected final AsyncContinuation _async = new AsyncContinuation();
118 private boolean _asyncSupported=true;
119 private volatile Attributes _attributes;
120 private Authentication _authentication;
121 private MultiMap<String> _baseParameters;
122 private String _characterEncoding;
123 protected HttpConnection _connection;
124 private ContextHandler.Context _context;
125 private boolean _newContext;
126 private String _contextPath;
127 private CookieCutter _cookies;
128 private boolean _cookiesExtracted=false;
129 private DispatcherType _dispatcherType;
130 private boolean _dns=false;
131 private EndPoint _endp;
132 private boolean _handled =false;
133 private int _inputState=__NONE;
134 private String _method;
135 private MultiMap<String> _parameters;
136 private boolean _paramsExtracted;
137 private String _pathInfo;
138 private int _port;
139 private String _protocol=HttpVersions.HTTP_1_1;
140 private String _queryEncoding;
141 private String _queryString;
142 private BufferedReader _reader;
143 private String _readerEncoding;
144 private String _remoteAddr;
145 private String _remoteHost;
146 private Object _requestAttributeListeners;
147 private String _requestedSessionId;
148 private boolean _requestedSessionIdFromCookie=false;
149 private String _requestURI;
150 private Map<Object,HttpSession> _savedNewSessions;
151 private String _scheme=URIUtil.HTTP;
152 private UserIdentity.Scope _scope;
153 private String _serverName;
154 private String _servletPath;
155 private HttpSession _session;
156 private SessionManager _sessionManager;
157 private long _timeStamp;
158
159 private Buffer _timeStampBuffer;
160 private HttpURI _uri;
161
162
163 public Request()
164 {
165 }
166
167
168 public Request(HttpConnection connection)
169 {
170 setConnection(connection);
171 }
172
173
174 public void addContinuationListener(ContinuationListener listener)
175 {
176 _async.addContinuationListener(listener);
177 }
178
179
180 public void addEventListener(final EventListener listener)
181 {
182 if (listener instanceof ServletRequestAttributeListener)
183 _requestAttributeListeners= LazyList.add(_requestAttributeListeners, listener);
184 if (listener instanceof ContinuationListener)
185 _async.addContinuationListener((ContinuationListener)listener);
186
187 }
188
189
190
191
192
193 public void extractParameters()
194 {
195 if (_baseParameters == null)
196 _baseParameters = new MultiMap(16);
197
198 if (_paramsExtracted)
199 {
200 if (_parameters==null)
201 _parameters=_baseParameters;
202 return;
203 }
204
205 _paramsExtracted = true;
206
207
208 if (_uri!=null && _uri.hasQuery())
209 {
210 if (_queryEncoding==null)
211 _uri.decodeQueryTo(_baseParameters);
212 else
213 {
214 try
215 {
216 _uri.decodeQueryTo(_baseParameters,_queryEncoding);
217 }
218 catch (UnsupportedEncodingException e)
219 {
220 if (Log.isDebugEnabled())
221 Log.warn(e);
222 else
223 Log.warn(e.toString());
224 }
225 }
226 }
227
228
229 String encoding = getCharacterEncoding();
230 String content_type = getContentType();
231 if (content_type != null && content_type.length() > 0)
232 {
233 content_type = HttpFields.valueParameters(content_type, null);
234
235 if (MimeTypes.FORM_ENCODED.equalsIgnoreCase(content_type) &&
236 (HttpMethods.POST.equals(getMethod()) || HttpMethods.PUT.equals(getMethod())))
237 {
238 int content_length = getContentLength();
239 if (content_length != 0)
240 {
241 try
242 {
243 int maxFormContentSize=-1;
244
245 if (_context!=null)
246 maxFormContentSize=_context.getContextHandler().getMaxFormContentSize();
247 else
248 {
249 Integer size = (Integer)_connection.getConnector().getServer().getAttribute("org.eclipse.jetty.server.Request.maxFormContentSize");
250 if (size!=null)
251 maxFormContentSize =size.intValue();
252 }
253
254 if (content_length>maxFormContentSize && maxFormContentSize > 0)
255 {
256 throw new IllegalStateException("Form too large"+content_length+">"+maxFormContentSize);
257 }
258 InputStream in = getInputStream();
259
260
261 UrlEncoded.decodeTo(in, _baseParameters, encoding,content_length<0?maxFormContentSize:-1);
262 }
263 catch (IOException e)
264 {
265 if (Log.isDebugEnabled())
266 Log.warn(e);
267 else
268 Log.warn(e.toString());
269 }
270 }
271 }
272 }
273
274 if (_parameters==null)
275 _parameters=_baseParameters;
276 else if (_parameters!=_baseParameters)
277 {
278
279 Iterator iter = _baseParameters.entrySet().iterator();
280 while (iter.hasNext())
281 {
282 Map.Entry entry = (Map.Entry)iter.next();
283 String name=(String)entry.getKey();
284 Object values=entry.getValue();
285 for (int i=0;i<LazyList.size(values);i++)
286 _parameters.add(name, LazyList.get(values, i));
287 }
288 }
289 }
290
291
292 public AsyncContext getAsyncContext()
293 {
294 if (_async.isInitial() && !isAsyncStarted())
295 throw new IllegalStateException(_async.getStatusString());
296 return _async;
297 }
298
299
300 public AsyncContinuation getAsyncContinuation()
301 {
302 return _async;
303 }
304
305
306
307
308
309 public Object getAttribute(String name)
310 {
311 Object attr=(_attributes==null)?null:_attributes.getAttribute(name);
312 if (attr==null && Continuation.ATTRIBUTE.equals(name))
313 return _async;
314 return attr;
315 }
316
317
318
319
320
321 public Enumeration getAttributeNames()
322 {
323 if (_attributes==null)
324 return Collections.enumeration(Collections.EMPTY_LIST);
325
326 return AttributesMap.getAttributeNamesCopy(_attributes);
327 }
328
329
330
331
332 public Attributes getAttributes()
333 {
334 if (_attributes==null)
335 _attributes=new AttributesMap();
336 return _attributes;
337 }
338
339
340
341
342
343 public Authentication getAuthentication()
344 {
345 return _authentication;
346 }
347
348
349
350
351
352 public String getAuthType()
353 {
354 if (_authentication instanceof Authentication.Deferred)
355 _authentication = ((Authentication.Deferred)_authentication).authenticate(this);
356
357 if (_authentication instanceof Authentication.User)
358 return ((Authentication.User)_authentication).getAuthMethod();
359 return null;
360 }
361
362
363
364
365
366 public String getCharacterEncoding()
367 {
368 return _characterEncoding;
369 }
370
371
372
373
374
375 public HttpConnection getConnection()
376 {
377 return _connection;
378 }
379
380
381
382
383
384 public int getContentLength()
385 {
386 return (int)_connection.getRequestFields().getLongField(HttpHeaders.CONTENT_LENGTH_BUFFER);
387 }
388
389 public long getContentRead()
390 {
391 if (_connection==null || _connection.getParser()==null)
392 return -1;
393
394 return ((HttpParser)_connection.getParser()).getContentRead();
395 }
396
397
398
399
400
401 public String getContentType()
402 {
403 return _connection.getRequestFields().getStringField(HttpHeaders.CONTENT_TYPE_BUFFER);
404 }
405
406
407
408
409
410
411 public Context getContext()
412 {
413 return _context;
414 }
415
416
417
418
419
420 public String getContextPath()
421 {
422 return _contextPath;
423 }
424
425
426
427
428
429 public Cookie[] getCookies()
430 {
431 if (_cookiesExtracted)
432 return _cookies==null?null:_cookies.getCookies();
433
434 _cookiesExtracted = true;
435
436 Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.COOKIE_BUFFER);
437
438
439 if (enm!=null)
440 {
441 if (_cookies==null)
442 _cookies=new CookieCutter();
443
444 while (enm.hasMoreElements())
445 {
446 String c = (String)enm.nextElement();
447 _cookies.addCookieField(c);
448 }
449 }
450
451 return _cookies==null?null:_cookies.getCookies();
452 }
453
454
455
456
457
458 public long getDateHeader(String name)
459 {
460 return _connection.getRequestFields().getDateField(name);
461 }
462
463
464 public DispatcherType getDispatcherType()
465 {
466 return _dispatcherType;
467 }
468
469
470
471
472
473 public String getHeader(String name)
474 {
475 return _connection.getRequestFields().getStringField(name);
476 }
477
478
479
480
481
482 public Enumeration getHeaderNames()
483 {
484 return _connection.getRequestFields().getFieldNames();
485 }
486
487
488
489
490
491 public Enumeration getHeaders(String name)
492 {
493 Enumeration e = _connection.getRequestFields().getValues(name);
494 if (e==null)
495 return Collections.enumeration(Collections.EMPTY_LIST);
496 return e;
497 }
498
499
500
501
502
503 public int getInputState()
504 {
505 return _inputState;
506 }
507
508
509
510
511
512 public ServletInputStream getInputStream() throws IOException
513 {
514 if (_inputState!=__NONE && _inputState!=_STREAM)
515 throw new IllegalStateException("READER");
516 _inputState=_STREAM;
517 return _connection.getInputStream();
518 }
519
520
521
522
523
524 public int getIntHeader(String name)
525 {
526 return (int)_connection.getRequestFields().getLongField(name);
527 }
528
529
530
531
532
533 public String getLocalAddr()
534 {
535 return _endp==null?null:_endp.getLocalAddr();
536 }
537
538
539
540
541
542 public Locale getLocale()
543 {
544 Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.ACCEPT_LANGUAGE, HttpFields.__separators);
545
546
547 if (enm == null || !enm.hasMoreElements())
548 return Locale.getDefault();
549
550
551 List acceptLanguage = HttpFields.qualityList(enm);
552 if (acceptLanguage.size()==0)
553 return Locale.getDefault();
554
555 int size=acceptLanguage.size();
556
557 if (size>0)
558 {
559 String language = (String)acceptLanguage.get(0);
560 language=HttpFields.valueParameters(language,null);
561 String country = "";
562 int dash = language.indexOf('-');
563 if (dash > -1)
564 {
565 country = language.substring(dash + 1).trim();
566 language = language.substring(0,dash).trim();
567 }
568 return new Locale(language,country);
569 }
570
571 return Locale.getDefault();
572 }
573
574
575
576
577
578 public Enumeration getLocales()
579 {
580
581 Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.ACCEPT_LANGUAGE, HttpFields.__separators);
582
583
584 if (enm == null || !enm.hasMoreElements())
585 return Collections.enumeration(__defaultLocale);
586
587
588 List acceptLanguage = HttpFields.qualityList(enm);
589
590 if (acceptLanguage.size()==0)
591 return
592 Collections.enumeration(__defaultLocale);
593
594 Object langs = null;
595 int size=acceptLanguage.size();
596
597
598 for (int i=0; i<size; i++)
599 {
600 String language = (String)acceptLanguage.get(i);
601 language=HttpFields.valueParameters(language,null);
602 String country = "";
603 int dash = language.indexOf('-');
604 if (dash > -1)
605 {
606 country = language.substring(dash + 1).trim();
607 language = language.substring(0,dash).trim();
608 }
609 langs=LazyList.ensureSize(langs,size);
610 langs=LazyList.add(langs,new Locale(language,country));
611 }
612
613 if (LazyList.size(langs)==0)
614 return Collections.enumeration(__defaultLocale);
615
616 return Collections.enumeration(LazyList.getList(langs));
617 }
618
619
620
621
622
623 public String getLocalName()
624 {
625 if (_dns)
626 return _endp==null?null:_endp.getLocalHost();
627 return _endp==null?null:_endp.getLocalAddr();
628 }
629
630
631
632
633
634 public int getLocalPort()
635 {
636 return _endp==null?0:_endp.getLocalPort();
637 }
638
639
640
641
642
643 public String getMethod()
644 {
645 return _method;
646 }
647
648
649
650
651
652 public String getParameter(String name)
653 {
654 if (!_paramsExtracted)
655 extractParameters();
656 return (String) _parameters.getValue(name, 0);
657 }
658
659
660
661
662
663 public Map getParameterMap()
664 {
665 if (!_paramsExtracted)
666 extractParameters();
667
668 return Collections.unmodifiableMap(_parameters.toStringArrayMap());
669 }
670
671
672
673
674
675 public Enumeration getParameterNames()
676 {
677 if (!_paramsExtracted)
678 extractParameters();
679 return Collections.enumeration(_parameters.keySet());
680 }
681
682
683
684
685
686 public MultiMap getParameters()
687 {
688 return _parameters;
689 }
690
691
692
693
694
695 public String[] getParameterValues(String name)
696 {
697 if (!_paramsExtracted)
698 extractParameters();
699 List vals = _parameters.getValues(name);
700 if (vals==null)
701 return null;
702 return (String[])vals.toArray(new String[vals.size()]);
703 }
704
705
706
707
708
709 public String getPathInfo()
710 {
711 return _pathInfo;
712 }
713
714
715
716
717
718 public String getPathTranslated()
719 {
720 if (_pathInfo==null || _context==null)
721 return null;
722 return _context.getRealPath(_pathInfo);
723 }
724
725
726
727
728
729 public String getProtocol()
730 {
731 return _protocol;
732 }
733
734
735 public String getQueryEncoding()
736 {
737 return _queryEncoding;
738 }
739
740
741
742
743
744 public String getQueryString()
745 {
746 if (_queryString==null && _uri!=null)
747 {
748 if (_queryEncoding==null)
749 _queryString=_uri.getQuery();
750 else
751 _queryString=_uri.getQuery(_queryEncoding);
752 }
753 return _queryString;
754 }
755
756
757
758
759
760 public BufferedReader getReader() throws IOException
761 {
762 if (_inputState!=__NONE && _inputState!=__READER)
763 throw new IllegalStateException("STREAMED");
764
765 if (_inputState==__READER)
766 return _reader;
767
768 String encoding=getCharacterEncoding();
769 if (encoding==null)
770 encoding=StringUtil.__ISO_8859_1;
771
772 if (_reader==null || !encoding.equalsIgnoreCase(_readerEncoding))
773 {
774 final ServletInputStream in = getInputStream();
775 _readerEncoding=encoding;
776 _reader=new BufferedReader(new InputStreamReader(in,encoding))
777 {
778 public void close() throws IOException
779 {
780 in.close();
781 }
782 };
783 }
784 _inputState=__READER;
785 return _reader;
786 }
787
788
789
790
791
792 public String getRealPath(String path)
793 {
794 if (_context==null)
795 return null;
796 return _context.getRealPath(path);
797 }
798
799
800
801
802
803 public String getRemoteAddr()
804 {
805 if (_remoteAddr != null)
806 return _remoteAddr;
807 return _endp==null?null:_endp.getRemoteAddr();
808 }
809
810
811
812
813
814 public String getRemoteHost()
815 {
816 if (_dns)
817 {
818 if (_remoteHost != null)
819 {
820 return _remoteHost;
821 }
822 return _endp==null?null:_endp.getRemoteHost();
823 }
824 return getRemoteAddr();
825 }
826
827
828
829
830
831 public int getRemotePort()
832 {
833 return _endp==null?0:_endp.getRemotePort();
834 }
835
836
837
838
839
840 public String getRemoteUser()
841 {
842 Principal p = getUserPrincipal();
843 if (p==null)
844 return null;
845 return p.getName();
846 }
847
848
849
850
851
852 public RequestDispatcher getRequestDispatcher(String path)
853 {
854 if (path == null || _context==null)
855 return null;
856
857
858 if (!path.startsWith("/"))
859 {
860 String relTo=URIUtil.addPaths(_servletPath,_pathInfo);
861 int slash=relTo.lastIndexOf("/");
862 if (slash>1)
863 relTo=relTo.substring(0,slash+1);
864 else
865 relTo="/";
866 path=URIUtil.addPaths(relTo,path);
867 }
868
869 return _context.getRequestDispatcher(path);
870 }
871
872
873
874
875
876 public String getRequestedSessionId()
877 {
878 return _requestedSessionId;
879 }
880
881
882
883
884
885 public String getRequestURI()
886 {
887 if (_requestURI==null && _uri!=null)
888 _requestURI=_uri.getPathAndParam();
889 return _requestURI;
890 }
891
892
893
894
895
896 public StringBuffer getRequestURL()
897 {
898 final StringBuffer url = new StringBuffer(48);
899 synchronized (url)
900 {
901 String scheme = getScheme();
902 int port = getServerPort();
903
904 url.append(scheme);
905 url.append("://");
906 url.append(getServerName());
907 if (_port>0 &&
908 ((scheme.equalsIgnoreCase(URIUtil.HTTP) && port != 80) ||
909 (scheme.equalsIgnoreCase(URIUtil.HTTPS) && port != 443)))
910 {
911 url.append(':');
912 url.append(_port);
913 }
914
915 url.append(getRequestURI());
916 return url;
917 }
918 }
919
920
921 public Response getResponse()
922 {
923 return _connection._response;
924 }
925
926
927
928
929
930
931
932
933
934
935
936
937
938 public StringBuilder getRootURL()
939 {
940 StringBuilder url = new StringBuilder(48);
941 String scheme = getScheme();
942 int port = getServerPort();
943
944 url.append(scheme);
945 url.append("://");
946 url.append(getServerName());
947
948 if (port > 0 && ((scheme.equalsIgnoreCase("http") && port != 80) || (scheme.equalsIgnoreCase("https") && port != 443)))
949 {
950 url.append(':');
951 url.append(port);
952 }
953 return url;
954 }
955
956
957
958
959
960 public String getScheme()
961 {
962 return _scheme;
963 }
964
965
966
967
968
969 public String getServerName()
970 {
971
972 if (_serverName != null)
973 return _serverName;
974
975
976 _serverName = _uri.getHost();
977 _port = _uri.getPort();
978 if (_serverName != null)
979 return _serverName;
980
981
982 Buffer hostPort = _connection.getRequestFields().get(HttpHeaders.HOST_BUFFER);
983 if (hostPort!=null)
984 {
985 for (int i=hostPort.length();i-->0;)
986 {
987 if (hostPort.peek(hostPort.getIndex()+i)==':')
988 {
989 _serverName=BufferUtil.to8859_1_String(hostPort.peek(hostPort.getIndex(), i));
990 _port=BufferUtil.toInt(hostPort.peek(hostPort.getIndex()+i+1, hostPort.length()-i-1));
991 return _serverName;
992 }
993 }
994 if (_serverName==null || _port<0)
995 {
996 _serverName=BufferUtil.to8859_1_String(hostPort);
997 _port = 0;
998 }
999
1000 return _serverName;
1001 }
1002
1003
1004 if (_connection != null)
1005 {
1006 _serverName = getLocalName();
1007 _port = getLocalPort();
1008 if (_serverName != null && !StringUtil.ALL_INTERFACES.equals(_serverName))
1009 return _serverName;
1010 }
1011
1012
1013 try
1014 {
1015 _serverName = InetAddress.getLocalHost().getHostAddress();
1016 }
1017 catch (java.net.UnknownHostException e)
1018 {
1019 Log.ignore(e);
1020 }
1021 return _serverName;
1022 }
1023
1024
1025
1026
1027
1028 public int getServerPort()
1029 {
1030 if (_port<=0)
1031 {
1032 if (_serverName==null)
1033 getServerName();
1034
1035 if (_port<=0)
1036 {
1037 if (_serverName!=null && _uri!=null)
1038 _port = _uri.getPort();
1039 else
1040 _port = _endp==null?0:_endp.getLocalPort();
1041 }
1042 }
1043
1044 if (_port<=0)
1045 {
1046 if (getScheme().equalsIgnoreCase(URIUtil.HTTPS))
1047 return 443;
1048 return 80;
1049 }
1050 return _port;
1051 }
1052
1053
1054 public ServletContext getServletContext()
1055 {
1056 return _context;
1057 }
1058
1059
1060
1061
1062 public String getServletName()
1063 {
1064 if (_scope!=null)
1065 return _scope.getName();
1066 return null;
1067 }
1068
1069
1070
1071
1072
1073 public String getServletPath()
1074 {
1075 if (_servletPath==null)
1076 _servletPath="";
1077 return _servletPath;
1078 }
1079
1080
1081 public ServletResponse getServletResponse()
1082 {
1083 return _connection.getResponse();
1084 }
1085
1086
1087
1088
1089
1090 public HttpSession getSession()
1091 {
1092 return getSession(true);
1093 }
1094
1095
1096
1097
1098
1099 public HttpSession getSession(boolean create)
1100 {
1101 if (_sessionManager==null && create)
1102 throw new IllegalStateException("No SessionHandler or SessionManager");
1103
1104 if (_session != null && _sessionManager!=null && _sessionManager.isValid(_session))
1105 return _session;
1106
1107 _session=null;
1108
1109 String id=getRequestedSessionId();
1110
1111 if (id != null && _sessionManager!=null)
1112 {
1113 _session=_sessionManager.getHttpSession(id);
1114 if (_session == null && !create)
1115 return null;
1116 }
1117
1118 if (_session == null && _sessionManager!=null && create )
1119 {
1120 _session=_sessionManager.newHttpSession(this);
1121 HttpCookie cookie=_sessionManager.getSessionCookie(_session,getContextPath(),isSecure());
1122 if (cookie!=null)
1123 _connection.getResponse().addCookie(cookie);
1124 }
1125
1126 return _session;
1127 }
1128
1129
1130
1131
1132
1133
1134 public SessionManager getSessionManager()
1135 {
1136 return _sessionManager;
1137 }
1138
1139
1140
1141
1142
1143
1144
1145 public long getTimeStamp()
1146 {
1147 return _timeStamp;
1148 }
1149
1150
1151
1152
1153
1154
1155
1156 public Buffer getTimeStampBuffer()
1157 {
1158 if (_timeStampBuffer == null && _timeStamp > 0)
1159 _timeStampBuffer = HttpFields.__dateCache.formatBuffer(_timeStamp);
1160 return _timeStampBuffer;
1161 }
1162
1163
1164
1165
1166
1167 public HttpURI getUri()
1168 {
1169 return _uri;
1170 }
1171
1172
1173 public UserIdentity getUserIdentity()
1174 {
1175 if (_authentication instanceof Authentication.Deferred)
1176 setAuthentication(((Authentication.Deferred)_authentication).authenticate(this));
1177
1178 if (_authentication instanceof Authentication.User)
1179 return ((Authentication.User)_authentication).getUserIdentity();
1180 return null;
1181 }
1182
1183
1184
1185
1186
1187
1188
1189 public UserIdentity getResolvedUserIdentity()
1190 {
1191 if (_authentication instanceof Authentication.User)
1192 return ((Authentication.User)_authentication).getUserIdentity();
1193 return null;
1194 }
1195
1196
1197 public UserIdentity.Scope getUserIdentityScope()
1198 {
1199 return _scope;
1200 }
1201
1202
1203
1204
1205
1206 public Principal getUserPrincipal()
1207 {
1208 if (_authentication instanceof Authentication.Deferred)
1209 setAuthentication(((Authentication.Deferred)_authentication).authenticate(this));
1210
1211 if (_authentication instanceof Authentication.User)
1212 {
1213 UserIdentity user = ((Authentication.User)_authentication).getUserIdentity();
1214 return user.getUserPrincipal();
1215 }
1216 return null;
1217 }
1218
1219
1220 public boolean isAsyncStarted()
1221 {
1222 return _async.isAsyncStarted();
1223 }
1224
1225
1226 public boolean isAsyncSupported()
1227 {
1228 return _asyncSupported;
1229 }
1230
1231
1232 public boolean isHandled()
1233 {
1234 return _handled;
1235 }
1236
1237
1238
1239
1240
1241 public boolean isRequestedSessionIdFromCookie()
1242 {
1243 return _requestedSessionId!=null && _requestedSessionIdFromCookie;
1244 }
1245
1246
1247
1248
1249
1250 public boolean isRequestedSessionIdFromUrl()
1251 {
1252 return _requestedSessionId!=null && !_requestedSessionIdFromCookie;
1253 }
1254
1255
1256
1257
1258
1259 public boolean isRequestedSessionIdFromURL()
1260 {
1261 return _requestedSessionId!=null && !_requestedSessionIdFromCookie;
1262 }
1263
1264
1265
1266
1267
1268 public boolean isRequestedSessionIdValid()
1269 {
1270 if (_requestedSessionId==null)
1271 return false;
1272
1273 HttpSession session=getSession(false);
1274 return (session != null && _sessionManager.getIdManager().getClusterId(_requestedSessionId).equals(_sessionManager.getClusterId(session)));
1275 }
1276
1277
1278
1279
1280
1281 public boolean isSecure()
1282 {
1283 return _connection.isConfidential(this);
1284 }
1285
1286
1287
1288
1289
1290 public boolean isUserInRole(String role)
1291 {
1292 if (_authentication instanceof Authentication.Deferred)
1293 setAuthentication(((Authentication.Deferred)_authentication).authenticate(this));
1294
1295 if (_authentication instanceof Authentication.User)
1296 return ((Authentication.User)_authentication).isUserInRole(_scope,role);
1297 return false;
1298 }
1299
1300
1301 public HttpSession recoverNewSession(Object key)
1302 {
1303 if (_savedNewSessions==null)
1304 return null;
1305 return _savedNewSessions.get(key);
1306 }
1307
1308
1309 protected void recycle()
1310 {
1311 setAuthentication(Authentication.NOT_CHECKED);
1312 _async.recycle();
1313 _asyncSupported=true;
1314 _handled=false;
1315 if (_context!=null)
1316 throw new IllegalStateException("Request in context!");
1317 if(_attributes!=null)
1318 _attributes.clearAttributes();
1319 _characterEncoding=null;
1320 if (_cookies!=null)
1321 _cookies.reset();
1322 _cookiesExtracted=false;
1323 _context=null;
1324 _serverName=null;
1325 _method=null;
1326 _pathInfo=null;
1327 _port=0;
1328 _protocol=HttpVersions.HTTP_1_1;
1329 _queryEncoding=null;
1330 _queryString=null;
1331 _requestedSessionId=null;
1332 _requestedSessionIdFromCookie=false;
1333 _session=null;
1334 _requestURI=null;
1335 _scope=null;
1336 _scheme=URIUtil.HTTP;
1337 _servletPath=null;
1338 _timeStamp=0;
1339 _timeStampBuffer=null;
1340 _uri=null;
1341 if (_baseParameters!=null)
1342 _baseParameters.clear();
1343 _parameters=null;
1344 _paramsExtracted=false;
1345 _inputState=__NONE;
1346
1347 if (_savedNewSessions!=null)
1348 _savedNewSessions.clear();
1349 _savedNewSessions=null;
1350 }
1351
1352
1353
1354
1355
1356 public void removeAttribute(String name)
1357 {
1358 Object old_value=_attributes==null?null:_attributes.getAttribute(name);
1359
1360 if (_attributes!=null)
1361 _attributes.removeAttribute(name);
1362
1363 if (old_value!=null)
1364 {
1365 if (_requestAttributeListeners!=null)
1366 {
1367 final ServletRequestAttributeEvent event =
1368 new ServletRequestAttributeEvent(_context,this,name, old_value);
1369 final int size=LazyList.size(_requestAttributeListeners);
1370 for(int i=0;i<size;i++)
1371 {
1372 final EventListener listener = (ServletRequestAttributeListener)LazyList.get(_requestAttributeListeners,i);
1373 if (listener instanceof ServletRequestAttributeListener)
1374 {
1375 final ServletRequestAttributeListener l = (ServletRequestAttributeListener)listener;
1376 l.attributeRemoved(event);
1377 }
1378 }
1379 }
1380 }
1381 }
1382
1383
1384 public void removeEventListener(final EventListener listener)
1385 {
1386 _requestAttributeListeners= LazyList.remove(_requestAttributeListeners, listener);
1387 }
1388
1389 public void saveNewSession(Object key,HttpSession session)
1390 {
1391 if (_savedNewSessions==null)
1392 _savedNewSessions=new HashMap<Object,HttpSession>();
1393 _savedNewSessions.put(key,session);
1394 }
1395
1396 public void setAsyncSupported(boolean supported)
1397 {
1398 _asyncSupported=supported;
1399 }
1400
1401
1402 public void setAsyncTimeout(long timeout)
1403 {
1404 _async.setAsyncTimeout(timeout);
1405 }
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418 public void setAttribute(String name, Object value)
1419 {
1420 Object old_value=_attributes==null?null:_attributes.getAttribute(name);
1421
1422 if ("org.eclipse.jetty.server.Request.queryEncoding".equals(name))
1423 setQueryEncoding(value==null?null:value.toString());
1424 else if("org.eclipse.jetty.server.sendContent".equals(name))
1425 {
1426 try
1427 {
1428 ((HttpConnection.Output)getServletResponse().getOutputStream()).sendContent(value);
1429 }
1430 catch (IOException e)
1431 {
1432 throw new RuntimeException(e);
1433 }
1434 }
1435 else if("org.eclipse.jetty.server.ResponseBuffer".equals(name))
1436 {
1437 try
1438 {
1439 final ByteBuffer byteBuffer=(ByteBuffer)value;
1440 synchronized (byteBuffer)
1441 {
1442 NIOBuffer buffer = byteBuffer.isDirect()
1443 ?new DirectNIOBuffer(byteBuffer,true)
1444 :new IndirectNIOBuffer(byteBuffer,true);
1445 ((HttpConnection.Output)getServletResponse().getOutputStream()).sendResponse(buffer);
1446 }
1447 }
1448 catch (IOException e)
1449 {
1450 throw new RuntimeException(e);
1451 }
1452 }
1453
1454 if (_attributes==null)
1455 _attributes=new AttributesMap();
1456 _attributes.setAttribute(name, value);
1457
1458 if (_requestAttributeListeners!=null)
1459 {
1460 final ServletRequestAttributeEvent event =
1461 new ServletRequestAttributeEvent(_context,this,name, old_value==null?value:old_value);
1462 final int size=LazyList.size(_requestAttributeListeners);
1463 for(int i=0;i<size;i++)
1464 {
1465 final EventListener listener = (ServletRequestAttributeListener)LazyList.get(_requestAttributeListeners,i);
1466 if (listener instanceof ServletRequestAttributeListener)
1467 {
1468 final ServletRequestAttributeListener l = (ServletRequestAttributeListener)listener;
1469
1470 if (old_value==null)
1471 l.attributeAdded(event);
1472 else if (value==null)
1473 l.attributeRemoved(event);
1474 else
1475 l.attributeReplaced(event);
1476 }
1477 }
1478 }
1479 }
1480
1481
1482
1483
1484 public void setAttributes(Attributes attributes)
1485 {
1486 _attributes=attributes;
1487 }
1488
1489
1490
1491
1492
1493
1494
1495
1496 public void setAuthentication(Authentication authentication)
1497 {
1498 _authentication = authentication;
1499 }
1500
1501
1502
1503
1504
1505 public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException
1506 {
1507 if (_inputState!=__NONE)
1508 return;
1509
1510 _characterEncoding=encoding;
1511
1512
1513 if (!StringUtil.isUTF8(encoding))
1514
1515 "".getBytes(encoding);
1516 }
1517
1518
1519
1520
1521
1522 public void setCharacterEncodingUnchecked(String encoding)
1523 {
1524 _characterEncoding=encoding;
1525 }
1526
1527
1528
1529 protected final void setConnection(HttpConnection connection)
1530 {
1531 _connection=connection;
1532 _async.setConnection(connection);
1533 _endp=connection.getEndPoint();
1534 _dns=connection.getResolveNames();
1535 }
1536
1537
1538
1539
1540
1541 public void setContentType(String contentType)
1542 {
1543 _connection.getRequestFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,contentType);
1544
1545 }
1546
1547
1548
1549
1550
1551 public void setContext(Context context)
1552 {
1553 _newContext=_context!=context;
1554 _context=context;
1555 }
1556
1557
1558
1559
1560
1561 public boolean takeNewContext()
1562 {
1563 boolean nc=_newContext;
1564 _newContext=false;
1565 return nc;
1566 }
1567
1568
1569
1570
1571
1572
1573 public void setContextPath(String contextPath)
1574 {
1575 _contextPath = contextPath;
1576 }
1577
1578
1579
1580
1581
1582 public void setCookies(Cookie[] cookies)
1583 {
1584 if (_cookies==null)
1585 _cookies=new CookieCutter();
1586 _cookies.setCookies(cookies);
1587 }
1588
1589
1590 public void setDispatcherType(DispatcherType type)
1591 {
1592 _dispatcherType=type;
1593 }
1594
1595
1596 public void setHandled(boolean h)
1597 {
1598 _handled=h;
1599 }
1600
1601
1602
1603
1604
1605 public void setMethod(String method)
1606 {
1607 _method = method;
1608 }
1609
1610
1611
1612
1613
1614 public void setParameters(MultiMap parameters)
1615 {
1616 _parameters= (parameters==null)?_baseParameters:parameters;
1617 if (_paramsExtracted && _parameters==null)
1618 throw new IllegalStateException();
1619 }
1620
1621
1622
1623
1624
1625 public void setPathInfo(String pathInfo)
1626 {
1627 _pathInfo = pathInfo;
1628 }
1629
1630
1631
1632
1633
1634 public void setProtocol(String protocol)
1635 {
1636 _protocol = protocol;
1637 }
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649 public void setQueryEncoding(String queryEncoding)
1650 {
1651 _queryEncoding=queryEncoding;
1652 _queryString=null;
1653 }
1654
1655
1656
1657
1658
1659 public void setQueryString(String queryString)
1660 {
1661 _queryString = queryString;
1662 }
1663
1664
1665
1666
1667
1668 public void setRemoteAddr(String addr)
1669 {
1670 _remoteAddr = addr;
1671 }
1672
1673
1674
1675
1676
1677 public void setRemoteHost(String host)
1678 {
1679 _remoteHost = host;
1680 }
1681
1682
1683
1684
1685
1686 public void setRequestedSessionId(String requestedSessionId)
1687 {
1688 _requestedSessionId = requestedSessionId;
1689 }
1690
1691
1692
1693
1694
1695 public void setRequestedSessionIdFromCookie(boolean requestedSessionIdCookie)
1696 {
1697 _requestedSessionIdFromCookie = requestedSessionIdCookie;
1698 }
1699
1700
1701
1702
1703
1704 public void setRequestURI(String requestURI)
1705 {
1706 _requestURI = requestURI;
1707 }
1708
1709
1710
1711
1712
1713 public void setScheme(String scheme)
1714 {
1715 _scheme = scheme;
1716 }
1717
1718
1719
1720
1721
1722 public void setServerName(String host)
1723 {
1724 _serverName = host;
1725 }
1726
1727
1728
1729
1730
1731 public void setServerPort(int port)
1732 {
1733 _port = port;
1734 }
1735
1736
1737
1738
1739
1740 public void setServletPath(String servletPath)
1741 {
1742 _servletPath = servletPath;
1743 }
1744
1745
1746
1747
1748
1749 public void setSession(HttpSession session)
1750 {
1751 _session = session;
1752 }
1753
1754
1755
1756
1757
1758 public void setSessionManager(SessionManager sessionManager)
1759 {
1760 _sessionManager = sessionManager;
1761 }
1762
1763
1764 public void setTimeStamp(long ts)
1765 {
1766 _timeStamp = ts;
1767 }
1768
1769
1770
1771
1772
1773 public void setUri(HttpURI uri)
1774 {
1775 _uri = uri;
1776 }
1777
1778
1779 public void setUserIdentityScope(UserIdentity.Scope scope)
1780 {
1781 _scope=scope;
1782 }
1783
1784
1785 public AsyncContext startAsync() throws IllegalStateException
1786 {
1787 if (!_asyncSupported)
1788 throw new IllegalStateException("!asyncSupported");
1789 _async.suspend(_context,this,_connection._response);
1790 return _async;
1791 }
1792
1793
1794 public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException
1795 {
1796 if (!_asyncSupported)
1797 throw new IllegalStateException("!asyncSupported");
1798 _async.suspend(_context,servletRequest,servletResponse);
1799 return _async;
1800 }
1801
1802
1803 public String toString()
1804 {
1805 return (_handled?"[":"(")+getMethod()+" "+_uri+(_handled?"]@":")@")+hashCode()+" "+super.toString();
1806 }
1807
1808 }
1809