2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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=========================================================
21 package org.onap.aai.babel.request;
23 import java.util.Optional;
24 import javax.ws.rs.core.HttpHeaders;
26 /** Bean to represent the ECOMP request/transaction IDs required for EELF logging. */
27 public class RequestHeaders {
29 // ECOMP request ID a.k.a. transaction ID or correlation ID
30 public static final String HEADER_REQUEST_ID = "X-ECOMP-RequestID";
31 public static final String HEADER_SERVICE_INSTANCE_ID = "X-ECOMP-ServiceInstanceID";
32 // This value should match with org.openecomp.restclient.client.Headers.TRANSACTION_ID
33 private static final String HEADER_X_TRANSACTION_ID = "X-TransactionId";
35 private String requestId;
36 private String instanceId;
37 private String transactionId;
39 public RequestHeaders(HttpHeaders headers) {
40 requestId = headers.getHeaderString(RequestHeaders.HEADER_REQUEST_ID);
41 instanceId = headers.getHeaderString(RequestHeaders.HEADER_SERVICE_INSTANCE_ID);
42 transactionId = headers.getHeaderString(RequestHeaders.HEADER_X_TRANSACTION_ID);
45 public String getRequestId() {
49 public String getInstanceId() {
53 public String getTransactionId() {
58 * Get the global request ID from the HTTP headers. The value will be taken from the header "X-ECOMP-RequestID" if
59 * this is set, or else the value of "X-TransactionId" (which may be null).
62 * If the correlation ID contains the symbol : then this character and any trailing characters are removed. This
63 * allows for an incrementing numeric sequence where there are multiple HTTP requests for a single transaction.
65 * @return the normalsed UUID used for correlating transactions across components, or else null (if no ID is set)
67 public String getCorrelationId() {
68 // If the request ID is missing, use the transaction ID (if present)
69 String uuid = Optional.ofNullable(getRequestId()).orElse(getTransactionId());
71 // Normalize the correlation ID by removing any suffix
72 if (uuid != null && uuid.contains(":")) {
73 uuid = uuid.split(":")[0];
80 public String toString() {
81 return "RequestHeaders [requestId=" + requestId + ", instanceId=" + instanceId + ", transactionId="
82 + transactionId + "]";