InfoRefsServlet.java

  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 java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.eclipse.jgit.http.server.ServletUtils.getRepository;

  13. import java.io.IOException;
  14. import java.io.OutputStreamWriter;

  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;

  18. import org.eclipse.jgit.lib.Constants;
  19. import org.eclipse.jgit.lib.Repository;
  20. import org.eclipse.jgit.transport.RefAdvertiser;
  21. import org.eclipse.jgit.util.HttpSupport;

  22. /** Send a complete list of current refs, including peeled values for tags. */
  23. class InfoRefsServlet extends HttpServlet {
  24.     private static final long serialVersionUID = 1L;

  25.     /** {@inheritDoc} */
  26.     @Override
  27.     public void doGet(final HttpServletRequest req,
  28.             final HttpServletResponse rsp) throws IOException {
  29.         // Assume a dumb client and send back the dumb client
  30.         // version of the info/refs file.
  31.         rsp.setContentType(HttpSupport.TEXT_PLAIN);
  32.         rsp.setCharacterEncoding(UTF_8.name());

  33.         final Repository db = getRepository(req);
  34.         try (OutputStreamWriter out = new OutputStreamWriter(
  35.                 new SmartOutputStream(req, rsp, true),
  36.                 UTF_8)) {
  37.             final RefAdvertiser adv = new RefAdvertiser() {
  38.                 @Override
  39.                 protected void writeOne(CharSequence line)
  40.                         throws IOException {
  41.                     // Whoever decided that info/refs should use a different
  42.                     // delimiter than the native git:// protocol shouldn't
  43.                     // be allowed to design this sort of stuff. :-(
  44.                     out.append(line.toString().replace(' ', '\t'));
  45.                 }

  46.                 @Override
  47.                 protected void end() {
  48.                     // No end marker required for info/refs format.
  49.                 }
  50.             };
  51.             adv.init(db);
  52.             adv.setDerefTags(true);
  53.             adv.send(db.getRefDatabase().getRefsByPrefix(Constants.R_REFS));
  54.         }
  55.     }
  56. }