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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. *
  106. * @return the large file repository storing large files.
  107. * @throws LfsException
  108. * implementations should throw more specific exceptions to
  109. * signal which type of error occurred:
  110. * <dl>
  111. * <dt>{@link LfsValidationError}</dt>
  112. * <dd>when there is a validation error with one or more of the
  113. * objects in the request</dd>
  114. * <dt>{@link LfsRepositoryNotFound}</dt>
  115. * <dd>when the repository does not exist for the user</dd>
  116. * <dt>{@link LfsRepositoryReadOnly}</dt>
  117. * <dd>when the user has read, but not write access. Only
  118. * applicable when the operation in the request is "upload"</dd>
  119. * <dt>{@link LfsRateLimitExceeded}</dt>
  120. * <dd>when the user has hit a rate limit with the server</dd>
  121. * <dt>{@link LfsBandwidthLimitExceeded}</dt>
  122. * <dd>when the bandwidth limit for the user or repository has
  123. * been exceeded</dd>
  124. * <dt>{@link LfsInsufficientStorage}</dt>
  125. * <dd>when there is insufficient storage on the server</dd>
  126. * <dt>{@link LfsUnavailable}</dt>
  127. * <dd>when LFS is not available</dd>
  128. * <dt>{@link LfsException}</dt>
  129. * <dd>when an unexpected internal server error occurred</dd>
  130. * </dl>
  131. * @since 4.5
  132. * @deprecated use
  133. * {@link #getLargeFileRepository(LfsRequest, String, String)}
  134. */
  135. @Deprecated
  136. protected LargeFileRepository getLargeFileRepository(LfsRequest request,
  137. String path) throws LfsException {
  138. return getLargeFileRepository(request, path, null);
  139. }
  140. /**
  141. * Get the large file repository for the given request and path.
  142. *
  143. * @param request
  144. * the request
  145. * @param path
  146. * the path
  147. * @param auth
  148. * the Authorization HTTP header
  149. *
  150. * @return the large file repository storing large files.
  151. * @throws LfsException
  152. * implementations should throw more specific exceptions to
  153. * signal which type of error occurred:
  154. * <dl>
  155. * <dt>{@link LfsValidationError}</dt>
  156. * <dd>when there is a validation error with one or more of the
  157. * objects in the request</dd>
  158. * <dt>{@link LfsRepositoryNotFound}</dt>
  159. * <dd>when the repository does not exist for the user</dd>
  160. * <dt>{@link LfsRepositoryReadOnly}</dt>
  161. * <dd>when the user has read, but not write access. Only
  162. * applicable when the operation in the request is "upload"</dd>
  163. * <dt>{@link LfsRateLimitExceeded}</dt>
  164. * <dd>when the user has hit a rate limit with the server</dd>
  165. * <dt>{@link LfsBandwidthLimitExceeded}</dt>
  166. * <dd>when the bandwidth limit for the user or repository has
  167. * been exceeded</dd>
  168. * <dt>{@link LfsInsufficientStorage}</dt>
  169. * <dd>when there is insufficient storage on the server</dd>
  170. * <dt>{@link LfsUnavailable}</dt>
  171. * <dd>when LFS is not available</dd>
  172. * <dt>{@link LfsException}</dt>
  173. * <dd>when an unexpected internal server error occurred</dd>
  174. * </dl>
  175. * @since 4.7
  176. */
  177. protected abstract LargeFileRepository getLargeFileRepository(
  178. LfsRequest request, String path, String auth) throws LfsException;
  179. /**
  180. * LFS request.
  181. *
  182. * @since 4.5
  183. */
  184. protected static class LfsRequest {
  185. private String operation;
  186. private List<LfsObject> objects;
  187. /**
  188. * Get the LFS operation.
  189. *
  190. * @return the operation
  191. */
  192. public String getOperation() {
  193. return operation;
  194. }
  195. /**
  196. * Get the LFS objects.
  197. *
  198. * @return the objects
  199. */
  200. public List<LfsObject> getObjects() {
  201. return objects;
  202. }
  203. /**
  204. * @return true if the operation is upload.
  205. * @since 4.7
  206. */
  207. public boolean isUpload() {
  208. return operation.equals(UPLOAD);
  209. }
  210. /**
  211. * @return true if the operation is download.
  212. * @since 4.7
  213. */
  214. public boolean isDownload() {
  215. return operation.equals(DOWNLOAD);
  216. }
  217. /**
  218. * @return true if the operation is verify.
  219. * @since 4.7
  220. */
  221. public boolean isVerify() {
  222. return operation.equals(VERIFY);
  223. }
  224. }
  225. @Override
  226. protected void doPost(HttpServletRequest req, HttpServletResponse res)
  227. throws ServletException, IOException {
  228. Writer w = new BufferedWriter(
  229. new OutputStreamWriter(res.getOutputStream(), UTF_8));
  230. Reader r = new BufferedReader(
  231. new InputStreamReader(req.getInputStream(), UTF_8));
  232. LfsRequest request = LfsGson.fromJson(r, LfsRequest.class);
  233. String path = req.getPathInfo();
  234. res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON);
  235. LargeFileRepository repo = null;
  236. try {
  237. repo = getLargeFileRepository(request, path,
  238. req.getHeader(HDR_AUTHORIZATION));
  239. if (repo == null) {
  240. String error = MessageFormat
  241. .format(LfsText.get().lfsFailedToGetRepository, path);
  242. LOG.error(error);
  243. throw new LfsException(error);
  244. }
  245. res.setStatus(SC_OK);
  246. TransferHandler handler = TransferHandler
  247. .forOperation(request.operation, repo, request.objects);
  248. LfsGson.toJson(handler.process(), w);
  249. } catch (LfsValidationError e) {
  250. sendError(res, w, SC_UNPROCESSABLE_ENTITY, e.getMessage());
  251. } catch (LfsRepositoryNotFound e) {
  252. sendError(res, w, SC_NOT_FOUND, e.getMessage());
  253. } catch (LfsRepositoryReadOnly e) {
  254. sendError(res, w, SC_FORBIDDEN, e.getMessage());
  255. } catch (LfsRateLimitExceeded e) {
  256. sendError(res, w, SC_RATE_LIMIT_EXCEEDED, e.getMessage());
  257. } catch (LfsBandwidthLimitExceeded e) {
  258. sendError(res, w, SC_BANDWIDTH_LIMIT_EXCEEDED, e.getMessage());
  259. } catch (LfsInsufficientStorage e) {
  260. sendError(res, w, SC_INSUFFICIENT_STORAGE, e.getMessage());
  261. } catch (LfsUnavailable e) {
  262. sendError(res, w, SC_SERVICE_UNAVAILABLE, e.getMessage());
  263. } catch (LfsUnauthorized e) {
  264. sendError(res, w, SC_UNAUTHORIZED, e.getMessage());
  265. } catch (LfsException e) {
  266. sendError(res, w, SC_INTERNAL_SERVER_ERROR, e.getMessage());
  267. } finally {
  268. w.flush();
  269. }
  270. }
  271. private void sendError(HttpServletResponse rsp, Writer writer, int status,
  272. String message) {
  273. rsp.setStatus(status);
  274. LfsGson.toJson(message, writer);
  275. }
  276. }