/*
 * 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()
            );
        }
    }
}

/*
 * 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.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import io.reinert.requestor.core.Requestor;
import io.reinert.requestor.core.auth.DigestAuth;

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

    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

    /**
     * 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()
            );
        }

        if (!DigestAuth.hasHashFunction("md5")) {
            try {
                MessageDigest.getInstance("MD5");
                DigestAuth.setHashFunction("md5", new DigestAuth.HashFunction() {
                    public String hash(String input) {
                        MessageDigest md = null;
                        try {
                            md = MessageDigest.getInstance("MD5");
                        } catch (NoSuchAlgorithmException e) {
                            throw new UnsupportedOperationException(
                                    "Cannot perform MD5 hashing because the MD5 MessageDigest is not available.", e);
                        }
                        md.update(input.getBytes(StandardCharsets.UTF_8));
                        byte[] digest = md.digest();
                        return bytesToHex(digest);
                    }
                });
            } catch (NoSuchAlgorithmException ignored) { }
        }
    }

    private static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Set MD5 DigestAuth on RequestorNet.init