0001: //========================================================================
0002: //$Id: Request.java,v 1.15 2005/11/16 22:02:40 gregwilkins Exp $
0003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
0004: //------------------------------------------------------------------------
0005: //Licensed under the Apache License, Version 2.0 (the "License");
0006: //you may not use this file except in compliance with the License.
0007: //You may obtain a copy of the License at
0008: //http://www.apache.org/licenses/LICENSE-2.0
0009: //Unless required by applicable law or agreed to in writing, software
0010: //distributed under the License is distributed on an "AS IS" BASIS,
0011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012: //See the License for the specific language governing permissions and
0013: //limitations under the License.
0014: //========================================================================
0015:
0016: package org.mortbay.jetty;
0017:
0018: import java.io.BufferedReader;
0019: import java.io.IOException;
0020: import java.io.InputStream;
0021: import java.io.InputStreamReader;
0022: import java.io.UnsupportedEncodingException;
0023: import java.net.InetAddress;
0024: import java.security.Principal;
0025: import java.util.Collection;
0026: import java.util.Collections;
0027: import java.util.Enumeration;
0028: import java.util.EventListener;
0029: import java.util.HashMap;
0030: import java.util.Iterator;
0031: import java.util.List;
0032: import java.util.Locale;
0033: import java.util.Map;
0034:
0035: import javax.servlet.RequestDispatcher;
0036: import javax.servlet.ServletInputStream;
0037: import javax.servlet.ServletRequestAttributeEvent;
0038: import javax.servlet.ServletRequestAttributeListener;
0039: import javax.servlet.ServletRequestWrapper;
0040: import javax.servlet.http.Cookie;
0041: import javax.servlet.http.HttpServletRequest;
0042: import javax.servlet.http.HttpSession;
0043:
0044: import org.mortbay.io.Buffer;
0045: import org.mortbay.io.BufferUtil;
0046: import org.mortbay.io.EndPoint;
0047: import org.mortbay.io.Portable;
0048: import org.mortbay.jetty.handler.ContextHandler;
0049: import org.mortbay.jetty.handler.ContextHandler.SContext;
0050: import org.mortbay.jetty.security.Authenticator;
0051: import org.mortbay.jetty.security.SecurityHandler;
0052: import org.mortbay.jetty.security.UserRealm;
0053: import org.mortbay.jetty.servlet.AbstractSessionManager.Session;
0054: import org.mortbay.log.Log;
0055: import org.mortbay.util.Attributes;
0056: import org.mortbay.util.AttributesMap;
0057: import org.mortbay.util.LazyList;
0058: import org.mortbay.util.MultiMap;
0059: import org.mortbay.util.QuotedStringTokenizer;
0060: import org.mortbay.util.StringUtil;
0061: import org.mortbay.util.URIUtil;
0062: import org.mortbay.util.UrlEncoded;
0063: import org.mortbay.util.ajax.Continuation;
0064:
0065: /* ------------------------------------------------------------ */
0066: /** Jetty Request.
0067: * <p>
0068: * Implements {@link javax.servlet.HttpServletRequest} from the {@link javax.servlet} package.
0069: * </p>
0070: * <p>
0071: * The standard interface of mostly getters,
0072: * is extended with setters so that the request is mutable by the handlers that it is
0073: * passed to. This allows the request object to be as lightweight as possible and not
0074: * actually implement any significant behaviour. For example<ul>
0075: *
0076: * <li>The {@link getContextPath} method will return null, until the requeset has been
0077: * passed to a {@link ContextHandler} which matches the {@link getPathInfo} with a context
0078: * path and calls {@link setContextPath} as a result.</li>
0079: *
0080: * <li>the HTTP session methods
0081: * will all return null sessions until such time as a request has been passed to
0082: * a {@link org.mortbay.jetty.servlet.SessionHandler} which checks for session cookies
0083: * and enables the ability to create new sessions.</li>
0084: *
0085: * <li>The {@link getServletPath} method will return null until the request has been
0086: * passed to a {@link org.mortbay.jetty.servlet.ServletHandler} and the pathInfo matched
0087: * against the servlet URL patterns and {@link setServletPath} called as a result.</li>
0088: * </ul>
0089: *
0090: * A request instance is created for each {@link HttpConnection} accepted by the server
0091: * and recycled for each HTTP request received via that connection. An effort is made
0092: * to avoid reparsing headers and cookies that are likely to be the same for
0093: * requests from the same connection.
0094: *
0095: * @author gregw
0096: *
0097: */
0098: public class Request implements HttpServletRequest {
0099: private static final Collection __defaultLocale = Collections
0100: .singleton(Locale.getDefault());
0101: private static final int __NONE = 0, _STREAM = 1, __READER = 2;
0102:
0103: private boolean _handled = false;
0104: private HttpConnection _connection;
0105: private EndPoint _endp;
0106: private Map _roleMap;
0107:
0108: private Attributes _attributes;
0109: private String _authType;
0110: private String _characterEncoding;
0111: private String _queryEncoding;
0112: private String _serverName;
0113: private String _method;
0114: private String _pathInfo;
0115: private int _port;
0116: private String _protocol = HttpVersions.HTTP_1_1;
0117: private String _queryString;
0118: private String _requestedSessionId;
0119: private boolean _requestedSessionIdFromCookie = false;
0120: private String _requestURI;
0121: private String _scheme = URIUtil.HTTP;
0122: private String _contextPath;
0123: private String _servletPath;
0124: private String _servletName;
0125: private HttpURI _uri;
0126: private Principal _userPrincipal;
0127: private MultiMap _parameters;
0128: private MultiMap _baseParameters;
0129: private boolean _paramsExtracted;
0130: private int _inputState = __NONE;
0131: private BufferedReader _reader;
0132: private boolean _dns = false;
0133: private ContextHandler.SContext _context;
0134: private HttpSession _session;
0135: private SessionManager _sessionManager;
0136: private boolean _cookiesExtracted = false;
0137: private Cookie[] _cookies;
0138: private String[] _lastCookies;
0139: private long _timeStamp;
0140: private String _timeStampStr;
0141: private Continuation _continuation;
0142: private Object _requestAttributeListeners;
0143: private Map _savedNewSessions;
0144: private UserRealm _userRealm;
0145:
0146: /* ------------------------------------------------------------ */
0147: /**
0148: *
0149: */
0150: public Request(HttpConnection connection) {
0151: _connection = connection;
0152: _endp = connection.getEndPoint();
0153: _dns = _connection.getResolveNames();
0154: }
0155:
0156: /* ------------------------------------------------------------ */
0157: protected void recycle() {
0158: _handled = false;
0159: if (_context != null)
0160: throw new IllegalStateException("Request in context!");
0161: if (_attributes != null)
0162: _attributes.clearAttributes();
0163: _authType = null;
0164: _characterEncoding = null;
0165: _queryEncoding = null;
0166: _context = null;
0167: _serverName = null;
0168: _method = null;
0169: _pathInfo = null;
0170: _port = 0;
0171: _protocol = HttpVersions.HTTP_1_1;
0172: _queryString = null;
0173: _requestedSessionId = null;
0174: _requestedSessionIdFromCookie = false;
0175: _session = null;
0176: _requestURI = null;
0177: _scheme = URIUtil.HTTP;
0178: _servletPath = null;
0179: _uri = null;
0180: _userPrincipal = null;
0181: if (_baseParameters != null)
0182: _baseParameters.clear();
0183: _parameters = null;
0184: _paramsExtracted = false;
0185: _inputState = __NONE;
0186: _reader = null;
0187: _cookiesExtracted = false;
0188: if (_savedNewSessions != null)
0189: _savedNewSessions.clear();
0190: _savedNewSessions = null;
0191: if (_continuation != null && _continuation.isPending())
0192: _continuation.reset();
0193: }
0194:
0195: /* ------------------------------------------------------------ */
0196: /**
0197: * Get Request TimeStamp
0198: *
0199: * @return The time that the request was received.
0200: */
0201: public String getTimeStampStr() {
0202: if (_timeStampStr == null && _timeStamp > 0)
0203: _timeStampStr = HttpFields.__dateCache.format(_timeStamp);
0204: return _timeStampStr;
0205: }
0206:
0207: /* ------------------------------------------------------------ */
0208: /**
0209: * Get Request TimeStamp
0210: *
0211: * @return The time that the request was received.
0212: */
0213: public long getTimeStamp() {
0214: return _timeStamp;
0215: }
0216:
0217: /* ------------------------------------------------------------ */
0218: public void setTimeStamp(long ts) {
0219: _timeStamp = ts;
0220: }
0221:
0222: /* ------------------------------------------------------------ */
0223: public boolean isHandled() {
0224: return _handled;
0225: }
0226:
0227: /* ------------------------------------------------------------ */
0228: public void setHandled(boolean h) {
0229: _handled = h;
0230: }
0231:
0232: /* ------------------------------------------------------------ */
0233: /*
0234: * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
0235: */
0236: public Object getAttribute(String name) {
0237: if ("org.mortbay.jetty.ajax.Continuation".equals(name))
0238: return getContinuation(true);
0239:
0240: if (_attributes == null)
0241: return null;
0242: return _attributes.getAttribute(name);
0243: }
0244:
0245: /* ------------------------------------------------------------ */
0246: /*
0247: * @see javax.servlet.ServletRequest#getAttributeNames()
0248: */
0249: public Enumeration getAttributeNames() {
0250: if (_attributes == null)
0251: return Collections.enumeration(Collections.EMPTY_LIST);
0252: return _attributes.getAttributeNames();
0253: }
0254:
0255: /* ------------------------------------------------------------ */
0256: /*
0257: * @see javax.servlet.http.HttpServletRequest#getAuthType()
0258: */
0259: public String getAuthType() {
0260: return _authType;
0261: }
0262:
0263: /* ------------------------------------------------------------ */
0264: /*
0265: * @see javax.servlet.ServletRequest#getCharacterEncoding()
0266: */
0267: public String getCharacterEncoding() {
0268: return _characterEncoding;
0269: }
0270:
0271: /* ------------------------------------------------------------ */
0272: /*
0273: * @see javax.servlet.ServletRequest#getContentLength()
0274: */
0275: public int getContentLength() {
0276: return (int) _connection.getRequestFields().getLongField(
0277: HttpHeaders.CONTENT_LENGTH_BUFFER);
0278: }
0279:
0280: /* ------------------------------------------------------------ */
0281: /*
0282: * @see javax.servlet.ServletRequest#getContentType()
0283: */
0284: public String getContentType() {
0285: return _connection.getRequestFields().getStringField(
0286: HttpHeaders.CONTENT_TYPE_BUFFER);
0287: }
0288:
0289: /* ------------------------------------------------------------ */
0290: /*
0291: * @see javax.servlet.ServletRequest#getContentType()
0292: */
0293: public void setContentType(String contentType) {
0294: _connection.getRequestFields().put(
0295: HttpHeaders.CONTENT_TYPE_BUFFER, contentType);
0296:
0297: }
0298:
0299: /* ------------------------------------------------------------ */
0300: /*
0301: * @see javax.servlet.http.HttpServletRequest#getContextPath()
0302: */
0303: public String getContextPath() {
0304: return _contextPath;
0305: }
0306:
0307: /* ------------------------------------------------------------ */
0308: /*
0309: * @see javax.servlet.http.HttpServletRequest#getCookies()
0310: */
0311: public Cookie[] getCookies() {
0312: if (_cookiesExtracted)
0313: return _cookies;
0314:
0315: try {
0316: // Handle no cookies
0317: if (!_connection.getRequestFields().containsKey(
0318: HttpHeaders.COOKIE_BUFFER)) {
0319: _cookies = null;
0320: _cookiesExtracted = true;
0321: _lastCookies = null;
0322: return _cookies;
0323: }
0324:
0325: // Check if cookie headers match last cookies
0326: if (_lastCookies != null) {
0327: int last = 0;
0328: Enumeration enm = _connection.getRequestFields()
0329: .getValues(HttpHeaders.COOKIE_BUFFER);
0330: while (enm.hasMoreElements()) {
0331: String c = (String) enm.nextElement();
0332: if (last >= _lastCookies.length
0333: || !c.equals(_lastCookies[last])) {
0334: _lastCookies = null;
0335: break;
0336: }
0337: last++;
0338: }
0339: if (_lastCookies != null) {
0340: _cookiesExtracted = true;
0341: return _cookies;
0342: }
0343: }
0344:
0345: // Get ready to parse cookies (Expensive!!!)
0346: Object cookies = null;
0347: Object lastCookies = null;
0348:
0349: int version = 0;
0350: Cookie cookie = null;
0351:
0352: // For each cookie header
0353: Enumeration enm = _connection.getRequestFields().getValues(
0354: HttpHeaders.COOKIE_BUFFER);
0355: while (enm.hasMoreElements()) {
0356: // Save a copy of the unparsed header as cache.
0357: String hdr = (String) enm.nextElement();
0358: lastCookies = LazyList.add(lastCookies, hdr);
0359:
0360: // Parse the header
0361: QuotedStringTokenizer tok = new QuotedStringTokenizer(
0362: hdr, ";", false, false);
0363: tok.setSingle(false);
0364: while (tok.hasMoreElements()) {
0365: String c = (String) tok.nextElement();
0366: if (c == null)
0367: continue;
0368: c = c.trim();
0369:
0370: try {
0371: String n;
0372: String v;
0373: int e = c.indexOf('=');
0374: if (e > 0) {
0375: n = c.substring(0, e);
0376: v = c.substring(e + 1);
0377: } else {
0378: n = c;
0379: v = "";
0380: }
0381:
0382: // Handle quoted values
0383: if (version > 0)
0384: v = StringUtil.unquote(v);
0385:
0386: // Ignore $ names
0387: if (n.startsWith("$")) {
0388: if ("$version".equalsIgnoreCase(n))
0389: version = Integer.parseInt(StringUtil
0390: .unquote(v));
0391: else if ("$path".equalsIgnoreCase(n)
0392: && cookie != null)
0393: cookie.setPath(v);
0394: else if ("$domain".equalsIgnoreCase(n)
0395: && cookie != null)
0396: cookie.setDomain(v);
0397: continue;
0398: }
0399:
0400: v = URIUtil.decodePath(v);
0401: cookie = new Cookie(n, v);
0402: if (version > 0)
0403: cookie.setVersion(version);
0404: cookies = LazyList.add(cookies, cookie);
0405: } catch (Exception ex) {
0406: Log.ignore(ex);
0407: }
0408: }
0409: }
0410:
0411: int l = LazyList.size(cookies);
0412: _cookiesExtracted = true;
0413: if (l > 0) {
0414: if (_cookies == null || _cookies.length != l)
0415: _cookies = new Cookie[l];
0416: for (int i = 0; i < l; i++)
0417: _cookies[i] = (Cookie) LazyList.get(cookies, i);
0418:
0419: l = LazyList.size(lastCookies);
0420: _lastCookies = new String[l];
0421: for (int i = 0; i < l; i++)
0422: _lastCookies[i] = (String) LazyList.get(
0423: lastCookies, i);
0424: }
0425: } catch (Exception e) {
0426: Log.warn(e);
0427: }
0428:
0429: if (_cookies == null || _cookies.length == 0)
0430: return null;
0431: return _cookies;
0432: }
0433:
0434: /* ------------------------------------------------------------ */
0435: /*
0436: * @see javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
0437: */
0438: public long getDateHeader(String name) {
0439: return _connection.getRequestFields().getDateField(name);
0440: }
0441:
0442: /* ------------------------------------------------------------ */
0443: /*
0444: * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
0445: */
0446: public String getHeader(String name) {
0447: return _connection.getRequestFields().getStringField(name);
0448: }
0449:
0450: /* ------------------------------------------------------------ */
0451: /*
0452: * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
0453: */
0454: public Enumeration getHeaderNames() {
0455: return _connection.getRequestFields().getFieldNames();
0456: }
0457:
0458: /* ------------------------------------------------------------ */
0459: /*
0460: * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
0461: */
0462: public Enumeration getHeaders(String name) {
0463: Enumeration e = _connection.getRequestFields().getValues(name);
0464: if (e == null)
0465: return Collections.enumeration(Collections.EMPTY_LIST);
0466: return e;
0467: }
0468:
0469: /* ------------------------------------------------------------ */
0470: /*
0471: * @see javax.servlet.ServletRequest#getInputStream()
0472: */
0473: public ServletInputStream getInputStream() throws IOException {
0474: if (_inputState != __NONE && _inputState != _STREAM)
0475: throw new IllegalStateException("READER");
0476: _inputState = _STREAM;
0477: return _connection.getInputStream();
0478: }
0479:
0480: /* ------------------------------------------------------------ */
0481: /*
0482: * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
0483: */
0484: public int getIntHeader(String name) {
0485: return (int) _connection.getRequestFields().getLongField(name);
0486: }
0487:
0488: /* ------------------------------------------------------------ */
0489: /*
0490: * @see javax.servlet.ServletRequest#getLocalAddr()
0491: */
0492: public String getLocalAddr() {
0493: return _endp == null ? null : _endp.getLocalAddr();
0494: }
0495:
0496: /* ------------------------------------------------------------ */
0497: /*
0498: * @see javax.servlet.ServletRequest#getLocale()
0499: */
0500: public Locale getLocale() {
0501: Enumeration enm = _connection.getRequestFields().getValues(
0502: HttpHeaders.ACCEPT_LANGUAGE, HttpFields.__separators);
0503:
0504: // handle no locale
0505: if (enm == null || !enm.hasMoreElements())
0506: return Locale.getDefault();
0507:
0508: // sort the list in quality order
0509: List acceptLanguage = HttpFields.qualityList(enm);
0510: if (acceptLanguage.size() == 0)
0511: return Locale.getDefault();
0512:
0513: int size = acceptLanguage.size();
0514:
0515: // convert to locals
0516: for (int i = 0; i < size; i++) {
0517: String language = (String) acceptLanguage.get(i);
0518: language = HttpFields.valueParameters(language, null);
0519: String country = "";
0520: int dash = language.indexOf('-');
0521: if (dash > -1) {
0522: country = language.substring(dash + 1).trim();
0523: language = language.substring(0, dash).trim();
0524: }
0525: return new Locale(language, country);
0526: }
0527:
0528: return Locale.getDefault();
0529: }
0530:
0531: /* ------------------------------------------------------------ */
0532: /*
0533: * @see javax.servlet.ServletRequest#getLocales()
0534: */
0535: public Enumeration getLocales() {
0536:
0537: Enumeration enm = _connection.getRequestFields().getValues(
0538: HttpHeaders.ACCEPT_LANGUAGE, HttpFields.__separators);
0539:
0540: // handle no locale
0541: if (enm == null || !enm.hasMoreElements())
0542: return Collections.enumeration(__defaultLocale);
0543:
0544: // sort the list in quality order
0545: List acceptLanguage = HttpFields.qualityList(enm);
0546:
0547: if (acceptLanguage.size() == 0)
0548: return Collections.enumeration(__defaultLocale);
0549:
0550: Object langs = null;
0551: int size = acceptLanguage.size();
0552:
0553: // convert to locals
0554: for (int i = 0; i < size; i++) {
0555: String language = (String) acceptLanguage.get(i);
0556: language = HttpFields.valueParameters(language, null);
0557: String country = "";
0558: int dash = language.indexOf('-');
0559: if (dash > -1) {
0560: country = language.substring(dash + 1).trim();
0561: language = language.substring(0, dash).trim();
0562: }
0563: langs = LazyList.ensureSize(langs, size);
0564: langs = LazyList.add(langs, new Locale(language, country));
0565: }
0566:
0567: if (LazyList.size(langs) == 0)
0568: return Collections.enumeration(__defaultLocale);
0569:
0570: return Collections.enumeration(LazyList.getList(langs));
0571: }
0572:
0573: /* ------------------------------------------------------------ */
0574: /*
0575: * @see javax.servlet.ServletRequest#getLocalName()
0576: */
0577: public String getLocalName() {
0578: if (_dns)
0579: return _endp == null ? null : _endp.getLocalHost();
0580: return _endp == null ? null : _endp.getLocalAddr();
0581: }
0582:
0583: /* ------------------------------------------------------------ */
0584: /*
0585: * @see javax.servlet.ServletRequest#getLocalPort()
0586: */
0587: public int getLocalPort() {
0588: return _endp == null ? 0 : _endp.getLocalPort();
0589: }
0590:
0591: /* ------------------------------------------------------------ */
0592: /*
0593: * @see javax.servlet.http.HttpServletRequest#getMethod()
0594: */
0595: public String getMethod() {
0596: return _method;
0597: }
0598:
0599: /* ------------------------------------------------------------ */
0600: /*
0601: * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
0602: */
0603: public String getParameter(String name) {
0604: if (!_paramsExtracted)
0605: extractParameters();
0606: return (String) _parameters.getValue(name, 0);
0607: }
0608:
0609: /* ------------------------------------------------------------ */
0610: /*
0611: * @see javax.servlet.ServletRequest#getParameterMap()
0612: */
0613: public Map getParameterMap() {
0614: if (!_paramsExtracted)
0615: extractParameters();
0616:
0617: return Collections.unmodifiableMap(_parameters
0618: .toStringArrayMap());
0619: }
0620:
0621: /* ------------------------------------------------------------ */
0622: /*
0623: * @see javax.servlet.ServletRequest#getParameterNames()
0624: */
0625: public Enumeration getParameterNames() {
0626: if (!_paramsExtracted)
0627: extractParameters();
0628: return Collections.enumeration(_parameters.keySet());
0629: }
0630:
0631: /* ------------------------------------------------------------ */
0632: /*
0633: * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
0634: */
0635: public String[] getParameterValues(String name) {
0636: if (!_paramsExtracted)
0637: extractParameters();
0638: List vals = _parameters.getValues(name);
0639: if (vals == null)
0640: return null;
0641: return (String[]) vals.toArray(new String[vals.size()]);
0642: }
0643:
0644: /* ------------------------------------------------------------ */
0645: /*
0646: * @see javax.servlet.http.HttpServletRequest#getPathInfo()
0647: */
0648: public String getPathInfo() {
0649: return _pathInfo;
0650: }
0651:
0652: /* ------------------------------------------------------------ */
0653: /*
0654: * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
0655: */
0656: public String getPathTranslated() {
0657: if (_pathInfo == null || _context == null)
0658: return null;
0659: return _context.getRealPath(_pathInfo);
0660: }
0661:
0662: /* ------------------------------------------------------------ */
0663: /*
0664: * @see javax.servlet.ServletRequest#getProtocol()
0665: */
0666: public String getProtocol() {
0667: return _protocol;
0668: }
0669:
0670: /* ------------------------------------------------------------ */
0671: /*
0672: * @see javax.servlet.ServletRequest#getReader()
0673: */
0674: public BufferedReader getReader() throws IOException {
0675: if (_inputState != __NONE && _inputState != __READER)
0676: throw new IllegalStateException("STREAMED");
0677: if (_reader == null) {
0678: String encoding = getCharacterEncoding();
0679: if (encoding == null)
0680: encoding = StringUtil.__ISO_8859_1;
0681: _reader = new BufferedReader(new InputStreamReader(
0682: getInputStream(), encoding));
0683:
0684: }
0685: _inputState = __READER;
0686: return _reader;
0687: }
0688:
0689: /* ------------------------------------------------------------ */
0690: /*
0691: * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
0692: */
0693: public String getRealPath(String path) {
0694: if (_context == null)
0695: return null;
0696: return _context.getRealPath(path);
0697: }
0698:
0699: /* ------------------------------------------------------------ */
0700: /*
0701: * @see javax.servlet.ServletRequest#getRemoteAddr()
0702: */
0703: public String getRemoteAddr() {
0704: return _endp == null ? null : _endp.getRemoteAddr();
0705: }
0706:
0707: /* ------------------------------------------------------------ */
0708: /*
0709: * @see javax.servlet.ServletRequest#getRemoteHost()
0710: */
0711: public String getRemoteHost() {
0712: if (_dns)
0713: return _endp == null ? null : _endp.getRemoteHost();
0714: return _endp == null ? null : _endp.getRemoteAddr();
0715: }
0716:
0717: /* ------------------------------------------------------------ */
0718: /*
0719: * @see javax.servlet.ServletRequest#getRemotePort()
0720: */
0721: public int getRemotePort() {
0722: return _endp == null ? 0 : _endp.getRemotePort();
0723: }
0724:
0725: /* ------------------------------------------------------------ */
0726: /*
0727: * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
0728: */
0729: public String getRemoteUser() {
0730: Principal p = getUserPrincipal();
0731: if (p == null)
0732: return null;
0733: return p.getName();
0734: }
0735:
0736: /* ------------------------------------------------------------ */
0737: /*
0738: * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
0739: */
0740: public RequestDispatcher getRequestDispatcher(String path) {
0741: if (path == null || _context == null)
0742: return null;
0743:
0744: // handle relative path
0745: if (!path.startsWith("/")) {
0746: String relTo = URIUtil.addPaths(_servletPath, _pathInfo);
0747: int slash = relTo.lastIndexOf("/");
0748: if (slash > 1)
0749: relTo = relTo.substring(0, slash + 1);
0750: else
0751: relTo = "/";
0752: path = URIUtil.addPaths(relTo, path);
0753: }
0754:
0755: return _context.getRequestDispatcher(path);
0756: }
0757:
0758: /* ------------------------------------------------------------ */
0759: /*
0760: * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
0761: */
0762: public String getRequestedSessionId() {
0763: return _requestedSessionId;
0764: }
0765:
0766: /* ------------------------------------------------------------ */
0767: /*
0768: * @see javax.servlet.http.HttpServletRequest#getRequestURI()
0769: */
0770: public String getRequestURI() {
0771: if (_requestURI == null && _uri != null)
0772: _requestURI = _uri.getPathAndParam();
0773: return _requestURI;
0774: }
0775:
0776: /* ------------------------------------------------------------ */
0777: /*
0778: * @see javax.servlet.http.HttpServletRequest#getRequestURL()
0779: */
0780: public StringBuffer getRequestURL() {
0781: StringBuffer url = new StringBuffer(48);
0782: synchronized (url) {
0783: String scheme = getScheme();
0784: int port = getServerPort();
0785:
0786: url.append(scheme);
0787: url.append("://");
0788: url.append(getServerName());
0789: if (_port > 0
0790: && ((scheme.equalsIgnoreCase(URIUtil.HTTP) && port != 80) || (scheme
0791: .equalsIgnoreCase(URIUtil.HTTPS) && port != 443))) {
0792: url.append(':');
0793: url.append(_port);
0794: }
0795:
0796: url.append(getRequestURI());
0797: return url;
0798: }
0799: }
0800:
0801: /* ------------------------------------------------------------ */
0802: /*
0803: * @see javax.servlet.ServletRequest#getScheme()
0804: */
0805: public String getScheme() {
0806: return _scheme;
0807: }
0808:
0809: /* ------------------------------------------------------------ */
0810: /*
0811: * @see javax.servlet.ServletRequest#getServerName()
0812: */
0813: public String getServerName() {
0814: // Return already determined host
0815: if (_serverName != null)
0816: return _serverName;
0817:
0818: // Return host from absolute URI
0819: _serverName = _uri.getHost();
0820: _port = _uri.getPort();
0821: if (_serverName != null)
0822: return _serverName;
0823:
0824: // Return host from header field
0825: Buffer hostPort = _connection.getRequestFields().get(
0826: HttpHeaders.HOST_BUFFER);
0827: if (hostPort != null) {
0828: for (int i = hostPort.length(); i-- > 0;) {
0829: if (hostPort.peek(hostPort.getIndex() + i) == ':') {
0830: _serverName = hostPort.peek(hostPort.getIndex(), i)
0831: .toString();
0832: _port = BufferUtil.toInt(hostPort.peek(hostPort
0833: .getIndex()
0834: + i + 1, hostPort.length() - i - 1));
0835: return _serverName;
0836: }
0837: }
0838: if (_serverName == null || _port < 0) {
0839: _serverName = hostPort.toString();
0840: _port = 0;
0841: }
0842:
0843: return _serverName;
0844: }
0845:
0846: // Return host from connection
0847: if (_connection != null) {
0848: _serverName = getLocalName();
0849: _port = getLocalPort();
0850: if (_serverName != null
0851: && !Portable.ALL_INTERFACES.equals(_serverName))
0852: return _serverName;
0853: }
0854:
0855: // Return the local host
0856: try {
0857: _serverName = InetAddress.getLocalHost().getHostAddress();
0858: } catch (java.net.UnknownHostException e) {
0859: Log.ignore(e);
0860: }
0861: return _serverName;
0862: }
0863:
0864: /* ------------------------------------------------------------ */
0865: /*
0866: * @see javax.servlet.ServletRequest#getServerPort()
0867: */
0868: public int getServerPort() {
0869: if (_port <= 0) {
0870: if (_serverName == null)
0871: getServerName();
0872:
0873: if (_port <= 0) {
0874: if (_serverName != null && _uri != null)
0875: _port = _uri.getPort();
0876: else
0877: _port = _endp == null ? 0 : _endp.getLocalPort();
0878: }
0879: }
0880:
0881: if (_port <= 0) {
0882: if (getScheme().equalsIgnoreCase(URIUtil.HTTPS))
0883: return 443;
0884: return 80;
0885: }
0886: return _port;
0887: }
0888:
0889: /* ------------------------------------------------------------ */
0890: /*
0891: * @see javax.servlet.http.HttpServletRequest#getServletPath()
0892: */
0893: public String getServletPath() {
0894: if (_servletPath == null)
0895: _servletPath = "";
0896: return _servletPath;
0897: }
0898:
0899: /* ------------------------------------------------------------ */
0900: /*
0901: */
0902: public String getServletName() {
0903: return _servletName;
0904: }
0905:
0906: /* ------------------------------------------------------------ */
0907: /*
0908: * @see javax.servlet.http.HttpServletRequest#getSession()
0909: */
0910: public HttpSession getSession() {
0911: return getSession(true);
0912: }
0913:
0914: /* ------------------------------------------------------------ */
0915: /*
0916: * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
0917: */
0918: public HttpSession getSession(boolean create) {
0919: if (_sessionManager == null && create)
0920: throw new IllegalStateException(
0921: "No SessionHandler or SessionManager");
0922:
0923: if (_session != null && _sessionManager != null
0924: && _sessionManager.isValid(_session))
0925: return _session;
0926:
0927: _session = null;
0928:
0929: String id = getRequestedSessionId();
0930:
0931: if (id != null && _sessionManager != null) {
0932: _session = _sessionManager.getHttpSession(id);
0933: if (_session == null && !create)
0934: return null;
0935: }
0936:
0937: if (_session == null && _sessionManager != null && create) {
0938: _session = _sessionManager.newHttpSession(this );
0939: Cookie cookie = _sessionManager.getSessionCookie(_session,
0940: getContextPath(), isSecure());
0941: if (cookie != null)
0942: _connection.getResponse().addCookie(cookie);
0943: }
0944:
0945: return _session;
0946: }
0947:
0948: /* ------------------------------------------------------------ */
0949: /*
0950: * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
0951: */
0952: public Principal getUserPrincipal() {
0953: if (_userPrincipal != null
0954: && _userPrincipal instanceof SecurityHandler.NotChecked) {
0955: SecurityHandler.NotChecked not_checked = (SecurityHandler.NotChecked) _userPrincipal;
0956: _userPrincipal = SecurityHandler.__NO_USER;
0957:
0958: Authenticator auth = not_checked.getSecurityHandler()
0959: .getAuthenticator();
0960: UserRealm realm = not_checked.getSecurityHandler()
0961: .getUserRealm();
0962: String pathInContext = getPathInfo() == null ? getServletPath()
0963: : (getServletPath() + getPathInfo());
0964:
0965: if (realm != null && auth != null) {
0966: try {
0967: auth.authenticate(realm, pathInContext, this , null);
0968: } catch (Exception e) {
0969: Log.ignore(e);
0970: }
0971: }
0972: }
0973:
0974: if (_userPrincipal == SecurityHandler.__NO_USER)
0975: return null;
0976: return _userPrincipal;
0977: }
0978:
0979: /* ------------------------------------------------------------ */
0980: /*
0981: * @see javax.servlet.http.HttpServletRequest#getQueryString()
0982: */
0983: public String getQueryString() {
0984: if (_queryString == null && _uri != null)
0985: _queryString = _uri.getQuery(_queryEncoding);
0986: return _queryString;
0987: }
0988:
0989: /* ------------------------------------------------------------ */
0990: /*
0991: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
0992: */
0993: public boolean isRequestedSessionIdFromCookie() {
0994: return _requestedSessionId != null
0995: && _requestedSessionIdFromCookie;
0996: }
0997:
0998: /* ------------------------------------------------------------ */
0999: /*
1000: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
1001: */
1002: public boolean isRequestedSessionIdFromUrl() {
1003: return _requestedSessionId != null
1004: && !_requestedSessionIdFromCookie;
1005: }
1006:
1007: /* ------------------------------------------------------------ */
1008: /*
1009: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
1010: */
1011: public boolean isRequestedSessionIdFromURL() {
1012: return _requestedSessionId != null
1013: && !_requestedSessionIdFromCookie;
1014: }
1015:
1016: /* ------------------------------------------------------------ */
1017: /*
1018: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
1019: */
1020: public boolean isRequestedSessionIdValid() {
1021: HttpSession session = null;
1022: return _requestedSessionId != null
1023: && (session = getSession(false)) != null
1024: && _requestedSessionId.equals(session.getId());
1025: }
1026:
1027: /* ------------------------------------------------------------ */
1028: /*
1029: * @see javax.servlet.ServletRequest#isSecure()
1030: */
1031: public boolean isSecure() {
1032: return _connection.isConfidential(this );
1033: }
1034:
1035: /* ------------------------------------------------------------ */
1036: /*
1037: * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
1038: */
1039: public boolean isUserInRole(String role) {
1040: if (_roleMap != null) {
1041: String r = (String) _roleMap.get(role);
1042: if (r != null)
1043: role = r;
1044: }
1045:
1046: if (_userRealm != null && _userPrincipal != null)
1047: return _userRealm.isUserInRole(_userPrincipal, role);
1048:
1049: return false;
1050: }
1051:
1052: /* ------------------------------------------------------------ */
1053: /*
1054: * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
1055: */
1056: public void removeAttribute(String name) {
1057: Object old_value = _attributes == null ? null : _attributes
1058: .getAttribute(name);
1059:
1060: if (_attributes != null)
1061: _attributes.removeAttribute(name);
1062:
1063: if (old_value != null) {
1064: if (_requestAttributeListeners != null) {
1065: ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(
1066: _context, this , name, old_value);
1067:
1068: for (int i = 0; i < LazyList
1069: .size(_requestAttributeListeners); i++)
1070: ((ServletRequestAttributeListener) LazyList.get(
1071: _requestAttributeListeners, i))
1072: .attributeRemoved(event);
1073: }
1074: }
1075: }
1076:
1077: /* ------------------------------------------------------------ */
1078: /*
1079: * Set a request attribute.
1080: * if the attribute name is "org.mortbay.jetty.Request.queryEncoding" then
1081: * the value is also passed in a call to {@link #setQueryEncoding}.
1082: *
1083: * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
1084: */
1085: public void setAttribute(String name, Object value) {
1086: Object old_value = _attributes == null ? null : _attributes
1087: .getAttribute(name);
1088:
1089: if ("org.mortbay.jetty.Request.queryEncoding".equals(name))
1090: setQueryEncoding(value == null ? null : value.toString());
1091:
1092: if (_attributes == null)
1093: _attributes = new AttributesMap();
1094: _attributes.setAttribute(name, value);
1095:
1096: if (_requestAttributeListeners != null) {
1097: ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(
1098: _context, this , name, old_value == null ? value
1099: : old_value);
1100:
1101: for (int i = 0; i < LazyList
1102: .size(_requestAttributeListeners); i++) {
1103: ServletRequestAttributeListener l = (ServletRequestAttributeListener) LazyList
1104: .get(_requestAttributeListeners, i);
1105:
1106: if (old_value == null)
1107: l.attributeAdded(event);
1108: else if (value == null)
1109: l.attributeRemoved(event);
1110: else
1111: l.attributeReplaced(event);
1112: }
1113: }
1114: }
1115:
1116: /* ------------------------------------------------------------ */
1117: /*
1118: * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
1119: */
1120: public void setCharacterEncoding(String encoding)
1121: throws UnsupportedEncodingException {
1122: if (_inputState != __NONE)
1123: return;
1124:
1125: _characterEncoding = encoding;
1126:
1127: // check encoding is supported
1128: "".getBytes(encoding);
1129: }
1130:
1131: /* ------------------------------------------------------------ */
1132: /*
1133: * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
1134: */
1135: public void setCharacterEncodingUnchecked(String encoding) {
1136: _characterEncoding = encoding;
1137: }
1138:
1139: /* ------------------------------------------------------------ */
1140: /*
1141: * Extract Paramters from query string and/or form _content.
1142: */
1143: private void extractParameters() {
1144: if (_baseParameters == null)
1145: _baseParameters = new MultiMap(16);
1146:
1147: if (_paramsExtracted) {
1148: if (_parameters == null)
1149: _parameters = _baseParameters;
1150: return;
1151: }
1152:
1153: _paramsExtracted = true;
1154:
1155: // Handle query string
1156: if (_uri != null && _uri.getQuery() != null) {
1157: try {
1158: _uri.decodeQueryTo(_baseParameters, _queryEncoding);
1159: } catch (UnsupportedEncodingException e) {
1160: if (Log.isDebugEnabled())
1161: Log.warn(e);
1162: else
1163: Log.warn(e.toString());
1164: }
1165: }
1166:
1167: // handle any _content.
1168: String encoding = getCharacterEncoding();
1169: String content_type = getContentType();
1170: if (content_type != null && content_type.length() > 0) {
1171: content_type = HttpFields.valueParameters(content_type,
1172: null);
1173:
1174: if (MimeTypes.FORM_ENCODED.equalsIgnoreCase(content_type)
1175: && HttpMethods.POST.equals(getMethod())) {
1176: int content_length = getContentLength();
1177: if (content_length != 0) {
1178: try {
1179: int maxFormContentSize = -1;
1180:
1181: if (_context != null)
1182: maxFormContentSize = _context
1183: .getContextHandler()
1184: .getMaxFormContentSize();
1185: else {
1186: Integer size = (Integer) _connection
1187: .getConnector()
1188: .getServer()
1189: .getAttribute(
1190: "org.mortbay.jetty.Request.maxFormContentSize");
1191: if (size != null)
1192: maxFormContentSize = size.intValue();
1193: }
1194:
1195: if (content_length > maxFormContentSize
1196: && maxFormContentSize > 0)
1197: throw new IllegalStateException(
1198: "Form too large");
1199: InputStream in = getInputStream();
1200:
1201: // Add form params to query params
1202: UrlEncoded.decodeTo(in, _baseParameters,
1203: encoding,
1204: content_length < 0 ? maxFormContentSize
1205: : -1);
1206: } catch (IOException e) {
1207: if (Log.isDebugEnabled())
1208: Log.warn(e);
1209: else
1210: Log.warn(e.toString());
1211: }
1212: }
1213: }
1214: }
1215:
1216: if (_parameters == null)
1217: _parameters = _baseParameters;
1218: else if (_parameters != _baseParameters) {
1219: // Merge parameters (needed if parameters extracted after a forward).
1220: Iterator iter = _baseParameters.entrySet().iterator();
1221: while (iter.hasNext()) {
1222: Map.Entry entry = (Map.Entry) iter.next();
1223: String name = (String) entry.getKey();
1224: Object values = entry.getValue();
1225: for (int i = 0; i < LazyList.size(values); i++)
1226: _parameters.add(name, LazyList.get(values, i));
1227: }
1228: }
1229: }
1230:
1231: /* ------------------------------------------------------------ */
1232: /**
1233: * @param host The host to set.
1234: */
1235: public void setServerName(String host) {
1236: _serverName = host;
1237: }
1238:
1239: /* ------------------------------------------------------------ */
1240: /**
1241: * @param host The host to set.
1242: */
1243: public void setServerPort(int port) {
1244: _port = port;
1245: }
1246:
1247: /* ------------------------------------------------------------ */
1248: /**
1249: * @return Returns the uri.
1250: */
1251: public HttpURI getUri() {
1252: return _uri;
1253: }
1254:
1255: /* ------------------------------------------------------------ */
1256: /**
1257: * @param uri The uri to set.
1258: */
1259: public void setUri(HttpURI uri) {
1260: _uri = uri;
1261: }
1262:
1263: /* ------------------------------------------------------------ */
1264: /**
1265: * @return Returns the connection.
1266: */
1267: public HttpConnection getConnection() {
1268: return _connection;
1269: }
1270:
1271: /* ------------------------------------------------------------ */
1272: /**
1273: * @return Returns the inputState.
1274: */
1275: public int getInputState() {
1276: return _inputState;
1277: }
1278:
1279: /* ------------------------------------------------------------ */
1280: /**
1281: * @param authType The authType to set.
1282: */
1283: public void setAuthType(String authType) {
1284: _authType = authType;
1285: }
1286:
1287: /* ------------------------------------------------------------ */
1288: /**
1289: * @param cookies The cookies to set.
1290: */
1291: public void setCookies(Cookie[] cookies) {
1292: _cookies = cookies;
1293: }
1294:
1295: /* ------------------------------------------------------------ */
1296: /**
1297: * @param method The method to set.
1298: */
1299: public void setMethod(String method) {
1300: _method = method;
1301: }
1302:
1303: /* ------------------------------------------------------------ */
1304: /**
1305: * @param pathInfo The pathInfo to set.
1306: */
1307: public void setPathInfo(String pathInfo) {
1308: _pathInfo = pathInfo;
1309: }
1310:
1311: /* ------------------------------------------------------------ */
1312: /**
1313: * @param protocol The protocol to set.
1314: */
1315: public void setProtocol(String protocol) {
1316: _protocol = protocol;
1317: }
1318:
1319: /* ------------------------------------------------------------ */
1320: /**
1321: * @param requestedSessionId The requestedSessionId to set.
1322: */
1323: public void setRequestedSessionId(String requestedSessionId) {
1324: _requestedSessionId = requestedSessionId;
1325: }
1326:
1327: /* ------------------------------------------------------------ */
1328: /**
1329: * @return Returns the sessionManager.
1330: */
1331: public SessionManager getSessionManager() {
1332: return _sessionManager;
1333: }
1334:
1335: /* ------------------------------------------------------------ */
1336: /**
1337: * @param sessionManager The sessionManager to set.
1338: */
1339: public void setSessionManager(SessionManager sessionManager) {
1340: _sessionManager = sessionManager;
1341: }
1342:
1343: /* ------------------------------------------------------------ */
1344: /**
1345: * @param requestedSessionIdCookie The requestedSessionIdCookie to set.
1346: */
1347: public void setRequestedSessionIdFromCookie(
1348: boolean requestedSessionIdCookie) {
1349: _requestedSessionIdFromCookie = requestedSessionIdCookie;
1350: }
1351:
1352: /* ------------------------------------------------------------ */
1353: /**
1354: * @param session The session to set.
1355: */
1356: public void setSession(HttpSession session) {
1357: _session = session;
1358: }
1359:
1360: /* ------------------------------------------------------------ */
1361: /**
1362: * @param scheme The scheme to set.
1363: */
1364: public void setScheme(String scheme) {
1365: _scheme = scheme;
1366: }
1367:
1368: /* ------------------------------------------------------------ */
1369: /**
1370: * @param queryString The queryString to set.
1371: */
1372: public void setQueryString(String queryString) {
1373: _queryString = queryString;
1374: }
1375:
1376: /* ------------------------------------------------------------ */
1377: /**
1378: * @param requestURI The requestURI to set.
1379: */
1380: public void setRequestURI(String requestURI) {
1381: _requestURI = requestURI;
1382: }
1383:
1384: /* ------------------------------------------------------------ */
1385: /**
1386: * Sets the "context path" for this request
1387: * @see HttpServletRequest#getContextPath
1388: */
1389: public void setContextPath(String contextPath) {
1390: _contextPath = contextPath;
1391: }
1392:
1393: /* ------------------------------------------------------------ */
1394: /**
1395: * @param servletPath The servletPath to set.
1396: */
1397: public void setServletPath(String servletPath) {
1398: _servletPath = servletPath;
1399: }
1400:
1401: /* ------------------------------------------------------------ */
1402: /**
1403: * @param name The servletName to set.
1404: */
1405: public void setServletName(String name) {
1406: _servletName = name;
1407: }
1408:
1409: /* ------------------------------------------------------------ */
1410: /**
1411: * @param userPrincipal The userPrincipal to set.
1412: */
1413: public void setUserPrincipal(Principal userPrincipal) {
1414: _userPrincipal = userPrincipal;
1415: }
1416:
1417: /* ------------------------------------------------------------ */
1418: /**
1419: * @param context
1420: */
1421: public void setContext(SContext context) {
1422: _context = context;
1423: }
1424:
1425: /* ------------------------------------------------------------ */
1426: /**
1427: * @return The current {@link SContext context} used for this request, or <code>null</code> if {@link #setContext} has not yet
1428: * been called.
1429: */
1430: public SContext getContext() {
1431: return _context;
1432: }
1433:
1434: /* ------------------------------------------------------------ */
1435: /**
1436: * Reconstructs the URL the client used to make the request. The returned URL contains a
1437: * protocol, server name, port number, and, but it does not include a path.
1438: * <p>
1439: * Because this method returns a <code>StringBuffer</code>, not a string, you can modify the
1440: * URL easily, for example, to append path and query parameters.
1441: *
1442: * This method is useful for creating redirect messages and for reporting errors.
1443: *
1444: * @return "scheme://host:port"
1445: */
1446: public StringBuffer getRootURL() {
1447: StringBuffer url = new StringBuffer(48);
1448: synchronized (url) {
1449: String scheme = getScheme();
1450: int port = getServerPort();
1451:
1452: url.append(scheme);
1453: url.append("://");
1454: url.append(getServerName());
1455:
1456: if (port > 0
1457: && ((scheme.equalsIgnoreCase("http") && port != 80) || (scheme
1458: .equalsIgnoreCase("https") && port != 443))) {
1459: url.append(':');
1460: url.append(port);
1461: }
1462: return url;
1463: }
1464: }
1465:
1466: /* ------------------------------------------------------------ */
1467: /*
1468: */
1469: public Attributes getAttributes() {
1470: if (_attributes == null)
1471: _attributes = new AttributesMap();
1472: return _attributes;
1473: }
1474:
1475: /* ------------------------------------------------------------ */
1476: /*
1477: */
1478: public void setAttributes(Attributes attributes) {
1479: _attributes = attributes;
1480: }
1481:
1482: /* ------------------------------------------------------------ */
1483: public Continuation getContinuation() {
1484: return _continuation;
1485: }
1486:
1487: /* ------------------------------------------------------------ */
1488: public Continuation getContinuation(boolean create) {
1489: if (_continuation == null && create)
1490: _continuation = getConnection().getConnector()
1491: .newContinuation();
1492: return _continuation;
1493: }
1494:
1495: /* ------------------------------------------------------------ */
1496: void setContinuation(Continuation cont) {
1497: _continuation = cont;
1498: }
1499:
1500: /* ------------------------------------------------------------ */
1501: /**
1502: * @return Returns the parameters.
1503: */
1504: public MultiMap getParameters() {
1505: return _parameters;
1506: }
1507:
1508: /* ------------------------------------------------------------ */
1509: /**
1510: * @param parameters The parameters to set.
1511: */
1512: public void setParameters(MultiMap parameters) {
1513: _parameters = (parameters == null) ? _baseParameters
1514: : parameters;
1515: if (_paramsExtracted && _parameters == null)
1516: throw new IllegalStateException();
1517: }
1518:
1519: /* ------------------------------------------------------------ */
1520: public String toString() {
1521: return getMethod() + " " + _uri + " " + getProtocol() + "\n"
1522: + _connection.getRequestFields().toString();
1523: }
1524:
1525: /* ------------------------------------------------------------ */
1526: public static Request getRequest(HttpServletRequest request) {
1527: if (request instanceof Request)
1528: return (Request) request;
1529:
1530: while (request instanceof ServletRequestWrapper)
1531: request = (HttpServletRequest) ((ServletRequestWrapper) request)
1532: .getRequest();
1533:
1534: if (request instanceof Request)
1535: return (Request) request;
1536:
1537: return HttpConnection.getCurrentConnection().getRequest();
1538: }
1539:
1540: /* ------------------------------------------------------------ */
1541: public synchronized void addEventListener(EventListener listener) {
1542: if (listener instanceof ServletRequestAttributeListener)
1543: _requestAttributeListeners = LazyList.add(
1544: _requestAttributeListeners, listener);
1545: }
1546:
1547: /* ------------------------------------------------------------ */
1548: public synchronized void removeEventListener(EventListener listener) {
1549: _requestAttributeListeners = LazyList.remove(
1550: _requestAttributeListeners, listener);
1551: }
1552:
1553: /* ------------------------------------------------------------ */
1554: public void saveNewSession(Object key, HttpSession session) {
1555: if (_savedNewSessions == null)
1556: _savedNewSessions = new HashMap();
1557: _savedNewSessions.put(key, session);
1558: }
1559:
1560: /* ------------------------------------------------------------ */
1561: public HttpSession recoverNewSession(Object key) {
1562: if (_savedNewSessions == null)
1563: return null;
1564: return (HttpSession) _savedNewSessions.get(key);
1565: }
1566:
1567: /* ------------------------------------------------------------ */
1568: /**
1569: * @return Returns the userRealm.
1570: */
1571: public UserRealm getUserRealm() {
1572: return _userRealm;
1573: }
1574:
1575: /* ------------------------------------------------------------ */
1576: /**
1577: * @param userRealm The userRealm to set.
1578: */
1579: public void setUserRealm(UserRealm userRealm) {
1580: _userRealm = userRealm;
1581: }
1582:
1583: /* ------------------------------------------------------------ */
1584: public String getQueryEncoding() {
1585: return _queryEncoding;
1586: }
1587:
1588: /* ------------------------------------------------------------ */
1589: /** Set the character encoding used for the query string.
1590: * This call will effect the return of getQueryString and getParamaters.
1591: * It must be called before any geParameter methods.
1592: *
1593: * The request attribute "org.mortbay.jetty.Request.queryEncoding"
1594: * may be set as an alternate method of calling setQueryEncoding.
1595: *
1596: * @param queryEncoding
1597: */
1598: public void setQueryEncoding(String queryEncoding) {
1599: _queryEncoding = queryEncoding;
1600: _queryString = null;
1601: }
1602:
1603: /* ------------------------------------------------------------ */
1604: public void setRoleMap(Map map) {
1605: _roleMap = map;
1606: }
1607:
1608: /* ------------------------------------------------------------ */
1609: public Map getRoleMap() {
1610: return _roleMap;
1611: }
1612: }
|