Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// ========================================================================
// Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.server;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.DispatcherType;
import javax.servlet.MultipartConfigElement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import org.eclipse.jetty.continuation.Continuation;
import org.eclipse.jetty.continuation.ContinuationListener;
import org.eclipse.jetty.http.HttpCookie;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.HttpVersions;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.BufferUtil;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.nio.DirectNIOBuffer;
import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
import org.eclipse.jetty.io.nio.NIOBuffer;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandler.Context;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.AttributesMap;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.MultiPartInputStream;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.UrlEncoded;
import org.eclipse.jetty.util.log.Log;
/* ------------------------------------------------------------ */
/** Jetty Request.
*
* Implements {@link javax.servlet.http.HttpServletRequest} from the javax.servlet.http package.
*
*
* The standard interface of mostly getters,
* is extended with setters so that the request is mutable by the handlers that it is
* passed to. This allows the request object to be as lightweight as possible and not
* actually implement any significant behavior. For example
*
*
The {@link Request#getContextPath()} method will return null, until the request has been
* passed to a {@link ContextHandler} which matches the {@link Request#getPathInfo()} with a context
* path and calls {@link Request#setContextPath(String)} as a result.
*
*
the HTTP session methods
* will all return null sessions until such time as a request has been passed to
* a {@link org.eclipse.jetty.server.session.SessionHandler} which checks for session cookies
* and enables the ability to create new sessions.
*
*
The {@link Request#getServletPath()} method will return null until the request has been
* passed to a org.eclipse.jetty.servlet.ServletHandler and the pathInfo matched
* against the servlet URL patterns and {@link Request#setServletPath(String)} called as a result.
*
*
* A request instance is created for each {@link HttpConnection} accepted by the server
* and recycled for each HTTP request received via that connection. An effort is made
* to avoid reparsing headers and cookies that are likely to be the same for
* requests from the same connection.
*
*/
public class Request implements HttpServletRequest
{
public static final String __MULTIPART_CONFIG_ELEMENT = "org.eclipse.multipartConfig";
private static final String __ASYNC_FWD="org.eclipse.asyncfwd";
private static final Collection __defaultLocale = Collections.singleton(Locale.getDefault());
private static final int __NONE=0, _STREAM=1, __READER=2;
/* ------------------------------------------------------------ */
public static Request getRequest(HttpServletRequest request)
{
if (request instanceof Request)
return (Request) request;
return HttpConnection.getCurrentConnection().getRequest();
}
protected final AsyncContinuation _async = new AsyncContinuation();
private boolean _asyncSupported=true;
private volatile Attributes _attributes;
private Authentication _authentication;
private MultiMap _baseParameters;
private String _characterEncoding;
protected HttpConnection _connection;
private ContextHandler.Context _context;
private boolean _newContext;
private String _contextPath;
private CookieCutter _cookies;
private boolean _cookiesExtracted=false;
private DispatcherType _dispatcherType;
private boolean _dns=false;
private EndPoint _endp;
private boolean _handled =false;
private int _inputState=__NONE;
private String _method;
private MultiMap _parameters;
private boolean _paramsExtracted;
private String _pathInfo;
private int _port;
private String _protocol=HttpVersions.HTTP_1_1;
private String _queryEncoding;
private String _queryString;
private BufferedReader _reader;
private String _readerEncoding;
private String _remoteAddr;
private String _remoteHost;
private Object _requestAttributeListeners;
private String _requestedSessionId;
private boolean _requestedSessionIdFromCookie=false;
private String _requestURI;
private Map if {@link #setContext} has not yet
* been called.
*/
public Context getContext()
{
return _context;
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getContextPath()
*/
public String getContextPath()
{
return _contextPath;
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getCookies()
*/
public Cookie[] getCookies()
{
if (_cookiesExtracted)
return _cookies==null?null:_cookies.getCookies();
_cookiesExtracted = true;
Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.COOKIE_BUFFER);
// Handle no cookies
if (enm!=null)
{
if (_cookies==null)
_cookies=new CookieCutter();
while (enm.hasMoreElements())
{
String c = (String)enm.nextElement();
_cookies.addCookieField(c);
}
}
return _cookies==null?null:_cookies.getCookies();
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
*/
public long getDateHeader(String name)
{
return _connection.getRequestFields().getDateField(name);
}
/* ------------------------------------------------------------ */
public DispatcherType getDispatcherType()
{
return _dispatcherType;
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
*/
public String getHeader(String name)
{
return _connection.getRequestFields().getStringField(name);
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getHeaderNames()
*/
public Enumeration getHeaderNames()
{
return _connection.getRequestFields().getFieldNames();
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
*/
public Enumeration getHeaders(String name)
{
Enumeration e = _connection.getRequestFields().getValues(name);
if (e==null)
return Collections.enumeration(Collections.EMPTY_LIST);
return e;
}
/* ------------------------------------------------------------ */
/**
* @return Returns the inputState.
*/
public int getInputState()
{
return _inputState;
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getInputStream()
*/
public ServletInputStream getInputStream() throws IOException
{
if (_inputState!=__NONE && _inputState!=_STREAM)
throw new IllegalStateException("READER");
_inputState=_STREAM;
return _connection.getInputStream();
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
*/
public int getIntHeader(String name)
{
return (int)_connection.getRequestFields().getLongField(name);
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getLocalAddr()
*/
public String getLocalAddr()
{
return _endp==null?null:_endp.getLocalAddr();
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getLocale()
*/
public Locale getLocale()
{
Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.ACCEPT_LANGUAGE, HttpFields.__separators);
// handle no locale
if (enm == null || !enm.hasMoreElements())
return Locale.getDefault();
// sort the list in quality order
List acceptLanguage = HttpFields.qualityList(enm);
if (acceptLanguage.size()==0)
return Locale.getDefault();
int size=acceptLanguage.size();
if (size>0)
{
String language = (String)acceptLanguage.get(0);
language=HttpFields.valueParameters(language,null);
String country = "";
int dash = language.indexOf('-');
if (dash > -1)
{
country = language.substring(dash + 1).trim();
language = language.substring(0,dash).trim();
}
return new Locale(language,country);
}
return Locale.getDefault();
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getLocales()
*/
public Enumeration getLocales()
{
Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.ACCEPT_LANGUAGE, HttpFields.__separators);
// handle no locale
if (enm == null || !enm.hasMoreElements())
return Collections.enumeration(__defaultLocale);
// sort the list in quality order
List acceptLanguage = HttpFields.qualityList(enm);
if (acceptLanguage.size()==0)
return
Collections.enumeration(__defaultLocale);
Object langs = null;
int size=acceptLanguage.size();
// convert to locals
for (int i=0; i -1)
{
country = language.substring(dash + 1).trim();
language = language.substring(0,dash).trim();
}
langs=LazyList.ensureSize(langs,size);
langs=LazyList.add(langs,new Locale(language,country));
}
if (LazyList.size(langs)==0)
return Collections.enumeration(__defaultLocale);
return Collections.enumeration(LazyList.getList(langs));
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getLocalName()
*/
public String getLocalName()
{
if (_endp==null)
return null;
if (_dns)
return _endp.getLocalHost();
String local = _endp.getLocalAddr();
if (local!=null && local.indexOf(':')>=0)
local="["+local+"]";
return local;
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getLocalPort()
*/
public int getLocalPort()
{
return _endp==null?0:_endp.getLocalPort();
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getMethod()
*/
public String getMethod()
{
return _method;
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getParameter(java.lang.String)
*/
public String getParameter(String name)
{
if (!_paramsExtracted)
extractParameters();
return (String) _parameters.getValue(name, 0);
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getParameterMap()
*/
public Map getParameterMap()
{
if (!_paramsExtracted)
extractParameters();
return Collections.unmodifiableMap(_parameters.toStringArrayMap());
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getParameterNames()
*/
public Enumeration getParameterNames()
{
if (!_paramsExtracted)
extractParameters();
return Collections.enumeration(_parameters.keySet());
}
/* ------------------------------------------------------------ */
/**
* @return Returns the parameters.
*/
public MultiMap getParameters()
{
return _parameters;
}
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
*/
public String[] getParameterValues(String name)
{
if (!_paramsExtracted)
extractParameters();
List