Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-124 Fixed async support for http client #125

Merged
merged 2 commits into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,17 @@ CloseableHttpClient client = HttpClientBuilder.create()
.build();
```

Since the `LogbookHttpResponseInterceptor` is incompatible with the `HttpAsyncClient` there is another way to log responses:

```java
CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
.addInterceptorFirst(new LogbookHttpRequestInterceptor(logbook))
.build();

// and then wrap your response consumer
client.execute(producer, new LogbookHttpAsyncResponseConsumer<>(consumer), callback)
```

### Spring Boot Starter

Logbook comes with a convenient auto configuration for Spring Boot users. It sets up all of the following parts automatically with sensible defaults:
Expand Down
49 changes: 2 additions & 47 deletions logbook-api/src/main/java/org/zalando/logbook/Logbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@
* #L%
*/

import lombok.Singular;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

public interface Logbook {

Expand All @@ -36,48 +31,8 @@ static Logbook create() {
return builder().build();
}

@lombok.Builder(builderClassName = "Builder")
static Logbook create(
@Nullable final Predicate<RawHttpRequest> condition,
@Singular final List<QueryObfuscator> queryObfuscators,
@Singular final List<HeaderObfuscator> headerObfuscators,
@Singular final List<BodyObfuscator> bodyObfuscators,
@Singular final List<RequestObfuscator> requestObfuscators,
@Singular final List<ResponseObfuscator> responseObfuscators,
@Nullable final HttpLogFormatter formatter,
@Nullable final HttpLogWriter writer) {

final LogbookFactory factory = LogbookFactory.INSTANCE;

final QueryObfuscator queryObfuscator = queryObfuscators.stream()
.reduce(QueryObfuscator::merge)
.orElse(null);

final HeaderObfuscator headerObfuscator = headerObfuscators.stream()
.reduce(HeaderObfuscator::merge)
.orElse(null);

final BodyObfuscator bodyObfuscator = bodyObfuscators.stream()
.reduce(BodyObfuscator::merge)
.orElse(null);

final RequestObfuscator requestObfuscator = requestObfuscators.stream()
.reduce(RequestObfuscator::merge)
.orElse(null);

final ResponseObfuscator responseObfuscator = responseObfuscators.stream()
.reduce(ResponseObfuscator::merge)
.orElse(null);

return factory.create(
condition,
queryObfuscator,
headerObfuscator,
bodyObfuscator,
requestObfuscator,
responseObfuscator,
formatter,
writer);
static LogbookCreator.Builder builder() {
return LogbookCreator.builder();
}

}
79 changes: 79 additions & 0 deletions logbook-api/src/main/java/org/zalando/logbook/LogbookCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.zalando.logbook;

/*
* #%L
* Logbook: API
* %%
* Copyright (C) 2015 - 2016 Zalando SE
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import lombok.Singular;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Predicate;

public final class LogbookCreator {

LogbookCreator() {
// package private so we can trick code coverage
}

@lombok.Builder(builderClassName = "Builder")
private static Logbook create(
@Nullable final Predicate<RawHttpRequest> condition,
@Singular final List<QueryObfuscator> queryObfuscators,
@Singular final List<HeaderObfuscator> headerObfuscators,
@Singular final List<BodyObfuscator> bodyObfuscators,
@Singular final List<RequestObfuscator> requestObfuscators,
@Singular final List<ResponseObfuscator> responseObfuscators,
@Nullable final HttpLogFormatter formatter,
@Nullable final HttpLogWriter writer) {

final LogbookFactory factory = LogbookFactory.INSTANCE;

final QueryObfuscator queryObfuscator = queryObfuscators.stream()
.reduce(QueryObfuscator::merge)
.orElse(null);

final HeaderObfuscator headerObfuscator = headerObfuscators.stream()
.reduce(HeaderObfuscator::merge)
.orElse(null);

final BodyObfuscator bodyObfuscator = bodyObfuscators.stream()
.reduce(BodyObfuscator::merge)
.orElse(null);

final RequestObfuscator requestObfuscator = requestObfuscators.stream()
.reduce(RequestObfuscator::merge)
.orElse(null);

final ResponseObfuscator responseObfuscator = responseObfuscators.stream()
.reduce(ResponseObfuscator::merge)
.orElse(null);

return factory.create(
condition,
queryObfuscator,
headerObfuscator,
bodyObfuscator,
requestObfuscator,
responseObfuscator,
formatter,
writer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,41 @@
public final class EnforceCoverageTest {

@Test
public void shouldUseRequestURIConstructor() {
new RequestURI();
public void shouldUseHeadersConstructor() {
new BaseHttpMessage.HeadersBuilder();
}

@Test
public void shouldUseHeadersConstructor() {
new BaseHttpMessage.HeadersBuilder();
public void shouldUseLogbookCreatorConstructor() {
new LogbookCreator();
}

@Test
public void shouldCoverUselessClearMethods() {
final LogbookCreator.Builder builder = Logbook.builder();

builder.clearQueryObfuscators();
builder.clearHeaderObfuscators();
builder.clearBodyObfuscators();
builder.clearRequestObfuscators();
builder.clearResponseObfuscators();

builder.queryObfuscator(mock(QueryObfuscator.class));
builder.headerObfuscator(mock(HeaderObfuscator.class));
builder.bodyObfuscator(mock(BodyObfuscator.class));
builder.requestObfuscator(mock(RequestObfuscator.class));
builder.responseObfuscator(mock(ResponseObfuscator.class));

builder.clearQueryObfuscators();
builder.clearHeaderObfuscators();
builder.clearBodyObfuscators();
builder.clearRequestObfuscators();
builder.clearResponseObfuscators();
}

@Test
public void shouldUseRequestURIConstructor() {
new RequestURI();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ private static <T> void addUnless(final Map<String, Object> target, final String
}
}

private void addBody(final HttpMessage request, final Map<String, Object> map) throws IOException {
final String body = request.getBodyAsString();
private void addBody(final HttpMessage message, final Map<String, Object> map) throws IOException {
final String body = message.getBodyAsString();

if (isJson(request.getContentType())) {
if (isJson(message.getContentType())) {
map.put("body", tryParseBodyAsJson(body));
} else {
addUnless(map, "body", body, String::isEmpty);
Expand Down
16 changes: 11 additions & 5 deletions logbook-httpclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
<developerConnection>scm:git:git@github.com:zalando//logbook.git</developerConnection>
</scm>

<properties>
<httpclient.version>4.5.1</httpclient.version>
</properties>

<dependencies>
<dependency>
<groupId>org.zalando</groupId>
Expand All @@ -36,7 +32,17 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.2</version>
</dependency>

<!-- testing -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.zalando.logbook.httpclient;

/*
* #%L
* Logbook: HTTP Client
* %%
* Copyright (C) 2015 - 2016 Zalando SE
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;

abstract class ForwardingHttpAsyncResponseConsumer<T> implements HttpAsyncResponseConsumer<T> {

protected abstract HttpAsyncResponseConsumer<T> delegate();

@Override
public void responseReceived(final HttpResponse response) throws IOException, HttpException {
delegate().responseReceived(response);
}

@Override
public void consumeContent(final ContentDecoder decoder, final IOControl control) throws IOException {
delegate().consumeContent(decoder, control);
}

@Override
public void responseCompleted(final HttpContext context) {
delegate().responseCompleted(context);
}

@Override
public boolean cancel() {
return delegate().cancel();
}

@Override
public boolean isDone() {
return delegate().isDone();
}

@Override
public T getResult() {
return delegate().getResult();
}

@Override
public void failed(final Exception e) {
delegate().failed(e);
}

@Override
public Exception getException() {
return delegate().getException();
}

@Override
public void close() throws IOException {
delegate().close();
}

}
Loading