This module directs Feign's http requests to javax.ws.rs.client.Client . Ex: GitHub github = Feign.builder().client(new JaxRSClient()).target(GitHub.class, "https://api.github.com");
| 43 | * </pre> |
| 44 | */ |
| 45 | public class JAXRSClient implements Client { |
| 46 | |
| 47 | private final ClientBuilder clientBuilder; |
| 48 | |
| 49 | public JAXRSClient() { |
| 50 | this(ClientBuilder.newBuilder()); |
| 51 | } |
| 52 | |
| 53 | public JAXRSClient(ClientBuilder clientBuilder) { |
| 54 | this.clientBuilder = clientBuilder; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public feign.Response execute(feign.Request request, Options options) throws IOException { |
| 59 | final Response response = |
| 60 | clientBuilder |
| 61 | .connectTimeout(options.connectTimeoutMillis(), TimeUnit.MILLISECONDS) |
| 62 | .readTimeout(options.readTimeoutMillis(), TimeUnit.MILLISECONDS) |
| 63 | .build() |
| 64 | .target(request.url()) |
| 65 | .request() |
| 66 | .headers(toMultivaluedMap(request.headers())) |
| 67 | .method(request.httpMethod().name(), createRequestEntity(request)); |
| 68 | |
| 69 | return feign.Response.builder() |
| 70 | .request(request) |
| 71 | .body( |
| 72 | response.readEntity(InputStream.class), |
| 73 | integerHeader(response, HttpHeaders.CONTENT_LENGTH)) |
| 74 | .headers(toMap(response.getStringHeaders())) |
| 75 | .status(response.getStatus()) |
| 76 | .reason(response.getStatusInfo().getReasonPhrase()) |
| 77 | .build(); |
| 78 | } |
| 79 | |
| 80 | private Entity<byte[]> createRequestEntity(feign.Request request) { |
| 81 | if (request.body() == null) { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | return Entity.entity( |
| 86 | request.body(), |
| 87 | new Variant( |
| 88 | mediaType(request.headers()), locale(request.headers()), encoding(request.charset()))); |
| 89 | } |
| 90 | |
| 91 | private Integer integerHeader(Response response, String header) { |
| 92 | final MultivaluedMap<String, String> headers = response.getStringHeaders(); |
| 93 | if (!headers.containsKey(header)) { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | try { |
| 98 | return Integer.valueOf(headers.getFirst(header)); |
| 99 | } catch (final NumberFormatException e) { |
| 100 | // not a number or too big to fit Integer |
| 101 | return null; |
| 102 | } |
nothing calls this directly
no outgoing calls
no test coverage detected