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.LfsServerText;
62
63 import com.google.gson.FieldNamingPolicy;
64 import com.google.gson.Gson;
65 import com.google.gson.GsonBuilder;
66
67
68
69
70
71
72
73
74
75 @WebServlet(asyncSupported = true)
76 public class FileLfsServlet extends HttpServlet {
77
78 private static final long serialVersionUID = 1L;
79
80 private final FileLfsRepository repository;
81
82 private final long timeout;
83
84 private static Gson gson = createGson();
85
86
87
88
89
90
91
92 public FileLfsServlet(FileLfsRepository repository, long timeout) {
93 this.repository = repository;
94 this.timeout = timeout;
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109 @Override
110 protected void doGet(HttpServletRequest req,
111 HttpServletResponse rsp) throws ServletException, IOException {
112 AnyLongObjectId obj = getObjectToTransfer(req, rsp);
113 if (obj != null) {
114 if (repository.getSize(obj) == -1) {
115 sendError(rsp, HttpStatus.SC_NOT_FOUND, MessageFormat
116 .format(LfsServerText.get().objectNotFound,
117 obj.getName()));
118 return;
119 }
120 AsyncContext context = req.startAsync();
121 context.setTimeout(timeout);
122 rsp.getOutputStream()
123 .setWriteListener(new ObjectDownloadListener(repository,
124 context, rsp, obj));
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 protected AnyLongObjectId getObjectToTransfer(HttpServletRequest req,
142 HttpServletResponse rsp) throws IOException {
143 String info = req.getPathInfo();
144 int length = 1 + Constants.LONG_OBJECT_ID_STRING_LENGTH;
145 if (info.length() != length) {
146 sendError(rsp, HttpStatus.SC_UNPROCESSABLE_ENTITY, MessageFormat
147 .format(LfsServerText.get().invalidPathInfo, info));
148 return null;
149 }
150 try {
151 return LongObjectId.fromString(info.substring(1, length));
152 } catch (InvalidLongObjectIdException e) {
153 sendError(rsp, HttpStatus.SC_UNPROCESSABLE_ENTITY, e.getMessage());
154 return null;
155 }
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170 @Override
171 protected void doPut(HttpServletRequest req,
172 HttpServletResponse rsp) throws ServletException, IOException {
173 AnyLongObjectId id = getObjectToTransfer(req, rsp);
174 if (id != null) {
175 AsyncContext context = req.startAsync();
176 context.setTimeout(timeout);
177 req.getInputStream().setReadListener(new ObjectUploadListener(
178 repository, context, req, rsp, id));
179 }
180 }
181
182 static class Error {
183 String message;
184
185 Error(String m) {
186 this.message = m;
187 }
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 protected static void sendError(HttpServletResponse rsp, int status, String message)
204 throws IOException {
205 rsp.setStatus(status);
206 PrintWriter writer = rsp.getWriter();
207 gson.toJson(new Error(message), writer);
208 writer.flush();
209 writer.close();
210 rsp.flushBuffer();
211 }
212
213 private static Gson createGson() {
214 GsonBuilder gb = new GsonBuilder()
215 .setFieldNamingPolicy(
216 FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
217 .setPrettyPrinting().disableHtmlEscaping();
218 return gb.create();
219 }
220 }