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 9.2KB

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