1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.server;
12
13 import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
14 import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
15 import static org.eclipse.jgit.http.server.ServletUtils.send;
16
17 import java.io.File;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20
21 import javax.servlet.http.HttpServlet;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.eclipse.jgit.util.HttpSupport;
26 import org.eclipse.jgit.util.IO;
27
28
29 class TextFileServlet extends HttpServlet {
30 private static final long serialVersionUID = 1L;
31
32 private final String fileName;
33
34 TextFileServlet(String name) {
35 this.fileName = name;
36 }
37
38
39 @Override
40 public void doGet(final HttpServletRequest req,
41 final HttpServletResponse rsp) throws IOException {
42 try {
43 rsp.setContentType(HttpSupport.TEXT_PLAIN);
44 send(read(req), req, rsp);
45 } catch (FileNotFoundException noFile) {
46 rsp.sendError(SC_NOT_FOUND);
47 }
48 }
49
50 private byte[] read(HttpServletRequest req) throws IOException {
51 final File gitdir = getRepository(req).getDirectory();
52 if (gitdir == null)
53 throw new FileNotFoundException(fileName);
54 return IO.readFully(new File(gitdir, fileName));
55 }
56 }