View Javadoc
1   /*
2    * Copyright (C) 2015, Sasa Zivkov <sasa.zivkov@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  
44  package org.eclipse.jgit.lfs.server;
45  
46  import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
47  import static org.eclipse.jgit.lfs.lib.Constants.DOWNLOAD;
48  import static org.eclipse.jgit.lfs.lib.Constants.UPLOAD;
49  import static org.eclipse.jgit.lfs.lib.Constants.VERIFY;
50  
51  import java.io.IOException;
52  import java.text.MessageFormat;
53  import java.util.ArrayList;
54  import java.util.HashMap;
55  import java.util.List;
56  
57  import org.eclipse.jgit.lfs.lib.LongObjectId;
58  import org.eclipse.jgit.lfs.server.Response.Action;
59  import org.eclipse.jgit.lfs.server.Response.Body;
60  import org.eclipse.jgit.lfs.server.internal.LfsServerText;
61  
62  abstract class TransferHandler {
63  
64  	static TransferHandler forOperation(String operation,
65  			LargeFileRepository repository, List<LfsObject> objects) {
66  		switch (operation) {
67  		case UPLOAD:
68  			return new Upload(repository, objects);
69  		case DOWNLOAD:
70  			return new Download(repository, objects);
71  		case VERIFY:
72  		default:
73  			throw new UnsupportedOperationException(MessageFormat.format(
74  					LfsServerText.get().unsupportedOperation, operation));
75  		}
76  	}
77  
78  	private static class Upload extends TransferHandler {
79  		Upload(LargeFileRepository repository,
80  				List<LfsObject> objects) {
81  			super(repository, objects);
82  		}
83  
84  		@Override
85  		Body process() throws IOException {
86  			Response.Body body = new Response.Body();
87  			if (!objects.isEmpty()) {
88  				body.objects = new ArrayList<>();
89  				for (LfsObject o : objects) {
90  					addObjectInfo(body, o);
91  				}
92  			}
93  			return body;
94  		}
95  
96  		private void addObjectInfo(Response.Body body, LfsObject o)
97  				throws IOException {
98  			Response.ObjectInfo info = new Response.ObjectInfo();
99  			body.objects.add(info);
100 			info.oid = o.oid;
101 			info.size = o.size;
102 
103 			LongObjectId oid = LongObjectId.fromString(o.oid);
104 			if (repository.getSize(oid) == -1) {
105 				info.actions = new HashMap<>();
106 				info.actions.put(UPLOAD,
107 						repository.getUploadAction(oid, o.size));
108 				Action verify = repository.getVerifyAction(oid);
109 				if (verify != null) {
110 					info.actions.put(VERIFY, verify);
111 				}
112 			}
113 		}
114 	}
115 
116 	private static class Download extends TransferHandler {
117 		Download(LargeFileRepository repository,
118 				List<LfsObject> objects) {
119 			super(repository, objects);
120 		}
121 
122 		@Override
123 		Body process() throws IOException {
124 			Response.Body body = new Response.Body();
125 			if (!objects.isEmpty()) {
126 				body.objects = new ArrayList<>();
127 				for (LfsObject o : objects) {
128 					addObjectInfo(body, o);
129 				}
130 			}
131 			return body;
132 		}
133 
134 		private void addObjectInfo(Response.Body body, LfsObject o)
135 				throws IOException {
136 			Response.ObjectInfo info = new Response.ObjectInfo();
137 			body.objects.add(info);
138 			info.oid = o.oid;
139 			info.size = o.size;
140 
141 			LongObjectId oid = LongObjectId.fromString(o.oid);
142 			if (repository.getSize(oid) >= 0) {
143 				info.actions = new HashMap<>();
144 				info.actions.put(DOWNLOAD,
145 						repository.getDownloadAction(oid));
146 			} else {
147 				info.error = new Response.Error();
148 				info.error.code = SC_NOT_FOUND;
149 				info.error.message = MessageFormat.format(
150 						LfsServerText.get().objectNotFound,
151 						oid.getName());
152 			}
153 		}
154 	}
155 
156 	final LargeFileRepository repository;
157 
158 	final List<LfsObject> objects;
159 
160 	TransferHandler(LargeFileRepository repository,
161 			List<LfsObject> objects) {
162 		this.repository = repository;
163 		this.objects = objects;
164 	}
165 
166 	abstract Response.Body process() throws IOException;
167 }