|
| 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.okhttp; |
| 17 | + |
| 18 | +import com.squareup.okhttp.Headers; |
| 19 | +import com.squareup.okhttp.MediaType; |
| 20 | +import com.squareup.okhttp.Request; |
| 21 | +import com.squareup.okhttp.RequestBody; |
| 22 | +import com.squareup.okhttp.Response; |
| 23 | +import com.squareup.okhttp.ResponseBody; |
| 24 | +import feign.Client; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.io.Reader; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.LinkedHashMap; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
| 33 | +/** |
| 34 | + * This module directs Feign's http requests to <a href="http://square.github.io/okhttp/">OkHttp</a>, which enables |
| 35 | + * SPDY and better network control. |
| 36 | + * Ex. |
| 37 | + * <pre> |
| 38 | + * GitHub github = Feign.builder().client(new OkHttpClient()).target(GitHub.class, "https://api.github.com"); |
| 39 | + */ |
| 40 | +public final class OkHttpClient implements Client { |
| 41 | + private final com.squareup.okhttp.OkHttpClient delegate; |
| 42 | + |
| 43 | + public OkHttpClient() { |
| 44 | + this(new com.squareup.okhttp.OkHttpClient()); |
| 45 | + } |
| 46 | + |
| 47 | + public OkHttpClient(com.squareup.okhttp.OkHttpClient delegate) { |
| 48 | + this.delegate = delegate; |
| 49 | + } |
| 50 | + |
| 51 | + @Override public feign.Response execute(feign.Request input, feign.Request.Options options) throws IOException { |
| 52 | + com.squareup.okhttp.OkHttpClient requestScoped; |
| 53 | + if (delegate.getConnectTimeout() != options.connectTimeoutMillis() |
| 54 | + || delegate.getReadTimeout() != options.readTimeoutMillis()) { |
| 55 | + requestScoped = delegate.clone(); |
| 56 | + requestScoped.setConnectTimeout(options.connectTimeoutMillis(), TimeUnit.MILLISECONDS); |
| 57 | + requestScoped.setReadTimeout(options.readTimeoutMillis(), TimeUnit.MILLISECONDS); |
| 58 | + } else { |
| 59 | + requestScoped = delegate; |
| 60 | + } |
| 61 | + Request request = toOkHttpRequest(input); |
| 62 | + Response response = requestScoped.newCall(request).execute(); |
| 63 | + return toFeignResponse(response); |
| 64 | + } |
| 65 | + |
| 66 | + static Request toOkHttpRequest(feign.Request input) { |
| 67 | + Request.Builder requestBuilder = new Request.Builder(); |
| 68 | + requestBuilder.url(input.url()); |
| 69 | + |
| 70 | + MediaType mediaType = null; |
| 71 | + for (String field : input.headers().keySet()) { |
| 72 | + for (String value : input.headers().get(field)) { |
| 73 | + if (field.equalsIgnoreCase("Content-Type")) { |
| 74 | + mediaType = MediaType.parse(value); |
| 75 | + if (input.charset() != null) mediaType.charset(input.charset()); |
| 76 | + } else { |
| 77 | + requestBuilder.addHeader(field, value); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + RequestBody body = input.body() != null ? RequestBody.create(mediaType, input.body()) : null; |
| 82 | + requestBuilder.method(input.method(), body); |
| 83 | + return requestBuilder.build(); |
| 84 | + } |
| 85 | + |
| 86 | + private static feign.Response toFeignResponse(Response input) { |
| 87 | + return feign.Response.create(input.code(), input.message(), toMap(input.headers()), toBody(input.body())); |
| 88 | + } |
| 89 | + |
| 90 | + private static Map<String, Collection<String>> toMap(Headers headers) { |
| 91 | + Map<String, Collection<String>> result = new LinkedHashMap<String, Collection<String>>(headers.size()); |
| 92 | + for (String name : headers.names()) { |
| 93 | + // TODO: this is very inefficient as headers.values iterate case insensitively. |
| 94 | + result.put(name, headers.values(name)); |
| 95 | + } |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
| 99 | + private static feign.Response.Body toBody(final ResponseBody input) { |
| 100 | + if (input == null || input.contentLength() == 0) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + if (input.contentLength() > Integer.MAX_VALUE) { |
| 104 | + throw new UnsupportedOperationException("Length too long "+ input.contentLength()); |
| 105 | + } |
| 106 | + final Integer length = input.contentLength() != -1 ? (int) input.contentLength() : null; |
| 107 | + |
| 108 | + return new feign.Response.Body() { |
| 109 | + |
| 110 | + @Override public void close() throws IOException { |
| 111 | + input.close(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override public Integer length() { |
| 115 | + return length; |
| 116 | + } |
| 117 | + |
| 118 | + @Override public boolean isRepeatable() { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + @Override public InputStream asInputStream() throws IOException { |
| 123 | + return input.byteStream(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override public Reader asReader() throws IOException { |
| 127 | + return input.charStream(); |
| 128 | + } |
| 129 | + }; |
| 130 | + } |
| 131 | +} |
0 commit comments