-
Notifications
You must be signed in to change notification settings - Fork 39
/
HttpHeaderUtils.java
72 lines (62 loc) · 2.47 KB
/
HttpHeaderUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.getindata.connectors.http.internal.utils;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.stream.Stream;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import com.getindata.connectors.http.internal.BasicAuthHeaderValuePreprocessor;
import com.getindata.connectors.http.internal.ComposeHeaderPreprocessor;
import com.getindata.connectors.http.internal.HeaderPreprocessor;
@NoArgsConstructor(access = AccessLevel.NONE)
public final class HttpHeaderUtils {
public static Map<String, String> prepareHeaderMap(
String headerKeyPrefix,
Properties properties,
HeaderPreprocessor headerPreprocessor) {
Map<String, String> headerMap =
ConfigUtils.propertiesToMap(properties, headerKeyPrefix, String.class);
for (Entry<String, String> headerAndValue : headerMap.entrySet()) {
String headerName = headerAndValue.getKey();
String headerValue = headerAndValue.getValue();
headerMap.replace(
ConfigUtils.extractPropertyLastElement(headerName),
headerPreprocessor.preprocessValueForHeader(headerName, headerValue)
);
}
return headerMap;
}
/**
* Flat map a given Map of header name and header value map to an array containing both header
* names and values. For example, header map of
* <pre>{@code
* Map.of(
* header1, val1,
* header2, val2
* )
* }</pre>
* will be converter to an array of:
* <pre>{@code
* String[] headers = {"header1", "val1", "header2", "val2"};
* }</pre>
*
* @param headerMap mapping of header names to header values
* @return an array containing both header names and values
*/
public static String[] toHeaderAndValueArray(Map<String, String> headerMap) {
return headerMap
.entrySet()
.stream()
.flatMap(entry -> {
String originalKey = entry.getKey();
String newKey = ConfigUtils.extractPropertyLastElement(originalKey);
return Stream.of(newKey, entry.getValue());
}).toArray(String[]::new);
}
public static HeaderPreprocessor createDefaultHeaderPreprocessor() {
return new ComposeHeaderPreprocessor(
Collections.singletonMap("Authorization", new BasicAuthHeaderValuePreprocessor())
);
}
}