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_UNAUTHORIZED;
49  import static javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE;
50  import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK;
51  import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK_REQUEST_TYPE;
52  import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK_RESULT_TYPE;
53  import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError;
54  import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_HANDLER;
55  import static org.eclipse.jgit.http.server.ServletUtils.consumeRequestBody;
56  import static org.eclipse.jgit.http.server.ServletUtils.getInputStream;
57  import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
58  import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
59  
60  import java.io.IOException;
61  import java.text.MessageFormat;
62  import java.util.List;
63  
64  import javax.servlet.Filter;
65  import javax.servlet.FilterChain;
66  import javax.servlet.FilterConfig;
67  import javax.servlet.ServletException;
68  import javax.servlet.ServletRequest;
69  import javax.servlet.ServletResponse;
70  import javax.servlet.http.HttpServlet;
71  import javax.servlet.http.HttpServletRequest;
72  import javax.servlet.http.HttpServletResponse;
73  
74  import org.eclipse.jgit.lib.Repository;
75  import org.eclipse.jgit.transport.InternalHttpServerGlue;
76  import org.eclipse.jgit.transport.PacketLineOut;
77  import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
78  import org.eclipse.jgit.transport.ServiceMayNotContinueException;
79  import org.eclipse.jgit.transport.UploadPack;
80  import org.eclipse.jgit.transport.UploadPackInternalServerErrorException;
81  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
82  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
83  import org.eclipse.jgit.transport.resolver.UploadPackFactory;
84  
85  /** Server side implementation of smart fetch over HTTP. */
86  class UploadPackServlet extends HttpServlet {
87  	private static final long serialVersionUID = 1L;
88  
89  	static class InfoRefs extends SmartServiceInfoRefs {
90  		private final UploadPackFactory<HttpServletRequest> uploadPackFactory;
91  
92  		InfoRefs(UploadPackFactory<HttpServletRequest> uploadPackFactory,
93  				List<Filter> filters) {
94  			super(UPLOAD_PACK, filters);
95  			this.uploadPackFactory = uploadPackFactory;
96  		}
97  
98  		@Override
99  		protected void begin(HttpServletRequest req, Repository db)
100 				throws IOException, ServiceNotEnabledException,
101 				ServiceNotAuthorizedException {
102 			UploadPack up = uploadPackFactory.create(req, db);
103 			InternalHttpServerGlue.setPeerUserAgent(
104 					up,
105 					req.getHeader(HDR_USER_AGENT));
106 			req.setAttribute(ATTRIBUTE_HANDLER, up);
107 		}
108 
109 		@Override
110 		protected void advertise(HttpServletRequest req,
111 				PacketLineOutRefAdvertiser pck) throws IOException,
112 				ServiceNotEnabledException, ServiceNotAuthorizedException {
113 			UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
114 			try {
115 				up.setBiDirectionalPipe(false);
116 				up.sendAdvertisedRefs(pck);
117 			} finally {
118 				// TODO(jonathantanmy): Move responsibility for closing the
119 				// RevWalk to UploadPack, either by making it AutoCloseable
120 				// or by making sendAdvertisedRefs clean up after itself.
121 				up.getRevWalk().close();
122 			}
123 		}
124 
125 		@Override
126 		protected void respond(HttpServletRequest req,
127 				PacketLineOut pckOut, String serviceName) throws IOException,
128 				ServiceNotEnabledException, ServiceNotAuthorizedException {
129 			UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
130 			try {
131 				up.setBiDirectionalPipe(false);
132 				up.sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut), serviceName);
133 			} finally {
134 				// TODO(jonathantanmy): Move responsibility for closing the
135 				// RevWalk to UploadPack, either by making it AutoCloseable
136 				// or by making sendAdvertisedRefs clean up after itself.
137 				up.getRevWalk().close();
138 			}
139 		}
140 	}
141 
142 	static class Factory implements Filter {
143 		private final UploadPackFactory<HttpServletRequest> uploadPackFactory;
144 
145 		Factory(UploadPackFactory<HttpServletRequest> uploadPackFactory) {
146 			this.uploadPackFactory = uploadPackFactory;
147 		}
148 
149 		@Override
150 		public void doFilter(ServletRequest request, ServletResponse response,
151 				FilterChain chain) throws IOException, ServletException {
152 			HttpServletRequest req = (HttpServletRequest) request;
153 			HttpServletResponse rsp = (HttpServletResponse) response;
154 			UploadPack rp;
155 			try {
156 				rp = uploadPackFactory.create(req, getRepository(req));
157 			} catch (ServiceNotAuthorizedException e) {
158 				rsp.sendError(SC_UNAUTHORIZED, e.getMessage());
159 				return;
160 			} catch (ServiceNotEnabledException e) {
161 				sendError(req, rsp, SC_FORBIDDEN, e.getMessage());
162 				return;
163 			}
164 
165 			try {
166 				req.setAttribute(ATTRIBUTE_HANDLER, rp);
167 				chain.doFilter(req, rsp);
168 			} finally {
169 				req.removeAttribute(ATTRIBUTE_HANDLER);
170 			}
171 		}
172 
173 		@Override
174 		public void init(FilterConfig filterConfig) throws ServletException {
175 			// Nothing.
176 		}
177 
178 		@Override
179 		public void destroy() {
180 			// Nothing.
181 		}
182 	}
183 
184 	/** {@inheritDoc} */
185 	@Override
186 	public void doPost(final HttpServletRequest req,
187 			final HttpServletResponse rsp) throws IOException {
188 		if (!UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType())) {
189 			rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE);
190 			return;
191 		}
192 
193 		SmartOutputStream out = new SmartOutputStream(req, rsp, false) {
194 			@Override
195 			public void flush() throws IOException {
196 				doFlush();
197 			}
198 		};
199 
200 		UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
201 		try {
202 			up.setBiDirectionalPipe(false);
203 			rsp.setContentType(UPLOAD_PACK_RESULT_TYPE);
204 
205 			up.upload(getInputStream(req), out, null);
206 			out.close();
207 
208 		} catch (ServiceMayNotContinueException e) {
209 			if (e.isOutput()) {
210 				consumeRequestBody(req);
211 				out.close();
212 			} else if (!rsp.isCommitted()) {
213 				rsp.reset();
214 				sendError(req, rsp, e.getStatusCode(), e.getMessage());
215 			}
216 			return;
217 
218 		} catch (UploadPackInternalServerErrorException e) {
219 			// Special case exception, error message was sent to client.
220 			log(up.getRepository(), e.getCause());
221 			consumeRequestBody(req);
222 			out.close();
223 
224 		} catch (Throwable e) {
225 			log(up.getRepository(), e);
226 			if (!rsp.isCommitted()) {
227 				rsp.reset();
228 				sendError(req, rsp, SC_INTERNAL_SERVER_ERROR);
229 			}
230 			return;
231 		}
232 	}
233 
234 	private void log(Repository git, Throwable e) {
235 		getServletContext().log(MessageFormat.format(
236 				HttpServerText.get().internalErrorDuringUploadPack,
237 				ServletUtils.identify(git)), e);
238 	}
239 }