├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── rx │ └── apache │ └── http │ ├── ObservableHttp.java │ ├── ObservableHttpResponse.java │ └── consumers │ ├── ExpandableByteBuffer.java │ ├── ResponseConsumerBasic.java │ ├── ResponseConsumerDelegate.java │ ├── ResponseConsumerEventStream.java │ └── ResponseDelegate.java └── test └── java └── rx └── apache └── http └── examples └── ExampleObservableHttp.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.so 9 | 10 | # Packages # 11 | ############ 12 | # it's better to unpack these files and commit the raw source 13 | # git has its own built in compression methods 14 | *.7z 15 | *.dmg 16 | *.gz 17 | *.iso 18 | *.jar 19 | *.rar 20 | *.tar 21 | *.zip 22 | 23 | # Logs and databases # 24 | ###################### 25 | *.log 26 | 27 | # OS generated files # 28 | ###################### 29 | .DS_Store* 30 | ehthumbs.db 31 | Icon? 32 | Thumbs.db 33 | 34 | # Editor Files # 35 | ################ 36 | *~ 37 | *.swp 38 | 39 | # Gradle Files # 40 | ################ 41 | .gradle 42 | .gradletasknamecache 43 | .m2 44 | 45 | # Build output directies 46 | target/ 47 | build/ 48 | 49 | # IntelliJ specific files/directories 50 | out 51 | .idea 52 | *.ipr 53 | *.iws 54 | *.iml 55 | atlassian-ide-plugin.xml 56 | 57 | # Eclipse specific files/directories 58 | .classpath 59 | .project 60 | .settings 61 | .metadata 62 | bin/ 63 | 64 | # NetBeans specific files/directories 65 | .nbattrs 66 | /.nb-gradle/profiles/private/ 67 | .nb-gradle-properties 68 | 69 | # Scala build 70 | *.cache 71 | /.nb-gradle/private/ 72 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | jdk: 4 | - oraclejdk7 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to RxJava 2 | 3 | If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request (on a branch other than `master` or `gh-pages`). 4 | 5 | When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. 6 | 7 | ## License 8 | 9 | By contributing your code, you agree to license your contribution under the terms of the APLv2: https://github.com/ReactiveX/RxJava/blob/master/LICENSE 10 | 11 | All files are released with the Apache 2.0 license. 12 | 13 | If you are adding a new file it should have a header like this: 14 | 15 | ``` 16 | /** 17 | * Copyright 2014 Netflix, Inc. 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); 20 | * you may not use this file except in compliance with the License. 21 | * You may obtain a copy of the License at 22 | * 23 | * http://www.apache.org/licenses/LICENSE-2.0 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, 27 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 28 | * See the License for the specific language governing permissions and 29 | * limitations under the License. 30 | */ 31 | ``` 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2012 Netflix, Inc. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rxjava-apache-http 2 | 3 | Observable API for Apache [HttpAsyncClient](http://hc.apache.org/httpcomponents-asyncclient-dev/) 4 | 5 | It is aware of Content-Type `text/event-stream` and will stream each event via `Observer.onNext`. 6 | 7 | Other Content-Types will be returned as a single call to `Observer.onNext`. 8 | 9 | Main Classes: 10 | 11 | - [ObservableHttp](https://github.com/Netflix/RxJava/blob/master/rxjava-contrib/rxjava-apache-http/src/main/java/rx/apache/http/ObservableHttp.java) 12 | - [ObservableHttpResponse](https://github.com/Netflix/RxJava/blob/master/rxjava-contrib/rxjava-apache-http/src/main/java/rx/apache/http/ObservableHttpResponse.java) 13 | 14 | 15 | # Binaries 16 | 17 | Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Ccom.netflix.rxjava). 18 | 19 | Example for [Maven](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxjava-apache-http%22): 20 | 21 | ```xml 22 | 23 | com.netflix.rxjava 24 | rxjava-apache-http 25 | x.y.z 26 | 27 | ``` 28 | 29 | and for Ivy: 30 | 31 | ```xml 32 | 33 | ``` 34 | 35 | # Sample Usage 36 | 37 | ### Create a Request 38 | 39 | ```java 40 | ObservableHttp.createGet("http://www.wikipedia.com", httpClient).toObservable(); 41 | ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), httpClient).toObservable(); 42 | ``` 43 | 44 | ### Http Client 45 | 46 | A basic default client: 47 | 48 | ```java 49 | CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault(); 50 | ``` 51 | 52 | or a custom client with configuration options: 53 | 54 | ```java 55 | final RequestConfig requestConfig = RequestConfig.custom() 56 | .setSocketTimeout(3000) 57 | .setConnectTimeout(500).build(); 58 | final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom() 59 | .setDefaultRequestConfig(requestConfig) 60 | .setMaxConnPerRoute(20) 61 | .setMaxConnTotal(50) 62 | .build(); 63 | ``` 64 | 65 | ### Normal Http GET 66 | 67 | Execute a request and transform the `byte[]` reponse to a `String`: 68 | 69 | ```groovy 70 | ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), client) 71 | .toObservable() 72 | .flatMap({ ObservableHttpResponse response -> 73 | return response.getContent().map({ byte[] bb -> 74 | return new String(bb); 75 | }); 76 | }) 77 | .toBlockingObservable() 78 | .forEach({ String resp -> 79 | // this will be invoked once with the response 80 | println(resp); 81 | }); 82 | ``` 83 | 84 | ### Streaming Http GET with [Server-Sent Events (text/event-stream)](http://www.w3.org/TR/eventsource/) Response 85 | 86 | Execute a request and transform the `byte[]` response of each event to a `String`: 87 | 88 | ```groovy 89 | ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://hostname/event.stream"), client) 90 | .toObservable() 91 | .flatMap({ ObservableHttpResponse response -> 92 | return response.getContent().map({ byte[] bb -> 93 | return new String(bb); 94 | }); 95 | }) 96 | .toBlockingObservable() 97 | .forEach({ String resp -> 98 | // this will be invoked for each event 99 | println(resp); 100 | }); 101 | ``` 102 | 103 | An example event-stream is from [Hystrix](https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-metrics-event-stream) used for streaming metrics. An [example webapp](https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp) can be used to test. 104 | 105 | Output looks like: 106 | 107 | ``` 108 | data: {"type":"HystrixCommand","name":"CreditCardCommand","group":"CreditCard","currentTime":1379823924934,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountCollapsedRequests":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackFailure":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":3000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1} 109 | data: {"type":"HystrixCommand","name":"GetPaymentInformationCommand","group":"PaymentInformation","currentTime":1379823924934,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountCollapsedRequests":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackFailure":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1} 110 | ``` 111 | 112 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { jcenter() } 3 | dependencies { classpath 'com.netflix.nebula:gradle-rxjava-project-plugin:1.12.+' } 4 | } 5 | 6 | apply plugin: 'rxjava-project' 7 | 8 | dependencies { 9 | compile 'io.reactivex:rxjava:1.0.+' 10 | compile 'org.apache.httpcomponents:httpclient:4.3' 11 | compile 'org.apache.httpcomponents:httpcore-nio:4.3' 12 | compile 'org.apache.httpcomponents:httpasyncclient:4.0' 13 | } 14 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | version=1.0.0-RC1-SNAPSHOT 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ReactiveX/RxApacheHttp/7494169cdc1199f11ca9744f18c28e6fcb694a6e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Feb 05 12:05:54 CET 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name='rxapache-http' 2 | -------------------------------------------------------------------------------- /src/main/java/rx/apache/http/ObservableHttp.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 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 | import org.apache.http.protocol.BasicHttpContext; 25 | import org.apache.http.protocol.HttpContext; 26 | 27 | import rx.Observable; 28 | import rx.Observable.OnSubscribe; 29 | import rx.Observer; 30 | import rx.Subscriber; 31 | import rx.apache.http.consumers.ResponseConsumerDelegate; 32 | import rx.subscriptions.CompositeSubscription; 33 | import rx.subscriptions.Subscriptions; 34 | 35 | /** 36 | * An {@link Observable} interface to Apache {@link HttpAsyncClient}. 37 | *

38 | * The initial {@link HttpResponse} is returned via {@link Observer#onNext} wrapped in a {@link ObservableHttpResponse}. 39 | *

40 | * The content stream is retrieved from {@link ObservableHttpResponse#getContent()}. 41 | *

42 | * It is aware of Content-Type text/event-stream and will stream each event via {@link Observer#onNext}. 43 | *

44 | * Other Content-Types will be returned as a single call to {@link Observer#onNext}. 45 | *

46 | * Examples: 47 | *

48 | *

 {@code
 49 |  * ObservableHttp.createGet("http://www.wikipedia.com", httpClient).toObservable();
 50 |  * } 
51 | *

52 | *

 {@code
 53 |  * ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), httpClient).toObservable();
 54 |  * } 
55 | * 56 | * An {@link HttpClient} can be created like this: 57 | * 58 | *
 {@code
 59 |  * CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
 60 |  * httpClient.start(); // start it
 61 |  * httpClient.stop(); // stop it
 62 |  * } 
63 | *

64 | * A client with custom configurations can be created like this: 65 | *

66 | *
 {@code
 67 |  * final RequestConfig requestConfig = RequestConfig.custom()
 68 |  *     .setSocketTimeout(1000)
 69 |  *     .setConnectTimeout(200).build();
 70 |  * final CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom()
 71 |  *     .setDefaultRequestConfig(requestConfig)
 72 |  *     .setMaxConnPerRoute(20)
 73 |  *     .setMaxConnTotal(50)
 74 |  *     .build();
 75 |  * httpClient.start();
 76 |  * }
77 | *

78 | * 79 | * @param 80 | */ 81 | public class ObservableHttp { 82 | 83 | private final OnSubscribe onSubscribe; 84 | 85 | private ObservableHttp(OnSubscribe onSubscribe) { 86 | this.onSubscribe = onSubscribe; 87 | } 88 | 89 | private static ObservableHttp create(OnSubscribe onSubscribe) { 90 | return new ObservableHttp(onSubscribe); 91 | } 92 | 93 | public Observable toObservable() { 94 | return Observable.create(new OnSubscribe() { 95 | 96 | @Override 97 | public void call(Subscriber observer) { 98 | onSubscribe.call(observer); 99 | } 100 | }); 101 | } 102 | 103 | public static ObservableHttp createGet(String uri, final HttpAsyncClient client) { 104 | return createRequest(HttpAsyncMethods.createGet(uri), client); 105 | } 106 | 107 | /** 108 | * Execute request using {@link HttpAsyncRequestProducer} to define HTTP Method, URI and payload (if applicable). 109 | *

110 | * If the response is chunked (or flushed progressively such as with text/event-stream Server-Sent Events) this will call 111 | * {@link Observer#onNext} multiple times. 112 | *

113 | * Use {@code HttpAsyncMethods.create* } factory methods to create {@link HttpAsyncRequestProducer} instances. 114 | *

115 | * A client can be retrieved like this: 116 | *

117 | *

 {@code      CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); } 
118 | *

119 | * A client with custom configurations can be created like this: 120 | *

121 | *
 {@code
122 |      * final RequestConfig requestConfig = RequestConfig.custom()
123 |      *     .setSocketTimeout(3000)
124 |      *     .setConnectTimeout(3000).build();
125 |      * final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
126 |      *     .setDefaultRequestConfig(requestConfig)
127 |      *     .setMaxConnPerRoute(20)
128 |      *     .setMaxConnTotal(50)
129 |      *     .build();
130 |      * httpclient.start();
131 |      * }
132 | * 133 | * 134 | * @param requestProducer 135 | * @param client 136 | * @return the observable HTTP response stream 137 | */ 138 | public static ObservableHttp createRequest(final HttpAsyncRequestProducer requestProducer, final HttpAsyncClient client) { 139 | return createRequest(requestProducer, client, new BasicHttpContext()); 140 | } 141 | 142 | /** 143 | * Execute request using {@link HttpAsyncRequestProducer} to define HTTP Method, URI and payload (if applicable). 144 | *

145 | * If the response is chunked (or flushed progressively such as with text/event-stream Server-Sent Events) this will call 146 | * {@link Observer#onNext} multiple times. 147 | *

148 | * Use {@code HttpAsyncMethods.create* } factory methods to create {@link HttpAsyncRequestProducer} instances. 149 | *

150 | * A client can be retrieved like this: 151 | *

152 | *

 {@code      CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); } 
153 | *

154 | * A client with custom configurations can be created like this: 155 | *

156 | *
 {@code
157 |      * final RequestConfig requestConfig = RequestConfig.custom()
158 |      *     .setSocketTimeout(3000)
159 |      *     .setConnectTimeout(3000).build();
160 |      * final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
161 |      *     .setDefaultRequestConfig(requestConfig)
162 |      *     .setMaxConnPerRoute(20)
163 |      *     .setMaxConnTotal(50)
164 |      *     .build();
165 |      * httpclient.start();
166 |      * }
167 | * 168 | * 169 | * @param requestProducer 170 | * @param client 171 | * @param context The HttpContext 172 | * @return the observable HTTP response stream 173 | */ 174 | public static ObservableHttp createRequest(final HttpAsyncRequestProducer requestProducer, final HttpAsyncClient client, final HttpContext context) { 175 | 176 | return ObservableHttp.create(new OnSubscribe() { 177 | 178 | @Override 179 | public void call(final Subscriber observer) { 180 | 181 | final CompositeSubscription parentSubscription = new CompositeSubscription(); 182 | observer.add(parentSubscription); 183 | 184 | // return a Subscription that wraps the Future so it can be cancelled 185 | parentSubscription.add(Subscriptions.from(client.execute(requestProducer, new ResponseConsumerDelegate(observer, parentSubscription), 186 | context, new FutureCallback() { 187 | 188 | @Override 189 | public void completed(HttpResponse result) { 190 | observer.onCompleted(); 191 | } 192 | 193 | @Override 194 | public void failed(Exception ex) { 195 | observer.onError(ex); 196 | } 197 | 198 | @Override 199 | public void cancelled() { 200 | observer.onCompleted(); 201 | } 202 | 203 | }))); 204 | } 205 | }); 206 | } 207 | 208 | } 209 | -------------------------------------------------------------------------------- /src/main/java/rx/apache/http/ObservableHttpResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 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 | 20 | import rx.Observable; 21 | 22 | /** 23 | * The {@link HttpResponse} for the entire request and accessor to {@link Observable} of the content stream. 24 | */ 25 | public class ObservableHttpResponse { 26 | 27 | private final HttpResponse response; 28 | private final Observable contentSubscription; 29 | 30 | public ObservableHttpResponse(HttpResponse response, Observable contentSubscription) { 31 | this.response = response; 32 | this.contentSubscription = contentSubscription; 33 | } 34 | 35 | /** 36 | * The {@link HttpResponse} returned by the Apache client at the beginning of the response. 37 | * 38 | * @return {@link HttpResponse} with HTTP status codes, headers, etc 39 | */ 40 | public HttpResponse getResponse() { 41 | return response; 42 | } 43 | 44 | /** 45 | * If the response is not chunked then only a single array will be returned. If chunked then multiple arrays. 46 | */ 47 | public Observable getContent() { 48 | return contentSubscription; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/rx/apache/http/consumers/ExpandableByteBuffer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 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.consumers; 17 | 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | 21 | import org.apache.http.nio.util.ExpandableBuffer; 22 | import org.apache.http.nio.util.HeapByteBufferAllocator; 23 | 24 | class ExpandableByteBuffer extends ExpandableBuffer { 25 | public ExpandableByteBuffer(int size) { 26 | super(size, HeapByteBufferAllocator.INSTANCE); 27 | } 28 | 29 | public ExpandableByteBuffer() { 30 | super(4 * 1024, HeapByteBufferAllocator.INSTANCE); 31 | } 32 | 33 | public void addByte(byte b) { 34 | if (this.buffer.remaining() == 0) { 35 | expand(); 36 | } 37 | this.buffer.put(b); 38 | } 39 | 40 | public boolean hasContent() { 41 | return this.buffer.position() > 0; 42 | } 43 | 44 | public byte[] getBytes() { 45 | byte[] data = new byte[this.buffer.position()]; 46 | this.buffer.position(0); 47 | this.buffer.get(data); 48 | return data; 49 | } 50 | 51 | public void reset() { 52 | clear(); 53 | } 54 | 55 | public void consumeInputStream(InputStream content) throws IOException { 56 | try { 57 | int b = -1; 58 | while ((b = content.read()) != -1) { 59 | addByte((byte) b); 60 | } 61 | } finally { 62 | content.close(); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /src/main/java/rx/apache/http/consumers/ResponseConsumerBasic.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 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.consumers; 17 | 18 | import java.io.IOException; 19 | 20 | import org.apache.http.HttpEntity; 21 | import org.apache.http.HttpException; 22 | import org.apache.http.HttpResponse; 23 | import org.apache.http.entity.ContentType; 24 | import org.apache.http.nio.ContentDecoder; 25 | import org.apache.http.nio.IOControl; 26 | import org.apache.http.nio.protocol.BasicAsyncResponseConsumer; 27 | import org.apache.http.protocol.HttpContext; 28 | 29 | import rx.Observable; 30 | import rx.Observable.OnSubscribe; 31 | import rx.Observer; 32 | import rx.Subscriber; 33 | import rx.apache.http.ObservableHttpResponse; 34 | import rx.subscriptions.CompositeSubscription; 35 | 36 | /** 37 | * Delegate wrapper around {@link BasicAsyncResponseConsumer} so it works with {@link ResponseConsumerDelegate} 38 | */ 39 | class ResponseConsumerBasic extends BasicAsyncResponseConsumer implements ResponseDelegate { 40 | 41 | private final Observer observer; 42 | private final CompositeSubscription parentSubscription; 43 | 44 | public ResponseConsumerBasic(final Observer observer, CompositeSubscription parentSubscription) { 45 | this.observer = observer; 46 | this.parentSubscription = parentSubscription; 47 | } 48 | 49 | @Override 50 | public void _onResponseReceived(HttpResponse response) throws HttpException, IOException { 51 | onResponseReceived(response); 52 | } 53 | 54 | @Override 55 | public void _onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException { 56 | if (parentSubscription.isUnsubscribed()) { 57 | ioctrl.shutdown(); 58 | } 59 | onContentReceived(decoder, ioctrl); 60 | } 61 | 62 | @Override 63 | public void _onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException { 64 | onEntityEnclosed(entity, contentType); 65 | } 66 | 67 | @Override 68 | public HttpResponse _buildResult(HttpContext context) throws Exception { 69 | final HttpResponse response = buildResult(context); 70 | 71 | Observable contentObservable = Observable.create(new OnSubscribe() { 72 | 73 | @Override 74 | public void call(Subscriber o) { 75 | o.add(parentSubscription); 76 | long length = response.getEntity().getContentLength(); 77 | if (length > Integer.MAX_VALUE) { 78 | o.onError(new IllegalStateException("Content Length too large for a byte[] => " + length)); 79 | } else { 80 | ExpandableByteBuffer buf = new ExpandableByteBuffer((int) length); 81 | try { 82 | buf.consumeInputStream(response.getEntity().getContent()); 83 | o.onNext(buf.getBytes()); 84 | o.onCompleted(); 85 | } catch (Throwable e) { 86 | o.onError(e); 87 | } 88 | } 89 | } 90 | }); 91 | 92 | observer.onNext(new ObservableHttpResponse(response, contentObservable)); 93 | return response; 94 | } 95 | 96 | @Override 97 | public void _releaseResources() { 98 | releaseResources(); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/rx/apache/http/consumers/ResponseConsumerDelegate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 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.consumers; 17 | 18 | import java.io.IOException; 19 | 20 | import org.apache.http.Header; 21 | import org.apache.http.HttpEntity; 22 | import org.apache.http.HttpException; 23 | import org.apache.http.HttpResponse; 24 | import org.apache.http.entity.ContentType; 25 | import org.apache.http.nio.ContentDecoder; 26 | import org.apache.http.nio.IOControl; 27 | import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer; 28 | import org.apache.http.protocol.HttpContext; 29 | 30 | import rx.Observer; 31 | import rx.apache.http.ObservableHttpResponse; 32 | import rx.subscriptions.CompositeSubscription; 33 | 34 | /** 35 | * AbstractAsyncResponseConsumer that chooses different implementations based on return headers. 36 | *

37 | *

    38 | *
  • Content-Type:text/event-stream == {@link ResponseConsumerEventStream}
  • 39 | *
  • All others == {@link ResponseConsumerBasic}
  • 40 | *
41 | */ 42 | public class ResponseConsumerDelegate extends AbstractAsyncResponseConsumer { 43 | 44 | private volatile ResponseDelegate consumer = null; 45 | final Observer observer; 46 | final CompositeSubscription subscription; 47 | 48 | public ResponseConsumerDelegate(final Observer observer, CompositeSubscription subscription) { 49 | this.observer = observer; 50 | this.subscription = subscription; 51 | } 52 | 53 | @Override 54 | protected void onResponseReceived(HttpResponse response) throws HttpException, IOException { 55 | // when we receive the response with headers we evaluate what type of consumer we want 56 | if (responseIsStreamLike(response)) { 57 | consumer = new ResponseConsumerEventStream(observer, subscription); 58 | } else { 59 | consumer = new ResponseConsumerBasic(observer, subscription); 60 | } 61 | // forward 'response' to actual consumer 62 | consumer._onResponseReceived(response); 63 | } 64 | 65 | private boolean responseIsStreamLike(HttpResponse response) { 66 | final Header contentType = response.getFirstHeader("Content-Type"); 67 | // use 'contains' instead of equals since Content-Type can contain additional information 68 | // such as charset ... see here: http://www.w3.org/International/O-HTTP-charset 69 | if (contentType != null && contentType.getValue().contains("text/event-stream")) { 70 | return true; 71 | } 72 | final Header transferEncoding = response.getFirstHeader("Transfer-Encoding"); 73 | if (transferEncoding != null && transferEncoding.getValue().equals("chunked")) { 74 | return true; 75 | } 76 | return false; 77 | } 78 | 79 | @Override 80 | protected void onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException { 81 | consumer._onContentReceived(decoder, ioctrl); 82 | } 83 | 84 | @Override 85 | protected void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException { 86 | consumer._onEntityEnclosed(entity, contentType); 87 | } 88 | 89 | @Override 90 | protected HttpResponse buildResult(HttpContext context) throws Exception { 91 | return consumer._buildResult(context); 92 | } 93 | 94 | @Override 95 | protected void releaseResources() { 96 | if (consumer != null) { 97 | consumer._releaseResources(); 98 | } 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/rx/apache/http/consumers/ResponseConsumerEventStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 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.consumers; 17 | 18 | import java.io.IOException; 19 | import java.nio.ByteBuffer; 20 | 21 | import org.apache.http.HttpEntity; 22 | import org.apache.http.HttpException; 23 | import org.apache.http.HttpResponse; 24 | import org.apache.http.entity.ContentType; 25 | import org.apache.http.nio.ContentDecoder; 26 | import org.apache.http.nio.IOControl; 27 | import org.apache.http.nio.client.methods.AsyncByteConsumer; 28 | import org.apache.http.nio.protocol.HttpAsyncResponseConsumer; 29 | import org.apache.http.protocol.HttpContext; 30 | 31 | import rx.Observable; 32 | import rx.Observable.OnSubscribe; 33 | import rx.Observer; 34 | import rx.Subscriber; 35 | import rx.apache.http.ObservableHttpResponse; 36 | import rx.subjects.PublishSubject; 37 | import rx.subscriptions.CompositeSubscription; 38 | 39 | /** 40 | * {@link HttpAsyncResponseConsumer} for Content-Type:text/event-stream 41 | *

42 | * It will emit a byte[] via {@link Observer#onNext} for each non-empty line. 43 | */ 44 | class ResponseConsumerEventStream extends AsyncByteConsumer implements ResponseDelegate { 45 | 46 | private final Observer observer; 47 | private final PublishSubject contentSubject = PublishSubject. create(); 48 | private final CompositeSubscription parentSubscription; 49 | 50 | public ResponseConsumerEventStream(final Observer observer, CompositeSubscription parentSubscription) { 51 | this.observer = observer; 52 | this.parentSubscription = parentSubscription; 53 | } 54 | 55 | final ExpandableByteBuffer dataBuffer = new ExpandableByteBuffer(); 56 | 57 | @Override 58 | public void _onResponseReceived(HttpResponse response) throws HttpException, IOException { 59 | onResponseReceived(response); 60 | } 61 | 62 | @Override 63 | protected void onByteReceived(ByteBuffer buf, IOControl ioctrl) throws IOException { 64 | if (parentSubscription.isUnsubscribed()) { 65 | ioctrl.shutdown(); 66 | } 67 | while (buf.position() < buf.limit()) { 68 | byte b = buf.get(); 69 | if (b == 10 || b == 13) { 70 | if (dataBuffer.hasContent()) { 71 | contentSubject.onNext(dataBuffer.getBytes()); 72 | } 73 | dataBuffer.reset(); 74 | } else { 75 | dataBuffer.addByte(b); 76 | } 77 | } 78 | } 79 | 80 | @Override 81 | protected void onResponseReceived(HttpResponse response) throws HttpException, IOException { 82 | 83 | // wrap the contentSubject so we can chain the Subscription between parent and child 84 | Observable contentObservable = Observable.create(new OnSubscribe() { 85 | 86 | @Override 87 | public void call(Subscriber observer) { 88 | observer.add(parentSubscription); 89 | parentSubscription.add(contentSubject.subscribe(observer)); 90 | } 91 | }); 92 | observer.onNext(new ObservableHttpResponse(response, contentObservable)); 93 | } 94 | 95 | @Override 96 | protected HttpResponse buildResult(HttpContext context) throws Exception { 97 | // streaming results, so not returning anything here 98 | return null; 99 | } 100 | 101 | @Override 102 | public void _onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException { 103 | onContentReceived(decoder, ioctrl); 104 | } 105 | 106 | @Override 107 | public void _onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException { 108 | onEntityEnclosed(entity, contentType); 109 | } 110 | 111 | @Override 112 | public HttpResponse _buildResult(HttpContext context) throws Exception { 113 | return buildResult(context); 114 | } 115 | 116 | @Override 117 | public void _releaseResources() { 118 | releaseResources(); 119 | } 120 | 121 | } -------------------------------------------------------------------------------- /src/main/java/rx/apache/http/consumers/ResponseDelegate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 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.consumers; 17 | 18 | import java.io.IOException; 19 | 20 | import org.apache.http.HttpEntity; 21 | import org.apache.http.HttpException; 22 | import org.apache.http.HttpResponse; 23 | import org.apache.http.entity.ContentType; 24 | import org.apache.http.nio.ContentDecoder; 25 | import org.apache.http.nio.IOControl; 26 | import org.apache.http.nio.protocol.HttpAsyncResponseConsumer; 27 | import org.apache.http.protocol.HttpContext; 28 | 29 | /** 30 | * Delegate methods for getting access to protected methods. 31 | */ 32 | abstract interface ResponseDelegate extends HttpAsyncResponseConsumer { 33 | 34 | public void _onResponseReceived(HttpResponse response) throws HttpException, IOException; 35 | 36 | public void _onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException; 37 | 38 | public void _onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException; 39 | 40 | public HttpResponse _buildResult(HttpContext context) throws Exception; 41 | 42 | public void _releaseResources(); 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/rx/apache/http/examples/ExampleObservableHttp.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 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.examples; 17 | 18 | import java.io.IOException; 19 | import java.net.URISyntaxException; 20 | 21 | import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; 22 | import org.apache.http.impl.nio.client.HttpAsyncClients; 23 | import org.apache.http.nio.client.HttpAsyncClient; 24 | import org.apache.http.nio.client.methods.HttpAsyncMethods; 25 | 26 | import rx.Observable; 27 | import rx.apache.http.ObservableHttp; 28 | import rx.apache.http.ObservableHttpResponse; 29 | import rx.functions.Action1; 30 | import rx.functions.Func1; 31 | 32 | public class ExampleObservableHttp { 33 | 34 | public static void main(String args[]) { 35 | CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); 36 | 37 | // final RequestConfig requestConfig = RequestConfig.custom() 38 | // .setSocketTimeout(3000) 39 | // .setConnectTimeout(3000).build(); 40 | // final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom() 41 | // .setDefaultRequestConfig(requestConfig) 42 | // .setMaxConnPerRoute(20) 43 | // .setMaxConnTotal(50) 44 | // .build(); 45 | 46 | try { 47 | httpclient.start(); 48 | executeViaObservableHttpWithForEach(httpclient); 49 | executeStreamingViaObservableHttpWithForEach(httpclient); 50 | } catch (Exception e) { 51 | e.printStackTrace(); 52 | } finally { 53 | try { 54 | httpclient.close(); 55 | } catch (IOException e1) { 56 | e1.printStackTrace(); 57 | } 58 | } 59 | 60 | CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault(); 61 | ObservableHttp.createGet("http://www.wikipedia.com", httpClient).toObservable(); 62 | ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), httpClient).toObservable(); 63 | } 64 | 65 | protected static void executeViaObservableHttpWithForEach(final HttpAsyncClient client) throws URISyntaxException, IOException, InterruptedException { 66 | System.out.println("---- executeViaObservableHttpWithForEach"); 67 | ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), client) 68 | .toObservable() 69 | .flatMap(new Func1>() { 70 | 71 | @Override 72 | public Observable call(ObservableHttpResponse response) { 73 | return response.getContent().map(new Func1() { 74 | 75 | @Override 76 | public String call(byte[] bb) { 77 | return new String(bb); 78 | } 79 | 80 | }); 81 | } 82 | }) 83 | .toBlocking() 84 | .forEach(new Action1() { 85 | 86 | @Override 87 | public void call(String resp) { 88 | System.out.println(resp); 89 | } 90 | }); 91 | } 92 | 93 | protected static void executeStreamingViaObservableHttpWithForEach(final HttpAsyncClient client) throws URISyntaxException, IOException, InterruptedException { 94 | System.out.println("---- executeStreamingViaObservableHttpWithForEach"); 95 | // URL against https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp 96 | // More information at https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-metrics-event-stream 97 | ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://localhost:8989/hystrix-examples-webapp/hystrix.stream"), client) 98 | .toObservable() 99 | .flatMap(new Func1>() { 100 | 101 | @Override 102 | public Observable call(ObservableHttpResponse response) { 103 | return response.getContent().map(new Func1() { 104 | 105 | @Override 106 | public String call(byte[] bb) { 107 | return new String(bb); 108 | } 109 | 110 | }); 111 | } 112 | }) 113 | .filter(new Func1() { 114 | 115 | @Override 116 | public Boolean call(String t1) { 117 | return !t1.startsWith(": ping"); 118 | } 119 | }) 120 | .take(3) 121 | .toBlocking() 122 | .forEach(new Action1() { 123 | 124 | @Override 125 | public void call(String resp) { 126 | System.out.println(resp); 127 | } 128 | }); 129 | } 130 | 131 | } 132 | --------------------------------------------------------------------------------