|
| 1 | +/** |
| 2 | + * Copyright 2012-2019 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package feign.googlehttpclient; |
| 16 | + |
| 17 | +import com.google.api.client.http.ByteArrayContent; |
| 18 | +import com.google.api.client.http.GenericUrl; |
| 19 | +import com.google.api.client.http.HttpContent; |
| 20 | +import com.google.api.client.http.HttpHeaders; |
| 21 | +import com.google.api.client.http.HttpTransport; |
| 22 | +import com.google.api.client.http.HttpRequestFactory; |
| 23 | +import com.google.api.client.http.HttpRequest; |
| 24 | +import com.google.api.client.http.HttpResponse; |
| 25 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.HashMap; |
| 31 | + |
| 32 | +import feign.Client; |
| 33 | +import feign.Request; |
| 34 | +import feign.Response; |
| 35 | +import feign.Util; |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * This module directs Feign's http requests to |
| 40 | + * <a href="https://developers.google.com/api-client-library/java/google-http-java-client/">Google HTTP Client</a>. |
| 41 | + * |
| 42 | + * <pre> |
| 43 | + * GitHub github = Feign.builder().client(new GoogleHttpCliest()).target(GitHub.class, |
| 44 | + * "https://api.github.com"); |
| 45 | + */ |
| 46 | +public class GoogleHttpClient implements Client { |
| 47 | + private final HttpTransport transport; |
| 48 | + private final HttpRequestFactory requestFactory; |
| 49 | + |
| 50 | + public GoogleHttpClient() { |
| 51 | + this(new NetHttpTransport()); |
| 52 | + } |
| 53 | + |
| 54 | + public GoogleHttpClient(final HttpTransport transport) { |
| 55 | + this.transport = transport; |
| 56 | + this.requestFactory = transport.createRequestFactory(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public final Response execute(final Request inputRequest, |
| 61 | + final Request.Options options) throws IOException { |
| 62 | + final HttpRequest request = convertRequest(inputRequest, options); |
| 63 | + final HttpResponse response = request.execute(); |
| 64 | + return convertResponse(inputRequest, response); |
| 65 | + } |
| 66 | + |
| 67 | + private final HttpRequest convertRequest(final Request inputRequest, |
| 68 | + final Request.Options options) throws IOException { |
| 69 | + // Setup the request body |
| 70 | + HttpContent content = null; |
| 71 | + if (inputRequest.requestBody().length() > 0) { |
| 72 | + final Collection<String> contentTypeValues = inputRequest.headers().get("Content-Type"); |
| 73 | + String contentType = null; |
| 74 | + if (contentTypeValues != null && contentTypeValues.size() > 0) { |
| 75 | + contentType = contentTypeValues.iterator().next(); |
| 76 | + } else { |
| 77 | + contentType = "application/octet-stream"; |
| 78 | + } |
| 79 | + content = new ByteArrayContent(contentType, inputRequest.requestBody().asBytes()); |
| 80 | + } |
| 81 | + |
| 82 | + // Build the request |
| 83 | + final HttpRequest request = requestFactory.buildRequest(inputRequest.httpMethod().name(), |
| 84 | + new GenericUrl(inputRequest.url()), |
| 85 | + content); |
| 86 | + // Setup headers |
| 87 | + final HttpHeaders headers = new HttpHeaders(); |
| 88 | + for (final Map.Entry<String, Collection<String>> header : inputRequest.headers().entrySet()) { |
| 89 | + headers.set(header.getKey(), header.getValue()); |
| 90 | + } |
| 91 | + // Some servers don't do well with no Accept header |
| 92 | + if (inputRequest.headers().get("Accept") == null) { |
| 93 | + headers.setAccept("*/*"); |
| 94 | + } |
| 95 | + request.setHeaders(headers); |
| 96 | + |
| 97 | + // Setup request options |
| 98 | + request.setReadTimeout(options.readTimeoutMillis()) |
| 99 | + .setConnectTimeout(options.connectTimeoutMillis()) |
| 100 | + .setFollowRedirects(options.isFollowRedirects()) |
| 101 | + .setThrowExceptionOnExecuteError(false); |
| 102 | + return request; |
| 103 | + } |
| 104 | + |
| 105 | + private final Response convertResponse(final Request inputRequest, |
| 106 | + final HttpResponse inputResponse) throws IOException { |
| 107 | + final HttpHeaders headers = inputResponse.getHeaders(); |
| 108 | + Integer contentLength = null; |
| 109 | + if (headers.getContentLength() != null && headers.getContentLength() <= Integer.MAX_VALUE) { |
| 110 | + contentLength = inputResponse.getHeaders().getContentLength().intValue(); |
| 111 | + } |
| 112 | + return Response.builder() |
| 113 | + .body(inputResponse.getContent(), contentLength) |
| 114 | + .status(inputResponse.getStatusCode()) |
| 115 | + .reason(inputResponse.getStatusMessage()) |
| 116 | + .headers(toMap(inputResponse.getHeaders())) |
| 117 | + .request(inputRequest) |
| 118 | + .build(); |
| 119 | + } |
| 120 | + |
| 121 | + private final Map<String, Collection<String>> toMap(final HttpHeaders headers) { |
| 122 | + final Map<String, Collection<String>> map = new HashMap<String, Collection<String>>(); |
| 123 | + for (final String header : headers.keySet()) { |
| 124 | + map.put(header, headers.getHeaderStringValues(header)); |
| 125 | + } |
| 126 | + return map; |
| 127 | + } |
| 128 | +} |
0 commit comments