You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServletUtils.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.http.server;
  44. import static org.eclipse.jgit.util.HttpSupport.ENCODING_GZIP;
  45. import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT_ENCODING;
  46. import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_ENCODING;
  47. import static org.eclipse.jgit.util.HttpSupport.HDR_ETAG;
  48. import static org.eclipse.jgit.util.HttpSupport.TEXT_PLAIN;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.io.OutputStream;
  53. import java.security.MessageDigest;
  54. import java.util.zip.GZIPInputStream;
  55. import java.util.zip.GZIPOutputStream;
  56. import javax.servlet.ServletRequest;
  57. import javax.servlet.http.HttpServletRequest;
  58. import javax.servlet.http.HttpServletResponse;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.Repository;
  62. /** Common utility functions for servlets. */
  63. public final class ServletUtils {
  64. /** Request attribute which stores the {@link Repository} instance. */
  65. public static final String ATTRIBUTE_REPOSITORY = "org.eclipse.jgit.Repository";
  66. /**
  67. * Get the selected repository from the request.
  68. *
  69. * @param req
  70. * the current request.
  71. * @return the repository; never null.
  72. * @throws IllegalStateException
  73. * the repository was not set by the filter, the servlet is
  74. * being invoked incorrectly and the programmer should ensure
  75. * the filter runs before the servlet.
  76. * @see #ATTRIBUTE_REPOSITORY
  77. */
  78. public static Repository getRepository(final ServletRequest req) {
  79. Repository db = (Repository) req.getAttribute(ATTRIBUTE_REPOSITORY);
  80. if (db == null)
  81. throw new IllegalStateException("Expected Repository attribute");
  82. return db;
  83. }
  84. /**
  85. * Open the request input stream, automatically inflating if necessary.
  86. * <p>
  87. * This method automatically inflates the input stream if the request
  88. * {@code Content-Encoding} header was set to {@code gzip} or the legacy
  89. * {@code x-gzip}.
  90. *
  91. * @param req
  92. * the incoming request whose input stream needs to be opened.
  93. * @return an input stream to read the raw, uncompressed request body.
  94. * @throws IOException
  95. * if an input or output exception occurred.
  96. */
  97. public static InputStream getInputStream(final HttpServletRequest req)
  98. throws IOException {
  99. InputStream in = req.getInputStream();
  100. final String enc = req.getHeader(HDR_CONTENT_ENCODING);
  101. if (ENCODING_GZIP.equals(enc) || "x-gzip".equals(enc)) //$NON-NLS-1$
  102. in = new GZIPInputStream(in);
  103. else if (enc != null)
  104. throw new IOException(HDR_CONTENT_ENCODING + " \"" + enc + "\""
  105. + ": not supported by this library.");
  106. return in;
  107. }
  108. /**
  109. * Send a plain text response to a {@code GET} or {@code HEAD} HTTP request.
  110. * <p>
  111. * The text response is encoded in the Git character encoding, UTF-8.
  112. * <p>
  113. * If the user agent supports a compressed transfer encoding and the content
  114. * is large enough, the content may be compressed before sending.
  115. * <p>
  116. * The {@code ETag} and {@code Content-Length} headers are automatically set
  117. * by this method. {@code Content-Encoding} is conditionally set if the user
  118. * agent supports a compressed transfer. Callers are responsible for setting
  119. * any cache control headers.
  120. *
  121. * @param content
  122. * to return to the user agent as this entity's body.
  123. * @param req
  124. * the incoming request.
  125. * @param rsp
  126. * the outgoing response.
  127. * @throws IOException
  128. * the servlet API rejected sending the body.
  129. */
  130. public static void sendPlainText(final String content,
  131. final HttpServletRequest req, final HttpServletResponse rsp)
  132. throws IOException {
  133. final byte[] raw = content.getBytes(Constants.CHARACTER_ENCODING);
  134. rsp.setContentType(TEXT_PLAIN);
  135. rsp.setCharacterEncoding(Constants.CHARACTER_ENCODING);
  136. send(raw, req, rsp);
  137. }
  138. /**
  139. * Send a response to a {@code GET} or {@code HEAD} HTTP request.
  140. * <p>
  141. * If the user agent supports a compressed transfer encoding and the content
  142. * is large enough, the content may be compressed before sending.
  143. * <p>
  144. * The {@code ETag} and {@code Content-Length} headers are automatically set
  145. * by this method. {@code Content-Encoding} is conditionally set if the user
  146. * agent supports a compressed transfer. Callers are responsible for setting
  147. * {@code Content-Type} and any cache control headers.
  148. *
  149. * @param content
  150. * to return to the user agent as this entity's body.
  151. * @param req
  152. * the incoming request.
  153. * @param rsp
  154. * the outgoing response.
  155. * @throws IOException
  156. * the servlet API rejected sending the body.
  157. */
  158. public static void send(byte[] content, final HttpServletRequest req,
  159. final HttpServletResponse rsp) throws IOException {
  160. content = sendInit(content, req, rsp);
  161. final OutputStream out = rsp.getOutputStream();
  162. try {
  163. out.write(content);
  164. out.flush();
  165. } finally {
  166. out.close();
  167. }
  168. }
  169. private static byte[] sendInit(byte[] content,
  170. final HttpServletRequest req, final HttpServletResponse rsp)
  171. throws IOException {
  172. rsp.setHeader(HDR_ETAG, etag(content));
  173. if (256 < content.length && acceptsGzipEncoding(req)) {
  174. content = compress(content);
  175. rsp.setHeader(HDR_CONTENT_ENCODING, ENCODING_GZIP);
  176. }
  177. rsp.setContentLength(content.length);
  178. return content;
  179. }
  180. private static boolean acceptsGzipEncoding(final HttpServletRequest req) {
  181. final String accepts = req.getHeader(HDR_ACCEPT_ENCODING);
  182. return accepts != null && 0 <= accepts.indexOf(ENCODING_GZIP);
  183. }
  184. private static byte[] compress(final byte[] raw) throws IOException {
  185. final int maxLen = raw.length + 32;
  186. final ByteArrayOutputStream out = new ByteArrayOutputStream(maxLen);
  187. final GZIPOutputStream gz = new GZIPOutputStream(out);
  188. gz.write(raw);
  189. gz.finish();
  190. gz.flush();
  191. return out.toByteArray();
  192. }
  193. private static String etag(final byte[] content) {
  194. final MessageDigest md = Constants.newMessageDigest();
  195. md.update(content);
  196. return ObjectId.fromRaw(md.digest()).getName();
  197. }
  198. private ServletUtils() {
  199. // static utility class only
  200. }
  201. }