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.0KB

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