forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSAXDecoder.java
More file actions
168 lines (152 loc) · 6 KB
/
SAXDecoder.java
File metadata and controls
168 lines (152 loc) · 6 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
/*
* 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.sax;
import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import javax.inject.Provider;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;
import static feign.Util.checkNotNull;
import static feign.Util.checkState;
import static feign.Util.ensureClosed;
import static feign.Util.resolveLastTypeParameter;
/**
* Decodes responses using SAX, which is supported both in normal JVM environments, as well Android.
* <br>
* <h4>Basic example with with Feign.Builder</h4>
* <br>
* <pre>
* api = Feign.builder()
* .decoder(SAXDecoder.builder()
* .registerContentHandler(ContentHandlerForFoo.class)
* .registerContentHandler(ContentHandlerForBar.class)
* .build())
* .target(MyApi.class, "http://api");
* </pre>
* <p/>
* <h4>Advanced example with Dagger</h4>
* <br>
* <pre>
* @Provides
* Decoder saxDecoder(Provider<ContentHandlerForFoo> foo, //
* Provider<ContentHandlerForBar> bar) {
* return SAXDecoder.builder() //
* .registerContentHandler(Foo.class, foo) //
* .registerContentHandler(Bar.class, bar) //
* .build();
* }
* </pre>
*/
public class SAXDecoder implements Decoder {
public static Builder builder() {
return new Builder();
}
// builder as dagger doesn't support wildcard bindings, map bindings, or set bindings of providers.
public static class Builder {
private final Map<Type, Provider<? extends ContentHandlerWithResult<?>>> handlerProviders =
new LinkedHashMap<Type, Provider<? extends ContentHandlerWithResult<?>>>();
/**
* Will call {@link Constructor#newInstance(Object...)} on {@code handlerClass} for each content stream.
* <p/>
* <h3>Note</h3>
* <br/>
* While this is costly vs {@code new}, it may not affect real performance due to the high cost of reading streams.
*
* @throws IllegalArgumentException if there's no no-arg constructor on {@code handlerClass}.
*/
public <T extends ContentHandlerWithResult<?>> Builder registerContentHandler(Class<T> handlerClass) {
Type type = resolveLastTypeParameter(checkNotNull(handlerClass, "handlerClass"), ContentHandlerWithResult.class);
return registerContentHandler(type, new NewInstanceProvider(handlerClass));
}
private static class NewInstanceProvider<T extends ContentHandlerWithResult<?>> implements Provider<T> {
private final Constructor<T> ctor;
private NewInstanceProvider(Class<T> clazz) {
try {
this.ctor = clazz.getDeclaredConstructor();
// allow private or package protected ctors
ctor.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("ensure " + clazz + " has a no-args constructor", e);
}
}
@Override public T get() {
try {
return ctor.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("exception attempting to instantiate " + ctor, e);
}
}
}
/**
* Will call {@link Provider#get()} on {@code handler} for each content stream.
* The {@code handler} is expected to have a generic parameter of {@code type}.
*/
public Builder registerContentHandler(Type type, Provider<? extends ContentHandlerWithResult<?>> handler) {
this.handlerProviders.put(checkNotNull(type, "type"), checkNotNull(handler, "handler"));
return this;
}
public SAXDecoder build() {
return new SAXDecoder(handlerProviders);
}
}
/**
* Implementations are not intended to be shared across requests.
*/
public interface ContentHandlerWithResult<T> extends ContentHandler {
/**
* expected to be set following a call to {@link XMLReader#parse(InputSource)}
*/
T result();
}
private final Map<Type, Provider<? extends ContentHandlerWithResult<?>>> handlerProviders;
private SAXDecoder(Map<Type, Provider<? extends ContentHandlerWithResult<?>>> handlerProviders) {
this.handlerProviders = handlerProviders;
}
@Override
public Object decode(Response response, Type type) throws IOException, DecodeException {
if (response.body() == null) {
return null;
}
Provider<? extends ContentHandlerWithResult<?>> handlerProvider = handlerProviders.get(type);
checkState(handlerProvider != null, "type %s not in configured handlers %s", type, handlerProviders.keySet());
ContentHandlerWithResult<?> handler = handlerProvider.get();
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
xmlReader.setFeature("http://xml.org/sax/features/validation", false);
xmlReader.setContentHandler(handler);
InputStream inputStream = response.body().asInputStream();
try {
xmlReader.parse(new InputSource(inputStream));
} finally {
ensureClosed(inputStream);
}
return handler.result();
} catch (SAXException e) {
throw new DecodeException(e.getMessage(), e);
}
}
}