001 002package io.vrap.rmf.base.client.utils; 003 004import java.nio.charset.StandardCharsets; 005import java.util.concurrent.CompletionException; 006import java.util.function.Function; 007 008import io.vrap.rmf.base.client.ApiHttpResponse; 009import io.vrap.rmf.base.client.utils.json.JsonUtils; 010 011public final class Utils { 012 013 public static <U, V> Function<U, V> wrapToCompletionException(final ExceptionalFunction<U, V> exceptionalFunction) { 014 return u -> { 015 try { 016 return exceptionalFunction.apply(u); 017 } 018 catch (Exception e) { 019 throw new CompletionException(e); 020 } 021 }; 022 } 023 024 public static String bytesToString(final byte[] input) { 025 if (input == null) { 026 return null; 027 } 028 return new String(input); 029 } 030 031 public static String bytesToUTF8String(final byte[] input) { 032 if (input == null) { 033 return null; 034 } 035 return new String(input, StandardCharsets.UTF_8); 036 } 037 038 public static <I, O> ApiHttpResponse<O> convertResponse(final ApiHttpResponse<byte[]> response, 039 final Class<O> outputType) { 040 if (response.getBody() == null) { 041 return (ApiHttpResponse<O>) response; 042 } 043 O newBody = JsonUtils.fromJsonByteArray(response.getBody(), outputType); 044 return new ApiHttpResponse<>(response.getStatusCode(), response.getHeaders(), newBody, response.getMessage(), 045 response.getContextMap()); 046 } 047}