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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 java.io.BufferedReader;
  54. import java.io.BufferedWriter;
  55. import java.io.IOException;
  56. import java.io.InputStreamReader;
  57. import java.io.OutputStreamWriter;
  58. import java.io.Reader;
  59. import java.io.Writer;
  60. import java.util.List;
  61. import javax.servlet.ServletException;
  62. import javax.servlet.http.HttpServlet;
  63. import javax.servlet.http.HttpServletRequest;
  64. import javax.servlet.http.HttpServletResponse;
  65. import org.eclipse.jgit.lfs.errors.LfsBandwidthLimitExceeded;
  66. import org.eclipse.jgit.lfs.errors.LfsException;
  67. import org.eclipse.jgit.lfs.errors.LfsInsufficientStorage;
  68. import org.eclipse.jgit.lfs.errors.LfsRateLimitExceeded;
  69. import org.eclipse.jgit.lfs.errors.LfsRepositoryNotFound;
  70. import org.eclipse.jgit.lfs.errors.LfsRepositoryReadOnly;
  71. import org.eclipse.jgit.lfs.errors.LfsUnauthorized;
  72. import org.eclipse.jgit.lfs.errors.LfsUnavailable;
  73. import org.eclipse.jgit.lfs.errors.LfsValidationError;
  74. import com.google.gson.FieldNamingPolicy;
  75. import com.google.gson.Gson;
  76. import com.google.gson.GsonBuilder;
  77. /**
  78. * LFS protocol handler implementing the LFS batch API [1]
  79. *
  80. * [1] https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md
  81. *
  82. * @since 4.3
  83. */
  84. public abstract class LfsProtocolServlet extends HttpServlet {
  85. private static final long serialVersionUID = 1L;
  86. private static final String CONTENTTYPE_VND_GIT_LFS_JSON =
  87. "application/vnd.git-lfs+json; charset=utf-8"; //$NON-NLS-1$
  88. private static final int SC_RATE_LIMIT_EXCEEDED = 429;
  89. private static final int SC_BANDWIDTH_LIMIT_EXCEEDED = 509;
  90. private Gson gson = createGson();
  91. /**
  92. * Get the large file repository for the given request and path.
  93. *
  94. * @param request
  95. * the request
  96. * @param path
  97. * the path
  98. *
  99. * @return the large file repository storing large files.
  100. * @throws LfsException
  101. * implementations should throw more specific exceptions to
  102. * signal which type of error occurred:
  103. * <dl>
  104. * <dt>{@link LfsValidationError}</dt>
  105. * <dd>when there is a validation error with one or more of the
  106. * objects in the request</dd>
  107. * <dt>{@link LfsRepositoryNotFound}</dt>
  108. * <dd>when the repository does not exist for the user</dd>
  109. * <dt>{@link LfsRepositoryReadOnly}</dt>
  110. * <dd>when the user has read, but not write access. Only
  111. * applicable when the operation in the request is "upload"</dd>
  112. * <dt>{@link LfsRateLimitExceeded}</dt>
  113. * <dd>when the user has hit a rate limit with the server</dd>
  114. * <dt>{@link LfsBandwidthLimitExceeded}</dt>
  115. * <dd>when the bandwidth limit for the user or repository has
  116. * been exceeded</dd>
  117. * <dt>{@link LfsInsufficientStorage}</dt>
  118. * <dd>when there is insufficient storage on the server</dd>
  119. * <dt>{@link LfsUnavailable}</dt>
  120. * <dd>when LFS is not available</dd>
  121. * <dt>{@link LfsException}</dt>
  122. * <dd>when an unexpected internal server error occurred</dd>
  123. * </dl>
  124. * @since 4.5
  125. */
  126. protected abstract LargeFileRepository getLargeFileRepository(
  127. LfsRequest request, String path) throws LfsException;
  128. /**
  129. * LFS request.
  130. *
  131. * @since 4.5
  132. */
  133. protected static class LfsRequest {
  134. private String operation;
  135. private List<LfsObject> objects;
  136. /**
  137. * Get the LFS operation.
  138. *
  139. * @return the operation
  140. */
  141. public String getOperation() {
  142. return operation;
  143. }
  144. /**
  145. * Get the LFS objects.
  146. *
  147. * @return the objects
  148. */
  149. public List<LfsObject> getObjects() {
  150. return objects;
  151. }
  152. }
  153. @Override
  154. protected void doPost(HttpServletRequest req, HttpServletResponse res)
  155. throws ServletException, IOException {
  156. Writer w = new BufferedWriter(
  157. new OutputStreamWriter(res.getOutputStream(), UTF_8));
  158. Reader r = new BufferedReader(
  159. new InputStreamReader(req.getInputStream(), UTF_8));
  160. LfsRequest request = gson.fromJson(r, LfsRequest.class);
  161. String path = req.getPathInfo();
  162. res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON);
  163. LargeFileRepository repo = null;
  164. try {
  165. repo = getLargeFileRepository(request, path);
  166. if (repo == null) {
  167. throw new LfsException("unexpected error"); //$NON-NLS-1$
  168. }
  169. res.setStatus(SC_OK);
  170. TransferHandler handler = TransferHandler
  171. .forOperation(request.operation, repo, request.objects);
  172. gson.toJson(handler.process(), w);
  173. } catch (LfsValidationError e) {
  174. sendError(res, w, SC_UNPROCESSABLE_ENTITY, e.getMessage());
  175. } catch (LfsRepositoryNotFound e) {
  176. sendError(res, w, SC_NOT_FOUND, e.getMessage());
  177. } catch (LfsRepositoryReadOnly e) {
  178. sendError(res, w, SC_FORBIDDEN, e.getMessage());
  179. } catch (LfsRateLimitExceeded e) {
  180. sendError(res, w, SC_RATE_LIMIT_EXCEEDED, e.getMessage());
  181. } catch (LfsBandwidthLimitExceeded e) {
  182. sendError(res, w, SC_BANDWIDTH_LIMIT_EXCEEDED, e.getMessage());
  183. } catch (LfsInsufficientStorage e) {
  184. sendError(res, w, SC_INSUFFICIENT_STORAGE, e.getMessage());
  185. } catch (LfsUnavailable e) {
  186. sendError(res, w, SC_SERVICE_UNAVAILABLE, e.getMessage());
  187. } catch (LfsUnauthorized e) {
  188. sendError(res, w, SC_UNAUTHORIZED, e.getMessage());
  189. } catch (LfsException e) {
  190. sendError(res, w, SC_INTERNAL_SERVER_ERROR, e.getMessage());
  191. } finally {
  192. w.flush();
  193. }
  194. }
  195. static class Error {
  196. String message;
  197. Error(String m) {
  198. this.message = m;
  199. }
  200. }
  201. private void sendError(HttpServletResponse rsp, Writer writer, int status,
  202. String message) {
  203. rsp.setStatus(status);
  204. gson.toJson(new Error(message), writer);
  205. }
  206. private Gson createGson() {
  207. return new GsonBuilder()
  208. .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
  209. .disableHtmlEscaping()
  210. .create();
  211. }
  212. }