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