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

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