Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6ceaed08a01c2f17ec19a7d1bdd436513d8f8d62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.egit.github.core.client;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.BasicScheme;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
import org.eclipse.egit.github.core.Assert;
import org.eclipse.egit.github.core.RequestError;

/**
 * Client class for interacting with GitHub HTTP/JSON API.
 */
public class GitHubClient {

	private static final NameValuePair PER_PAGE_PARAM = new NameValuePair(
			IGitHubConstants.PARAM_PER_PAGE, Integer.toString(100));

	private static final AuthScope ANY_SCOPE = new AuthScope(
			AuthScope.ANY_HOST, AuthScope.ANY_PORT);

	private HostConfiguration hostConfig;

	private HttpClient client = new HttpClient();

	private Gson gson = new GsonBuilder()
			.registerTypeAdapter(Date.class, new DateFormatter())
			.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
			.serializeNulls().create();

	private boolean sendCredentials = false;

	/**
	 * Create default client
	 */
	public GitHubClient() {
		this.hostConfig = new HostConfiguration();
		this.hostConfig.setHost(IGitHubConstants.HOST_API, -1,
				Protocol.getProtocol(IGitHubConstants.PROTOCOL_HTTPS));
	}

	/**
	 * Create client for configuration
	 * 
	 * @param configuration
	 */
	public GitHubClient(HostConfiguration configuration) {
		Assert.notNull("Configuration cannot be null", configuration); //$NON-NLS-1$
		this.hostConfig = configuration;
	}

	/**
	 * Create standard post method
	 * 
	 * @param uri
	 * @return post
	 */
	protected PostMethod createPost(String uri) {
		PostMethod method = new PostMethod(uri);
		setMethodDefaults(method);
		return method;
	}

	/**
	 * Create standard post method
	 * 
	 * @param uri
	 * @return post
	 */
	protected PutMethod createPut(String uri) {
		PutMethod method = new PutMethod(uri);
		setMethodDefaults(method);
		return method;
	}

	/**
	 * Set method defaults
	 * 
	 * @param method
	 * @return method
	 */
	protected HttpMethod setMethodDefaults(HttpMethod method) {
		if (this.sendCredentials) {
			method.setDoAuthentication(true);
			method.getHostAuthState().setPreemptive();
			method.getHostAuthState().setAuthScheme(new BasicScheme());
		}
		return method;
	}

	/**
	 * Create get method
	 * 
	 * @param uri
	 * @return get method
	 */
	protected GetMethod createGet(String uri) {
		GetMethod method = new GetMethod(uri);
		method.setFollowRedirects(true);
		setMethodDefaults(method);
		return method;
	}

	/**
	 * Set credentials
	 * 
	 * @param user
	 * @param password
	 */
	public void setCredentials(String user, String password) {
		this.sendCredentials = user != null && password != null;
		Credentials credentials = null;
		if (this.sendCredentials)
			credentials = new UsernamePasswordCredentials(user, password);
		this.client.getState().setCredentials(ANY_SCOPE, credentials);
	}

	/**
	 * Parse json to specified type
	 * 
	 * @param <V>
	 * @param method
	 * @param type
	 * @return type
	 * @throws IOException
	 */
	protected <V> V parseJson(HttpMethodBase method, Type type)
			throws IOException {
		InputStream stream = method.getResponseBodyAsStream();
		if (stream == null)
			throw new JsonParseException("Empty body"); //$NON-NLS-1$
		InputStreamReader reader = new InputStreamReader(stream);
		try {
			return this.gson.fromJson(reader, type);
		} catch (JsonParseException jpe) {
			jpe.printStackTrace();
			throw new IOException(jpe);
		}
	}

	/**
	 * Get name value pairs for data map.
	 * 
	 * @param data
	 * @param page
	 * @return name value pair array
	 */
	protected NameValuePair[] getPairs(Map<String, String> data, int page) {
		if (data == null || data.isEmpty())
			return new NameValuePair[] {
					new NameValuePair(IGitHubConstants.PARAM_PAGE,
							Integer.toString(page)), PER_PAGE_PARAM };

		int size = data.containsKey(IGitHubConstants.PARAM_PER_PAGE) ? data
				.size() : data.size() + 1;
		NameValuePair[] pairs = new NameValuePair[size];
		int i = 0;
		for (Entry<String, String> entry : data.entrySet())
			pairs[i++] = new NameValuePair(entry.getKey(), entry.getValue());
		if (i < size)
			pairs[i] = PER_PAGE_PARAM;
		return pairs;
	}

	/**
	 * Get response stream from uri. It is the responsibility of the calling
	 * method to close the returned stream.
	 * 
	 * @param request
	 * @return stream
	 * @throws IOException
	 */
	public InputStream getStream(GitHubRequest request) throws IOException {
		GetMethod method = createGet(request.getUri());
		if (method.getQueryString() == null)
			method.setQueryString(getPairs(request.getParams(),
					request.getPage()));
		try {
			int status = this.client.executeMethod(this.hostConfig, method);
			switch (status) {
			case 200:
				return method.getResponseBodyAsStream();
			case 400:
			case 401:
			case 403:
			case 404:
			case 422:
			case 500:
				RequestError error = parseJson(method, RequestError.class);
				throw new RequestException(error, status);
			default:
				throw new IOException(method.getStatusText());
			}
		} catch (JsonParseException jpe) {
			throw new IOException(jpe.getMessage());
		}
	}

	/**
	 * Get response from uri and bind to specified type
	 * 
	 * @param request
	 * @return response
	 * @throws IOException
	 */
	public GitHubResponse get(GitHubRequest request) throws IOException {
		GetMethod method = createGet(request.getUri());
		if (method.getQueryString() == null)
			method.setQueryString(getPairs(request.getParams(),
					request.getPage()));
		try {
			int status = this.client.executeMethod(this.hostConfig, method);
			switch (status) {
			case 200:
				return new GitHubResponse(method, parseJson(method,
						request.getType()));
			case 400:
			case 401:
			case 403:
			case 404:
			case 422:
			case 500:
				RequestError error = parseJson(method, RequestError.class);
				throw new RequestException(error, status);
			default:
				throw new IOException(method.getStatusText());
			}
		} catch (JsonParseException jpe) {
			throw new IOException(jpe.getMessage());
		} finally {
			method.releaseConnection();
		}
	}

	/**
	 * Send json using specified method
	 * 
	 * @param <V>
	 * @param method
	 * @param params
	 * @param type
	 * @return resource
	 * @throws IOException
	 */
	protected <V> V sendJson(EntityEnclosingMethod method, Object params,
			Type type) throws IOException {
		if (params != null) {
			StringBuilder payload = new StringBuilder();
			this.gson.toJson(params, payload);
			method.setRequestEntity(new StringRequestEntity(payload.toString(),
					IGitHubConstants.CONTENT_TYPE_JSON,
					IGitHubConstants.CHARSET_UTF8));
		}

		try {
			int status = this.client.executeMethod(this.hostConfig, method);
			switch (status) {
			case 200:
			case 201:
				if (type != null)
					return parseJson(method, type);
			case 204:
				break;
			case 400:
			case 401:
			case 403:
			case 404:
			case 422:
			case 500:
				RequestError error = parseJson(method, RequestError.class);
				throw new RequestException(error, status);
			default:
				throw new IOException(method.getStatusText());
			}
		} finally {
			method.releaseConnection();
		}
		return null;
	}

	/**
	 * Post data to uri
	 * 
	 * @param <V>
	 * @param uri
	 * @param params
	 * @param type
	 * @return response
	 * @throws IOException
	 */
	public <V> V post(String uri, Object params, Type type) throws IOException {
		PostMethod method = createPost(uri);
		return sendJson(method, params, type);
	}

	/**
	 * Put data to uri
	 * 
	 * @param <V>
	 * @param uri
	 * @param params
	 * @param type
	 * @return response
	 * @throws IOException
	 */
	public <V> V put(String uri, Object params, Type type) throws IOException {
		PutMethod method = createPut(uri);
		return sendJson(method, params, type);
	}

}

Back to the top