2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright (c) 2017-2019 European Software Marketing Ltd.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.request;
24 import java.util.Optional;
25 import javax.ws.rs.core.HttpHeaders;
27 /** Bean to represent the ECOMP request/transaction IDs required for EELF logging. */
28 public class RequestHeaders {
30 // ECOMP request ID a.k.a. transaction ID or correlation ID
31 public static final String HEADER_REQUEST_ID = "X-ECOMP-RequestID";
32 public static final String HEADER_SERVICE_INSTANCE_ID = "X-ECOMP-ServiceInstanceID";
33 // This value should match with org.openecomp.restclient.client.Headers.TRANSACTION_ID
34 private static final String HEADER_X_TRANSACTION_ID = "X-TransactionId";
36 private String requestId;
37 private String instanceId;
38 private String transactionId;
40 public RequestHeaders(HttpHeaders headers) {
41 requestId = headers.getHeaderString(RequestHeaders.HEADER_REQUEST_ID);
42 instanceId = headers.getHeaderString(RequestHeaders.HEADER_SERVICE_INSTANCE_ID);
43 transactionId = headers.getHeaderString(RequestHeaders.HEADER_X_TRANSACTION_ID);
46 public String getRequestId() {
50 public String getInstanceId() {
54 public String getTransactionId() {
59 * Get the global request ID from the HTTP headers. The value will be taken from the header "X-ECOMP-RequestID" if
60 * this is set, or else the value of "X-TransactionId" (which may be null).
63 * If the correlation ID contains the symbol : then this character and any trailing characters are removed. This
64 * allows for an incrementing numeric sequence where there are multiple HTTP requests for a single transaction.
66 * @return the normalized UUID used for correlating transactions across components, or else null (if no ID is set)
68 public String getCorrelationId() {
69 // If the request ID is missing, use the transaction ID (if present)
70 String uuid = Optional.ofNullable(getRequestId()).orElse(getTransactionId());
72 // Normalize the correlation ID by removing any suffix
73 if (uuid != null && uuid.contains(":")) {
74 uuid = uuid.split(":")[0];
81 public String toString() {
82 return "RequestHeaders [requestId=" + requestId + ", instanceId=" + instanceId + ", transactionId="
83 + transactionId + "]";