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_UNAUTHORIZED;
48  import static org.eclipse.jgit.http.server.GitSmartHttpTools.infoRefsResultType;
49  import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError;
50  import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_HANDLER;
51  import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
52  
53  import java.io.IOException;
54  import java.util.List;
55  
56  import javax.servlet.Filter;
57  import javax.servlet.FilterChain;
58  import javax.servlet.FilterConfig;
59  import javax.servlet.ServletException;
60  import javax.servlet.ServletRequest;
61  import javax.servlet.ServletResponse;
62  import javax.servlet.http.HttpServletRequest;
63  import javax.servlet.http.HttpServletResponse;
64  
65  import org.eclipse.jgit.lib.Repository;
66  import org.eclipse.jgit.transport.PacketLineOut;
67  import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
68  import org.eclipse.jgit.transport.ServiceMayNotContinueException;
69  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
70  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
71  
72  /** Filter in front of {@link InfoRefsServlet} to catch smart service requests. */
73  abstract class SmartServiceInfoRefs implements Filter {
74  	private final String svc;
75  
76  	private final Filter[] filters;
77  
78  	SmartServiceInfoRefs(String service, List<Filter> filters) {
79  		this.svc = service;
80  		this.filters = filters.toArray(new Filter[filters.size()]);
81  	}
82  
83  	/** {@inheritDoc} */
84  	@Override
85  	public void init(FilterConfig config) throws ServletException {
86  		// Do nothing.
87  	}
88  
89  	/** {@inheritDoc} */
90  	@Override
91  	public void destroy() {
92  		// Do nothing.
93  	}
94  
95  	/** {@inheritDoc} */
96  	@Override
97  	public void doFilter(ServletRequest request, ServletResponse response,
98  			FilterChain chain) throws IOException, ServletException {
99  		final HttpServletRequest req = (HttpServletRequest) request;
100 		final HttpServletResponse res = (HttpServletResponse) response;
101 
102 		if (svc.equals(req.getParameter("service"))) {
103 			final Repository db = getRepository(req);
104 			try {
105 				begin(req, db);
106 			} catch (ServiceNotAuthorizedException e) {
107 				res.sendError(SC_UNAUTHORIZED, e.getMessage());
108 				return;
109 			} catch (ServiceNotEnabledException e) {
110 				sendError(req, res, SC_FORBIDDEN, e.getMessage());
111 				return;
112 			}
113 
114 			try {
115 				if (filters.length == 0)
116 					service(req, response);
117 				else
118 					new Chain().doFilter(request, response);
119 			} finally {
120 				req.removeAttribute(ATTRIBUTE_HANDLER);
121 			}
122 		} else {
123 			chain.doFilter(request, response);
124 		}
125 	}
126 
127 	private void service(ServletRequest request, ServletResponse response)
128 			throws IOException {
129 		final HttpServletRequest req = (HttpServletRequest) request;
130 		final HttpServletResponse res = (HttpServletResponse) response;
131 		final SmartOutputStream buf = new SmartOutputStream(req, res, true);
132 		try {
133 			res.setContentType(infoRefsResultType(svc));
134 
135 			final PacketLineOut out = new PacketLineOut(buf);
136 			respond(req, out, svc);
137 			buf.close();
138 		} catch (ServiceNotAuthorizedException e) {
139 			res.sendError(SC_UNAUTHORIZED, e.getMessage());
140 		} catch (ServiceNotEnabledException e) {
141 			sendError(req, res, SC_FORBIDDEN, e.getMessage());
142 		} catch (ServiceMayNotContinueException e) {
143 			if (e.isOutput())
144 				buf.close();
145 			else
146 				sendError(req, res, e.getStatusCode(), e.getMessage());
147 		}
148 	}
149 
150 	/**
151 	 * Begin service.
152 	 *
153 	 * @param req
154 	 *            request
155 	 * @param db
156 	 *            repository
157 	 * @throws IOException
158 	 * @throws ServiceNotEnabledException
159 	 * @throws ServiceNotAuthorizedException
160 	 */
161 	protected abstract void begin(HttpServletRequest req, Repository db)
162 			throws IOException, ServiceNotEnabledException,
163 			ServiceNotAuthorizedException;
164 
165 	/**
166 	 * Advertise.
167 	 *
168 	 * @param req
169 	 *            request
170 	 * @param pck
171 	 * @throws IOException
172 	 * @throws ServiceNotEnabledException
173 	 * @throws ServiceNotAuthorizedException
174 	 */
175 	protected abstract void advertise(HttpServletRequest req,
176 			PacketLineOutRefAdvertiser pck) throws IOException,
177 			ServiceNotEnabledException, ServiceNotAuthorizedException;
178 
179 	/**
180 	 * Writes the appropriate response to an info/refs request received by
181 	 * a smart service. In protocol v0, this starts with "#
182 	 * service=serviceName" followed by a flush packet, but this is not
183 	 * necessarily the case in other protocol versions.
184 	 * <p>
185 	 * The default implementation writes "# service=serviceName" and a
186 	 * flush packet, then calls {@link #advertise}. Subclasses should
187 	 * override this method if they support protocol versions other than
188 	 * protocol v0.
189 	 *
190 	 * @param req
191 	 *            request
192 	 * @param pckOut
193 	 *            destination of response
194 	 * @param serviceName
195 	 *            service name to be written out in protocol v0; may or may
196 	 *            not be used in other versions
197 	 * @throws IOException
198 	 * @throws ServiceNotEnabledException
199 	 * @throws ServiceNotAuthorizedException
200 	 */
201 	protected void respond(HttpServletRequest req,
202 			PacketLineOut pckOut, String serviceName)
203 			throws IOException, ServiceNotEnabledException,
204 			ServiceNotAuthorizedException {
205 		pckOut.writeString("# service=" + svc + '\n'); //$NON-NLS-1$
206 		pckOut.end();
207 		advertise(req, new PacketLineOutRefAdvertiser(pckOut));
208 	}
209 
210 	private class Chain implements FilterChain {
211 		private int filterIdx;
212 
213 		@Override
214 		public void doFilter(ServletRequest req, ServletResponse rsp)
215 				throws IOException, ServletException {
216 			if (filterIdx < filters.length)
217 				filters[filterIdx++].doFilter(req, rsp, this);
218 			else
219 				service(req, rsp);
220 		}
221 	}
222 }