|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.agent.transport.compat; |
| 18 | + |
| 19 | +import com.cloud.utils.StringUtils; |
| 20 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 21 | +import com.google.gson.Gson; |
| 22 | +import com.google.gson.JsonElement; |
| 23 | +import com.google.gson.JsonObject; |
| 24 | +import com.google.gson.JsonSerializationContext; |
| 25 | +import com.google.gson.JsonSerializer; |
| 26 | + |
| 27 | +import java.lang.reflect.Type; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * JSON serializer adapter for transport classes (com.cloud.agent.api.to.*) that ensures backward compatibility |
| 33 | + * with older Agent versions due to rename of the fields |
| 34 | + * (see https://github.com/apache/cloudstack/pull/10514) |
| 35 | + * |
| 36 | + * This class does not build its own Gson instance: doing so would silently drop whichever exclusion |
| 37 | + * strategy (e.g. log redaction) and sibling compat adaptors (for nested TOs) the enclosing Gson was |
| 38 | + * configured with. Instead, whoever registers an instance of this class into a GsonBuilder is |
| 39 | + * responsible for also calling {@link #initGson(Gson)} with a Gson that (a) carries that same |
| 40 | + * exclusion strategy and (b) has adapters registered for any nested TO types that also need field |
| 41 | + * renaming, but not for this adaptor's own type (to avoid infinite recursion). See |
| 42 | + * {@link com.cloud.serializer.GsonHelper#setDefaultGsonConfig(com.google.gson.GsonBuilder)}. |
| 43 | + */ |
| 44 | +public class AbstractTOAdaptor<T> implements JsonSerializer<T> { |
| 45 | + private Gson gson; |
| 46 | + private Map<String, String> fieldMappings; |
| 47 | + |
| 48 | + protected AbstractTOAdaptor(String... fields) { |
| 49 | + this.fieldMappings = new LinkedHashMap<>(); |
| 50 | + for (int i = 0; i + 1 < fields.length; i += 2) { |
| 51 | + String sourceField = fields[i]; |
| 52 | + String destinationField = fields[i + 1]; |
| 53 | + // skip empty fields |
| 54 | + if (StringUtils.isBlank(sourceField) || StringUtils.isBlank(destinationField)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + this.fieldMappings.put(sourceField, destinationField); |
| 58 | + } |
| 59 | + if (this.fieldMappings.isEmpty()) { |
| 60 | + throw new CloudRuntimeException("Field mappings must not be empty"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void initGson(Gson gson) { |
| 65 | + this.gson = gson; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { |
| 70 | + if (src == null) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + JsonElement tree = gson.toJsonTree(src); |
| 74 | + if (tree.isJsonObject()) { |
| 75 | + JsonObject obj = tree.getAsJsonObject(); |
| 76 | + for (Map.Entry<String, String> field : fieldMappings.entrySet()) { |
| 77 | + String sourceField = field.getKey(); |
| 78 | + String destinationField = field.getValue(); |
| 79 | + if (obj.has(sourceField)) { |
| 80 | + obj.add(destinationField, obj.get(sourceField)); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + return tree; |
| 85 | + } |
| 86 | +} |
0 commit comments