View Javadoc
1   /*
2    * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com>
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  package org.eclipse.jgit.lfs.server.fs;
44  
45  import java.io.FileNotFoundException;
46  import java.io.IOException;
47  import java.nio.ByteBuffer;
48  import java.nio.channels.Channels;
49  import java.nio.channels.ReadableByteChannel;
50  import java.nio.channels.WritableByteChannel;
51  import java.util.logging.Level;
52  import java.util.logging.Logger;
53  
54  import javax.servlet.AsyncContext;
55  import javax.servlet.ReadListener;
56  import javax.servlet.ServletInputStream;
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.http.HttpServletResponse;
59  
60  import org.apache.http.HttpStatus;
61  import org.eclipse.jgit.lfs.errors.CorruptLongObjectException;
62  import org.eclipse.jgit.lfs.internal.AtomicObjectOutputStream;
63  import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
64  import org.eclipse.jgit.lfs.lib.Constants;
65  
66  /**
67   * Handle asynchronous object upload.
68   *
69   * @since 4.6
70   */
71  public class ObjectUploadListener implements ReadListener {
72  
73  	private static Logger LOG = Logger
74  			.getLogger(ObjectUploadListener.class.getName());
75  
76  	private final AsyncContext context;
77  
78  	private final HttpServletResponse response;
79  
80  	private final ServletInputStream in;
81  
82  	private final ReadableByteChannel inChannel;
83  
84  	private final AtomicObjectOutputStream out;
85  
86  	private WritableByteChannel channel;
87  
88  	private final ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
89  
90  	/**
91  	 * @param repository
92  	 *            the repository storing large objects
93  	 * @param context
94  	 * @param request
95  	 * @param response
96  	 * @param id
97  	 * @throws FileNotFoundException
98  	 * @throws IOException
99  	 */
100 	public ObjectUploadListener(FileLfsRepository repository,
101 			AsyncContext context, HttpServletRequest request,
102 			HttpServletResponse response, AnyLongObjectId id)
103 					throws FileNotFoundException, IOException {
104 		this.context = context;
105 		this.response = response;
106 		this.in = request.getInputStream();
107 		this.inChannel = Channels.newChannel(in);
108 		this.out = repository.getOutputStream(id);
109 		this.channel = Channels.newChannel(out);
110 		response.setContentType(Constants.CONTENT_TYPE_GIT_LFS_JSON);
111 	}
112 
113 	/**
114 	 * Writes all the received data to the output channel
115 	 *
116 	 * @throws IOException
117 	 */
118 	@Override
119 	public void onDataAvailable() throws IOException {
120 		while (in.isReady()) {
121 			if (inChannel.read(buffer) > 0) {
122 				buffer.flip();
123 				channel.write(buffer);
124 				buffer.compact();
125 			} else {
126 				buffer.flip();
127 				while (buffer.hasRemaining()) {
128 					channel.write(buffer);
129 				}
130 				close();
131 				return;
132 			}
133 		}
134 	}
135 
136 	/**
137 	 * @throws IOException
138 	 */
139 	@Override
140 	public void onAllDataRead() throws IOException {
141 		close();
142 	}
143 
144 	/**
145 	 * @throws IOException
146 	 */
147 	protected void close() throws IOException {
148 		try {
149 			inChannel.close();
150 			channel.close();
151 			// TODO check if status 200 is ok for PUT request, HTTP foresees 204
152 			// for successful PUT without response body
153 			response.setStatus(HttpServletResponse.SC_OK);
154 		} finally {
155 			context.complete();
156 		}
157 	}
158 
159 	/**
160 	 * @param e
161 	 *            the exception that caused the problem
162 	 */
163 	@Override
164 	public void onError(Throwable e) {
165 		try {
166 			out.abort();
167 			inChannel.close();
168 			channel.close();
169 			int status;
170 			if (e instanceof CorruptLongObjectException) {
171 				status = HttpStatus.SC_BAD_REQUEST;
172 				LOG.log(Level.WARNING, e.getMessage(), e);
173 			} else {
174 				status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
175 				LOG.log(Level.SEVERE, e.getMessage(), e);
176 			}
177 			FileLfsServlet.sendError(response, status, e.getMessage());
178 		} catch (IOException ex) {
179 			LOG.log(Level.SEVERE, ex.getMessage(), ex);
180 		}
181 	}
182 }