View Javadoc
1   /*
2    * Copyright (c) 2019, Google LLC  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    * http://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  
12  import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
13  import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
14  import static javax.servlet.http.HttpServletResponse.SC_OK;
15  
16  import java.io.IOException;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.eclipse.jgit.errors.PackProtocolException;
22  import org.eclipse.jgit.transport.ServiceMayNotContinueException;
23  import org.eclipse.jgit.transport.UploadPack;
24  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
25  
26  /**
27   * Handle git-upload-pack errors.
28   *
29   * <p>
30   * This is an entry point for customizing an error handler for git-upload-pack.
31   * Right before calling {@link UploadPack#uploadWithExceptionPropagation}, JGit
32   * will call this handler if specified through {@link GitFilter}. The
33   * implementation of this handler is responsible for calling
34   * {@link UploadPackRunnable} and handling exceptions for clients.
35   *
36   * <p>
37   * If a custom handler is not specified, JGit will use the default error
38   * handler.
39   *
40   * @since 5.6
41   */
42  public interface UploadPackErrorHandler {
43  	/**
44  	 * Maps a thrown git related Exception to an appropriate HTTP status code.
45  	 *
46  	 * @param error
47  	 *            The thrown Exception.
48  	 * @return the HTTP status code as an int
49  	 * @since 6.1.1
50  	 */
51  	public static int statusCodeForThrowable(Throwable error) {
52  		if (error instanceof ServiceNotEnabledException) {
53  			return SC_FORBIDDEN;
54  		}
55  		if (error instanceof PackProtocolException) {
56  			// Internal git errors are not errors from an HTTP standpoint.
57  			return SC_OK;
58  		}
59  		return SC_INTERNAL_SERVER_ERROR;
60  	}
61  	/**
62  	 * @param req
63  	 *            The HTTP request
64  	 * @param rsp
65  	 *            The HTTP response
66  	 * @param r
67  	 *            A continuation that handles a git-upload-pack request.
68  	 * @throws IOException
69  	 */
70  	void upload(HttpServletRequest req, HttpServletResponse rsp,
71  			UploadPackRunnable r) throws IOException;
72  
73  	/** Process a git-upload-pack request. */
74  	public interface UploadPackRunnable {
75  		/**
76  		 * See {@link UploadPack#uploadWithExceptionPropagation}.
77  		 *
78  		 * @throws ServiceMayNotContinueException
79  		 * @throws IOException
80  		 */
81  		void upload() throws ServiceMayNotContinueException, IOException;
82  	}
83  }