use of com.google.cloud.tools.jib.image.json.ManifestTemplate in project jib by google.
the class ManifestPuller method getManifestTemplateFromJson.
/**
* Instantiates a {@link ManifestTemplate} from a JSON string. This checks the {@code
* schemaVersion} field of the JSON to determine which manifest version to use.
*/
private T getManifestTemplateFromJson(String jsonString) throws IOException, UnknownManifestFormatException {
ObjectNode node = new ObjectMapper().readValue(jsonString, ObjectNode.class);
if (!node.has("schemaVersion")) {
throw new UnknownManifestFormatException("Cannot find field 'schemaVersion' in manifest");
}
if (!manifestTemplateClass.equals(ManifestTemplate.class)) {
return JsonTemplateMapper.readJson(jsonString, manifestTemplateClass);
}
int schemaVersion = node.get("schemaVersion").asInt(-1);
if (schemaVersion == -1) {
throw new UnknownManifestFormatException("`schemaVersion` field is not an integer");
}
if (schemaVersion == 1) {
return manifestTemplateClass.cast(JsonTemplateMapper.readJson(jsonString, V21ManifestTemplate.class));
}
if (schemaVersion == 2) {
// 'schemaVersion' of 2 can be either Docker V2.2 or OCI.
String mediaType = node.get("mediaType").asText();
if (V22ManifestTemplate.MANIFEST_MEDIA_TYPE.equals(mediaType)) {
return manifestTemplateClass.cast(JsonTemplateMapper.readJson(jsonString, V22ManifestTemplate.class));
}
if (OCIManifestTemplate.MANIFEST_MEDIA_TYPE.equals(mediaType)) {
return manifestTemplateClass.cast(JsonTemplateMapper.readJson(jsonString, OCIManifestTemplate.class));
}
throw new UnknownManifestFormatException("Unknown mediaType: " + mediaType);
}
throw new UnknownManifestFormatException("Unknown schemaVersion: " + schemaVersion + " - only 1 and 2 are supported");
}
Aggregations