diff src/org/eclipse/jetty/server/Response.java @ 999:74b9daf2826c

simplify Response
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 19 Oct 2016 00:59:46 -0600
parents 688c39c50ee3
children 0114d373748e
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/Response.java	Tue Oct 18 23:01:00 2016 -0600
+++ b/src/org/eclipse/jetty/server/Response.java	Wed Oct 19 00:59:46 2016 -0600
@@ -57,7 +57,7 @@
  * Implements {@link javax.servlet.http.HttpServletResponse} from the <code>javax.servlet.http</code> package.
  * </p>
  */
-public class Response implements HttpServletResponse
+public final class Response implements HttpServletResponse
 {
 	private static final Logger LOG = LoggerFactory.getLogger(Response.class);
 
@@ -81,7 +81,7 @@
 	public final static String HTTP_ONLY_COMMENT="__HTTP_ONLY__";
 	
 	private final AbstractHttpConnection _connection;
-	private int _status=SC_OK;
+	private int _status = SC_OK;
 	private String _reason;
 	private Locale _locale;
 	private String _mimeType;
@@ -92,47 +92,32 @@
 	private volatile int _outputState;
 	private PrintWriter _writer;
 
-	/* ------------------------------------------------------------ */
-	/**
-	 *
-	 */
 	public Response(AbstractHttpConnection connection)
 	{
-		_connection=connection;
+		_connection = connection;
 	}
 
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#reset()
-	 */
-	protected void recycle()
+	void recycle()
 	{
-		_status=SC_OK;
-		_reason=null;
-		_locale=null;
-		_mimeType=null;
-		_cachedMimeType=null;
-		_characterEncoding=null;
-		_explicitEncoding=false;
-		_contentType=null;
-		_writer=null;
-		_outputState=NONE;
+		_status = SC_OK;
+		_reason = null;
+		_locale = null;
+		_mimeType = null;
+		_cachedMimeType = null;
+		_characterEncoding = null;
+		_explicitEncoding = false;
+		_contentType = null;
+		_writer = null;
+		_outputState = NONE;
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie)
-	 */
 	public void addCookie(HttpCookie cookie)
 	{
 		_connection._responseFields.addSetCookie(cookie);
 	}
 	
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie)
-	 */
+	@Override
 	public void addCookie(Cookie cookie)
 	{
 		String comment=cookie.getComment();
@@ -160,65 +145,53 @@
 				cookie.getVersion());
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#containsHeader(java.lang.String)
-	 */
+	@Override
 	public boolean containsHeader(String name)
 	{
 		return _connection._responseFields.containsKey(name);
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#encodeURL(java.lang.String)
-	 */
+	@Override
 	public String encodeURL(String url)
 	{
 		throw new UnsupportedOperationException();
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @see javax.servlet.http.HttpServletResponse#encodeRedirectURL(java.lang.String)
-	 */
+	@Override
 	public String encodeRedirectURL(String url)
 	{
-		return encodeURL(url);
+		throw new UnsupportedOperationException();
 	}
 
-	/* ------------------------------------------------------------ */
+	@Override
 	@Deprecated
 	public String encodeUrl(String url)
 	{
-		return encodeURL(url);
+		throw new UnsupportedOperationException();
 	}
 
-	/* ------------------------------------------------------------ */
+	@Override
 	@Deprecated
 	public String encodeRedirectUrl(String url)
 	{
-		return encodeRedirectURL(url);
+		throw new UnsupportedOperationException();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#sendError(int, java.lang.String)
-	 */
+	@Override
 	public void sendError(int code, String message) throws IOException
 	{
 		if (isCommitted())
 			LOG.warn("Committed before "+code+" "+message);
 
 		resetBuffer();
-		_characterEncoding=null;
+		_characterEncoding = null;
 		setHeader(HttpHeaders.EXPIRES,null);
 		setHeader(HttpHeaders.LAST_MODIFIED,null);
 		setHeader(HttpHeaders.CACHE_CONTROL,null);
 		setHeader(HttpHeaders.CONTENT_TYPE,null);
 		setHeader(HttpHeaders.CONTENT_LENGTH,null);
 
-		_outputState=NONE;
+		_outputState = NONE;
 		setStatus(code,message);
 
 		if (message==null)
@@ -278,44 +251,21 @@
 		{
 			_connection._requestFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER);
 			_connection._requestFields.remove(HttpHeaders.CONTENT_LENGTH_BUFFER);
-			_characterEncoding=null;
-			_mimeType=null;
-			_cachedMimeType=null;
+			_characterEncoding = null;
+			_mimeType = null;
+			_cachedMimeType = null;
 		}
 
 		complete();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#sendError(int)
-	 */
+	@Override
 	public void sendError(int sc) throws IOException
 	{
-		if (sc==102)
-			sendProcessing();
-		else
-			sendError(sc,null);
+		sendError(sc,null);
 	}
 
-	/* ------------------------------------------------------------ */
-	/* Send a 102-Processing response.
-	 * If the connection is a HTTP connection, the version is 1.1 and the
-	 * request has a Expect header starting with 102, then a 102 response is
-	 * sent. This indicates that the request still be processed and real response
-	 * can still be sent.   This method is called by sendError if it is passed 102.
-	 * @see javax.servlet.http.HttpServletResponse#sendError(int)
-	 */
-	public void sendProcessing() throws IOException
-	{
-		if (_connection.isExpecting102Processing() && !isCommitted())
-			_connection._generator.send1xx(HttpStatus.PROCESSING_102);
-	}
-
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#sendRedirect(java.lang.String)
-	 */
+	@Override
 	public void sendRedirect(String location) throws IOException
 	{
 		if (location==null)
@@ -377,28 +327,19 @@
 
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#setDateHeader(java.lang.String, long)
-	 */
+	@Override
 	public void setDateHeader(String name, long date)
 	{
 		_connection._responseFields.putDateField(name, date);
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#addDateHeader(java.lang.String, long)
-	 */
+	@Override
 	public void addDateHeader(String name, long date)
 	{
 		_connection._responseFields.addDateField(name, date);
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String)
-	 */
+	@Override
 	public void setHeader(String name, String value)
 	{
 		if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name))
@@ -417,24 +358,20 @@
 	}
 
 
-	/* ------------------------------------------------------------ */
+	@Override
 	public Collection<String> getHeaderNames()
 	{
 		final HttpFields fields=_connection._responseFields;
 		return fields.getFieldNamesCollection();
 	}
 	
-	/* ------------------------------------------------------------ */
-	/*
-	 */
+	@Override
 	public String getHeader(String name)
 	{
 		return _connection._responseFields.getStringField(name);
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 */
+	@Override
 	public Collection<String> getHeaders(String name)
 	{
 		final HttpFields fields=_connection._responseFields;
@@ -444,10 +381,7 @@
 		return i;
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String)
-	 */
+	@Override
 	public void addHeader(String name, String value)
 	{
 
@@ -462,10 +396,7 @@
 			_connection._generator.setContentLength(Long.parseLong(value));
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#setIntHeader(java.lang.String, int)
-	 */
+	@Override
 	public void setIntHeader(String name, int value)
 	{
 		_connection._responseFields.putLongField(name, value);
@@ -473,10 +404,7 @@
 			_connection._generator.setContentLength(value);
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#addIntHeader(java.lang.String, int)
-	 */
+	@Override
 	public void addIntHeader(String name, int value)
 	{
 		_connection._responseFields.addLongField(name, value);
@@ -484,83 +412,47 @@
 			_connection._generator.setContentLength(value);
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#setStatus(int)
-	 */
+	@Override
 	public void setStatus(int sc)
 	{
 		setStatus(sc,null);
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.http.HttpServletResponse#setStatus(int, java.lang.String)
-	 */
+	@Override
 	public void setStatus(int sc, String sm)
 	{
 		if (sc<=0)
 			throw new IllegalArgumentException();
-		_status=sc;
-		_reason=sm;
+		_status = sc;
+		_reason = sm;
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#getCharacterEncoding()
-	 */
+	@Override
 	public String getCharacterEncoding()
 	{
 		if (_characterEncoding==null)
-			_characterEncoding=StringUtil.__ISO_8859_1;
+			_characterEncoding = StringUtil.__ISO_8859_1;
 		return _characterEncoding;
 	}
 	
-	/* ------------------------------------------------------------ */
-	String getSetCharacterEncoding()
-	{
-		return _characterEncoding;
-	}
-
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#getContentType()
-	 */
+	@Override
 	public String getContentType()
 	{
 		return _contentType;
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#getOutputStream()
-	 */
+	@Override
 	public ServletOutputStream getOutputStream() throws IOException
 	{
 		if (_outputState!=NONE && _outputState!=STREAM)
 			throw new IllegalStateException("WRITER");
 
 		ServletOutputStream out = _connection.getOutputStream();
-		_outputState=STREAM;
+		_outputState = STREAM;
 		return out;
 	}
 
-	/* ------------------------------------------------------------ */
-	public boolean isWriting()
-	{
-		return _outputState==WRITER;
-	}
-
-	/* ------------------------------------------------------------ */
-	public boolean isOutputing()
-	{
-		return _outputState!=NONE;
-	}
-
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#getWriter()
-	 */
+	@Override
 	public PrintWriter getWriter() throws IOException
 	{
 		if (_outputState!=NONE && _outputState!=WRITER)
@@ -591,15 +483,12 @@
 		return _writer;
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#setCharacterEncoding(java.lang.String)
-	 */
+	@Override
 	public void setCharacterEncoding(String encoding)
 	{
 		if (this._outputState==0 && !isCommitted())
 		{
-			_explicitEncoding=true;
+			_explicitEncoding = true;
 
 			if (encoding==null)
 			{
@@ -608,11 +497,11 @@
 				{
 					_characterEncoding=null;
 					if (_cachedMimeType!=null)
-						_contentType=_cachedMimeType.toString();
+						_contentType = _cachedMimeType.toString();
 					else if (_mimeType!=null)
-						_contentType=_mimeType;
+						_contentType = _mimeType;
 					else
-						_contentType=null;
+						_contentType = null;
 
 					if (_contentType==null)
 						_connection._responseFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER);
@@ -635,7 +524,7 @@
 							CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding);
 							if (content_type!=null)
 							{
-								_contentType=content_type.toString();
+								_contentType = content_type.toString();
 								_connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type);
 							}
 						}
@@ -669,10 +558,7 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#setContentLength(int)
-	 */
+	@Override
 	public void setContentLength(int len)
 	{
 		// Protect from setting after committed as default handling
@@ -703,25 +589,7 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#setContentLength(int)
-	 */
-	public void setLongContentLength(long len)
-	{
-		// Protect from setting after committed as default handling
-		// of a servlet HEAD request ALWAYS sets _content length, even
-		// if the getHandling committed the response!
-		if (isCommitted())
-			return;
-		_connection._generator.setContentLength(len);
-		_connection._responseFields.putLongField(HttpHeaders.CONTENT_LENGTH, len);
-	}
-
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
-	 */
+	@Override
 	public void setContentType(String contentType)
 	{
 		if (isCommitted())
@@ -734,9 +602,9 @@
 		if (contentType==null)
 		{
 			if (_locale==null)
-				_characterEncoding=null;
-			_mimeType=null;
-			_cachedMimeType=null;
+				_characterEncoding = null;
+			_mimeType = null;
+			_cachedMimeType = null;
 			_contentType=null;
 			_connection._responseFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER);
 		}
@@ -750,14 +618,14 @@
 				// we have content type parameters
 
 				// Extract params off mimetype
-				_mimeType=contentType.substring(0,i0).trim();
-				_cachedMimeType=MimeTypes.CACHE.get(_mimeType);
+				_mimeType = contentType.substring(0,i0).trim();
+				_cachedMimeType = MimeTypes.CACHE.get(_mimeType);
 
 				// Look for charset
 				int i1=contentType.indexOf("charset=",i0+1);
 				if (i1>=0)
 				{
-					_explicitEncoding=true;
+					_explicitEncoding = true;
 					int i8=i1+8;
 					int i2 = contentType.indexOf(' ',i8);
 
@@ -776,13 +644,13 @@
 								}
 								else
 								{
-									_contentType=_mimeType+";charset="+_characterEncoding;
+									_contentType = _mimeType+";charset="+_characterEncoding;
 									_connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
 								}
 							}
 							else
 							{
-								_contentType=_mimeType+";charset="+_characterEncoding;
+								_contentType = _mimeType+";charset="+_characterEncoding;
 								_connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
 							}
 						}
@@ -800,7 +668,7 @@
 					else if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' '))
 					{
 						// The params are just the char encoding
-						_cachedMimeType=MimeTypes.CACHE.get(_mimeType);
+						_cachedMimeType = MimeTypes.CACHE.get(_mimeType);
 						_characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8));
 
 						if (_cachedMimeType!=null)
@@ -838,15 +706,15 @@
 				}
 				else // No encoding in the params.
 				{
-					_cachedMimeType=null;
+					_cachedMimeType = null;
 					_contentType=_characterEncoding==null?contentType:contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= ");
 					_connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
 				}
 			}
 			else // No params at all
 			{
-				_mimeType=contentType;
-				_cachedMimeType=MimeTypes.CACHE.get(_mimeType);
+				_mimeType = contentType;
+				_cachedMimeType = MimeTypes.CACHE.get(_mimeType);
 
 				if (_characterEncoding!=null)
 				{
@@ -860,7 +728,7 @@
 						}
 						else
 						{
-							_contentType=_mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= ");
+							_contentType = _mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= ");
 							_connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
 						}
 					}
@@ -872,7 +740,7 @@
 				}
 				else if (_cachedMimeType!=null)
 				{
-					_contentType=_cachedMimeType.toString();
+					_contentType = _cachedMimeType.toString();
 					_connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_cachedMimeType);
 				}
 				else
@@ -884,43 +752,31 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#setBufferSize(int)
-	 */
+	@Override
 	public void setBufferSize(int size)
 	{
 		throw new UnsupportedOperationException();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#getBufferSize()
-	 */
+	@Override
 	public int getBufferSize()
 	{
 		return _connection._generator.getContentBufferSize();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#flushBuffer()
-	 */
+	@Override
 	public void flushBuffer() throws IOException
 	{
 		_connection.flushResponse();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#reset()
-	 */
+	@Override
 	public void reset()
 	{
 		resetBuffer();
 		fwdReset();
-		_status=200;
-		_reason=null;
+		_status = 200;
+		_reason = null;
 		
 		HttpFields response_fields=_connection._responseFields;
 		
@@ -954,34 +810,8 @@
 		}
 	}
 	
-
-	public void reset(boolean preserveCookies)
-	{ 
-		if (!preserveCookies)
-			reset();
-		else
-		{
-			HttpFields response_fields=_connection._responseFields;
-
-			ArrayList<String> cookieValues = new ArrayList<String>(5);
-			Enumeration<String> vals = response_fields.getValues(HttpHeaders.SET_COOKIE);
-			while (vals.hasMoreElements())
-				cookieValues.add((String)vals.nextElement());
-
-			reset();
-
-			for (String v:cookieValues)
-				response_fields.add(HttpHeaders.SET_COOKIE, v);
-		}
-	}
 	
-	
-	
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#reset()
-	 */
-	public void fwdReset()
+	private void fwdReset()
 	{
 		resetBuffer();
 
@@ -989,10 +819,7 @@
 		_outputState=NONE;
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#resetBuffer()
-	 */
+	@Override
 	public void resetBuffer()
 	{
 		if (isCommitted())
@@ -1000,20 +827,14 @@
 		_connection._generator.resetBuffer();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#isCommitted()
-	 */
+	@Override
 	public boolean isCommitted()
 	{
 		return _connection._generator.isCommitted();
 	}
 
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#setLocale(java.util.Locale)
-	 */
+	@Override
 	public void setLocale(Locale locale)
 	{
 		if (locale == null || isCommitted())
@@ -1023,10 +844,7 @@
 		_connection._responseFields.put(HttpHeaders.CONTENT_LANGUAGE_BUFFER,locale.toString().replace('_','-'));
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see javax.servlet.ServletResponse#getLocale()
-	 */
+	@Override
 	public Locale getLocale()
 	{
 		if (_locale==null)
@@ -1054,9 +872,6 @@
 		return _reason;
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 */
 	public void complete()
 		throws IOException
 	{
@@ -1074,13 +889,11 @@
 		return _connection._generator.getContentWritten();
 	}
 
-	/* ------------------------------------------------------------ */
 	public HttpFields getHttpFields()
 	{
 		return _connection._responseFields;
 	}
 
-	/* ------------------------------------------------------------ */
 	@Override
 	public String toString()
 	{
@@ -1088,31 +901,4 @@
 		_connection._responseFields.toString();
 	}
 	
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	private static class NullOutput extends ServletOutputStream
-	{
-		@Override
-		public void write(int b) throws IOException
-		{
-		}
-
-		@Override
-		public void print(String s) throws IOException
-		{
-		}
-
-		@Override
-		public void println(String s) throws IOException
-		{
-		}
-
-		@Override
-		public void write(byte[] b, int off, int len) throws IOException
-		{
-		}
-
-	}
-
 }