import java.net.URI;
import java.util.Locale;
import java.util.Set;

public class ValidasiHost {
    // Allowlist: satu-satunya tujuan yang boleh dihubungi aplikasi
    static final Set<String> HOST_DIIZINKAN = Set.of("api.example.com", "cdn.example.com");

    static String periksa(String teksUrl) {
        URI uri;
        try {
            uri = URI.create(teksUrl);
        } catch (IllegalArgumentException e) {
            return "DITOLAK (URI tidak valid)";
        }
        if (!"https".equalsIgnoreCase(uri.getScheme())) {
            return "DITOLAK (scheme bukan https)";
        }
        String host = uri.getHost();
        if (host == null) {
            return "DITOLAK (host kosong)";
        }
        if (!HOST_DIIZINKAN.contains(host.toLowerCase(Locale.ROOT))) {
            return "DITOLAK (host tidak ada di allowlist)";
        }
        return "DIIZINKAN";
    }

    public static void main(String[] args) {
        String[] kandidat = {
                "https://api.example.com/v1/produk",
                "https://CDN.example.com/gambar/1.png",
                "http://api.example.com/v1/produk",
                "https://penyerang.example.net/v1/produk",
                "https://api.example.com@penyerang.example.net/v1/produk",
                "https://192.168.1.10/admin",
                "file:///etc/hosts",
        };
        for (String url : kandidat) {
            System.out.printf("%-56s -> %s%n", url, periksa(url));
        }
    }
}
