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   * Opens a repository named by the path info through {@link RepositoryResolver}.
75   * <p>
76   * This filter assumes it is invoked by {@link GitServlet} and is likely to not
77   * work as expected if called from any other class. This filter assumes the path
78   * info of the current request is a repository name which can be used by the
79   * configured {@link RepositoryResolver} to open a {@link Repository} and attach
80   * it to the current request.
81   * <p>
82   * This filter sets request attribute {@link ServletUtils#ATTRIBUTE_REPOSITORY}
83   * when it discovers the repository, and automatically closes and removes the
84   * attribute when the request is complete.
85   */
86  public class RepositoryFilter implements Filter {
87  	private final RepositoryResolver<HttpServletRequest> resolver;
88  
89  	private ServletContext context;
90  
91  	/**
92  	 * Create a new filter.
93  	 *
94  	 * @param resolver
95  	 *            the resolver which will be used to translate the URL name
96  	 *            component to the actual {@link Repository} instance for the
97  	 *            current web request.
98  	 */
99  	public RepositoryFilter(final RepositoryResolver<HttpServletRequest> resolver) {
100 		this.resolver = resolver;
101 	}
102 
103 	public void init(final FilterConfig config) throws ServletException {
104 		context = config.getServletContext();
105 	}
106 
107 	public void destroy() {
108 		context = null;
109 	}
110 
111 	public void doFilter(final ServletRequest request,
112 			final ServletResponse response, final FilterChain chain)
113 			throws IOException, ServletException {
114 		HttpServletRequest req = (HttpServletRequest) request;
115 		HttpServletResponse res = (HttpServletResponse) response;
116 
117 		if (request.getAttribute(ATTRIBUTE_REPOSITORY) != null) {
118 			context.log(MessageFormat.format(HttpServerText.get().internalServerErrorRequestAttributeWasAlreadySet
119 					, ATTRIBUTE_REPOSITORY
120 					, getClass().getName()));
121 			sendError(req, res, SC_INTERNAL_SERVER_ERROR);
122 			return;
123 		}
124 
125 		String name = req.getPathInfo();
126 		while (name != null && 0 < name.length() && name.charAt(0) == '/')
127 			name = name.substring(1);
128 		if (name == null || name.length() == 0) {
129 			sendError(req, res, SC_NOT_FOUND);
130 			return;
131 		}
132 
133 		final Repository db;
134 		try {
135 			db = resolver.open(req, name);
136 		} catch (RepositoryNotFoundException e) {
137 			sendError(req, res, SC_NOT_FOUND);
138 			return;
139 		} catch (ServiceNotEnabledException e) {
140 			sendError(req, res, SC_FORBIDDEN);
141 			return;
142 		} catch (ServiceNotAuthorizedException e) {
143 			res.sendError(SC_UNAUTHORIZED);
144 			return;
145 		} catch (ServiceMayNotContinueException e) {
146 			sendError(req, res, SC_FORBIDDEN, e.getMessage());
147 			return;
148 		}
149 		try {
150 			request.setAttribute(ATTRIBUTE_REPOSITORY, db);
151 			chain.doFilter(request, response);
152 		} finally {
153 			request.removeAttribute(ATTRIBUTE_REPOSITORY);
154 			db.close();
155 		}
156 	}
157 }