Skip to main content
summaryrefslogtreecommitdiffstats
blob: dd7286b95a575171419e0d4fa6a320549a3e69d1 (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
/*********************************************************************
 * Copyright (c) 2011 Tasktop Technologies and others.
 * 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:
 *      Tasktop Technologies - initial API and implementation
 *********************************************************************/
package org.eclipse.mylyn.internal.gerrit.core.client;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.JsonEntity;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtjsonrpc.client.RemoteJsonService;

/**
 * @author Steffen Pingel
 */
public class GerritService implements InvocationHandler {

	public static class GerritRequest {

		private static ThreadLocal<GerritRequest> currentRequest = new ThreadLocal<GerritRequest>();

		public static GerritRequest getCurrentRequest() {
			return currentRequest.get();
		}

		public static void setCurrentRequest(GerritRequest request) {
			currentRequest.set(request);
		}

		private final IProgressMonitor monitor;

		public GerritRequest(IProgressMonitor monitor) {
			this.monitor = monitor;
		}

		public IProgressMonitor getMonitor() {
			return monitor;
		}

	}

	public static <T extends RemoteJsonService> T create(Class<T> serviceClass, GerritHttpClient gerritHttpClient) {
		InvocationHandler handler = new GerritService(gerritHttpClient, "/gerrit/rpc/" + serviceClass.getSimpleName()); //$NON-NLS-1$
		return serviceClass.cast(Proxy.newProxyInstance(GerritService.class.getClassLoader(),
				new Class<?>[] { serviceClass }, handler));
	}

	private final String uri;

	protected GerritHttpClient client;

	public GerritService(GerritHttpClient client, String uri) {
		this.client = client;
		this.uri = uri;
	}

	public String getServiceUri() {
		return uri;
	}

	public Object invoke(Object proxy, final Method method, Object[] args) {
		final JSonSupport json = new JSonSupport();

		// construct request
		final List<Object> parameters = new ArrayList<Object>(args.length - 1);
		for (int i = 0; i < args.length - 1; i++) {
			parameters.add(args[i]);
		}
		@SuppressWarnings("unchecked")
		AsyncCallback<Object> callback = (AsyncCallback<Object>) args[args.length - 1];

		try {
			GerritRequest request = GerritRequest.getCurrentRequest();
			IProgressMonitor monitor = (request != null) ? request.getMonitor() : null;

			// execute request
			String responseMessage = client.postJsonRequest(getServiceUri(), new JsonEntity() {
				@Override
				public String getContent() {
					String methodName = method.getName();
					if (methodName.endsWith("X")) { //$NON-NLS-1$
						methodName = methodName.substring(0, methodName.length() - 1);
					}
					return json.createRequest(client.getId(), client.getXsrfKey(), methodName, parameters);
				}
			}, monitor);

			// the last parameter is a parameterized callback that defines the return type
			Type[] types = method.getGenericParameterTypes();
			final Type resultType = ((ParameterizedType) types[types.length - 1]).getActualTypeArguments()[0];

			Object result = json.parseResponse(responseMessage, resultType);
			callback.onSuccess(result);
		} catch (Throwable e) {
			callback.onFailure(e);
		}
		// all methods are designed to be asynchronous and expected to return void 
		return null;
	};

}

Back to the top