/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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.gravitee.gateway.reactor.processor.transaction;

import java.util.UUID;

/**
 * @author Eric LELEU (eric.leleu at graviteesource.com)
 * @author GraviteeSource Team
 */
public class TraceparentHelper {
    private static final String FLAGS = "-01";
    private static final String VERSION = "00-";

    public static String buildTraceparentFrom(UUID uuid) {
        String parentId = Long.toHexString(uuid.getLeastSignificantBits()); // 8-byte array (16 hex digits) - all bytes as zero is forbidden
        String traceId = Long.toHexString(uuid.getMostSignificantBits()) + parentId; // 16-byte array  (32 hex digits) - all bytes as zero is forbidden
        return VERSION + traceId + "-" + parentId + FLAGS;
    }

    public static boolean isValid(String traceparent) {
        String[] array = traceparent.split("-");
        boolean valid = isHexString(array[0], 2, 'f') // 1 byte arr => 2 hex char
                        && isHexString(array[1], 32, '0') // 16 bytes arr => 32 hex char
                        && isHexString(array[2], 16, '0') // 8 bytes arr => 16 hex char
                        && (array[3].length() == 2); // 1 byte arr => 2 hex char

        return valid;
    }

    /**
     * check if the value string contains only expectedLength hex digits in lower case.
     * all digits as 'notAll' is invalid
     * @param value
     * @param expectedLength
     * @param notAll
     * @return
     */
    private static final boolean isHexString(String value, int expectedLength, char notAll) {
        boolean sameCharForbidden = true;
        for (char c : value.toCharArray()) {
            if (!((c >= '0' && c <= '9')
                    || (c >= 'a' && c <= 'f'))) {
                return false;
            }
            sameCharForbidden = sameCharForbidden && (c == notAll);
            --expectedLength;
        }
        return 0 == expectedLength && !sameCharForbidden;
    }

}

@@ -706,16 +706,48 @@
 il.UUID;
+%0Aimport java.util.stream.Stream;
 %0A%0A/**%0A *
@@ -966,16 +966,402 @@
 %2200-%22;%0A%0A
+    public static String leftPad(String value, int length) %7B%0A        if (value.length() %3C length) %7B%0A            StringBuilder builder = new StringBuilder(length);%0A            for (int i = value.length(); i %3C length; ++i) %7B%0A                builder.append('0');%0A            %7D%0A            builder.append(value);%0A            return builder.toString();%0A        %7D%0A        return value;%0A    %7D%0A
     publ
@@ -1729,23 +1729,36 @@
 RSION + 
+leftPad(
 traceId
+, 32)
  + %22-%22 +
@@ -1758,24 +1758,32 @@
 + %22-%22 + 
+leftPad(
 parentId
  + FLAGS
@@ -1774,16 +1774,21 @@
 parentId
+, 16)
  + FLAGS

fix traceparent format issue Depending to the long values generated by the UUID, traceparent header may have invalid length