-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathMapCollapseUtils.java
131 lines (121 loc) · 4.44 KB
/
MapCollapseUtils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright 2017-2023 original authors
*
* 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
*
* https://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 io.micronaut.function.aws.proxy;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.MutableHttpHeaders;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Utility methods for collapsing headers.
*/
@Internal
public final class MapCollapseUtils {
private static final List<String> HEADERS_ALLOWING_COMMAS = Arrays.asList(HttpHeaders.DATE, HttpHeaders.USER_AGENT);
private MapCollapseUtils() {
}
/**
* Collapse the headers into a single value map.
*
* @param headers The headers
* @return The map
*/
public static Map<String, String> getSingleValueHeaders(MutableHttpHeaders headers) {
Map<String, String> result = new HashMap<>();
for (String paramName : headers.names()) {
result.put(paramName, headers.get(paramName));
}
return result;
}
/**
* Collapse the headers into a multi value map.
*
* @param headers The headers
* @return The map
*/
public static Map<String, List<String>> getMultiHeaders(MutableHttpHeaders headers) {
Map<String, List<String>> result = new HashMap<>();
for (String paramName : headers.names()) {
result.put(paramName, headers.getAll(paramName));
}
return result;
}
/**
* Collapse the aws single and multi headers into a single value map.
*
* @param multi The multi value map
* @param single The single value map
* @return The map
*/
public static Map<String, List<String>> collapse(Map<String, List<String>> multi, Map<String, String> single) {
if (multi == null && single == null) {
return Collections.emptyMap();
}
Map<String, List<String>> values = new HashMap<>();
if (multi != null) {
for (var entry: multi.entrySet()) {
values.computeIfAbsent(entry.getKey(), s -> new ArrayList<>()).addAll(entry.getValue());
}
}
if (CollectionUtils.isNotEmpty(single)) {
for (var entry: single.entrySet()) {
String headerName = entry.getKey();
List<String> strings = values.computeIfAbsent(headerName, s -> new ArrayList<>());
String value = entry.getValue();
if (HEADERS_ALLOWING_COMMAS.contains(headerName)) {
if (!strings.contains(value)) {
strings.add(value);
}
} else {
for (String v : splitCommaSeparatedValue(value)) {
v = v.trim();
if (!strings.contains(v)) {
strings.add(v);
}
}
}
}
}
return values;
}
/**
* Collapse a map whose value is a list of strings into a map whose value is a comma separated string.
*
* @param input Map with key String and value List of Strings
* @return Map with key String and value String with comma separated values
*/
public static Map<String, String> collapse(Map<String, List<String>> input) {
Map<String, String> result = new HashMap<>();
for (Map.Entry<String, List<String>> entry : input.entrySet()) {
result.put(entry.getKey(), String.join(",", entry.getValue()));
}
return result;
}
@NonNull
private static List<String> splitCommaSeparatedValue(@Nullable String value) {
if (value == null) {
return Collections.emptyList();
}
return Arrays.asList(value.split(","));
}
}