Skip to content

Commit

Permalink
apache http client 5 (#1058)
Browse files Browse the repository at this point in the history
Co-authored-by: sokomishalov <sokolovm@dev.vtb>
  • Loading branch information
sokomishalov and sokomishalov authored Jun 17, 2021
1 parent 51fefc4 commit 7fd7635
Show file tree
Hide file tree
Showing 24 changed files with 1,464 additions and 1 deletion.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Logbook is ready to use out of the box for most common setups. Even for uncommon
- Java 8
- Any build tool using Maven Central, or direct download
- Servlet Container (optional)
- Apache HTTP Client (optional)
- Apache HTTP Client 4.x **or 5.x** (optional)
- JAX-RS 2.x Client and Server (optional)
- Netty 4.x (optional)
- OkHttp 2.x **or 3.x** (optional)
Expand Down Expand Up @@ -618,6 +618,28 @@ CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
client.execute(producer, new LogbookHttpAsyncResponseConsumer<>(consumer), callback)
```
### HTTP Client 5
The `logbook-httpclient5` module contains both an `HttpRequestInterceptor` and an `HttpResponseInterceptor` to use with the `HttpClient`:
```java
CloseableHttpClient client = HttpClientBuilder.create()
.addRequestInterceptorFirst(new LogbookHttpRequestInterceptor(logbook))
.addResponseInterceptorFirst(new LogbookHttpResponseInterceptor())
.build();
```
Since the `LogbookHttpResponseInterceptor` is incompatible with the `HttpAsyncClient` there is another way to log responses:
```java
CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
.addRequestInterceptorFirst(new LogbookHttpRequestInterceptor(logbook))
.build();
// and then wrap your response consumer
client.execute(producer, new LogbookHttpAsyncResponseConsumer<>(consumer), callback)
```
### JAX-RS
The `logbook-jaxrs` module contains:
Expand Down
5 changes: 5 additions & 0 deletions logbook-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<artifactId>logbook-httpclient</artifactId>
<version>2.11.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-httpclient5</artifactId>
<version>2.11.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-jaxrs</artifactId>
Expand Down
45 changes: 45 additions & 0 deletions logbook-httpclient5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.zalando</groupId>
<artifactId>logbook-parent</artifactId>
<version>2.11.0-SNAPSHOT</version>
</parent>
<artifactId>logbook-httpclient5</artifactId>
<description>HTTP Client interceptor for request and response logging</description>
<scm>
<url>https://github.com/zalando/logbook</url>
<connection>scm:git:git@github.com:zalando/logbook.git</connection>
<developerConnection>scm:git:git@github.com:zalando/logbook.git</developerConnection>
</scm>
<dependencies>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-test</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
</dependency>
<dependency>
<groupId>com.github.rest-driver</groupId>
<artifactId>rest-client-driver</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.zalando.logbook.httpclient5;

import lombok.experimental.UtilityClass;
import org.zalando.logbook.Logbook;

@UtilityClass
final class Attributes {
static final String STAGE = Logbook.class.getName() + ".STAGE";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.zalando.logbook.httpclient5;

import lombok.RequiredArgsConstructor;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.nio.DataStreamChannel;

import java.nio.ByteBuffer;
import java.util.List;

import static lombok.AccessLevel.PACKAGE;

@RequiredArgsConstructor(access = PACKAGE)
final class BufferingFixedSizeDataStreamChannel implements DataStreamChannel {
private final byte[] buffer;

public byte[] getBuffer() {
return buffer;
}

@Override
public void requestOutput() {
}

@Override
public int write(ByteBuffer src) {
ByteBufferUtils.fixedSizeCopy(src, buffer);
return 0;
}

@Override
public void endStream() {
}

@Override
public void endStream(List<? extends Header> trailers) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.zalando.logbook.httpclient5;

import lombok.experimental.UtilityClass;

import java.nio.ByteBuffer;

@UtilityClass
class ByteBufferUtils {

static void fixedSizeCopy(ByteBuffer src, byte[] dest) {
if (src.hasArray()) {
byte[] array = src.array();
System.arraycopy(array, 0, dest, 0, dest.length);
} else {
src.get(dest, 0, dest.length);
src.flip();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.zalando.logbook.httpclient5;

import lombok.experimental.Delegate;
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;

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

@Delegate
protected abstract AsyncResponseConsumer<T> delegate();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.zalando.logbook.httpclient5;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import lombok.experimental.UtilityClass;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;

import java.io.IOException;

import static org.apache.hc.core5.http.io.entity.EntityUtils.toByteArray;

@UtilityClass
final class HttpEntities {

interface Copy extends HttpEntity {
byte[] getBody();
}

Copy copy(final HttpEntity entity) throws IOException {
final byte[] body = toByteArray(entity);
ContentType contentType = ContentType.parse(entity.getContentType());
boolean chunked = entity.isChunked();
String contentEncoding = entity.getContentEncoding();

final ByteArrayEntity copy = new ByteArrayEntity(body, contentType, contentEncoding, chunked);

return new DefaultCopy(copy, body);
}

@RequiredArgsConstructor
private static final class DefaultCopy implements Copy {

@Delegate
private final HttpEntity entity;

@Getter
private final byte[] body;

}

}
Loading

0 comments on commit 7fd7635

Please sign in to comment.