/** * Returns a stream with the template parameter expressions replaced */ public static InputStream replaceValues(InputStream is, Map<String, String> valuesMap) { return new ReplaceValueStream(valuesMap).createInputStream(is); }
public InputStream createInputStream(InputStream is) { try { String json = IOHelpers.readFully(is); String replaced = replaceValues(json); return new ByteArrayInputStream(replaced.getBytes()); } catch (IOException e) { throw KubernetesClientException.launderThrowable(e); } }
/** * Unmarshals an {@link InputStream} optionally performing placeholder substitution to the stream. * * @param is The {@link InputStream}. * @param type The {@link TypeReference}. * @param parameters A {@link Map} with parameters for placeholder substitution. * @param <T> * @return * @throws KubernetesClientException */ public static <T> T unmarshal(InputStream is, TypeReference<T> type, Map<String, String> parameters) throws KubernetesClientException { InputStream wrapped = parameters != null && !parameters.isEmpty() ? new ReplaceValueStream(parameters).createInputStream(is) : is; try (BufferedInputStream bis = new BufferedInputStream(wrapped)) { bis.mark(-1); int intch; do { intch = bis.read(); } while (intch > -1 && Character.isWhitespace(intch)); bis.reset(); ObjectMapper mapper = JSON_MAPPER; if (intch != '{') { mapper = YAML_MAPPER; } return mapper.readValue(bis, type); } catch (IOException e) { throw KubernetesClientException.launderThrowable(e); } }