View Javadoc
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 java.nio.charset.StandardCharsets.UTF_8;
14  import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
15  
16  import java.io.IOException;
17  import java.io.OutputStreamWriter;
18  
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.eclipse.jgit.lib.Constants;
24  import org.eclipse.jgit.lib.Repository;
25  import org.eclipse.jgit.transport.RefAdvertiser;
26  import org.eclipse.jgit.util.HttpSupport;
27  
28  /** Send a complete list of current refs, including peeled values for tags. */
29  class InfoRefsServlet extends HttpServlet {
30  	private static final long serialVersionUID = 1L;
31  
32  	/** {@inheritDoc} */
33  	@Override
34  	public void doGet(final HttpServletRequest req,
35  			final HttpServletResponse rsp) throws IOException {
36  		// Assume a dumb client and send back the dumb client
37  		// version of the info/refs file.
38  		rsp.setContentType(HttpSupport.TEXT_PLAIN);
39  		rsp.setCharacterEncoding(UTF_8.name());
40  
41  		final Repository db = getRepository(req);
42  		try (OutputStreamWriter out = new OutputStreamWriter(
43  				new SmartOutputStream(req, rsp, true),
44  				UTF_8)) {
45  			final RefAdvertiser adv = new RefAdvertiser() {
46  				@Override
47  				protected void writeOne(CharSequence line)
48  						throws IOException {
49  					// Whoever decided that info/refs should use a different
50  					// delimiter than the native git:// protocol shouldn't
51  					// be allowed to design this sort of stuff. :-(
52  					out.append(line.toString().replace(' ', '\t'));
53  				}
54  
55  				@Override
56  				protected void end() {
57  					// No end marker required for info/refs format.
58  				}
59  			};
60  			adv.init(db);
61  			adv.setDerefTags(true);
62  			adv.send(db.getRefDatabase().getRefsByPrefix(Constants.R_REFS));
63  		}
64  	}
65  }