private <RS> Maybe<Response<RS>> executeInternal(final HttpUriRequest rq,
            final HttpEntityCallback<RS> callback) {

        Maybe<Response<RS>> result = Maybe.create(new MaybeOnSubscribe<Response<RS>>() {
            @Override
            public void subscribe(final MaybeEmitter<Response<RS>> maybeEmitter) throws Exception {

                httpClient.execute(rq, new FutureCallback<HttpResponse>() {
                    @Override
                    public void completed(final HttpResponse response) {

                        final Closer closer = Closer.create();
                        try {
                            LazyByteSource bodySupplier = new LazyByteSource(response.getEntity());
                            closer.register(bodySupplier);

                            /* convert headers to multimap */
                            Header[] allHeaders = response.getAllHeaders();
                            ImmutableMultimap.Builder<String, String> headersBuilder = ImmutableMultimap.builder();
                            for (Header header : allHeaders) {
                                for (HeaderElement element : header.getElements()) {
                                    headersBuilder.put(header.getName(),
                                            null == element.getValue() ? "" : element.getValue());
                                }
                            }

                            /* convert entire response */
                            Response<ByteSource> rs = new Response<ByteSource>(rq.getURI(),
                                    HttpMethod.valueOf(rq.getMethod()),
                                    response.getStatusLine().getStatusCode(),
                                    response.getStatusLine().getReasonPhrase(),
                                    headersBuilder.build(),
                                    bodySupplier);

                            /* check whether there is error in the response */
                            if (errorHandler.hasError(rs)) {
                                errorHandler.handle(rs);
                            }

                            /* parse Content-Type header to be able to find appropriate serializer */
                            MediaType contentType = null == response.getEntity().getContentType() ? MediaType.ANY_TYPE
                                    : MediaType.parse(response.getEntity().getContentType().getValue());

                            /* build response with converted instance */
                            Response<RS> converterRS = new Response<RS>(rs.getUri(),
                                    rs.getHttpMethod(),
                                    rs.getStatus(),
                                    rs.getReason(),
                                    rs.getHeaders(),
                                    callback.callback(contentType, bodySupplier.read()));

                            maybeEmitter.onSuccess(converterRS);

                        } catch (SerializerException e) {
                            maybeEmitter.onError(e);
                        } catch (IOException e) {
                            maybeEmitter.onError(new RestEndpointIOException("Unable to execute request", e));
                        } catch (Exception e) {
                            maybeEmitter.onError(e);
                        } finally {
                            try {
                                closer.close();
                            } catch (IOException e) {
                                maybeEmitter.onError(e);
                            }
                        }

                    }

                    @Override
                    public void failed(final Exception ex) {
                        maybeEmitter.onError(new RestEndpointIOException("Unable to execute request", ex));
                    }

                    @Override
                    public void cancelled() {
                        final TimeoutException timeoutException = new TimeoutException();
                        maybeEmitter.onError(timeoutException);
                    }
                });
            }
        }).cache().subscribeOn(Schedulers.io());
        /* subscribe to trigger request execution! TBD does it really needed */
//        result.subscribe();
        return result;
    }
private < RS > Maybe < Response < RS > > executeInternal ( final HttpUriRequest rq , final HttpEntityCallback < RS > callback ) { Maybe < Response < RS > > result = Maybe . create ( new MaybeOnSubscribe < Response < RS > > ( ) { @ Override public void subscribe ( final MaybeEmitter < Response < RS > > maybeEmitter ) throws Exception { httpClient . execute ( rq , new FutureCallback < HttpResponse > ( ) { @ Override public void completed ( final HttpResponse response ) { final Closer closer = Closer . create ( ) ; try { LazyByteSource bodySupplier = new LazyByteSource ( response . getEntity ( ) ) ; closer . register ( bodySupplier ) ; Header [ ] allHeaders = response . getAllHeaders ( ) ; ImmutableMultimap . Builder < String , String > headersBuilder = ImmutableMultimap . builder ( ) ; for ( Header header : allHeaders ) { for ( HeaderElement element : header . getElements ( ) ) { headersBuilder . put ( header . getName ( ) , null == element . getValue ( ) ? "" : element . getValue ( ) ) ; } } Response < ByteSource > rs = new Response < ByteSource > ( rq . getURI ( ) , HttpMethod . valueOf ( rq . getMethod ( ) ) , response . getStatusLine ( ) . getStatusCode ( ) , response . getStatusLine ( ) . getReasonPhrase ( ) , headersBuilder . build ( ) , bodySupplier ) ; if ( errorHandler . hasError ( rs ) ) { errorHandler . handle ( rs ) ; } MediaType contentType = null == response . getEntity ( ) . getContentType ( ) ? MediaType . ANY_TYPE : MediaType . parse ( response . getEntity ( ) . getContentType ( ) . getValue ( ) ) ; Response < RS > converterRS = new Response < RS > ( rs . getUri ( ) , rs . getHttpMethod ( ) , rs . getStatus ( ) , rs . getReason ( ) , rs . getHeaders ( ) , callback . callback ( contentType , bodySupplier . read ( ) ) ) ; maybeEmitter . onSuccess ( converterRS ) ; } catch ( SerializerException e ) { maybeEmitter . onError ( e ) ; } catch ( IOException e ) { maybeEmitter . onError ( new RestEndpointIOException ( "Unable to execute request" , e ) ) ; } catch ( Exception e ) { maybeEmitter . onError ( e ) ; } finally { try { closer . close ( ) ; } catch ( IOException e ) { maybeEmitter . onError ( e ) ; } } } @ Override public void failed ( final Exception ex ) { maybeEmitter . onError ( new RestEndpointIOException ( "Unable to execute request" , ex ) ) ; } @ Override public void cancelled ( ) { final TimeoutException timeoutException = new TimeoutException ( ) ; maybeEmitter . onError ( timeoutException ) ; } } ) ; } } ) . cache ( ) . subscribeOn ( Schedulers . io ( ) ) ; return result ; }
Executes {@link HttpUriRequest}

@param rq       - Request
@param callback - Callback to be applied on response
@param <RS>     type of response
@return - Serialized Response Body