xref: /JGit/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/TextFileServlet.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
15e33a1deSShawn O. Pearce /*
2*5c5f7c6bSMatthias Sohn  * Copyright (C) 2009-2010, Google Inc. and others
35e33a1deSShawn O. Pearce  *
4*5c5f7c6bSMatthias Sohn  * This program and the accompanying materials are made available under the
5*5c5f7c6bSMatthias Sohn  * terms of the Eclipse Distribution License v. 1.0 which is available at
6*5c5f7c6bSMatthias Sohn  * https://www.eclipse.org/org/documents/edl-v10.php.
75e33a1deSShawn O. Pearce  *
8*5c5f7c6bSMatthias Sohn  * SPDX-License-Identifier: BSD-3-Clause
95e33a1deSShawn O. Pearce  */
105e33a1deSShawn O. Pearce 
115e33a1deSShawn O. Pearce package org.eclipse.jgit.http.server;
125e33a1deSShawn O. Pearce 
135e33a1deSShawn O. Pearce import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
145e33a1deSShawn O. Pearce import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
155e33a1deSShawn O. Pearce import static org.eclipse.jgit.http.server.ServletUtils.send;
165e33a1deSShawn O. Pearce 
175e33a1deSShawn O. Pearce import java.io.File;
185e33a1deSShawn O. Pearce import java.io.FileNotFoundException;
195e33a1deSShawn O. Pearce import java.io.IOException;
205e33a1deSShawn O. Pearce 
215e33a1deSShawn O. Pearce import javax.servlet.http.HttpServlet;
225e33a1deSShawn O. Pearce import javax.servlet.http.HttpServletRequest;
235e33a1deSShawn O. Pearce import javax.servlet.http.HttpServletResponse;
245e33a1deSShawn O. Pearce 
255e33a1deSShawn O. Pearce import org.eclipse.jgit.util.HttpSupport;
265e33a1deSShawn O. Pearce import org.eclipse.jgit.util.IO;
275e33a1deSShawn O. Pearce 
285e33a1deSShawn O. Pearce /** Sends a small text meta file from the repository. */
295e33a1deSShawn O. Pearce class TextFileServlet extends HttpServlet {
305e33a1deSShawn O. Pearce 	private static final long serialVersionUID = 1L;
315e33a1deSShawn O. Pearce 
325e33a1deSShawn O. Pearce 	private final String fileName;
335e33a1deSShawn O. Pearce 
TextFileServlet(String name)34f3ec7cf3SHan-Wen Nienhuys 	TextFileServlet(String name) {
355e33a1deSShawn O. Pearce 		this.fileName = name;
365e33a1deSShawn O. Pearce 	}
375e33a1deSShawn O. Pearce 
383b00041cSMatthias Sohn 	/** {@inheritDoc} */
397ac182f4SDavid Pursehouse 	@Override
doGet(final HttpServletRequest req, final HttpServletResponse rsp)405e33a1deSShawn O. Pearce 	public void doGet(final HttpServletRequest req,
415e33a1deSShawn O. Pearce 			final HttpServletResponse rsp) throws IOException {
425e33a1deSShawn O. Pearce 		try {
435e33a1deSShawn O. Pearce 			rsp.setContentType(HttpSupport.TEXT_PLAIN);
445e33a1deSShawn O. Pearce 			send(read(req), req, rsp);
455e33a1deSShawn O. Pearce 		} catch (FileNotFoundException noFile) {
465e33a1deSShawn O. Pearce 			rsp.sendError(SC_NOT_FOUND);
475e33a1deSShawn O. Pearce 		}
485e33a1deSShawn O. Pearce 	}
495e33a1deSShawn O. Pearce 
read(HttpServletRequest req)506d370d83SHan-Wen Nienhuys 	private byte[] read(HttpServletRequest req) throws IOException {
515e33a1deSShawn O. Pearce 		final File gitdir = getRepository(req).getDirectory();
52ffe0614dSShawn O. Pearce 		if (gitdir == null)
53ffe0614dSShawn O. Pearce 			throw new FileNotFoundException(fileName);
545e33a1deSShawn O. Pearce 		return IO.readFully(new File(gitdir, fileName));
555e33a1deSShawn O. Pearce 	}
565e33a1deSShawn O. Pearce }
57