|
| 1 | +/** |
| 2 | + * Copyright 2013 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 rx.apache.http; |
| 17 | + |
| 18 | +import org.apache.http.HttpResponse; |
| 19 | +import org.apache.http.client.HttpClient; |
| 20 | +import org.apache.http.concurrent.FutureCallback; |
| 21 | +import org.apache.http.nio.client.HttpAsyncClient; |
| 22 | +import org.apache.http.nio.client.methods.HttpAsyncMethods; |
| 23 | +import org.apache.http.nio.protocol.HttpAsyncRequestProducer; |
| 24 | + |
| 25 | +import rx.Observable; |
| 26 | +import rx.Observable.OnSubscribeFunc; |
| 27 | +import rx.Observer; |
| 28 | +import rx.Subscription; |
| 29 | +import rx.apache.http.consumers.ResponseConsumerDelegate; |
| 30 | +import rx.subscriptions.CompositeSubscription; |
| 31 | +import rx.subscriptions.Subscriptions; |
| 32 | + |
| 33 | +/** |
| 34 | + * An {@link Observable} interface to Apache {@link HttpAsyncClient}. |
| 35 | + * <p> |
| 36 | + * The initial {@link HttpResponse} is returned via {@link Observer#onNext} wrapped in a {@link ObservableHttpResponse}. |
| 37 | + * <p> |
| 38 | + * The content stream is retrieved from {@link ObservableHttpResponse#getContent()}. |
| 39 | + * <p> |
| 40 | + * It is aware of Content-Type <i>text/event-stream</i> and will stream each event via {@link Observer#onNext}. |
| 41 | + * <p> |
| 42 | + * Other Content-Types will be returned as a single call to {@link Observer#onNext}. |
| 43 | + * <p> |
| 44 | + * Examples: |
| 45 | + * <p> |
| 46 | + * <pre> {@code |
| 47 | + * ObservableHttp.createGet("http://www.wikipedia.com", httpClient).toObservable(); |
| 48 | + * } </pre> |
| 49 | + * <p> |
| 50 | + * <pre> {@code |
| 51 | + * ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), httpClient).toObservable(); |
| 52 | + * } </pre> |
| 53 | + * |
| 54 | + * An {@link HttpClient} can be created like this: |
| 55 | + * |
| 56 | + * <pre> {@code |
| 57 | + * CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault(); |
| 58 | + * httpClient.start(); // start it |
| 59 | + * httpClient.stop(); // stop it |
| 60 | + * } </pre> |
| 61 | + * <p> |
| 62 | + * A client with custom configurations can be created like this: |
| 63 | + * </p> |
| 64 | + * <pre> {@code |
| 65 | + * final RequestConfig requestConfig = RequestConfig.custom() |
| 66 | + * .setSocketTimeout(1000) |
| 67 | + * .setConnectTimeout(200).build(); |
| 68 | + * final CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom() |
| 69 | + * .setDefaultRequestConfig(requestConfig) |
| 70 | + * .setMaxConnPerRoute(20) |
| 71 | + * .setMaxConnTotal(50) |
| 72 | + * .build(); |
| 73 | + * httpClient.start(); |
| 74 | + * }</pre> |
| 75 | + * <p> |
| 76 | + * |
| 77 | + * @param <T> |
| 78 | + */ |
| 79 | +public class ObservableHttp<T> { |
| 80 | + |
| 81 | + private final OnSubscribeFunc<T> onSubscribe; |
| 82 | + |
| 83 | + private ObservableHttp(OnSubscribeFunc<T> onSubscribe) { |
| 84 | + this.onSubscribe = onSubscribe; |
| 85 | + } |
| 86 | + |
| 87 | + private static <T> ObservableHttp<T> create(OnSubscribeFunc<T> onSubscribe) { |
| 88 | + return new ObservableHttp<T>(onSubscribe); |
| 89 | + } |
| 90 | + |
| 91 | + public Observable<T> toObservable() { |
| 92 | + return Observable.create(new OnSubscribeFunc<T>() { |
| 93 | + |
| 94 | + @Override |
| 95 | + public Subscription onSubscribe(Observer<? super T> observer) { |
| 96 | + return onSubscribe.onSubscribe(observer); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + public static ObservableHttp<ObservableHttpResponse> createGet(String uri, final HttpAsyncClient client) { |
| 102 | + return createRequest(HttpAsyncMethods.createGet(uri), client); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Execute request using {@link HttpAsyncRequestProducer} to define HTTP Method, URI and payload (if applicable). |
| 107 | + * <p> |
| 108 | + * If the response is chunked (or flushed progressively such as with <i>text/event-stream</i> <a href="http://www.w3.org/TR/2009/WD-eventsource-20091029/">Server-Sent Events</a>) this will call |
| 109 | + * {@link Observer#onNext} multiple times. |
| 110 | + * <p> |
| 111 | + * Use {@code HttpAsyncMethods.create* } factory methods to create {@link HttpAsyncRequestProducer} instances. |
| 112 | + * <p> |
| 113 | + * A client can be retrieved like this: |
| 114 | + * <p> |
| 115 | + * <pre> {@code CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); } </pre> </p> |
| 116 | + * <p> |
| 117 | + * A client with custom configurations can be created like this: |
| 118 | + * </p> |
| 119 | + * <pre> {@code |
| 120 | + * final RequestConfig requestConfig = RequestConfig.custom() |
| 121 | + * .setSocketTimeout(3000) |
| 122 | + * .setConnectTimeout(3000).build(); |
| 123 | + * final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom() |
| 124 | + * .setDefaultRequestConfig(requestConfig) |
| 125 | + * .setMaxConnPerRoute(20) |
| 126 | + * .setMaxConnTotal(50) |
| 127 | + * .build(); |
| 128 | + * httpclient.start(); |
| 129 | + * }</pre> |
| 130 | + * |
| 131 | + * |
| 132 | + * @param requestProducer |
| 133 | + * @param client |
| 134 | + * @return |
| 135 | + */ |
| 136 | + public static ObservableHttp<ObservableHttpResponse> createRequest(final HttpAsyncRequestProducer requestProducer, final HttpAsyncClient client) { |
| 137 | + |
| 138 | + return ObservableHttp.create(new OnSubscribeFunc<ObservableHttpResponse>() { |
| 139 | + |
| 140 | + @Override |
| 141 | + public Subscription onSubscribe(final Observer<? super ObservableHttpResponse> observer) { |
| 142 | + |
| 143 | + final CompositeSubscription parentSubscription = new CompositeSubscription(); |
| 144 | + |
| 145 | + // return a Subscription that wraps the Future so it can be cancelled |
| 146 | + parentSubscription.add(Subscriptions.create(client.execute(requestProducer, new ResponseConsumerDelegate(observer, parentSubscription), |
| 147 | + new FutureCallback<HttpResponse>() { |
| 148 | + |
| 149 | + @Override |
| 150 | + public void completed(HttpResponse result) { |
| 151 | + observer.onCompleted(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void failed(Exception ex) { |
| 156 | + observer.onError(ex); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void cancelled() { |
| 161 | + observer.onCompleted(); |
| 162 | + } |
| 163 | + |
| 164 | + }))); |
| 165 | + |
| 166 | + return parentSubscription; |
| 167 | + } |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments