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