PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/jetty-server/src/main/java/org/eclipse/jetty/server/handler/GzipHandler.java

https://github.com/dekellum/jetty
Java | 309 lines | 167 code | 24 blank | 118 comment | 20 complexity | 8b9c4a7a4c35fc38b4c63ef7a1b585bb MD5 | raw file
  1. // ========================================================================
  2. // Copyright (c) Webtide LLC
  3. // ------------------------------------------------------------------------
  4. // All rights reserved. This program and the accompanying materials
  5. // are made available under the terms of the Eclipse Public License v1.0
  6. // and Apache License v2.0 which accompanies this distribution.
  7. // The Eclipse Public License is available at
  8. // http://www.eclipse.org/legal/epl-v10.html
  9. // The Apache License v2.0 is available at
  10. // http://www.opensource.org/licenses/apache2.0.php
  11. // You may elect to redistribute this code under either of these licenses.
  12. // ========================================================================
  13. package org.eclipse.jetty.server.handler;
  14. import java.io.IOException;
  15. import java.io.OutputStream;
  16. import java.io.OutputStreamWriter;
  17. import java.io.PrintWriter;
  18. import java.io.UnsupportedEncodingException;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import java.util.StringTokenizer;
  22. import javax.servlet.ServletException;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import org.eclipse.jetty.continuation.Continuation;
  26. import org.eclipse.jetty.continuation.ContinuationListener;
  27. import org.eclipse.jetty.continuation.ContinuationSupport;
  28. import org.eclipse.jetty.http.HttpMethods;
  29. import org.eclipse.jetty.http.gzip.GzipResponseWrapper;
  30. import org.eclipse.jetty.server.Request;
  31. import org.eclipse.jetty.util.log.Log;
  32. import org.eclipse.jetty.util.log.Logger;
  33. /* ------------------------------------------------------------ */
  34. /**
  35. * GZIP Handler This handler will gzip the content of a response if:
  36. * <ul>
  37. * <li>The filter is mapped to a matching path</li>
  38. * <li>The response status code is >=200 and <300
  39. * <li>The content length is unknown or more than the <code>minGzipSize</code> initParameter or the minGzipSize is 0(default)</li>
  40. * <li>The content-type is in the comma separated list of mimeTypes set in the <code>mimeTypes</code> initParameter or if no mimeTypes are defined the
  41. * content-type is not "application/gzip"</li>
  42. * <li>No content-encoding is specified by the resource</li>
  43. * </ul>
  44. *
  45. * <p>
  46. * Compressing the content can greatly improve the network bandwidth usage, but at a cost of memory and CPU cycles. If this handler is used for static content,
  47. * then use of efficient direct NIO may be prevented, thus use of the gzip mechanism of the <code>org.eclipse.jetty.servlet.DefaultServlet</code> is advised instead.
  48. * </p>
  49. */
  50. public class GzipHandler extends HandlerWrapper
  51. {
  52. private static final Logger LOG = Log.getLogger(GzipHandler.class);
  53. protected Set<String> _mimeTypes;
  54. protected Set<String> _excluded;
  55. protected int _bufferSize = 8192;
  56. protected int _minGzipSize = 256;
  57. /* ------------------------------------------------------------ */
  58. /**
  59. * Instantiates a new gzip handler.
  60. */
  61. public GzipHandler()
  62. {
  63. }
  64. /* ------------------------------------------------------------ */
  65. /**
  66. * Get the mime types.
  67. *
  68. * @return mime types to set
  69. */
  70. public Set<String> getMimeTypes()
  71. {
  72. return _mimeTypes;
  73. }
  74. /* ------------------------------------------------------------ */
  75. /**
  76. * Set the mime types.
  77. *
  78. * @param mimeTypes
  79. * the mime types to set
  80. */
  81. public void setMimeTypes(Set<String> mimeTypes)
  82. {
  83. _mimeTypes = mimeTypes;
  84. }
  85. /* ------------------------------------------------------------ */
  86. /**
  87. * Set the mime types.
  88. *
  89. * @param mimeTypes
  90. * the mime types to set
  91. */
  92. public void setMimeTypes(String mimeTypes)
  93. {
  94. if (mimeTypes != null)
  95. {
  96. _mimeTypes = new HashSet<String>();
  97. StringTokenizer tok = new StringTokenizer(mimeTypes,",",false);
  98. while (tok.hasMoreTokens())
  99. {
  100. _mimeTypes.add(tok.nextToken());
  101. }
  102. }
  103. }
  104. /* ------------------------------------------------------------ */
  105. /**
  106. * Get the excluded user agents.
  107. *
  108. * @return excluded user agents
  109. */
  110. public Set<String> getExcluded()
  111. {
  112. return _excluded;
  113. }
  114. /* ------------------------------------------------------------ */
  115. /**
  116. * Set the excluded user agents.
  117. *
  118. * @param excluded
  119. * excluded user agents to set
  120. */
  121. public void setExcluded(Set<String> excluded)
  122. {
  123. _excluded = excluded;
  124. }
  125. /* ------------------------------------------------------------ */
  126. /**
  127. * Set the excluded user agents.
  128. *
  129. * @param excluded
  130. * excluded user agents to set
  131. */
  132. public void setExcluded(String excluded)
  133. {
  134. if (excluded != null)
  135. {
  136. _excluded = new HashSet<String>();
  137. StringTokenizer tok = new StringTokenizer(excluded,",",false);
  138. while (tok.hasMoreTokens())
  139. _excluded.add(tok.nextToken());
  140. }
  141. }
  142. /* ------------------------------------------------------------ */
  143. /**
  144. * Get the buffer size.
  145. *
  146. * @return the buffer size
  147. */
  148. public int getBufferSize()
  149. {
  150. return _bufferSize;
  151. }
  152. /* ------------------------------------------------------------ */
  153. /**
  154. * Set the buffer size.
  155. *
  156. * @param bufferSize
  157. * buffer size to set
  158. */
  159. public void setBufferSize(int bufferSize)
  160. {
  161. _bufferSize = bufferSize;
  162. }
  163. /* ------------------------------------------------------------ */
  164. /**
  165. * Get the minimum reponse size.
  166. *
  167. * @return minimum reponse size
  168. */
  169. public int getMinGzipSize()
  170. {
  171. return _minGzipSize;
  172. }
  173. /* ------------------------------------------------------------ */
  174. /**
  175. * Set the minimum reponse size.
  176. *
  177. * @param minGzipSize
  178. * minimum reponse size
  179. */
  180. public void setMinGzipSize(int minGzipSize)
  181. {
  182. _minGzipSize = minGzipSize;
  183. }
  184. /* ------------------------------------------------------------ */
  185. /**
  186. * @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  187. */
  188. @Override
  189. public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  190. {
  191. if (_handler!=null && isStarted())
  192. {
  193. String ae = request.getHeader("accept-encoding");
  194. if (ae != null && ae.indexOf("gzip")>=0 && !response.containsHeader("Content-Encoding")
  195. && !HttpMethods.HEAD.equalsIgnoreCase(request.getMethod()))
  196. {
  197. if (_excluded!=null)
  198. {
  199. String ua = request.getHeader("User-Agent");
  200. if (_excluded.contains(ua))
  201. {
  202. _handler.handle(target,baseRequest, request, response);
  203. return;
  204. }
  205. }
  206. final GzipResponseWrapper wrappedResponse = newGzipResponseWrapper(request,response);
  207. boolean exceptional=true;
  208. try
  209. {
  210. _handler.handle(target, baseRequest, request, wrappedResponse);
  211. exceptional=false;
  212. }
  213. finally
  214. {
  215. Continuation continuation = ContinuationSupport.getContinuation(request);
  216. if (continuation.isSuspended() && continuation.isResponseWrapped())
  217. {
  218. continuation.addContinuationListener(new ContinuationListener()
  219. {
  220. public void onComplete(Continuation continuation)
  221. {
  222. try
  223. {
  224. wrappedResponse.finish();
  225. }
  226. catch(IOException e)
  227. {
  228. LOG.warn(e);
  229. }
  230. }
  231. public void onTimeout(Continuation continuation)
  232. {}
  233. });
  234. }
  235. else if (exceptional && !response.isCommitted())
  236. {
  237. wrappedResponse.resetBuffer();
  238. wrappedResponse.noGzip();
  239. }
  240. else
  241. wrappedResponse.finish();
  242. }
  243. }
  244. else
  245. {
  246. _handler.handle(target,baseRequest, request, response);
  247. }
  248. }
  249. }
  250. /**
  251. * Allows derived implementations to replace ResponseWrapper implementation.
  252. *
  253. * @param request the request
  254. * @param response the response
  255. * @return the gzip response wrapper
  256. */
  257. protected GzipResponseWrapper newGzipResponseWrapper(HttpServletRequest request, HttpServletResponse response)
  258. {
  259. return new GzipResponseWrapper(request, response)
  260. {
  261. {
  262. setMimeTypes(GzipHandler.this._mimeTypes);
  263. setBufferSize(GzipHandler.this._bufferSize);
  264. setMinGzipSize(GzipHandler.this._minGzipSize);
  265. }
  266. @Override
  267. protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
  268. {
  269. return GzipHandler.this.newWriter(out,encoding);
  270. }
  271. };
  272. }
  273. /**
  274. * Allows derived implementations to replace PrintWriter implementation.
  275. *
  276. * @param out the out
  277. * @param encoding the encoding
  278. * @return the prints the writer
  279. * @throws UnsupportedEncodingException
  280. */
  281. protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
  282. {
  283. return encoding==null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding));
  284. }
  285. }