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

Extracted copying HTTP entity into reusable method #791

Merged
merged 1 commit into from
Aug 2, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.zalando.logbook.httpclient;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import lombok.experimental.UtilityClass;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ByteArrayEntity;

import java.io.IOException;

import static org.apache.http.util.EntityUtils.toByteArray;

@UtilityClass
final class HttpEntities {

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

Copy copy(final HttpEntity entity) throws IOException {
final byte[] body = toByteArray(entity);

final ByteArrayEntity copy = new ByteArrayEntity(body);
copy.setChunked(entity.isChunked());
copy.setContentEncoding(entity.getContentEncoding());
copy.setContentType(entity.getContentType());

return new DefaultCopy(copy, body);
}

@RequiredArgsConstructor
private static final class DefaultCopy implements Copy {

@Delegate
private final HttpEntity entity;

@Getter
private final byte[] body;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import lombok.AllArgsConstructor;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.zalando.logbook.HttpHeaders;
import org.zalando.logbook.Origin;

import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
Expand All @@ -25,7 +26,6 @@
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.apache.http.util.EntityUtils.toByteArray;
import static org.zalando.fauxpas.FauxPas.throwingUnaryOperator;

final class LocalRequest implements org.zalando.logbook.HttpRequest {
Expand Down Expand Up @@ -75,12 +75,14 @@ public State without() {
public State buffer(final HttpRequest request) throws IOException {
if (request instanceof HttpEntityEnclosingRequest) {
final HttpEntityEnclosingRequest original = (HttpEntityEnclosingRequest) request;
if (original.getEntity() == null) {
@Nullable final HttpEntity entity = original.getEntity();

if (entity == null) {
return new Passing();
} else {
final byte[] body = toByteArray(original.getEntity());
original.setEntity(new ByteArrayEntity(body));
return new Buffering(body);
final HttpEntities.Copy copy = HttpEntities.copy(entity);
original.setEntity(copy);
return new Buffering(copy.getBody());
}
} else {
return new Passing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.zalando.logbook.HttpHeaders;
import org.zalando.logbook.Origin;
Expand All @@ -23,7 +22,6 @@
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.apache.http.util.EntityUtils.toByteArray;
import static org.zalando.fauxpas.FauxPas.throwingUnaryOperator;

@AllArgsConstructor
Expand Down Expand Up @@ -75,16 +73,9 @@ public State buffer(final HttpResponse response) throws IOException {
if (entity == null) {
return new Passing();
} else {
final byte[] body = toByteArray(entity);

final ByteArrayEntity copy = new ByteArrayEntity(body);
copy.setChunked(entity.isChunked());
copy.setContentEncoding(entity.getContentEncoding());
copy.setContentType(entity.getContentType());

final HttpEntities.Copy copy = HttpEntities.copy(entity);
response.setEntity(copy);

return new Buffering(body);
return new Buffering(copy.getBody());
}
}

Expand Down