forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectiveFeign.java
More file actions
266 lines (231 loc) · 10.1 KB
/
ReflectiveFeign.java
File metadata and controls
266 lines (231 loc) · 10.1 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
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
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feign;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Maps;
import com.google.common.reflect.AbstractInvocationHandler;
import com.google.common.reflect.Reflection;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Inject;
import dagger.Provides;
import feign.MethodHandler.Factory;
import feign.Request.Options;
import feign.codec.BodyEncoder;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.codec.FormEncoder;
import feign.codec.ToStringDecoder;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static feign.Contract.parseAndValidatateMetadata;
import static java.lang.String.format;
@SuppressWarnings("rawtypes")
public class ReflectiveFeign extends Feign {
private final Function<Target, Map<String, MethodHandler>> targetToHandlersByName;
@Inject ReflectiveFeign(Function<Target, Map<String, MethodHandler>> targetToHandlersByName) {
this.targetToHandlersByName = targetToHandlersByName;
}
/**
* creates an api binding to the {@code target}. As this invokes reflection,
* care should be taken to cache the result.
*/
@Override public <T> T newInstance(Target<T> target) {
Map<String, MethodHandler> nameToHandler = targetToHandlersByName.apply(target);
Builder<Method, MethodHandler> methodToHandler = ImmutableMap.builder();
for (Method method : target.type().getDeclaredMethods()) {
if (method.getDeclaringClass() == Object.class)
continue;
methodToHandler.put(method, nameToHandler.get(Feign.configKey(method)));
}
FeignInvocationHandler handler = new FeignInvocationHandler(target, methodToHandler.build());
return Reflection.newProxy(target.type(), handler);
}
static class FeignInvocationHandler extends AbstractInvocationHandler {
private final Target target;
private final Map<Method, MethodHandler> methodToHandler;
FeignInvocationHandler(Target target, ImmutableMap<Method, MethodHandler> methodToHandler) {
this.target = checkNotNull(target, "target");
this.methodToHandler = checkNotNull(methodToHandler, "methodToHandler for %s", target);
}
@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
return methodToHandler.get(method).invoke(args);
}
@Override public int hashCode() {
return target.hashCode();
}
@Override public boolean equals(Object obj) {
if (this == obj)
return true;
if (FeignInvocationHandler.class != obj.getClass())
return false;
FeignInvocationHandler that = FeignInvocationHandler.class.cast(obj);
return this.target.equals(that.target);
}
@Override public String toString() {
return Objects.toStringHelper("").add("name", target.name()).add("url", target.url()).toString();
}
}
@dagger.Module(complete = false,// Config
injects = Feign.class, library = true// provides Feign
)
public static class Module {
@Provides Feign provideFeign(ReflectiveFeign in) {
return in;
}
@Provides
Function<Target, Map<String, MethodHandler>> targetToHandlersByName(ParseHandlersByName parseHandlersByName) {
return parseHandlersByName;
}
}
private static IllegalStateException noConfig(String configKey, Class<?> type) {
return new IllegalStateException(format("no configuration for %s present for %s!", configKey,
type.getSimpleName()));
}
static final class ParseHandlersByName implements Function<Target, Map<String, MethodHandler>> {
private final Map<String, Options> options;
private final Map<String, BodyEncoder> bodyEncoders;
private final Map<String, FormEncoder> formEncoders;
private final Map<String, Decoder> decoders;
private final Map<String, ErrorDecoder> errorDecoders;
private final Factory factory;
@Inject ParseHandlersByName(Map<String, Options> options, Map<String, BodyEncoder> bodyEncoders,
Map<String, FormEncoder> formEncoders, Map<String, Decoder> decoders,
Map<String, ErrorDecoder> errorDecoders, Factory factory) {
this.options = options;
this.bodyEncoders = bodyEncoders;
this.formEncoders = formEncoders;
this.decoders = decoders;
this.factory = factory;
this.errorDecoders = errorDecoders;
}
@Override public Map<String, MethodHandler> apply(Target key) {
Set<MethodMetadata> metadata = parseAndValidatateMetadata(key.type());
ImmutableMap.Builder<String, MethodHandler> builder = ImmutableMap.builder();
for (MethodMetadata md : metadata) {
Options options = forMethodOrClass(this.options, md.configKey());
if (options == null) {
options = new Options();
}
Decoder decoder = forMethodOrClass(decoders, md.configKey());
if (decoder == null
&& (md.returnType().getRawType() == void.class || md.returnType().getRawType() == Response.class)) {
decoder = new ToStringDecoder();
}
if (decoder == null) {
throw noConfig(md.configKey(), Decoder.class);
}
ErrorDecoder errorDecoder = forMethodOrClass(errorDecoders, md.configKey());
if (errorDecoder == null) {
errorDecoder = ErrorDecoder.DEFAULT;
}
Function<Object[], RequestTemplate> buildTemplateFromArgs;
if (!md.formParams().isEmpty() && !md.template().bodyTemplate().isPresent()) {
FormEncoder formEncoder = forMethodOrClass(formEncoders, md.configKey());
if (formEncoder == null) {
throw noConfig(md.configKey(), FormEncoder.class);
}
buildTemplateFromArgs = new BuildFormEncodedTemplateFromArgs(md, formEncoder);
} else if (md.bodyIndex() != null) {
BodyEncoder bodyEncoder = forMethodOrClass(bodyEncoders, md.configKey());
if (bodyEncoder == null) {
throw noConfig(md.configKey(), BodyEncoder.class);
}
buildTemplateFromArgs = new BuildBodyEncodedTemplateFromArgs(md, bodyEncoder);
} else {
buildTemplateFromArgs = new BuildTemplateFromArgs(md);
}
builder.put(md.configKey(),
factory.create(key, md, buildTemplateFromArgs, options, decoder, errorDecoder));
}
return builder.build();
}
}
private static class BuildTemplateFromArgs implements Function<Object[], RequestTemplate> {
protected final MethodMetadata metadata;
private BuildTemplateFromArgs(MethodMetadata metadata) {
this.metadata = metadata;
}
@Override
public RequestTemplate apply(Object[] argv) {
RequestTemplate mutable = new RequestTemplate(metadata.template());
if (metadata.urlIndex() != null) {
int urlIndex = metadata.urlIndex();
checkArgument(argv[urlIndex] != null, "URI parameter %s was null", urlIndex);
mutable.insert(0, String.valueOf(argv[urlIndex]));
}
ImmutableMap.Builder<String, Object> varBuilder = ImmutableMap.builder();
for (Entry<Integer, Collection<String>> entry : metadata.indexToName().asMap().entrySet()) {
Object value = argv[entry.getKey()];
if (value != null) { // Null values are skipped.
for (String name : entry.getValue())
varBuilder.put(name, value);
}
}
return resolve(argv, mutable, varBuilder.build());
}
protected RequestTemplate resolve(Object[] argv, RequestTemplate mutable, ImmutableMap<String, Object> variables) {
return mutable.resolve(variables);
}
}
private static class BuildFormEncodedTemplateFromArgs extends BuildTemplateFromArgs {
private final FormEncoder formEncoder;
private BuildFormEncodedTemplateFromArgs(MethodMetadata metadata, FormEncoder formEncoder) {
super(metadata);
this.formEncoder = formEncoder;
}
@Override
protected RequestTemplate resolve(Object[] argv, RequestTemplate mutable, ImmutableMap<String, Object> variables) {
formEncoder.encodeForm(Maps.filterKeys(variables, Predicates.in(metadata.formParams())), mutable);
return super.resolve(argv, mutable, variables);
}
}
private static class BuildBodyEncodedTemplateFromArgs extends BuildTemplateFromArgs {
private final BodyEncoder bodyEncoder;
private BuildBodyEncodedTemplateFromArgs(MethodMetadata metadata, BodyEncoder bodyEncoder) {
super(metadata);
this.bodyEncoder = bodyEncoder;
}
@Override
protected RequestTemplate resolve(Object[] argv, RequestTemplate mutable, ImmutableMap<String, Object> variables) {
Object body = argv[metadata.bodyIndex()];
checkArgument(body != null, "Body parameter %s was null", metadata.bodyIndex());
bodyEncoder.encodeBody(body, mutable);
return super.resolve(argv, mutable, variables);
}
}
static <T> T forMethodOrClass(Map<String, T> config, String configKey) {
if (config.containsKey(configKey)) {
return config.get(configKey);
}
String classKey = toClassKey(configKey);
if (config.containsKey(classKey)) {
return config.get(classKey);
}
return null;
}
public static String toClassKey(String methodKey) {
return methodKey.substring(0, methodKey.indexOf('#'));
}
}