|
| 1 | +/* |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package feign.httpclient; |
| 17 | + |
| 18 | +import feign.Client; |
| 19 | +import feign.Request; |
| 20 | +import feign.Response; |
| 21 | +import feign.Util; |
| 22 | +import org.apache.http.Header; |
| 23 | +import org.apache.http.HttpEntity; |
| 24 | +import org.apache.http.HttpResponse; |
| 25 | +import org.apache.http.NameValuePair; |
| 26 | +import org.apache.http.StatusLine; |
| 27 | +import org.apache.http.client.HttpClient; |
| 28 | +import org.apache.http.client.config.RequestConfig; |
| 29 | +import org.apache.http.client.methods.HttpUriRequest; |
| 30 | +import org.apache.http.client.methods.RequestBuilder; |
| 31 | +import org.apache.http.client.utils.URIBuilder; |
| 32 | +import org.apache.http.client.utils.URLEncodedUtils; |
| 33 | +import org.apache.http.entity.ByteArrayEntity; |
| 34 | +import org.apache.http.entity.StringEntity; |
| 35 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 36 | +import org.apache.http.util.EntityUtils; |
| 37 | + |
| 38 | +import java.io.ByteArrayInputStream; |
| 39 | +import java.io.IOException; |
| 40 | +import java.io.InputStream; |
| 41 | +import java.io.InputStreamReader; |
| 42 | +import java.io.Reader; |
| 43 | +import java.io.UnsupportedEncodingException; |
| 44 | +import java.net.MalformedURLException; |
| 45 | +import java.net.URI; |
| 46 | +import java.net.URISyntaxException; |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.Collection; |
| 49 | +import java.util.HashMap; |
| 50 | +import java.util.List; |
| 51 | +import java.util.Map; |
| 52 | + |
| 53 | +/** |
| 54 | + * This module directs Feign's http requests to Apache's |
| 55 | + * <a href="https://hc.apache.org/httpcomponents-client-ga/">HttpClient</a>. Ex. |
| 56 | + * <pre> |
| 57 | + * GitHub github = Feign.builder().client(new ApacheHttpClient()).target(GitHub.class, |
| 58 | + * "https://api.github.com"); |
| 59 | + */ |
| 60 | +/* |
| 61 | + * Based on Square, Inc's Retrofit ApacheClient implementation |
| 62 | + */ |
| 63 | +public final class ApacheHttpClient implements Client { |
| 64 | + private static final String ACCEPT_HEADER_NAME = "Accept"; |
| 65 | + |
| 66 | + private final HttpClient client; |
| 67 | + |
| 68 | + public ApacheHttpClient() { |
| 69 | + this(HttpClientBuilder.create().build()); |
| 70 | + } |
| 71 | + |
| 72 | + public ApacheHttpClient(HttpClient client) { |
| 73 | + this.client = client; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Response execute(Request request, Request.Options options) throws IOException { |
| 78 | + HttpUriRequest httpUriRequest; |
| 79 | + try { |
| 80 | + httpUriRequest = toHttpUriRequest(request, options); |
| 81 | + } catch (URISyntaxException e) { |
| 82 | + throw new IOException("URL '" + request.url() + "' couldn't be parsed into a URI", e); |
| 83 | + } |
| 84 | + HttpResponse httpResponse = client.execute(httpUriRequest); |
| 85 | + return toFeignResponse(httpResponse); |
| 86 | + } |
| 87 | + |
| 88 | + HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws |
| 89 | + UnsupportedEncodingException, MalformedURLException, URISyntaxException { |
| 90 | + RequestBuilder requestBuilder = RequestBuilder.create(request.method()); |
| 91 | + |
| 92 | + //per request timeouts |
| 93 | + RequestConfig requestConfig = RequestConfig |
| 94 | + .custom() |
| 95 | + .setConnectTimeout(options.connectTimeoutMillis()) |
| 96 | + .setSocketTimeout(options.readTimeoutMillis()) |
| 97 | + .build(); |
| 98 | + requestBuilder.setConfig(requestConfig); |
| 99 | + |
| 100 | + URI uri = new URIBuilder(request.url()).build(); |
| 101 | + |
| 102 | + //request url |
| 103 | + requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getPath()); |
| 104 | + |
| 105 | + //request query params |
| 106 | + List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset().name()); |
| 107 | + for (NameValuePair queryParam: queryParams) { |
| 108 | + requestBuilder.addParameter(queryParam); |
| 109 | + } |
| 110 | + |
| 111 | + //request body |
| 112 | + if (request.body() != null) { |
| 113 | + HttpEntity entity = request.charset() != null ? |
| 114 | + new StringEntity(new String(request.body(), request.charset())) : |
| 115 | + new ByteArrayEntity(request.body()); |
| 116 | + requestBuilder.setEntity(entity); |
| 117 | + } |
| 118 | + |
| 119 | + //request headers |
| 120 | + boolean hasAcceptHeader = false; |
| 121 | + for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) { |
| 122 | + String headerName = headerEntry.getKey(); |
| 123 | + if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) { |
| 124 | + hasAcceptHeader = true; |
| 125 | + } |
| 126 | + if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH) && |
| 127 | + requestBuilder.getHeaders(headerName) != null) { |
| 128 | + //if the 'Content-Length' header is already present, it's been set from HttpEntity, so we |
| 129 | + //won't add it again |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + for (String headerValue : headerEntry.getValue()) { |
| 134 | + requestBuilder.addHeader(headerName, headerValue); |
| 135 | + } |
| 136 | + } |
| 137 | + //some servers choke on the default accept string, so we'll set it to anything |
| 138 | + if (!hasAcceptHeader) { |
| 139 | + requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*"); |
| 140 | + } |
| 141 | + |
| 142 | + return requestBuilder.build(); |
| 143 | + } |
| 144 | + |
| 145 | + Response toFeignResponse(HttpResponse httpResponse) throws IOException { |
| 146 | + StatusLine statusLine = httpResponse.getStatusLine(); |
| 147 | + int statusCode = statusLine.getStatusCode(); |
| 148 | + |
| 149 | + String reason = statusLine.getReasonPhrase(); |
| 150 | + |
| 151 | + Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>(); |
| 152 | + for (Header header : httpResponse.getAllHeaders()) { |
| 153 | + String name = header.getName(); |
| 154 | + String value = header.getValue(); |
| 155 | + |
| 156 | + Collection<String> headerValues = headers.get(name); |
| 157 | + if (headerValues == null) { |
| 158 | + headerValues = new ArrayList<String>(); |
| 159 | + headers.put(name, headerValues); |
| 160 | + } |
| 161 | + headerValues.add(value); |
| 162 | + } |
| 163 | + |
| 164 | + return Response.create(statusCode, reason, headers, toFeignBody(httpResponse)); |
| 165 | + } |
| 166 | + |
| 167 | + Response.Body toFeignBody(HttpResponse httpResponse) throws IOException { |
| 168 | + HttpEntity entity = httpResponse.getEntity(); |
| 169 | + final Integer length = entity != null && entity.getContentLength() != -1 ? |
| 170 | + (int) entity.getContentLength() : |
| 171 | + null; |
| 172 | + final InputStream input = entity != null ? |
| 173 | + new ByteArrayInputStream(EntityUtils.toByteArray(entity)) : |
| 174 | + null; |
| 175 | + |
| 176 | + return new Response.Body() { |
| 177 | + |
| 178 | + @Override |
| 179 | + public void close() throws IOException { |
| 180 | + if (input != null) { |
| 181 | + input.close(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public Integer length() { |
| 187 | + return length; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public boolean isRepeatable() { |
| 192 | + return false; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public InputStream asInputStream() throws IOException { |
| 197 | + return input; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public Reader asReader() throws IOException { |
| 202 | + return new InputStreamReader(input); |
| 203 | + } |
| 204 | + }; |
| 205 | + } |
| 206 | + |
| 207 | +} |
0 commit comments