View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.http.server;
45  
46  import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
47  import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
48  import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
49  import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
50  import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError;
51  import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_REPOSITORY;
52  
53  import java.io.IOException;
54  import java.text.MessageFormat;
55  
56  import javax.servlet.Filter;
57  import javax.servlet.FilterChain;
58  import javax.servlet.FilterConfig;
59  import javax.servlet.ServletContext;
60  import javax.servlet.ServletException;
61  import javax.servlet.ServletRequest;
62  import javax.servlet.ServletResponse;
63  import javax.servlet.http.HttpServletRequest;
64  import javax.servlet.http.HttpServletResponse;
65  
66  import org.eclipse.jgit.errors.RepositoryNotFoundException;
67  import org.eclipse.jgit.lib.Repository;
68  import org.eclipse.jgit.transport.ServiceMayNotContinueException;
69  import org.eclipse.jgit.transport.resolver.RepositoryResolver;
70  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
71  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
72  
73  /**
74   * Open a repository named by the path info through
75   * {@link org.eclipse.jgit.transport.resolver.RepositoryResolver}.
76   * <p>
77   * This filter assumes it is invoked by
78   * {@link org.eclipse.jgit.http.server.GitServlet} and is likely to not work as
79   * expected if called from any other class. This filter assumes the path info of
80   * the current request is a repository name which can be used by the configured
81   * {@link org.eclipse.jgit.transport.resolver.RepositoryResolver} to open a
82   * {@link org.eclipse.jgit.lib.Repository} and attach it to the current request.
83   * <p>
84   * This filter sets request attribute
85   * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_REPOSITORY} when
86   * it discovers the repository, and automatically closes and removes the
87   * attribute when the request is complete.
88   */
89  public class RepositoryFilter implements Filter {
90  	private final RepositoryResolver<HttpServletRequest> resolver;
91  
92  	private ServletContext context;
93  
94  	/**
95  	 * Create a new filter.
96  	 *
97  	 * @param resolver
98  	 *            the resolver which will be used to translate the URL name
99  	 *            component to the actual
100 	 *            {@link org.eclipse.jgit.lib.Repository} instance for the
101 	 *            current web request.
102 	 */
103 	public RepositoryFilter(RepositoryResolver<HttpServletRequest> resolver) {
104 		this.resolver = resolver;
105 	}
106 
107 	/** {@inheritDoc} */
108 	@Override
109 	public void init(FilterConfig config) throws ServletException {
110 		context = config.getServletContext();
111 	}
112 
113 	/** {@inheritDoc} */
114 	@Override
115 	public void destroy() {
116 		context = null;
117 	}
118 
119 	/** {@inheritDoc} */
120 	@Override
121 	public void doFilter(final ServletRequest request,
122 			final ServletResponse response, final FilterChain chain)
123 			throws IOException, ServletException {
124 		HttpServletRequest req = (HttpServletRequest) request;
125 		HttpServletResponse res = (HttpServletResponse) response;
126 
127 		if (request.getAttribute(ATTRIBUTE_REPOSITORY) != null) {
128 			context.log(MessageFormat.format(HttpServerText.get().internalServerErrorRequestAttributeWasAlreadySet
129 					, ATTRIBUTE_REPOSITORY
130 					, getClass().getName()));
131 			sendError(req, res, SC_INTERNAL_SERVER_ERROR);
132 			return;
133 		}
134 
135 		String name = req.getPathInfo();
136 		while (name != null && 0 < name.length() && name.charAt(0) == '/')
137 			name = name.substring(1);
138 		if (name == null || name.length() == 0) {
139 			sendError(req, res, SC_NOT_FOUND);
140 			return;
141 		}
142 
143 		try (Repository db = resolver.open(req, name)) {
144 			request.setAttribute(ATTRIBUTE_REPOSITORY, db);
145 			chain.doFilter(request, response);
146 		} catch (RepositoryNotFoundException e) {
147 			sendError(req, res, SC_NOT_FOUND);
148 			return;
149 		} catch (ServiceNotEnabledException e) {
150 			sendError(req, res, SC_FORBIDDEN, e.getMessage());
151 			return;
152 		} catch (ServiceNotAuthorizedException e) {
153 			res.sendError(SC_UNAUTHORIZED, e.getMessage());
154 			return;
155 		} catch (ServiceMayNotContinueException e) {
156 			sendError(req, res, e.getStatusCode(), e.getMessage());
157 			return;
158 		} finally {
159 			request.removeAttribute(ATTRIBUTE_REPOSITORY);
160 		}
161 	}
162 }