Fix bug throwing exception when first event is collected
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / common / HeaderUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2019 VMware, Inc. All rights reserved.
6  * Copyright (C) 2019-2020 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcae.common;
23
24 import java.util.Collections;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27 import javax.servlet.http.HttpServletRequest;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.stereotype.Component;
30
31 /**
32  * A class with methods used in HTTP header management.
33  */
34 @Component
35 public class HeaderUtils {
36
37     public String getRestApiIdentify(String uri) {
38         return isBatchRequest(uri) ? "eventListener_eventBatch" : "eventListener";
39     }
40
41     public Map<String, String> extractHeaders(HttpServletRequest request) {
42         return Collections.list(request.getHeaderNames()).stream()
43                 .collect(Collectors.toMap(h -> h, request::getHeader));
44     }
45
46     public HttpHeaders fillHeaders(Map<String, String> headers) {
47         HttpHeaders httpHeaders = new HttpHeaders();
48         httpHeaders.setAll(headers);
49         return httpHeaders;
50     }
51
52     private boolean isBatchRequest(String request) {
53         return request.contains("eventBatch");
54     }
55 }