blob: c5c39fb659004f018712936f2ce7db2d2b382bb2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.identityprovider.api.bindings;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DatabindContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.yahoo.vespa.athenz.identityprovider.api.SignedIdentityDocument;
import java.io.IOException;
import java.util.Objects;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "document-version", visible = true)
@JsonTypeIdResolver(SignedIdentityDocumentEntityTypeResolver.class)
public interface SignedIdentityDocumentEntity {
int documentVersion();
}
class SignedIdentityDocumentEntityTypeResolver implements TypeIdResolver {
JavaType javaType;
@Override
public void init(JavaType javaType) {
this.javaType = javaType;
}
@Override
public String idFromValue(Object o) {
return idFromValueAndType(o, o.getClass());
}
@Override
public String idFromValueAndType(Object o, Class<?> aClass) {
if (Objects.isNull(o)) {
throw new IllegalArgumentException("Cannot serialize null oject");
} else {
if (o instanceof SignedIdentityDocumentEntity s) {
return Integer.toString(s.documentVersion());
} else {
throw new IllegalArgumentException("Cannot serialize class: " + o.getClass());
}
}
}
@Override
public String idFromBaseType() {
return idFromValueAndType(null, javaType.getRawClass());
}
@Override
public JavaType typeFromId(DatabindContext databindContext, String s) throws IOException {
try {
int version = Integer.parseInt(s);
Class<? extends SignedIdentityDocumentEntity> cls = version <= SignedIdentityDocument.LEGACY_DEFAULT_DOCUMENT_VERSION
? LegacySignedIdentityDocumentEntity.class
: DefaultSignedIdentityDocumentEntity.class;
return TypeFactory.defaultInstance().constructSpecializedType(javaType,cls);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Unable to deserialize document with version: \"%s\"".formatted(s));
}
}
@Override
public String getDescForKnownTypeIds() {
return "Type resolver for SignedIdentityDocumentEntity";
}
@Override
public JsonTypeInfo.Id getMechanism() {
return JsonTypeInfo.Id.CUSTOM;
}
}
|