Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
maesenka committed Jun 25, 2019
2 parents eb7071f + 7a4d7e2 commit b30d918
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;

Expand Down Expand Up @@ -117,7 +118,10 @@ public String toString() {
public void addHeader(String header, String value) {
List<String> hv = new ArrayList<>();
hv.add(value);
this.getHeaders().put(header, hv);
this.request.getHeaders().add(header, value);
}



}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package be.wegenenverkeer.rxhttp;

/**
* Created by Karel Maesen, Geovise BVBA on 2019-06-25.
*/
public interface ClientRequestLogFormatter {

String toLogMessage(ClientRequest request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package be.wegenenverkeer.rxhttp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by Karel Maesen, Geovise BVBA on 2019-06-25.
*/
public class DefaultClientRequestLogFormatter implements ClientRequestLogFormatter {

private final List<String> headersToLog;
private final List<String> formParamNamesToLog;

@SuppressWarnings("unchecked")
public DefaultClientRequestLogFormatter() {
this(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
}

public DefaultClientRequestLogFormatter(List<String> headersToLog, List<String> formParamNamesToLog) {
this.headersToLog = headersToLog.stream().map( String::toLowerCase).collect(Collectors.toList());
this.formParamNamesToLog = formParamNamesToLog.stream().map(String::toLowerCase).collect(Collectors.toList());
}

@Override
public String toLogMessage(ClientRequest request) {
StringBuilder sb = new StringBuilder(request.getMethod())
.append(" ")
.append(request.getUrl());

List<String> headerLogs = headersToLog.stream().map(header ->
header + ":" + String.join("; ", request.unwrap().getHeaders().getAll(header))
).collect(Collectors.toList());

if (!headerLogs.isEmpty()) {
sb.append("\theaders:");
headerLogs.forEach(hl -> {
sb.append("\t").append(hl);
});
}

List<String> paramList = request.unwrap().getFormParams().stream()
.filter(fparam -> formParamNamesToLog.contains(fparam.getName().toLowerCase()))
.map(fparam -> fparam.getName() + ":" + fparam.getValue())
.collect(Collectors.toList());

if (!paramList.isEmpty()) {
sb.append("\tformParams:");
paramList.forEach(pl -> {
sb.append("\t").append(pl);
});
}

return sb.toString();
}

}

Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package be.wegenenverkeer.rxhttp;

import be.wegenenverkeer.rxhttp.aws.AwsCredentialsProvider;
import be.wegenenverkeer.rxhttp.aws.AwsRegion;
import be.wegenenverkeer.rxhttp.aws.AwsService;
import be.wegenenverkeer.rxhttp.aws.AwsServiceEndPoint;
import be.wegenenverkeer.rxhttp.aws.AwsSignature4Signer;

import be.wegenenverkeer.rxhttp.aws.*;
import io.netty.handler.ssl.SslContext;
import org.asynchttpclient.*;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.Response;
import org.asynchttpclient.filter.RequestFilter;
import org.asynchttpclient.filter.ThrottleRequestFilter;
import org.slf4j.Logger;
Expand All @@ -19,18 +17,10 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.function.Function;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

import static be.wegenenverkeer.rxhttp.CompleteResponseHandler.withCompleteResponse;
import static be.wegenenverkeer.rxhttp.ServerResponse.wrap;
Expand All @@ -47,18 +37,20 @@ public class RxHttpClient {
final private AsyncHttpClient innerClient;
final private RestClientConfig config;
final private List<RequestSigner> requestSigners;
final private ClientRequestLogFormatter logFormatter;

protected RxHttpClient(AsyncHttpClient innerClient, RestClientConfig config, RequestSigner... requestSigners) {
protected RxHttpClient(AsyncHttpClient innerClient, RestClientConfig config, ClientRequestLogFormatter logFmt, RequestSigner... requestSigners) {
this.innerClient = innerClient;
this.config = config;
this.requestSigners = Collections.unmodifiableList(Arrays.asList(requestSigners));
this.logFormatter = logFmt;
}

/**
* * Executes a request and returns an Observable for the complete response.
*/
public <F> CompletableFuture<F> execute(ClientRequest request, Function<ServerResponse, F> transformer) {
logger.info("Sending Request: " + request.toString());
logger.info("Sending Request: " + toLogMessage(request));
//Note: we don't use Observable.toBlocking().toFuture()
//because we need a CompletableFuture so that interop with Scala is possible
final CompletableFuture<F> future = new CompletableFuture<>();
Expand Down Expand Up @@ -105,7 +97,7 @@ public void onThrowable(Throwable t) {
*/
public <F> Observable<F> executeToCompletion(ClientRequest request, Function<ServerResponse, F> transformer) {
return Observable.defer(() -> {
logger.info("Sending Request: " + request.toString());
logger.info("Sending Request: " + toLogMessage(request));
AsyncSubject<F> subject = AsyncSubject.create();
innerClient.executeRequest(request.unwrap(), new AsyncCompletionHandlerWrapper<>(subject, transformer));
return subject;
Expand Down Expand Up @@ -199,6 +191,11 @@ public ClientRequestBuilder requestBuilder() {
return new ClientRequestBuilder(this);
}

protected String toLogMessage(ClientRequest request) {
return this.logFormatter.toLogMessage(request);
}


AsyncHttpClient inner() {
return this.innerClient;
}
Expand Down Expand Up @@ -277,6 +274,8 @@ static public class Builder {
private AwsServiceEndPoint awsServiceEndPoint;
private AwsCredentialsProvider awsCredentialsProvider;
private List<RequestSigner> requestSigners = new LinkedList<>();
private List<String> headersToLog = new ArrayList<>();
private ArrayList<String> formParmsToLog = new ArrayList<>();

public RxHttpClient build() {
addRestClientConfigsToConfigBuilder();
Expand All @@ -299,7 +298,9 @@ public RxHttpClient build() {
requestSigners.add(new AwsSignature4Signer(this.awsServiceEndPoint, this.awsCredentialsProvider));
}

return new RxHttpClient(innerClient, rcConfig, requestSigners.toArray(new RequestSigner[0]));
ClientRequestLogFormatter logFmt = new DefaultClientRequestLogFormatter(headersToLog, formParmsToLog);

return new RxHttpClient(innerClient, rcConfig, logFmt, requestSigners.toArray(new RequestSigner[0]));
}

/**
Expand Down Expand Up @@ -420,6 +421,7 @@ public RxHttpClient.Builder setMaxConnections(int maxConnections) {
* @param allowPoolingConnections true if connection can be pooled by a ChannelPool
* @return a {@link RxHttpClient.Builder}
*/
@Deprecated
public RxHttpClient.Builder setAllowPoolingConnections(boolean allowPoolingConnections) {
configBuilder.setKeepAlive(allowPoolingConnections);
return this;
Expand Down Expand Up @@ -750,6 +752,7 @@ public RxHttpClient.Builder setFollowRedirect(boolean followRedirect) {
* @param disableUrlEncodingForBoundedRequests disables the url encoding if set to true
* @return this Builder
*/
@Deprecated
public RxHttpClient.Builder setDisableUrlEncodingForBoundedRequests(boolean disableUrlEncodingForBoundedRequests) {
configBuilder.setDisableUrlEncodingForBoundRequests(disableUrlEncodingForBoundedRequests);
return this;
Expand Down Expand Up @@ -855,6 +858,17 @@ public Builder setAwsCredentialsProvider(AwsCredentialsProvider provider) {
this.awsCredentialsProvider = provider;
return this;
}

public Builder logHeaders(List<String> headerNames) {
this.headersToLog = new ArrayList<>(headerNames);
return this;
}

public Builder logFormParams(List<String> formParameterNames) {
this.formParmsToLog = new ArrayList<>(formParameterNames);
return this;
}

}

static private class BuildValidation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void demonstrateComposableObservable() throws InterruptedException {
.setUrlRelativetoBase(contactUrl).build();
return client
.executeToCompletion(followUp, ServerResponse::getResponseBody)
.finallyDo(() -> LOGGER.info("ContactUrl " + contactUrl + " retrieved"));
.doAfterTerminate(() -> LOGGER.info("ContactUrl " + contactUrl + " retrieved"));
};

LOGGER.info("Creating Observable...");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package be.wegenenverkeer.designtests;

import be.wegenenverkeer.rxhttp.*;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import rx.Observable;
import rx.observers.TestSubscriber;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;

/**
* Tests that demonstrate processing a long and slow chunked response.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void resetServer() {
WireMock.resetToDefault();
}

@SuppressWarnings("unchecked")
public <V> List<V> items(V... v) {
return Arrays.asList(v);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package be.wegenenverkeer.rxhttp;

import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;

/**
* Created by Karel Maesen, Geovise BVBA on 2019-06-25.
*/
public class TestLoggerConf {

private RxHttpClient client = null;

@Test
public void testLoggerConfDefault(){
client = new RxHttpClient.Builder().setBaseUrl("http://foo.com").build();
try {
ClientRequest request = client.requestBuilder().setMethod("GET").setUrlRelativetoBase("/test").build();
assertEquals("GET http://foo.com/test", client.toLogMessage(request));
} finally {
client.close();
}
}

@Test
public void testLoggerConfWithHeader(){
client = new RxHttpClient.Builder()
.setBaseUrl("http://foo.com")
.logHeaders(Arrays.asList("Test-Header"))
.build();
try {
ClientRequest request = client
.requestBuilder()
.setMethod("GET")
.setUrlRelativetoBase("/test")
.setHeader("Test-Header", "testvalue")
.build();
assertEquals("GET http://foo.com/test\theaders:\ttest-header:testvalue", client.toLogMessage(request));
} finally {
client.close();
}
}

@Test
public void testLoggerConfWithHeaderIsCaseInsentivie(){
client = new RxHttpClient.Builder()
.setBaseUrl("http://foo.com")
.logHeaders(Arrays.asList("Test-Header"))
.build();
try {
ClientRequest request = client
.requestBuilder()
.setMethod("GET")
.setUrlRelativetoBase("/test")
.setHeader("test-Header", "testvalue")
.build();
assertEquals("GET http://foo.com/test\theaders:\ttest-header:testvalue", client.toLogMessage(request));
} finally {
client.close();
}
}

@Test
public void testLoggerConfWithFParamIsCaseInsensitive(){
client = new RxHttpClient.Builder()
.setBaseUrl("http://foo.com")
.logFormParams(Arrays.asList("TestParam"))
.build();
try {
ClientRequest request = client
.requestBuilder()
.setMethod("GET")
.setUrlRelativetoBase("/test")
.addFormParam("testparam", "testparamValue")
.build();
assertEquals("GET http://foo.com/test\tformParams:\ttestparam:testparamValue", client.toLogMessage(request));
} finally {
client.close();
}

}

@Test
public void testLoggerConfWithFormParam(){
client = new RxHttpClient.Builder()
.setBaseUrl("http://foo.com")
.logFormParams(Arrays.asList("TestParam"))
.build();
try {
ClientRequest request = client
.requestBuilder()
.setMethod("GET")
.setUrlRelativetoBase("/test")
.addFormParam("TestParam", "testparamValue")
.build();
assertEquals("GET http://foo.com/test\tformParams:\tTestParam:testparamValue", client.toLogMessage(request));
} finally {
client.close();
}

}

}
Loading

0 comments on commit b30d918

Please sign in to comment.