1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
66
67
68
69
70
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
83
84
85
86
87
88
89 public FileLfsServlet(FileLfsRepository repository, long timeout) {
90 this.repository = repository;
91 this.timeout = timeout;
92 }
93
94
95
96
97
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
120
121
122
123
124
125
126
127
128
129
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
150
151
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
167
168
169
170
171
172
173
174
175
176
177
178 protected static void sendError(HttpServletResponse rsp, int status, String message)
179 throws IOException {
180 if (rsp.isCommitted()) {
181 rsp.getOutputStream().close();
182 return;
183 }
184 rsp.reset();
185 rsp.setStatus(status);
186 try (PrintWriter writer = rsp.getWriter()) {
187 LfsGson.toJson(message, writer);
188 writer.flush();
189 }
190 rsp.flushBuffer();
191 }
192 }