diff src/org/eclipse/jetty/server/handler/GzipHandler.java @ 916:4de7f6e9c453

remove ContinuationSupport
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 09 Oct 2016 02:08:40 -0600
parents 8e9db0bbf4f9
children aa7dc1802d29
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/handler/GzipHandler.java	Sun Oct 09 01:52:29 2016 -0600
+++ b/src/org/eclipse/jetty/server/handler/GzipHandler.java	Sun Oct 09 02:08:40 2016 -0600
@@ -35,7 +35,6 @@
 
 import org.eclipse.jetty.continuation.Continuation;
 import org.eclipse.jetty.continuation.ContinuationListener;
-import org.eclipse.jetty.continuation.ContinuationSupport;
 import org.eclipse.jetty.http.HttpMethods;
 import org.eclipse.jetty.http.gzip.CompressedResponseWrapper;
 import org.eclipse.jetty.http.gzip.AbstractCompressedStream;
@@ -62,295 +61,274 @@
  */
 public class GzipHandler extends HandlerWrapper
 {
-    private static final Logger LOG = LoggerFactory.getLogger(GzipHandler.class);
+	private static final Logger LOG = LoggerFactory.getLogger(GzipHandler.class);
 
-    protected Set<String> _mimeTypes;
-    protected Set<String> _excluded;
-    protected int _bufferSize = 8192;
-    protected int _minGzipSize = 256;
-    protected String _vary = "Accept-Encoding, User-Agent";
+	protected Set<String> _mimeTypes;
+	protected Set<String> _excluded;
+	protected int _bufferSize = 8192;
+	protected int _minGzipSize = 256;
+	protected String _vary = "Accept-Encoding, User-Agent";
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Instantiates a new gzip handler.
-     */
-    public GzipHandler()
-    {
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Instantiates a new gzip handler.
+	 */
+	public GzipHandler()
+	{
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Get the mime types.
-     * 
-     * @return mime types to set
-     */
-    public Set<String> getMimeTypes()
-    {
-        return _mimeTypes;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Get the mime types.
+	 * 
+	 * @return mime types to set
+	 */
+	public Set<String> getMimeTypes()
+	{
+		return _mimeTypes;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Set the mime types.
-     * 
-     * @param mimeTypes
-     *            the mime types to set
-     */
-    public void setMimeTypes(Set<String> mimeTypes)
-    {
-        _mimeTypes = mimeTypes;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Set the mime types.
+	 * 
+	 * @param mimeTypes
+	 *            the mime types to set
+	 */
+	public void setMimeTypes(Set<String> mimeTypes)
+	{
+		_mimeTypes = mimeTypes;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Set the mime types.
-     * 
-     * @param mimeTypes
-     *            the mime types to set
-     */
-    public void setMimeTypes(String mimeTypes)
-    {
-        if (mimeTypes != null)
-        {
-            _mimeTypes = new HashSet<String>();
-            StringTokenizer tok = new StringTokenizer(mimeTypes,",",false);
-            while (tok.hasMoreTokens())
-            {
-                _mimeTypes.add(tok.nextToken());
-            }
-        }
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Set the mime types.
+	 * 
+	 * @param mimeTypes
+	 *            the mime types to set
+	 */
+	public void setMimeTypes(String mimeTypes)
+	{
+		if (mimeTypes != null)
+		{
+			_mimeTypes = new HashSet<String>();
+			StringTokenizer tok = new StringTokenizer(mimeTypes,",",false);
+			while (tok.hasMoreTokens())
+			{
+				_mimeTypes.add(tok.nextToken());
+			}
+		}
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Get the excluded user agents.
-     * 
-     * @return excluded user agents
-     */
-    public Set<String> getExcluded()
-    {
-        return _excluded;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Get the excluded user agents.
+	 * 
+	 * @return excluded user agents
+	 */
+	public Set<String> getExcluded()
+	{
+		return _excluded;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Set the excluded user agents.
-     * 
-     * @param excluded
-     *            excluded user agents to set
-     */
-    public void setExcluded(Set<String> excluded)
-    {
-        _excluded = excluded;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Set the excluded user agents.
+	 * 
+	 * @param excluded
+	 *            excluded user agents to set
+	 */
+	public void setExcluded(Set<String> excluded)
+	{
+		_excluded = excluded;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Set the excluded user agents.
-     * 
-     * @param excluded
-     *            excluded user agents to set
-     */
-    public void setExcluded(String excluded)
-    {
-        if (excluded != null)
-        {
-            _excluded = new HashSet<String>();
-            StringTokenizer tok = new StringTokenizer(excluded,",",false);
-            while (tok.hasMoreTokens())
-                _excluded.add(tok.nextToken());
-        }
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Set the excluded user agents.
+	 * 
+	 * @param excluded
+	 *            excluded user agents to set
+	 */
+	public void setExcluded(String excluded)
+	{
+		if (excluded != null)
+		{
+			_excluded = new HashSet<String>();
+			StringTokenizer tok = new StringTokenizer(excluded,",",false);
+			while (tok.hasMoreTokens())
+				_excluded.add(tok.nextToken());
+		}
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @return The value of the Vary header set if a response can be compressed.
-     */
-    public String getVary()
-    {
-        return _vary;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * @return The value of the Vary header set if a response can be compressed.
+	 */
+	public String getVary()
+	{
+		return _vary;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Set the value of the Vary header sent with responses that could be compressed.  
-     * <p>
-     * By default it is set to 'Accept-Encoding, User-Agent' since IE6 is excluded by 
-     * default from the excludedAgents. If user-agents are not to be excluded, then 
-     * this can be set to 'Accept-Encoding'.  Note also that shared caches may cache 
-     * many copies of a resource that is varied by User-Agent - one per variation of the 
-     * User-Agent, unless the cache does some normalization of the UA string.
-     * @param vary The value of the Vary header set if a response can be compressed.
-     */
-    public void setVary(String vary)
-    {
-        _vary = vary;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Set the value of the Vary header sent with responses that could be compressed.  
+	 * <p>
+	 * By default it is set to 'Accept-Encoding, User-Agent' since IE6 is excluded by 
+	 * default from the excludedAgents. If user-agents are not to be excluded, then 
+	 * this can be set to 'Accept-Encoding'.  Note also that shared caches may cache 
+	 * many copies of a resource that is varied by User-Agent - one per variation of the 
+	 * User-Agent, unless the cache does some normalization of the UA string.
+	 * @param vary The value of the Vary header set if a response can be compressed.
+	 */
+	public void setVary(String vary)
+	{
+		_vary = vary;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Get the buffer size.
-     * 
-     * @return the buffer size
-     */
-    public int getBufferSize()
-    {
-        return _bufferSize;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Get the buffer size.
+	 * 
+	 * @return the buffer size
+	 */
+	public int getBufferSize()
+	{
+		return _bufferSize;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Set the buffer size.
-     * 
-     * @param bufferSize
-     *            buffer size to set
-     */
-    public void setBufferSize(int bufferSize)
-    {
-        _bufferSize = bufferSize;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Set the buffer size.
+	 * 
+	 * @param bufferSize
+	 *            buffer size to set
+	 */
+	public void setBufferSize(int bufferSize)
+	{
+		_bufferSize = bufferSize;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Get the minimum reponse size.
-     * 
-     * @return minimum reponse size
-     */
-    public int getMinGzipSize()
-    {
-        return _minGzipSize;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Get the minimum reponse size.
+	 * 
+	 * @return minimum reponse size
+	 */
+	public int getMinGzipSize()
+	{
+		return _minGzipSize;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Set the minimum reponse size.
-     * 
-     * @param minGzipSize
-     *            minimum reponse size
-     */
-    public void setMinGzipSize(int minGzipSize)
-    {
-        _minGzipSize = minGzipSize;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Set the minimum reponse size.
+	 * 
+	 * @param minGzipSize
+	 *            minimum reponse size
+	 */
+	public void setMinGzipSize(int minGzipSize)
+	{
+		_minGzipSize = minGzipSize;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
-     */
-    @Override
-    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
-    {
-        if (_handler!=null && isStarted())
-        {
-            String ae = request.getHeader("accept-encoding");
-            if (ae != null && ae.indexOf("gzip")>=0 && !response.containsHeader("Content-Encoding")
-                    && !HttpMethods.HEAD.equalsIgnoreCase(request.getMethod()))
-            {
-                if (_excluded!=null)
-                {
-                    String ua = request.getHeader("User-Agent");
-                    if (_excluded.contains(ua))
-                    {
-                        _handler.handle(target,baseRequest, request, response);
-                        return;
-                    }
-                }
+	/* ------------------------------------------------------------ */
+	/**
+	 * @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+	 */
+	@Override
+	public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
+	{
+		if (_handler!=null && isStarted())
+		{
+			String ae = request.getHeader("accept-encoding");
+			if (ae != null && ae.indexOf("gzip")>=0 && !response.containsHeader("Content-Encoding")
+					&& !HttpMethods.HEAD.equalsIgnoreCase(request.getMethod()))
+			{
+				if (_excluded!=null)
+				{
+					String ua = request.getHeader("User-Agent");
+					if (_excluded.contains(ua))
+					{
+						_handler.handle(target,baseRequest, request, response);
+						return;
+					}
+				}
 
-                final CompressedResponseWrapper wrappedResponse = newGzipResponseWrapper(request,response);
-                
-                boolean exceptional=true;
-                try
-                {
-                    _handler.handle(target, baseRequest, request, wrappedResponse);
-                    exceptional=false;
-                }
-                finally
-                {
-                    Continuation continuation = ContinuationSupport.getContinuation(request);
-                    if (continuation.isSuspended() && continuation.isResponseWrapped())   
-                    {
-                        continuation.addContinuationListener(new ContinuationListener()
-                        {
-                            public void onComplete(Continuation continuation)
-                            {
-                                try
-                                {
-                                    wrappedResponse.finish();
-                                }
-                                catch(IOException e)
-                                {
-                                    LOG.warn("",e);
-                                }
-                            }
-
-                            public void onTimeout(Continuation continuation)
-                            {}
-                        });
-                    }
-                    else if (exceptional && !response.isCommitted())
-                    {
-                        wrappedResponse.resetBuffer();
-                        wrappedResponse.noCompression();
-                    }
-                    else
-                        wrappedResponse.finish();
-                }
-            }
-            else
-            {
-                _handler.handle(target,baseRequest, request, response);
-            }
-        }
-    }
+				final CompressedResponseWrapper wrappedResponse = newGzipResponseWrapper(request,response);
+				
+				boolean exceptional=true;
+				try
+				{
+					_handler.handle(target, baseRequest, request, wrappedResponse);
+					exceptional=false;
+				}
+				finally
+				{
+					if (exceptional && !response.isCommitted())
+					{
+						wrappedResponse.resetBuffer();
+						wrappedResponse.noCompression();
+					}
+					else
+						wrappedResponse.finish();
+				}
+			}
+			else
+			{
+				_handler.handle(target,baseRequest, request, response);
+			}
+		}
+	}
 
-    /**
-     * Allows derived implementations to replace ResponseWrapper implementation.
-     *
-     * @param request the request
-     * @param response the response
-     * @return the gzip response wrapper
-     */
-    protected CompressedResponseWrapper newGzipResponseWrapper(HttpServletRequest request, HttpServletResponse response)
-    {
-        return new CompressedResponseWrapper(request,response)
-        {
-            {
-                super.setMimeTypes(GzipHandler.this._mimeTypes);
-                super.setBufferSize(GzipHandler.this._bufferSize);
-                super.setMinCompressSize(GzipHandler.this._minGzipSize);
-            }
-            
-            @Override
-            protected AbstractCompressedStream newCompressedStream(HttpServletRequest request,HttpServletResponse response) throws IOException
-            {
-                return new AbstractCompressedStream("gzip",request,this,_vary)
-                {
-                    @Override
-                    protected DeflaterOutputStream createStream() throws IOException
-                    {
-                        return new GZIPOutputStream(_response.getOutputStream(),_bufferSize);
-                    }
-                };
-            }
-            
-            @Override
-            protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
-            {
-                return GzipHandler.this.newWriter(out,encoding);
-            }
-        };
-    }
-    
-    /**
-     * Allows derived implementations to replace PrintWriter implementation.
-     *
-     * @param out the out
-     * @param encoding the encoding
-     * @return the prints the writer
-     * @throws UnsupportedEncodingException
-     */
-    protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
-    {
-        return encoding==null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding));
-    }
+	/**
+	 * Allows derived implementations to replace ResponseWrapper implementation.
+	 *
+	 * @param request the request
+	 * @param response the response
+	 * @return the gzip response wrapper
+	 */
+	protected CompressedResponseWrapper newGzipResponseWrapper(HttpServletRequest request, HttpServletResponse response)
+	{
+		return new CompressedResponseWrapper(request,response)
+		{
+			{
+				super.setMimeTypes(GzipHandler.this._mimeTypes);
+				super.setBufferSize(GzipHandler.this._bufferSize);
+				super.setMinCompressSize(GzipHandler.this._minGzipSize);
+			}
+			
+			@Override
+			protected AbstractCompressedStream newCompressedStream(HttpServletRequest request,HttpServletResponse response) throws IOException
+			{
+				return new AbstractCompressedStream("gzip",request,this,_vary)
+				{
+					@Override
+					protected DeflaterOutputStream createStream() throws IOException
+					{
+						return new GZIPOutputStream(_response.getOutputStream(),_bufferSize);
+					}
+				};
+			}
+			
+			@Override
+			protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
+			{
+				return GzipHandler.this.newWriter(out,encoding);
+			}
+		};
+	}
+	
+	/**
+	 * Allows derived implementations to replace PrintWriter implementation.
+	 *
+	 * @param out the out
+	 * @param encoding the encoding
+	 * @return the prints the writer
+	 * @throws UnsupportedEncodingException
+	 */
+	protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
+	{
+		return encoding==null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding));
+	}
 }