xref: /JGit/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/TextFileServlet.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1 /*
2  * Copyright (C) 2009-2010, Google Inc. and others
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Distribution License v. 1.0 which is available at
6  * https://www.eclipse.org/org/documents/edl-v10.php.
7  *
8  * SPDX-License-Identifier: BSD-3-Clause
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 /** Sends a small text meta file from the repository. */
29 class TextFileServlet extends HttpServlet {
30 	private static final long serialVersionUID = 1L;
31 
32 	private final String fileName;
33 
TextFileServlet(String name)34 	TextFileServlet(String name) {
35 		this.fileName = name;
36 	}
37 
38 	/** {@inheritDoc} */
39 	@Override
doGet(final HttpServletRequest req, final HttpServletResponse rsp)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 
read(HttpServletRequest req)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 }
57