You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextFileServlet.java 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. package org.eclipse.jgit.http.server;
  11. import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
  12. import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
  13. import static org.eclipse.jgit.http.server.ServletUtils.send;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import org.eclipse.jgit.util.HttpSupport;
  21. import org.eclipse.jgit.util.IO;
  22. /** Sends a small text meta file from the repository. */
  23. class TextFileServlet extends HttpServlet {
  24. private static final long serialVersionUID = 1L;
  25. private final String fileName;
  26. TextFileServlet(String name) {
  27. this.fileName = name;
  28. }
  29. /** {@inheritDoc} */
  30. @Override
  31. public void doGet(final HttpServletRequest req,
  32. final HttpServletResponse rsp) throws IOException {
  33. try {
  34. rsp.setContentType(HttpSupport.TEXT_PLAIN);
  35. send(read(req), req, rsp);
  36. } catch (FileNotFoundException noFile) {
  37. rsp.sendError(SC_NOT_FOUND);
  38. }
  39. }
  40. private byte[] read(HttpServletRequest req) throws IOException {
  41. final File gitdir = getRepository(req).getDirectory();
  42. if (gitdir == null)
  43. throw new FileNotFoundException(fileName);
  44. return IO.readFully(new File(gitdir, fileName));
  45. }
  46. }