|
1 | 1 | package de.predic8.kubernetesclient.client;
|
2 | 2 |
|
| 3 | +import com.google.gson.GsonBuilder; |
| 4 | +import com.google.gson.JsonDeserializationContext; |
| 5 | +import com.google.gson.JsonDeserializer; |
| 6 | +import com.google.gson.JsonElement; |
| 7 | +import com.google.gson.JsonParseException; |
| 8 | +import com.google.gson.JsonPrimitive; |
| 9 | +import com.google.gson.JsonSerializationContext; |
| 10 | +import com.google.gson.JsonSerializer; |
3 | 11 | import io.kubernetes.client.ApiClient;
|
| 12 | +import io.kubernetes.client.JSON; |
| 13 | +import org.joda.time.DateTime; |
| 14 | +import org.joda.time.LocalDate; |
| 15 | + |
| 16 | +import java.lang.reflect.Type; |
| 17 | +import java.util.Base64; |
| 18 | +import java.util.Date; |
4 | 19 |
|
5 | 20 | public abstract class NamespacedApiClient extends ApiClient {
|
6 | 21 |
|
| 22 | + |
| 23 | + public NamespacedApiClient() { |
| 24 | + setJSON(new ByteArrayHandlingJSON()); |
| 25 | + } |
| 26 | + |
7 | 27 | /**
|
8 | 28 | * @return "default" (overrideable using the "kubernetes.client.namespace" Spring property) if running outside of the cluster,
|
9 | 29 | * the pod's own namespace, if running on the inside.
|
10 | 30 | */
|
11 | 31 | public abstract String getMyNamespace();
|
12 | 32 |
|
| 33 | + private static class ByteArrayHandlingJSON extends JSON { |
| 34 | + public ByteArrayHandlingJSON() { |
| 35 | + setGson(new GsonBuilder() |
| 36 | + .registerTypeAdapter(Date.class, new DateTypeAdapter()) |
| 37 | + .registerTypeAdapter(java.sql.Date.class, new SqlDateTypeAdapter()) |
| 38 | + .registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter()) |
| 39 | + .registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter()) |
| 40 | + .registerTypeAdapter(byte[].class, new ByteArrayBase64StringTypeAdapter()) |
| 41 | + .create()); |
| 42 | + |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static class ByteArrayBase64StringTypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> { |
| 47 | + |
| 48 | + public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
| 49 | + return Base64.getDecoder().decode(json.getAsString()); |
| 50 | + } |
| 51 | + public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) { |
| 52 | + return new JsonPrimitive(Base64.getEncoder().encodeToString(src)); |
| 53 | + } |
| 54 | + } |
13 | 55 | }
|
0 commit comments