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