Skip to content

Latest commit

 

History

History
74 lines (52 loc) · 3.64 KB

README.md

File metadata and controls

74 lines (52 loc) · 3.64 KB

Fahrschein HTTP API and Implementation

Build Status Release Maven Central

A fork of the spring ClientHttpRequestFactory api and implementations without dependency on the remainder of the spring framework. The main usecase is for the fahrschein nakadi client.

Changes compared to the spring implementation

  • The ClientHttpResponse#close methods do not try to consume remaining data from the stream, instead the connection is aborted (see SPR-14040 and SPR-14882).

Usage

Sending a request body serialized with Jackson and deserializing the response

    final URI uri = URI.create(...);
    final ObjectMapper objectMapper = new ObjectMapper();

    // RequestData and ResponseData can be arbitrary java objects which can be serialized and deserialized by jackson
    final RequestData requestData = ...

    final SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    requestFactoryDelegate.setConnectTimeout(500);
    requestFactoryDelegate.setReadTimeout(60 * 1000);

    final ClientHttpRequest request = clientHttpRequestFactory.createRequest(uri, HttpMethod.POST);

    request.getHeaders().setContentType(MediaType.APPLICATION_JSON);

    try (final OutputStream os = request.getBody()) {
        objectMapper.writeValue(os, requestData);
    }

    try (final ClientHttpResponse response = request.execute()) {
        try (final InputStream is = response.getBody()) {
            final ResponseData responseData = objectMapper.readValue(is, ResponseData.class);
            ...
        }
    }

Using the Apache HttpComponents implementation

final RequestConfig config = RequestConfig.custom().setSocketTimeout(readTimeout)
                                                   .setConnectTimeout(connectTimeout)
                                                   .setConnectionRequestTimeout(requestTimeout)
                                                   .build();

final CloseableHttpClient httpClient = HttpClients.custom()
                                                  .setConnectionTimeToLive(readTimeout, TimeUnit.MILLISECONDS)
                                                  .disableAutomaticRetries()
                                                  .setDefaultRequestConfig(config)
                                                  .disableRedirectHandling()
                                                  .setMaxConnTotal(8)
                                                  .setMaxConnPerRoute(2)
                                                  .build();

final ClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

Getting help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's issue tracker.

Getting involved

To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change.