View Javadoc
1   /*
2    * Copyright (C) 2018, Google LLC. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.transport;
11  
12  import static java.util.Objects.requireNonNull;
13  
14  import java.util.ArrayList;
15  import java.util.Collections;
16  import java.util.List;
17  
18  import org.eclipse.jgit.annotations.NonNull;
19  import org.eclipse.jgit.annotations.Nullable;
20  
21  /**
22   * ls-refs protocol v2 request.
23   *
24   * <p>
25   * This is used as an input to {@link ProtocolV2Hook}.
26   *
27   * @since 5.1
28   */
29  public final class LsRefsV2Request {
30  	private final List<String> refPrefixes;
31  
32  	private final boolean symrefs;
33  
34  	private final boolean peel;
35  
36  	@Nullable
37  	private final String agent;
38  
39  	private final String clientSID;
40  
41  	@NonNull
42  	private final List<String> serverOptions;
43  
44  	private LsRefsV2Request(List<String> refPrefixes, boolean symrefs,
45  			boolean peel, @Nullable String agent,
46  			@NonNull List<String> serverOptions,
47  			@Nullable String clientSID) {
48  		this.refPrefixes = refPrefixes;
49  		this.symrefs = symrefs;
50  		this.peel = peel;
51  		this.agent = agent;
52  		this.serverOptions = requireNonNull(serverOptions);
53  		this.clientSID = clientSID;
54  	}
55  
56  	/** @return ref prefixes that the client requested. */
57  	public List<String> getRefPrefixes() {
58  		return refPrefixes;
59  	}
60  
61  	/** @return true if the client requests symbolic references. */
62  	public boolean getSymrefs() {
63  		return symrefs;
64  	}
65  
66  	/** @return true if the client requests tags to be peeled. */
67  	public boolean getPeel() {
68  		return peel;
69  	}
70  
71  	/**
72  	 * @return agent as reported by the client
73  	 *
74  	 * @since 5.2
75  	 */
76  	@Nullable
77  	public String getAgent() {
78  		return agent;
79  	}
80  
81  	/**
82  	 * @return session-id as reported by the client
83  	 *
84  	 * @since 6.4
85  	 */
86  	@Nullable
87  	public String getClientSID() {
88  		return clientSID;
89  	}
90  
91  	/**
92  	 * Get application-specific options provided by the client using
93  	 * --server-option.
94  	 * <p>
95  	 * It returns just the content, without the "server-option=" prefix. E.g. a
96  	 * request with server-option=A and server-option=B lines returns the list
97  	 * [A, B].
98  	 *
99  	 * @return application-specific options from the client as an unmodifiable
100 	 *         list
101 	 *
102 	 * @since 5.2
103 	 */
104 	@NonNull
105 	public List<String> getServerOptions() {
106 		return serverOptions;
107 	}
108 
109 	/** @return A builder of {@link LsRefsV2Request}. */
110 	public static Builder builder() {
111 		return new Builder();
112 	}
113 
114 	/** A builder for {@link LsRefsV2Request}. */
115 	public static final class Builder {
116 		private List<String> refPrefixes = Collections.emptyList();
117 
118 		private boolean symrefs;
119 
120 		private boolean peel;
121 
122 		private final List<String> serverOptions = new ArrayList<>();
123 
124 		private String agent;
125 
126 		private String clientSID;
127 
128 		private Builder() {
129 		}
130 
131 		/**
132 		 * @param value
133 		 * @return the Builder
134 		 */
135 		public Builder setRefPrefixes(List<String> value) {
136 			refPrefixes = value;
137 			return this;
138 		}
139 
140 		/**
141 		 * @param value
142 		 * @return the Builder
143 		 */
144 		public Builder setSymrefs(boolean value) {
145 			symrefs = value;
146 			return this;
147 		}
148 
149 		/**
150 		 * @param value
151 		 * @return the Builder
152 		 */
153 		public Builder setPeel(boolean value) {
154 			peel = value;
155 			return this;
156 		}
157 
158 		/**
159 		 * Records an application-specific option supplied in a server-option
160 		 * line, for later retrieval with
161 		 * {@link LsRefsV2Request#getServerOptions}.
162 		 *
163 		 * @param value
164 		 *            the client-supplied server-option capability, without
165 		 *            leading "server-option=".
166 		 * @return this builder
167 		 * @since 5.2
168 		 */
169 		public Builder addServerOption(@NonNull String value) {
170 			serverOptions.add(value);
171 			return this;
172 		}
173 
174 		/**
175 		 * Value of an agent line received after the command and before the
176 		 * arguments. E.g. "agent=a.b.c/1.0" should set "a.b.c/1.0".
177 		 *
178 		 * @param value
179 		 *            the client-supplied agent capability, without leading
180 		 *            "agent="
181 		 * @return this builder
182 		 *
183 		 * @since 5.2
184 		 */
185 		public Builder setAgent(@Nullable String value) {
186 			agent = value;
187 			return this;
188 		}
189 
190 		/**
191 		 * Value of a session-id line received after the command and before the
192 		 * arguments. E.g. "session-id=a.b.c" should set "a.b.c".
193 		 *
194 		 * @param value
195 		 *            the client-supplied session-id capability, without leading
196 		 *            "session-id="
197 		 * @return this builder
198 		 *
199 		 * @since 6.4
200 		 */
201 		public Builder setClientSID(@Nullable String value) {
202 			clientSID = value;
203 			return this;
204 		}
205 
206 		/** @return LsRefsV2Request */
207 		public LsRefsV2Request build() {
208 			return new LsRefsV2Request(
209 					Collections.unmodifiableList(refPrefixes), symrefs, peel,
210 					agent, Collections.unmodifiableList(serverOptions),
211 					clientSID);
212 		}
213 	}
214 }