Skip to content

Commit

Permalink
Changed headers type to map of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderYastrebov committed May 13, 2016
1 parent 602ae55 commit eab365e
Show file tree
Hide file tree
Showing 37 changed files with 372 additions and 250 deletions.
40 changes: 30 additions & 10 deletions logbook-api/src/main/java/org/zalando/logbook/BaseHttpMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* #L%
*/

import com.google.common.collect.ListMultimap;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;

import static com.google.common.collect.Multimaps.newListMultimap;
import static java.lang.String.CASE_INSENSITIVE_ORDER;

public interface BaseHttpMessage {
Expand All @@ -35,22 +35,42 @@ public interface BaseHttpMessage {

Origin getOrigin();

ListMultimap<String, String> getHeaders();
Map<String, List<String>> getHeaders();

String getContentType();

Charset getCharset();

class Headers {
class HeadersBuilder {

private final Map<String, List<String>> headers;

Headers() {
public HeadersBuilder() {
// package private so we can trick code coverage
headers = new TreeMap<>(CASE_INSENSITIVE_ORDER);
}

public static ListMultimap<String, String> create() {
return newListMultimap(new TreeMap<>(CASE_INSENSITIVE_ORDER), ArrayList::new);
public HeadersBuilder put(final String key, final String value) {
final List<String> values = headers.get(key);
if (values != null) {
values.add(value);
} else {
final ArrayList<String> list = new ArrayList<>();
list.add(value);
headers.put(key, list);
}
return this;
}

}
public HeadersBuilder put(final String key, final Iterable<String> values) {
for (String value : values) {
put(key, value);
}
return this;
}

public Map<String, List<String>> build() {
return Collections.unmodifiableMap(headers);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
* #L%
*/

import com.google.common.collect.ListMultimap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.Test;

import static org.hamcrest.Matchers.hasItem;
Expand All @@ -30,9 +32,15 @@ public final class BaseHttpMessageTest {

@Test
public void shouldUseCaseInsensitiveHeaders() {
final ListMultimap<String, String> headers = BaseHttpMessage.Headers.create();
headers.put("X-Secret", "s3cr3t");
final Map<String, List<String>> headers = new BaseHttpMessage.HeadersBuilder()
.put("X-Secret", "s3cr3t")
.put("X-Secret", "knowledge")
.put("Y-Secret", Arrays.asList("one", "two"))
.build();

assertThat(headers.get("x-secret"), hasItem("s3cr3t"));
assertThat(headers.get("x-secret"), hasItem("knowledge"));
assertThat(headers.get("Y-SECRET"), hasItem("one"));
assertThat(headers.get("Y-SECRET"), hasItem("two"));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void shouldUseRequestURIConstructor() {

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

@Test
Expand Down
13 changes: 7 additions & 6 deletions logbook-core/src/main/java/org/zalando/logbook/Conditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -70,18 +71,18 @@ public static Predicate<RawHttpRequest> contentType(final String contentType) {

public static Predicate<RawHttpRequest> header(final String key, final String value) {
return request ->
request.getHeaders().containsEntry(key, value);
request.getHeaders().getOrDefault(key, Collections.emptyList()).contains(value);
}

public static Predicate<RawHttpRequest> header(final String key, final Predicate<String> predicate) {
return request ->
request.getHeaders().get(key).stream().anyMatch(predicate);
}

public static Predicate<RawHttpRequest> header(final BiPredicate<String, String> predicate) {
return request ->
request.getHeaders().entries().stream()
.anyMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
// public static Predicate<RawHttpRequest> header(final BiPredicate<String, String> predicate) {
// return request ->
// request.getHeaders().entries().stream()
// .anyMatch(e -> predicate.test(e.getKey(), e.getValue()));
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ private <H extends HttpMessage> String format(final H message, final String type
lines.addAll(formatHeaders(message));

final String body = message.getBodyAsString();

if (!body.isEmpty()) {
lines.add("");
lines.add(body);
}

return join(lines);
}

Expand All @@ -80,14 +80,14 @@ private String direction(final HttpMessage request) {
}

private List<String> formatHeaders(final HttpMessage message) {
return message.getHeaders().asMap().entrySet().stream()
return message.getHeaders().entrySet().stream()
.collect(toMap(Map.Entry::getKey, this::formatHeaderValues))
.entrySet().stream()
.map(this::formatHeader)
.collect(toList());
}

private String formatHeaderValues(final Map.Entry<String, Collection<String>> entry) {
private String formatHeaderValues(final Map.Entry<String, List<String>> entry) {
return entry.getValue().stream().collect(joining(", "));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
*/

import com.google.common.collect.ForwardingObject;
import com.google.common.collect.ListMultimap;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

public abstract class ForwardingHttpMessage extends ForwardingObject implements HttpMessage {

Expand All @@ -37,7 +38,7 @@ public String getProtocolVersion() {
}

@Override
public ListMultimap<String, String> getHeaders() {
public Map<String, List<String>> getHeaders() {
return delegate().getHeaders();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
*/

import com.google.common.collect.ForwardingObject;
import com.google.common.collect.ListMultimap;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

public abstract class ForwardingRawHttpRequest extends ForwardingObject implements RawHttpRequest {

@Override
protected abstract RawHttpRequest delegate();

@Override
public ListMultimap<String, String> getHeaders() {
public Map<String, List<String>> getHeaders() {
return delegate().getHeaders();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String format(final Precorrelation<HttpRequest> precorrelation) throws IO
builder.put("method", request.getMethod());
builder.put("uri", request.getRequestUri());

addUnless(builder, "headers", request.getHeaders().asMap(), Map::isEmpty);
addUnless(builder, "headers", request.getHeaders(), Map::isEmpty);
addBody(request, builder);

final ImmutableMap<String, Object> content = builder.build();
Expand All @@ -89,7 +89,7 @@ public String format(final Correlation<HttpRequest, HttpResponse> correlation) t
builder.put("correlation", correlationId);
builder.put("protocol", response.getProtocolVersion());
builder.put("status", response.getStatus());
addUnless(builder, "headers", response.getHeaders().asMap(), Map::isEmpty);
addUnless(builder, "headers", response.getHeaders(), Map::isEmpty);
addBody(response, builder);

final ImmutableMap<String, Object> content = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@
* #L%
*/

import com.google.common.collect.ListMultimap;

import java.io.IOException;

import static com.google.common.collect.Multimaps.transformEntries;
import java.util.List;
import java.util.Map;

public final class ObfuscatedHttpRequest extends ForwardingHttpRequest {

private final HttpRequest request;
private final QueryObfuscator queryObfuscator;
private final BodyObfuscator bodyObfuscator;
private final ListMultimap<String, String> headers;
private final Map<String, List<String>> headers;

ObfuscatedHttpRequest(final HttpRequest request,
final QueryObfuscator queryObfuscator,
Expand All @@ -40,7 +39,7 @@ public final class ObfuscatedHttpRequest extends ForwardingHttpRequest {
this.request = request;
this.queryObfuscator = queryObfuscator;
this.bodyObfuscator = bodyObfuscator;
this.headers = transformEntries(request.getHeaders(), headerObfuscator::obfuscate);
this.headers = Obfuscators.obfuscateHeaders(request.getHeaders(), headerObfuscator::obfuscate);
}

@Override
Expand All @@ -60,7 +59,7 @@ public String getQuery() {
}

@Override
public ListMultimap<String, String> getHeaders() {
public Map<String, List<String>> getHeaders() {
return headers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@
* #L%
*/

import com.google.common.collect.ListMultimap;

import java.io.IOException;

import static com.google.common.collect.Multimaps.transformEntries;
import java.util.List;
import java.util.Map;

public final class ObfuscatedHttpResponse extends ForwardingHttpResponse {

private final HttpResponse response;
private final BodyObfuscator bodyObfuscator;
private final ListMultimap<String, String> headers;
private final Map<String, List<String>> headers;

ObfuscatedHttpResponse(final HttpResponse response, final HeaderObfuscator headerObfuscator,
final BodyObfuscator bodyObfuscator) {
this.response = response;
this.bodyObfuscator = bodyObfuscator;
this.headers = transformEntries(response.getHeaders(), headerObfuscator::obfuscate);
this.headers = Obfuscators.obfuscateHeaders(response.getHeaders(), headerObfuscator::obfuscate);
}

@Override
Expand All @@ -45,7 +44,7 @@ protected HttpResponse delegate() {
}

@Override
public ListMultimap<String, String> getHeaders() {
public Map<String, List<String>> getHeaders() {
return headers;
}

Expand Down
16 changes: 13 additions & 3 deletions logbook-core/src/main/java/org/zalando/logbook/Obfuscators.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
* limitations under the License.
* #L%
*/

import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.util.regex.Pattern.quote;

Expand All @@ -36,8 +39,7 @@ public static QueryObfuscator obfuscate(final String name, final String replacem
final Pattern pattern = Pattern.compile("((?:^|&)" + quote(name) + "=)(?:.*?)(&|$)");
final String replacementPattern = "$1" + replacement + "$2";

return query ->
pattern.matcher(query).replaceAll(replacementPattern);
return query -> pattern.matcher(query).replaceAll(replacementPattern);
}

public static QueryObfuscator accessToken() {
Expand All @@ -56,4 +58,12 @@ public static HeaderObfuscator authorization() {
return obfuscate("Authorization"::equalsIgnoreCase, "XXX");
}

static Map<String, List<String>> obfuscateHeaders(final Map<String, List<String>> map, final BiFunction<String, String, String> f) {
final BaseHttpMessage.HeadersBuilder builder = new BaseHttpMessage.HeadersBuilder();
for (Map.Entry<String, List<String>> e : map.entrySet()) {
final String k = e.getKey();
builder.put(k, e.getValue().stream().map(x -> f.apply(k, x)).collect(Collectors.toList()));
}
return builder.build();
}
}
Loading

0 comments on commit eab365e

Please sign in to comment.