View Javadoc
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  
45  import static org.eclipse.jgit.lib.Constants.CHARSET;
46  import static org.apache.http.HttpStatus.SC_FORBIDDEN;
47  import static org.apache.http.HttpStatus.SC_INSUFFICIENT_STORAGE;
48  import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR;
49  import static org.apache.http.HttpStatus.SC_NOT_FOUND;
50  import static org.apache.http.HttpStatus.SC_OK;
51  import static org.apache.http.HttpStatus.SC_SERVICE_UNAVAILABLE;
52  import static org.apache.http.HttpStatus.SC_UNAUTHORIZED;
53  import static org.apache.http.HttpStatus.SC_UNPROCESSABLE_ENTITY;
54  import static org.eclipse.jgit.lfs.lib.Constants.DOWNLOAD;
55  import static org.eclipse.jgit.lfs.lib.Constants.UPLOAD;
56  import static org.eclipse.jgit.lfs.lib.Constants.VERIFY;
57  import static org.eclipse.jgit.util.HttpSupport.HDR_AUTHORIZATION;
58  
59  import java.io.BufferedReader;
60  import java.io.BufferedWriter;
61  import java.io.IOException;
62  import java.io.InputStreamReader;
63  import java.io.OutputStreamWriter;
64  import java.io.Reader;
65  import java.io.Writer;
66  import java.text.MessageFormat;
67  import java.util.List;
68  
69  import javax.servlet.ServletException;
70  import javax.servlet.http.HttpServlet;
71  import javax.servlet.http.HttpServletRequest;
72  import javax.servlet.http.HttpServletResponse;
73  
74  import org.eclipse.jgit.lfs.errors.LfsBandwidthLimitExceeded;
75  import org.eclipse.jgit.lfs.errors.LfsException;
76  import org.eclipse.jgit.lfs.errors.LfsInsufficientStorage;
77  import org.eclipse.jgit.lfs.errors.LfsRateLimitExceeded;
78  import org.eclipse.jgit.lfs.errors.LfsRepositoryNotFound;
79  import org.eclipse.jgit.lfs.errors.LfsRepositoryReadOnly;
80  import org.eclipse.jgit.lfs.errors.LfsUnauthorized;
81  import org.eclipse.jgit.lfs.errors.LfsUnavailable;
82  import org.eclipse.jgit.lfs.errors.LfsValidationError;
83  import org.eclipse.jgit.lfs.internal.LfsText;
84  import org.eclipse.jgit.lfs.server.internal.LfsGson;
85  import org.slf4j.Logger;
86  import org.slf4j.LoggerFactory;
87  
88  /**
89   * LFS protocol handler implementing the LFS batch API [1]
90   *
91   * [1] https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md
92   *
93   * @since 4.3
94   */
95  public abstract class LfsProtocolServlet extends HttpServlet {
96  	private static Logger LOG = LoggerFactory
97  			.getLogger(LfsProtocolServlet.class);
98  
99  	private static final long serialVersionUID = 1L;
100 
101 	private static final String CONTENTTYPE_VND_GIT_LFS_JSON =
102 			"application/vnd.git-lfs+json; charset=utf-8"; //$NON-NLS-1$
103 
104 	private static final int SC_RATE_LIMIT_EXCEEDED = 429;
105 
106 	private static final int SC_BANDWIDTH_LIMIT_EXCEEDED = 509;
107 
108 	/**
109 	 * Get the large file repository for the given request and path.
110 	 *
111 	 * @param request
112 	 *            the request
113 	 * @param path
114 	 *            the path
115 	 * @param auth
116 	 *            the Authorization HTTP header
117 	 * @return the large file repository storing large files.
118 	 * @throws org.eclipse.jgit.lfs.errors.LfsException
119 	 *             implementations should throw more specific exceptions to
120 	 *             signal which type of error occurred:
121 	 *             <dl>
122 	 *             <dt>{@link org.eclipse.jgit.lfs.errors.LfsValidationError}</dt>
123 	 *             <dd>when there is a validation error with one or more of the
124 	 *             objects in the request</dd>
125 	 *             <dt>{@link org.eclipse.jgit.lfs.errors.LfsRepositoryNotFound}</dt>
126 	 *             <dd>when the repository does not exist for the user</dd>
127 	 *             <dt>{@link org.eclipse.jgit.lfs.errors.LfsRepositoryReadOnly}</dt>
128 	 *             <dd>when the user has read, but not write access. Only
129 	 *             applicable when the operation in the request is "upload"</dd>
130 	 *             <dt>{@link org.eclipse.jgit.lfs.errors.LfsRateLimitExceeded}</dt>
131 	 *             <dd>when the user has hit a rate limit with the server</dd>
132 	 *             <dt>{@link org.eclipse.jgit.lfs.errors.LfsBandwidthLimitExceeded}</dt>
133 	 *             <dd>when the bandwidth limit for the user or repository has
134 	 *             been exceeded</dd>
135 	 *             <dt>{@link org.eclipse.jgit.lfs.errors.LfsInsufficientStorage}</dt>
136 	 *             <dd>when there is insufficient storage on the server</dd>
137 	 *             <dt>{@link org.eclipse.jgit.lfs.errors.LfsUnavailable}</dt>
138 	 *             <dd>when LFS is not available</dd>
139 	 *             <dt>{@link org.eclipse.jgit.lfs.errors.LfsException}</dt>
140 	 *             <dd>when an unexpected internal server error occurred</dd>
141 	 *             </dl>
142 	 * @since 4.7
143 	 */
144 	protected abstract LargeFileRepository getLargeFileRepository(
145 			LfsRequest request, String path, String auth) throws LfsException;
146 
147 	/**
148 	 * LFS request.
149 	 *
150 	 * @since 4.5
151 	 */
152 	protected static class LfsRequest {
153 		private String operation;
154 
155 		private List<LfsObject> objects;
156 
157 		/**
158 		 * Get the LFS operation.
159 		 *
160 		 * @return the operation
161 		 */
162 		public String getOperation() {
163 			return operation;
164 		}
165 
166 		/**
167 		 * Get the LFS objects.
168 		 *
169 		 * @return the objects
170 		 */
171 		public List<LfsObject> getObjects() {
172 			return objects;
173 		}
174 
175 		/**
176 		 * @return true if the operation is upload.
177 		 * @since 4.7
178 		 */
179 		public boolean isUpload() {
180 			return operation.equals(UPLOAD);
181 		}
182 
183 		/**
184 		 * @return true if the operation is download.
185 		 * @since 4.7
186 		 */
187 		public boolean isDownload() {
188 			return operation.equals(DOWNLOAD);
189 		}
190 
191 		/**
192 		 * @return true if the operation is verify.
193 		 * @since 4.7
194 		 */
195 		public boolean isVerify() {
196 			return operation.equals(VERIFY);
197 		}
198 	}
199 
200 	/** {@inheritDoc} */
201 	@Override
202 	protected void doPost(HttpServletRequest req, HttpServletResponse res)
203 			throws ServletException, IOException {
204 		Writer w = new BufferedWriter(
205 				new OutputStreamWriter(res.getOutputStream(), CHARSET));
206 
207 		Reader r = new BufferedReader(
208 				new InputStreamReader(req.getInputStream(), CHARSET));
209 		LfsRequest request = LfsGson.fromJson(r, LfsRequest.class);
210 		String path = req.getPathInfo();
211 
212 		res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON);
213 		LargeFileRepository repo = null;
214 		try {
215 			repo = getLargeFileRepository(request, path,
216 					req.getHeader(HDR_AUTHORIZATION));
217 			if (repo == null) {
218 				String error = MessageFormat
219 						.format(LfsText.get().lfsFailedToGetRepository, path);
220 				LOG.error(error);
221 				throw new LfsException(error);
222 			}
223 			res.setStatus(SC_OK);
224 			TransferHandler handler = TransferHandler
225 					.forOperation(request.operation, repo, request.objects);
226 			LfsGson.toJson(handler.process(), w);
227 		} catch (LfsValidationError e) {
228 			sendError(res, w, SC_UNPROCESSABLE_ENTITY, e.getMessage());
229 		} catch (LfsRepositoryNotFound e) {
230 			sendError(res, w, SC_NOT_FOUND, e.getMessage());
231 		} catch (LfsRepositoryReadOnly e) {
232 			sendError(res, w, SC_FORBIDDEN, e.getMessage());
233 		} catch (LfsRateLimitExceeded e) {
234 			sendError(res, w, SC_RATE_LIMIT_EXCEEDED, e.getMessage());
235 		} catch (LfsBandwidthLimitExceeded e) {
236 			sendError(res, w, SC_BANDWIDTH_LIMIT_EXCEEDED, e.getMessage());
237 		} catch (LfsInsufficientStorage e) {
238 			sendError(res, w, SC_INSUFFICIENT_STORAGE, e.getMessage());
239 		} catch (LfsUnavailable e) {
240 			sendError(res, w, SC_SERVICE_UNAVAILABLE, e.getMessage());
241 		} catch (LfsUnauthorized e) {
242 			sendError(res, w, SC_UNAUTHORIZED, e.getMessage());
243 		} catch (LfsException e) {
244 			sendError(res, w, SC_INTERNAL_SERVER_ERROR, e.getMessage());
245 		} finally {
246 			w.flush();
247 		}
248 	}
249 
250 	private void sendError(HttpServletResponse rsp, Writer writer, int status,
251 			String message) {
252 		rsp.setStatus(status);
253 		LfsGson.toJson(message, writer);
254 	}
255 }