/*
 * Copyright 2021-2022 Danilo Reinert
 *
 * 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.reinert.requestor.net;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

import io.reinert.requestor.core.Requestor;

/**
 * This class provides a static initializer for Requestor's deferred bindings for JVM environment.
 *
 * @author Danilo Reinert
 */
public class RequestorNet {

    /**
     * Initializes static lazy bindings to proper usage of Requestor in JVM environment.
     * <p></p>
     * Call this method in a static block in the app's EntryPoint.
     */
    public static void init() {
        if (!Requestor.isInitialized()) {
            // TODO: define the charcode
            Requestor.init(
                    text -> Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8)),
                    new NetUriCodec()
            );
        }
    }
}

@@ -673,16 +673,99 @@
 arsets;%0A
+import java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0A
 import j
@@ -825,16 +825,66 @@
 questor;
+%0Aimport io.reinert.requestor.core.auth.DigestAuth;
 %0A%0A/**%0A *
@@ -1042,16 +1042,95 @@
 rNet %7B%0A%0A
+    private static final char%5B%5D HEX_ARRAY = %220123456789ABCDEF%22.toCharArray();%0A%0A
     /**%0A
@@ -1616,16 +1616,1336 @@
        %7D
+%0A%0A        if (!DigestAuth.hasHashFunction(%22md5%22)) %7B%0A            try %7B%0A                MessageDigest.getInstance(%22MD5%22);%0A                DigestAuth.setHashFunction(%22md5%22, new DigestAuth.HashFunction() %7B%0A                    public String hash(String input) %7B%0A                        MessageDigest md = null;%0A                        try %7B%0A                            md = MessageDigest.getInstance(%22MD5%22);%0A                        %7D catch (NoSuchAlgorithmException e) %7B%0A                            throw new UnsupportedOperationException(%0A                                    %22Cannot perform MD5 hashing because the MD5 MessageDigest is not available.%22, e);%0A                        %7D%0A                        md.update(input.getBytes(StandardCharsets.UTF_8));%0A                        byte%5B%5D digest = md.digest();%0A                        return bytesToHex(digest);%0A                    %7D%0A                %7D);%0A            %7D catch (NoSuchAlgorithmException ignored) %7B %7D%0A        %7D%0A    %7D%0A%0A    private static String bytesToHex(byte%5B%5D bytes) %7B%0A        char%5B%5D hexChars = new char%5Bbytes.length * 2%5D;%0A        for (int j = 0; j %3C bytes.length; j++) %7B%0A            int v = bytes%5Bj%5D & 0xFF;%0A            hexChars%5Bj * 2%5D = HEX_ARRAY%5Bv %3E%3E%3E 4%5D;%0A            hexChars%5Bj * 2 + 1%5D = HEX_ARRAY%5Bv & 0x0F%5D;%0A        %7D%0A        return new String(hexChars);
 %0A    %7D%0A%7D

Set MD5 DigestAuth on RequestorNet.init