2 * ================================================================================
3 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
20 package org.onap.dcae.analytics.web.util;
23 * @author Rajiv Singla
26 import java.nio.charset.Charset;
27 import java.util.Collections;
28 import java.util.Date;
30 import java.util.Optional;
32 import javax.annotation.Nullable;
34 import org.onap.dcae.analytics.model.AnalyticsHttpConstants;
35 import org.onap.dcae.analytics.model.AnalyticsModelConstants;
36 import org.onap.dcae.analytics.model.configbindingservice.ConfigBindingServiceConstants;
37 import org.onap.dcae.analytics.model.util.supplier.CreationTimestampSupplier;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.MediaType;
40 import org.springframework.messaging.MessageHeaders;
43 * Provides utility methods for Analytics HTTP Operations
45 * @author Rajiv Singla
47 public abstract class AnalyticsHttpUtils {
50 * Creates default http headers for analytics http requests to other services like DMaaP, AAI etc with randomly
51 * generated request id header
53 * @return default analytics http headers
55 public static HttpHeaders createDefaultHttpHeaders() {
56 return createDefaultHttpHeaders(null);
60 * Creates default http headers for analytics http requests to other services like DMaaP, AAI etc
62 * @param requestId request id
64 * @return default analytics http headers
66 public static HttpHeaders createDefaultHttpHeaders(@Nullable final String requestId) {
68 final HttpHeaders httpHeaders = new HttpHeaders();
70 // set analytics from app name header.
71 // Look up service name set by config service "SERVICE_NAME" environment variable or assign default value
73 AnalyticsHttpConstants.REQUEST_APP_NAME_HEADER_KEY,
74 ConfigBindingServiceConstants.SERVICE_NAME_ENV_VARIABLE_VALUE != null ?
75 ConfigBindingServiceConstants.SERVICE_NAME_ENV_VARIABLE_VALUE :
76 AnalyticsHttpConstants.REQUEST_APP_NAME_HEADER_DEFAULT_VALUE);
78 // if request id is not present create random UUID
79 httpHeaders.add(AnalyticsHttpConstants.REQUEST_ID_HEADER_KEY, requestId != null ?
80 requestId : AnalyticsWebUtils.REQUEST_ID_SUPPLIER.get());
82 // sub transaction id is created randomly for each http request
83 httpHeaders.add(AnalyticsHttpConstants.REQUEST_TRANSACTION_ID_HEADER_KEY,
84 AnalyticsWebUtils.RANDOM_ID_SUPPLIER.get());
86 // by default analytics will accept only json
87 httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
88 httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
89 httpHeaders.setAcceptCharset(
90 Collections.singletonList(Charset.forName(AnalyticsModelConstants.UTF8_CHARSET_NAME)));
96 public static String getRequestId(final MessageHeaders messageHeaders) {
97 return Optional.ofNullable(messageHeaders.get(AnalyticsHttpConstants.REQUEST_ID_HEADER_KEY))
98 .map(requestId -> (String) requestId).orElse("UNKNOWN-REQUEST_ID");
101 public static String getTransactionId(final MessageHeaders messageHeaders) {
102 return Optional.ofNullable(messageHeaders.get(AnalyticsHttpConstants.REQUEST_TRANSACTION_ID_HEADER_KEY))
103 .map(transactionId -> (String) transactionId).orElse("UNKNOWN-TRANSACTION_ID");
106 public static Date getTimestampFromHeaders(final Map<String, Object> headers, final String headerTsKey) {
107 return Optional.ofNullable(headers.get(headerTsKey))
108 .map(ts -> CreationTimestampSupplier.getParsedDate((String) ts)).orElse(new Date());
111 public static String getRequestId(final HttpHeaders httpHeaders) {
112 return Optional.ofNullable(
113 httpHeaders.get(AnalyticsHttpConstants.REQUEST_ID_HEADER_KEY)).map(headerList ->
114 headerList.get(0)).orElse(AnalyticsWebUtils.REQUEST_ID_SUPPLIER.get());
117 public static String getTransactionId(final HttpHeaders httpHeaders) {
118 return Optional.ofNullable(
119 httpHeaders.get(AnalyticsHttpConstants.REQUEST_TRANSACTION_ID_HEADER_KEY)).map(headerList ->
120 headerList.get(0)).orElse(AnalyticsWebUtils.RANDOM_ID_SUPPLIER.get());