1 /*
2 * Copyright (C) 2015, Matthias Sohn <matthias.sohn@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.fs;
44
45 import java.io.IOException;
46 import java.text.MessageFormat;
47
48 import javax.servlet.AsyncContext;
49 import javax.servlet.ServletException;
50 import javax.servlet.annotation.WebServlet;
51 import javax.servlet.http.HttpServlet;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55 import org.apache.http.HttpStatus;
56 import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
57 import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
58 import org.eclipse.jgit.lfs.lib.Constants;
59 import org.eclipse.jgit.lfs.lib.LongObjectId;
60 import org.eclipse.jgit.lfs.server.internal.LfsServerText;
61
62 /**
63 * Servlet supporting upload and download of large objects as defined by the
64 * GitHub Large File Storage extension API extending git to allow separate
65 * storage of large files
66 * (https://github.com/github/git-lfs/tree/master/docs/api).
67 *
68 * @since 4.3
69 */
70 @WebServlet(asyncSupported = true)
71 public class FileLfsServlet extends HttpServlet {
72
73 private static final long serialVersionUID = 1L;
74
75 private final FileLfsRepository repository;
76
77 private final long timeout;
78
79 /**
80 * @param repository
81 * the repository storing the large objects
82 * @param timeout
83 * timeout for object upload / download in milliseconds
84 */
85 public FileLfsServlet(FileLfsRepository repository, long timeout) {
86 this.repository = repository;
87 this.timeout = timeout;
88 }
89
90 /**
91 * Handles object downloads
92 *
93 * @param req
94 * servlet request
95 * @param rsp
96 * servlet response
97 * @throws ServletException
98 * if a servlet-specific error occurs
99 * @throws IOException
100 * if an I/O error occurs
101 */
102 @Override
103 protected void doGet(HttpServletRequest req,
104 HttpServletResponse rsp) throws ServletException, IOException {
105 AnyLongObjectId obj = getObjectToTransfer(req, rsp);
106 if (obj != null) {
107 if (repository.getSize(obj) == -1) {
108 sendError(rsp, HttpStatus.SC_NOT_FOUND, MessageFormat
109 .format(LfsServerText.get().objectNotFound, obj));
110 return;
111 }
112 AsyncContext context = req.startAsync();
113 context.setTimeout(timeout);
114 rsp.getOutputStream()
115 .setWriteListener(new ObjectDownloadListener(repository,
116 context, rsp, obj));
117 }
118 }
119
120 private AnyLongObjectId getObjectToTransfer(HttpServletRequest req,
121 HttpServletResponse rsp) throws IOException {
122 String info = req.getPathInfo();
123 if (info.length() != 1 + Constants.LONG_OBJECT_ID_STRING_LENGTH) {
124 sendError(rsp, HttpStatus.SC_BAD_REQUEST, MessageFormat
125 .format(LfsServerText.get().invalidPathInfo, info));
126 return null;
127 }
128 try {
129 return LongObjectId.fromString(info.substring(1, 65));
130 } catch (InvalidLongObjectIdException e) {
131 sendError(rsp, HttpStatus.SC_BAD_REQUEST, e.getMessage());
132 return null;
133 }
134 }
135
136 /**
137 * Handle object uploads
138 *
139 * @param req
140 * servlet request
141 * @param rsp
142 * servlet response
143 * @throws ServletException
144 * if a servlet-specific error occurs
145 * @throws IOException
146 * if an I/O error occurs
147 */
148 @Override
149 protected void doPut(HttpServletRequest req,
150 HttpServletResponse rsp) throws ServletException, IOException {
151 AnyLongObjectId id = getObjectToTransfer(req, rsp);
152 if (id != null) {
153 AsyncContext context = req.startAsync();
154 context.setTimeout(timeout);
155 req.getInputStream().setReadListener(new ObjectUploadListener(
156 repository, context, req, rsp, id));
157 }
158 }
159
160 static void sendError(HttpServletResponse rsp, int status, String message)
161 throws IOException {
162 rsp.setStatus(status);
163 // TODO return message in response body in json format as specified in
164 // https://github.com/github/git-lfs/blob/master/docs/api/http-v1-batch.md
165 rsp.flushBuffer();
166 }
167 }