From aa70190226d2d5194818ff31dccdfdf8cdc8fb12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Thu, 29 Aug 2019 21:23:05 -0300 Subject: [PATCH] The header mapper for spec 0.3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- .../http/BinaryFormatHeaderMapperImpl.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 api/src/main/java/io/cloudevents/v03/http/BinaryFormatHeaderMapperImpl.java diff --git a/api/src/main/java/io/cloudevents/v03/http/BinaryFormatHeaderMapperImpl.java b/api/src/main/java/io/cloudevents/v03/http/BinaryFormatHeaderMapperImpl.java new file mode 100644 index 00000000..47ed8a4c --- /dev/null +++ b/api/src/main/java/io/cloudevents/v03/http/BinaryFormatHeaderMapperImpl.java @@ -0,0 +1,66 @@ +/** + * Copyright 2019 The CloudEvents 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 + * + * 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 io.cloudevents.v03.http; + +import static io.cloudevents.v02.http.BinaryFormatAttributeMapperImpl.HEADER_PREFIX; + +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.AbstractMap.SimpleEntry; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import io.cloudevents.v03.ContextAttributes; + +/** + * + * @author fabiojose + * + */ +public class BinaryFormatHeaderMapperImpl { + private BinaryFormatHeaderMapperImpl() {} + + public static Map map(Map attributes, + Map extensions) { + Objects.requireNonNull(attributes); + Objects.requireNonNull(extensions); + + Map result = attributes.entrySet() + .stream() + .map(header -> new SimpleEntry<>(header.getKey() + .toLowerCase(Locale.US), header.getValue())) + .filter(header -> !header.getKey() + .equals(ContextAttributes.datacontenttype.name())) + .map(header -> new SimpleEntry<>(HEADER_PREFIX+header.getKey(), + header.getValue())) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + + result.putAll( + extensions.entrySet() + .stream() + .map(header -> new SimpleEntry<>(HEADER_PREFIX+header.getKey(), + header.getValue())) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)) + ); + + result.put("Content-Type", attributes + .get(ContextAttributes.datacontenttype.name())); + + return result; + } + +}