com.github.tomakehurst.wiremock.http.HttpHeadersJsonDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wiremock-standalone Show documentation
Show all versions of wiremock-standalone Show documentation
A web service test double for all occasions - standalone edition
/*
* Copyright (C) 2012-2023 Thomas Akehurst
*
* 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.
*/
package com.github.tomakehurst.wiremock.http;
import static com.google.common.collect.Iterables.transform;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
public class HttpHeadersJsonDeserializer extends JsonDeserializer {
@Override
public HttpHeaders deserialize(JsonParser parser, DeserializationContext context)
throws IOException {
JsonNode rootNode = parser.readValueAsTree();
return new HttpHeaders(transform(all(rootNode.fields()), toHttpHeaders()));
}
private static Function, HttpHeader> toHttpHeaders() {
return new Function, HttpHeader>() {
@Override
public HttpHeader apply(Map.Entry field) {
String key = field.getKey();
if (field.getValue().isArray()) {
return new HttpHeader(
key,
ImmutableList.copyOf(transform(all(field.getValue().elements()), toStringValues())));
} else {
return new HttpHeader(key, field.getValue().textValue());
}
}
};
}
private static Function toStringValues() {
return new Function() {
public String apply(JsonNode node) {
return node.textValue();
}
};
}
public static Iterable all(final Iterator underlyingIterator) {
return () -> underlyingIterator;
}
}