Skip to content

Commit

Permalink
Merge pull request Azure#244 from RikkiGibson/PagingSupport
Browse files Browse the repository at this point in the history
Paging support
  • Loading branch information
RikkiGibson authored Oct 2, 2017
2 parents 70373df + cf78a92 commit 95a25b7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import rx.Observable;
import rx.Observer;
import rx.Single;
import rx.exceptions.Exceptions;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.observables.SyncOnSubscribe;
Expand All @@ -26,6 +27,8 @@
import javax.net.ssl.SSLEngine;
import java.io.IOException;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -44,6 +47,7 @@
*/
public class RxNettyAdapter extends HttpClient {
private final List<ChannelHandlerConfig> handlerConfigs;
private final CookieHandler cookies = new CookieManager();

/**
* Creates RxNettyClient.
Expand All @@ -68,14 +72,17 @@ private SSLEngine getSSLEngine(String host) {
}

@Override
public Single<HttpResponse> sendRequestInternalAsync(HttpRequest request) {
public Single<HttpResponse> sendRequestInternalAsync(final HttpRequest request) {
Single<HttpResponse> result;

try {
final URI uri = new URI(request.url());

// Unfortunately necessary due to conflicting APIs
Map<String, List<String>> cookieHeaders = new HashMap<>();
Map<String, Set<Object>> rxnHeaders = new HashMap<>();
for (HttpHeader header : request.headers()) {
cookieHeaders.put(header.name(), Arrays.asList(request.headers().values(header.name())));
rxnHeaders.put(header.name(), Collections.<Object>singleton(header.value()));
}

Expand Down Expand Up @@ -104,9 +111,20 @@ public Single<HttpResponse> sendRequestInternalAsync(HttpRequest request) {
rxnClient = rxnClient.secure(getSSLEngine(uri.getHost()));
}

HttpClientRequest<ByteBuf, ByteBuf> rxnReq = rxnClient
Map<String, List<String>> requestCookies = cookies.get(uri, cookieHeaders);
Map<String, Iterable<Object>> rxnCookies = new HashMap<>();
for (Map.Entry<String, List<String>> entry : requestCookies.entrySet()) {
if (entry.getValue().size() != 0) {
List<Object> cookieValues = new ArrayList<>();
cookieValues.addAll(entry.getValue());
rxnCookies.put(entry.getKey(), cookieValues);
}
}

final HttpClientRequest<ByteBuf, ByteBuf> rxnReq = rxnClient
.createRequest(HttpMethod.valueOf(request.httpMethod()), uri.toASCIIString())
.addHeaders(rxnHeaders);
.addHeaders(rxnHeaders)
.addHeaders(rxnCookies);

Observable<HttpClientResponse<ByteBuf>> obsResponse = rxnReq;

Expand All @@ -117,10 +135,21 @@ public Single<HttpResponse> sendRequestInternalAsync(HttpRequest request) {
}
}


result = obsResponse
.map(new Func1<HttpClientResponse<ByteBuf>, HttpResponse>() {
@Override
public HttpResponse call(HttpClientResponse<ByteBuf> rxnRes) {
Map<String, List<String>> responseHeaders = new HashMap<>();
for (String headerName : rxnRes.getHeaderNames()) {
responseHeaders.put(headerName, rxnRes.getAllHeaderValues(headerName));
}
try {
cookies.put(uri, responseHeaders);
} catch (IOException e) {
throw Exceptions.propagate(e);
}

return new RxNettyResponse(rxnRes);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,21 @@ public UrlBuilder withHost(String host) {
* @return This UrlBuilder so that multiple setters can be chained together.
*/
public UrlBuilder withPath(String path) {
if (path != null && !path.startsWith("/")) {
path = "/" + path;
if (path != null) {
String[] parts = path.split("\\?");
this.path = parts[0];
if (parts.length > 1) {
String[] queryPairs = parts[1].split("&");
for (String queryPair : queryPairs) {
String[] nameAndValue = queryPair.split("=");
if (nameAndValue.length != 2) {
throw new IllegalArgumentException("Path contained malformed query: " + path);
}

withQueryParameter(nameAndValue[0], nameAndValue[1]);
}
}
}
this.path = path;
return this;
}

Expand Down Expand Up @@ -76,19 +87,25 @@ public UrlBuilder withQueryParameter(String queryParameterName, String queryPara
public String toString() {
final StringBuilder result = new StringBuilder();

if (scheme != null) {
result.append(scheme);
final boolean isAbsolutePath = path != null && (path.startsWith("http://") || path.startsWith("https://"));
if (!isAbsolutePath) {
if (scheme != null) {
result.append(scheme);

if (!scheme.endsWith("://")) {
result.append("://");
if (!scheme.endsWith("://")) {
result.append("://");
}
}
}

if (host != null) {
result.append(host);
if (host != null) {
result.append(host);
}
}

if (path != null) {
if (result.length() != 0 && !path.startsWith("/")) {
result.append('/');
}
result.append(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,33 @@ public void withSchemeAndHostAndPathAndTwoQueryParameters() {
.withPath("index.html");
assertEquals("http://www.example.com/index.html?A=B&C=D", builder.toString());
}

@Test
public void withAbsolutePath() {
final UrlBuilder builder = new UrlBuilder()
.withScheme("http")
.withHost("www.example.com")
.withPath("http://www.othersite.com");
assertEquals("http://www.othersite.com", builder.toString());
}

@Test
public void withQueryInPath() {
final UrlBuilder builder = new UrlBuilder()
.withScheme("http")
.withHost("www.example.com")
.withPath("mypath?thing=stuff")
.withQueryParameter("otherthing", "otherstuff");
assertEquals("http://www.example.com/mypath?thing=stuff&otherthing=otherstuff", builder.toString());
}

@Test
public void withAbsolutePathAndQuery() {
final UrlBuilder builder = new UrlBuilder()
.withScheme("http")
.withHost("www.example.com")
.withPath("http://www.othersite.com/mypath?thing=stuff")
.withQueryParameter("otherthing", "otherstuff");
assertEquals("http://www.othersite.com/mypath?thing=stuff&otherthing=otherstuff", builder.toString());
}
}

0 comments on commit 95a25b7

Please sign in to comment.