xref: /JGit/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/Protocol.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1 /*
2  * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.com>
3  * Copyright (C) 2015, Sasa Zivkov <sasa.zivkov@sap.com> and others
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Distribution License v. 1.0 which is available at
7  * https://www.eclipse.org/org/documents/edl-v10.php.
8  *
9  * SPDX-License-Identifier: BSD-3-Clause
10  */
11 package org.eclipse.jgit.lfs;
12 
13 import java.util.List;
14 import java.util.Map;
15 
16 import com.google.gson.FieldNamingPolicy;
17 import com.google.gson.Gson;
18 import com.google.gson.GsonBuilder;
19 
20 /**
21  * This interface describes the network protocol used between lfs client and lfs
22  * server
23  *
24  * @since 4.11
25  */
26 public interface Protocol {
27 	/** A request sent to an LFS server */
28 	class Request {
29 		/** The operation of this request */
30 		public String operation;
31 
32 		/** The objects of this request */
33 		public List<ObjectSpec> objects;
34 	}
35 
36 	/** A response received from an LFS server */
37 	class Response {
38 		public List<ObjectInfo> objects;
39 	}
40 
41 	/**
42 	 * MetaData of an LFS object. Needs to be specified when requesting objects
43 	 * from the LFS server and is also returned in the response
44 	 */
45 	class ObjectSpec {
46 		public String oid; // the objectid
47 
48 		public long size; // the size of the object
49 	}
50 
51 	/**
52 	 * Describes in a response all actions the LFS server offers for a single
53 	 * object
54 	 */
55 	class ObjectInfo extends ObjectSpec {
56 		public Map<String, Action> actions; // Maps operation to action
57 
58 		public Error error;
59 	}
60 
61 	/**
62 	 * Describes in a Response a single action the client can execute on a
63 	 * single object
64 	 */
65 	class Action {
66 		public String href;
67 
68 		public Map<String, String> header;
69 	}
70 
71 	/**
72 	 * An action with an additional expiration timestamp
73 	 *
74 	 * @since 4.11
75 	 */
76 	class ExpiringAction extends Action {
77 		/**
78 		 * Absolute date/time in format "yyyy-MM-dd'T'HH:mm:ss.SSSX"
79 		 */
80 		public String expiresAt;
81 
82 		/**
83 		 * Validity time in milliseconds (preferred over expiresAt as specified:
84 		 * https://github.com/git-lfs/git-lfs/blob/master/docs/api/authentication.md)
85 		 */
86 		public String expiresIn;
87 	}
88 
89 	/** Describes an error to be returned by the LFS batch API */
90 	class Error {
91 		public int code;
92 
93 		public String message;
94 	}
95 
96 	/**
97 	 * The "download" operation
98 	 */
99 	String OPERATION_DOWNLOAD = "download"; //$NON-NLS-1$
100 
101 	/**
102 	 * The "upload" operation
103 	 */
104 	String OPERATION_UPLOAD = "upload"; //$NON-NLS-1$
105 
106 	/**
107 	 * The contenttype used in LFS requests
108 	 */
109 	String CONTENTTYPE_VND_GIT_LFS_JSON = "application/vnd.git-lfs+json; charset=utf-8"; //$NON-NLS-1$
110 
111 	/**
112 	 * Authorization header when auto-discovering via SSH.
113 	 */
114 	String HDR_AUTH = "Authorization"; //$NON-NLS-1$
115 
116 	/**
117 	 * Prefix of authentication token obtained through SSH.
118 	 */
119 	String HDR_AUTH_SSH_PREFIX = "Ssh: "; //$NON-NLS-1$
120 
121 	/**
122 	 * Path to the LFS info servlet.
123 	 */
124 	String INFO_LFS_ENDPOINT = "/info/lfs"; //$NON-NLS-1$
125 
126 	/**
127 	 * Path to the LFS objects servlet.
128 	 */
129 	String OBJECTS_LFS_ENDPOINT = "/objects/batch"; //$NON-NLS-1$
130 
131 	/**
132 	 * @return a {@link Gson} instance suitable for handling this
133 	 *         {@link Protocol}
134 	 *
135 	 * @since 4.11
136 	 */
gson()137 	public static Gson gson() {
138 		return new GsonBuilder()
139 				.setFieldNamingPolicy(
140 						FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
141 				.disableHtmlEscaping().create();
142 	}
143 }
144