com.twosigma.webtau.http.validation.HeaderDataNode Maven / Gradle / Ivy
/*
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* 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.twosigma.webtau.http.validation;
import com.twosigma.webtau.data.traceable.TraceableValue;
import com.twosigma.webtau.http.HttpHeader;
import com.twosigma.webtau.http.HttpResponse;
import com.twosigma.webtau.http.datanode.DataNode;
import com.twosigma.webtau.http.datanode.DataNodeBuilder;
import com.twosigma.webtau.http.datanode.DataNodeId;
import com.twosigma.webtau.http.datanode.NullDataNode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class HeaderDataNode implements DataNode {
private static final Set translations = setOf(
new CamelCaseTranslation("Content-Location", "contentLocation"),
new CamelCaseTranslation("Content-Length", "contentLength", Integer::valueOf),
new CamelCaseTranslation("Content-Encoding", "contentEncoding")
);
private final DataNode dataNode;
private final HttpHeader responseHeader;
public HeaderDataNode(HttpResponse response) {
Map headerData = new HashMap<>();
headerData.put("statusCode", response.getStatusCode());
headerData.put("contentType", response.getContentType());
response.getHeader().forEachProperty(headerData::put);
translations.forEach(translation -> addCamelCaseVersion(headerData, translation));
this.dataNode = DataNodeBuilder.fromMap(new DataNodeId("header"), headerData);
this.responseHeader = response.getHeader();
}
private static void addCamelCaseVersion(Map headerData, CamelCaseTranslation translation) {
Optional existingHeaderName = findMatchingCaseInsensitiveKey(translation.originalName, headerData.keySet().stream());
if (existingHeaderName.isPresent()) {
Object converted = translation.conversion.apply((String) headerData.get(existingHeaderName.get()));
headerData.put(translation.camelCaseName, converted);
headerData.put(translation.originalName, converted);
}
}
public HttpHeader getResponseHeader() {
return responseHeader;
}
@Override
public DataNodeId id() {
return dataNode.id();
}
@Override
public DataNode get(String name) {
Optional matchingKey = findMatchingCaseInsensitiveKey(name);
return matchingKey
.map(dataNode::get)
.orElse(new NullDataNode(id().child(name)));
}
@Override
public boolean has(String name) {
Optional matchingKey = findMatchingCaseInsensitiveKey(name);
return matchingKey.isPresent();
}
@Override
public DataNode get(int idx) {
return dataNode.get(idx);
}
@Override
public TraceableValue getTraceableValue() {
return dataNode.getTraceableValue();
}
@Override
public E get() {
return dataNode.get();
}
@Override
public boolean isList() {
return dataNode.isList();
}
@Override
public boolean isSingleValue() {
return false;
}
@Override
public List elements() {
return dataNode.elements();
}
@Override
public Iterator iterator() {
return dataNode.iterator();
}
@Override
public int numberOfChildren() {
return dataNode.numberOfChildren();
}
@Override
public int numberOfElements() {
return dataNode.numberOfElements();
}
@Override
public Map asMap() {
return dataNode.asMap();
}
@Override
public String toString() {
return dataNode.toString();
}
public DataNode statusCode() {
return dataNode.get("statusCode");
}
private Optional findMatchingCaseInsensitiveKey(String name) {
return findMatchingCaseInsensitiveKey(name, dataNode.asMap().keySet().stream());
}
private static Optional findMatchingCaseInsensitiveKey(String name, Stream keys) {
String lowerCaseName = name.toLowerCase();
return keys
.filter(k -> k != null && k.toLowerCase().equals(lowerCaseName))
.findFirst();
}
private static Set setOf(T... things) {
return Arrays.stream(things).collect(Collectors.toSet());
}
private static class CamelCaseTranslation {
private final String originalName;
private final String camelCaseName;
private final Function conversion;
private CamelCaseTranslation(String originalName, String camelCaseName) {
this.originalName = originalName;
this.camelCaseName = camelCaseName;
this.conversion = (v) -> v;
}
private CamelCaseTranslation(String originalName, String camelCaseName, Function conversion) {
this.originalName = originalName;
this.camelCaseName = camelCaseName;
this.conversion = conversion;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy