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.

LfsProtocolServlet.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2015, Sasa Zivkov <sasa.zivkov@sap.com>
  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.lfs.server;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.apache.http.HttpStatus.SC_FORBIDDEN;
  46. import static org.apache.http.HttpStatus.SC_INSUFFICIENT_STORAGE;
  47. import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR;
  48. import static org.apache.http.HttpStatus.SC_NOT_FOUND;
  49. import static org.apache.http.HttpStatus.SC_OK;
  50. import static org.apache.http.HttpStatus.SC_SERVICE_UNAVAILABLE;
  51. import static org.apache.http.HttpStatus.SC_UNAUTHORIZED;
  52. import static org.apache.http.HttpStatus.SC_UNPROCESSABLE_ENTITY;
  53. import static org.eclipse.jgit.lfs.lib.Constants.DOWNLOAD;
  54. import static org.eclipse.jgit.lfs.lib.Constants.UPLOAD;
  55. import static org.eclipse.jgit.lfs.lib.Constants.VERIFY;
  56. import static org.eclipse.jgit.util.HttpSupport.HDR_AUTHORIZATION;
  57. import java.io.BufferedReader;
  58. import java.io.BufferedWriter;
  59. import java.io.IOException;
  60. import java.io.InputStreamReader;
  61. import java.io.OutputStreamWriter;
  62. import java.io.Reader;
  63. import java.io.Writer;
  64. import java.text.MessageFormat;
  65. import java.util.List;
  66. import javax.servlet.ServletException;
  67. import javax.servlet.http.HttpServlet;
  68. import javax.servlet.http.HttpServletRequest;
  69. import javax.servlet.http.HttpServletResponse;
  70. import org.eclipse.jgit.lfs.errors.LfsBandwidthLimitExceeded;
  71. import org.eclipse.jgit.lfs.errors.LfsException;
  72. import org.eclipse.jgit.lfs.errors.LfsInsufficientStorage;
  73. import org.eclipse.jgit.lfs.errors.LfsRateLimitExceeded;
  74. import org.eclipse.jgit.lfs.errors.LfsRepositoryNotFound;
  75. import org.eclipse.jgit.lfs.errors.LfsRepositoryReadOnly;
  76. import org.eclipse.jgit.lfs.errors.LfsUnauthorized;
  77. import org.eclipse.jgit.lfs.errors.LfsUnavailable;
  78. import org.eclipse.jgit.lfs.errors.LfsValidationError;
  79. import org.eclipse.jgit.lfs.internal.LfsText;
  80. import org.eclipse.jgit.lfs.server.internal.LfsGson;
  81. import org.slf4j.Logger;
  82. import org.slf4j.LoggerFactory;
  83. /**
  84. * LFS protocol handler implementing the LFS batch API [1]
  85. *
  86. * [1] https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md
  87. *
  88. * @since 4.3
  89. */
  90. public abstract class LfsProtocolServlet extends HttpServlet {
  91. private static Logger LOG = LoggerFactory
  92. .getLogger(LfsProtocolServlet.class);
  93. private static final long serialVersionUID = 1L;
  94. private static final String CONTENTTYPE_VND_GIT_LFS_JSON =
  95. "application/vnd.git-lfs+json; charset=utf-8"; //$NON-NLS-1$
  96. private static final int SC_RATE_LIMIT_EXCEEDED = 429;
  97. private static final int SC_BANDWIDTH_LIMIT_EXCEEDED = 509;
  98. /**
  99. * Get the large file repository for the given request and path.
  100. *
  101. * @param request
  102. * the request
  103. * @param path
  104. * the path
  105. * @return the large file repository storing large files.
  106. * @throws org.eclipse.jgit.lfs.errors.LfsException
  107. * implementations should throw more specific exceptions to
  108. * signal which type of error occurred:
  109. * <dl>
  110. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsValidationError}</dt>
  111. * <dd>when there is a validation error with one or more of the
  112. * objects in the request</dd>
  113. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsRepositoryNotFound}</dt>
  114. * <dd>when the repository does not exist for the user</dd>
  115. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsRepositoryReadOnly}</dt>
  116. * <dd>when the user has read, but not write access. Only
  117. * applicable when the operation in the request is "upload"</dd>
  118. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsRateLimitExceeded}</dt>
  119. * <dd>when the user has hit a rate limit with the server</dd>
  120. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsBandwidthLimitExceeded}</dt>
  121. * <dd>when the bandwidth limit for the user or repository has
  122. * been exceeded</dd>
  123. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsInsufficientStorage}</dt>
  124. * <dd>when there is insufficient storage on the server</dd>
  125. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsUnavailable}</dt>
  126. * <dd>when LFS is not available</dd>
  127. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsException}</dt>
  128. * <dd>when an unexpected internal server error occurred</dd>
  129. * </dl>
  130. * @since 4.5
  131. * @deprecated use
  132. * {@link #getLargeFileRepository(LfsRequest, String, String)}
  133. */
  134. @Deprecated
  135. protected LargeFileRepository getLargeFileRepository(LfsRequest request,
  136. String path) throws LfsException {
  137. return getLargeFileRepository(request, path, null);
  138. }
  139. /**
  140. * Get the large file repository for the given request and path.
  141. *
  142. * @param request
  143. * the request
  144. * @param path
  145. * the path
  146. * @param auth
  147. * the Authorization HTTP header
  148. * @return the large file repository storing large files.
  149. * @throws org.eclipse.jgit.lfs.errors.LfsException
  150. * implementations should throw more specific exceptions to
  151. * signal which type of error occurred:
  152. * <dl>
  153. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsValidationError}</dt>
  154. * <dd>when there is a validation error with one or more of the
  155. * objects in the request</dd>
  156. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsRepositoryNotFound}</dt>
  157. * <dd>when the repository does not exist for the user</dd>
  158. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsRepositoryReadOnly}</dt>
  159. * <dd>when the user has read, but not write access. Only
  160. * applicable when the operation in the request is "upload"</dd>
  161. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsRateLimitExceeded}</dt>
  162. * <dd>when the user has hit a rate limit with the server</dd>
  163. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsBandwidthLimitExceeded}</dt>
  164. * <dd>when the bandwidth limit for the user or repository has
  165. * been exceeded</dd>
  166. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsInsufficientStorage}</dt>
  167. * <dd>when there is insufficient storage on the server</dd>
  168. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsUnavailable}</dt>
  169. * <dd>when LFS is not available</dd>
  170. * <dt>{@link org.eclipse.jgit.lfs.errors.LfsException}</dt>
  171. * <dd>when an unexpected internal server error occurred</dd>
  172. * </dl>
  173. * @since 4.7
  174. */
  175. protected abstract LargeFileRepository getLargeFileRepository(
  176. LfsRequest request, String path, String auth) throws LfsException;
  177. /**
  178. * LFS request.
  179. *
  180. * @since 4.5
  181. */
  182. protected static class LfsRequest {
  183. private String operation;
  184. private List<LfsObject> objects;
  185. /**
  186. * Get the LFS operation.
  187. *
  188. * @return the operation
  189. */
  190. public String getOperation() {
  191. return operation;
  192. }
  193. /**
  194. * Get the LFS objects.
  195. *
  196. * @return the objects
  197. */
  198. public List<LfsObject> getObjects() {
  199. return objects;
  200. }
  201. /**
  202. * @return true if the operation is upload.
  203. * @since 4.7
  204. */
  205. public boolean isUpload() {
  206. return operation.equals(UPLOAD);
  207. }
  208. /**
  209. * @return true if the operation is download.
  210. * @since 4.7
  211. */
  212. public boolean isDownload() {
  213. return operation.equals(DOWNLOAD);
  214. }
  215. /**
  216. * @return true if the operation is verify.
  217. * @since 4.7
  218. */
  219. public boolean isVerify() {
  220. return operation.equals(VERIFY);
  221. }
  222. }
  223. /** {@inheritDoc} */
  224. @Override
  225. protected void doPost(HttpServletRequest req, HttpServletResponse res)
  226. throws ServletException, IOException {
  227. Writer w = new BufferedWriter(
  228. new OutputStreamWriter(res.getOutputStream(), UTF_8));
  229. Reader r = new BufferedReader(
  230. new InputStreamReader(req.getInputStream(), UTF_8));
  231. LfsRequest request = LfsGson.fromJson(r, LfsRequest.class);
  232. String path = req.getPathInfo();
  233. res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON);
  234. LargeFileRepository repo = null;
  235. try {
  236. repo = getLargeFileRepository(request, path,
  237. req.getHeader(HDR_AUTHORIZATION));
  238. if (repo == null) {
  239. String error = MessageFormat
  240. .format(LfsText.get().lfsFailedToGetRepository, path);
  241. LOG.error(error);
  242. throw new LfsException(error);
  243. }
  244. res.setStatus(SC_OK);
  245. TransferHandler handler = TransferHandler
  246. .forOperation(request.operation, repo, request.objects);
  247. LfsGson.toJson(handler.process(), w);
  248. } catch (LfsValidationError e) {
  249. sendError(res, w, SC_UNPROCESSABLE_ENTITY, e.getMessage());
  250. } catch (LfsRepositoryNotFound e) {
  251. sendError(res, w, SC_NOT_FOUND, e.getMessage());
  252. } catch (LfsRepositoryReadOnly e) {
  253. sendError(res, w, SC_FORBIDDEN, e.getMessage());
  254. } catch (LfsRateLimitExceeded e) {
  255. sendError(res, w, SC_RATE_LIMIT_EXCEEDED, e.getMessage());
  256. } catch (LfsBandwidthLimitExceeded e) {
  257. sendError(res, w, SC_BANDWIDTH_LIMIT_EXCEEDED, e.getMessage());
  258. } catch (LfsInsufficientStorage e) {
  259. sendError(res, w, SC_INSUFFICIENT_STORAGE, e.getMessage());
  260. } catch (LfsUnavailable e) {
  261. sendError(res, w, SC_SERVICE_UNAVAILABLE, e.getMessage());
  262. } catch (LfsUnauthorized e) {
  263. sendError(res, w, SC_UNAUTHORIZED, e.getMessage());
  264. } catch (LfsException e) {
  265. sendError(res, w, SC_INTERNAL_SERVER_ERROR, e.getMessage());
  266. } finally {
  267. w.flush();
  268. }
  269. }
  270. private void sendError(HttpServletResponse rsp, Writer writer, int status,
  271. String message) {
  272. rsp.setStatus(status);
  273. LfsGson.toJson(message, writer);
  274. }
  275. }