-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJsonRpcClient.java
More file actions
152 lines (137 loc) · 5.2 KB
/
Copy pathJsonRpcClient.java
File metadata and controls
152 lines (137 loc) · 5.2 KB
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
package com.github.arteam.simplejsonrpc.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.arteam.simplejsonrpc.client.builder.BatchRequestBuilder;
import com.github.arteam.simplejsonrpc.client.builder.NotificationRequestBuilder;
import com.github.arteam.simplejsonrpc.client.builder.ObjectApiBuilder;
import com.github.arteam.simplejsonrpc.client.builder.RequestBuilder;
import com.github.arteam.simplejsonrpc.client.generator.IdGenerator;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Proxy;
/**
* Date: 8/9/14
* Time: 8:58 PM
* <p/>
* JSON-RPC client. Represents a factory for a fluent client API {@link com.github.arteam.simplejsonrpc.client.builder.RequestBuilder}.
* It's parametrized by {@link Transport} and Jackson {@link ObjectMapper}
*
* @author Artem Prigoda
*/
public class JsonRpcClient {
/**
* Transport for performing JSON-RPC requests and returning responses
*/
@NotNull
private Transport transport;
/**
* JSON mapper for conversion between JSON and Java types
*/
@NotNull
private ObjectMapper mapper;
/**
* Constructs a new JSON-RPC client with a specified transport
*
* @param transport transport implementation
*/
public JsonRpcClient(@NotNull Transport transport) {
this(transport, new ObjectMapper());
}
/**
* Constructs a new JSON-RPC client with a specified transport and user-definder JSON mapper
*
* @param transport transport implementation
* @param mapper JSON mapper
*/
public JsonRpcClient(@NotNull Transport transport, @NotNull ObjectMapper mapper) {
this.transport = transport;
this.mapper = mapper;
}
/**
* Creates a builder of a JSON-RPC request in initial state
*
* @return request builder
*/
@NotNull
public RequestBuilder<Object> createRequest() {
return new RequestBuilder<Object>(transport, mapper);
}
/**
* Creates a builder of a JSON-RPC notification request in initial state
*
* @return notification request builder
*/
@NotNull
public NotificationRequestBuilder createNotification() {
return new NotificationRequestBuilder(transport, mapper);
}
/**
* Creates a builder of a JSON-RPC batch request in initial state
*
* @return batch request builder
*/
@NotNull
public BatchRequestBuilder<?, ?> createBatchRequest() {
return new BatchRequestBuilder<Object, Object>(transport, mapper);
}
/**
* Creates a new proxy for accessing a remote JSON-RPC service through an interface
*
* @param clazz interface metadata
* @param <T> interface type
* @return a new proxy
*/
@SuppressWarnings("unchecked")
@NotNull
public <T> T onDemand(@NotNull Class<T> clazz) {
return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz},
new ObjectApiBuilder(clazz, transport, mapper, null, null));
}
/**
* Creates a new proxy for accessing a remote JSON-RPC service through an interface
* with a custom id generator that overrides the interface generator.
*
* @param clazz interface metadata
* @param idGenerator custom id generator
* @param <T> interface type
* @return a new proxy
*/
@SuppressWarnings("unchecked")
@NotNull
public <T> T onDemand(@NotNull Class<T> clazz, @NotNull IdGenerator<?> idGenerator) {
return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz},
new ObjectApiBuilder(clazz, transport, mapper, null, idGenerator));
}
/**
* Creates a new proxy for accessing a remote JSON-RPC service through an interface
* with a custom type of request params.
* It applies for all methods and overrides interface and method level settings.
*
* @param clazz interface metadata
* @param paramsType custom type of request params
* @param <T> interface type
* @return a new proxy
*/
@SuppressWarnings("unchecked")
@NotNull
public <T> T onDemand(@NotNull Class<T> clazz, @NotNull ParamsType paramsType) {
return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz},
new ObjectApiBuilder(clazz, transport, mapper, paramsType, null));
}
/**
* Creates a new proxy for accessing a remote JSON-RPC service through an interface
* with a custom id generator and custom type of request params.
* The generator overrides the interface generator.
* The type applies for all methods and overrides interface and method level settings.
*
* @param clazz interface metadata
* @param idGenerator custom id generator
* @param paramsType custom type of request params
* @param <T> interface type
* @return a new proxy
*/
@SuppressWarnings("unchecked")
@NotNull
public <T> T onDemand(Class<T> clazz, @NotNull ParamsType paramsType, @NotNull IdGenerator<?> idGenerator) {
return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz},
new ObjectApiBuilder(clazz, transport, mapper, paramsType, idGenerator));
}
}