/
Credentials.java
102 lines (89 loc) · 3.57 KB
/
Credentials.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.model.connectivity;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;
/**
* Credentials of a connection. Credential types are extensible. To add a new credential type, implement this
* interface and {@link CredentialsVisitor} and their serialization.
*/
public interface Credentials {
/**
* Evaluate this object by a visitor.
*
* @param visitor the visitor.
* @param <T> type of the evaluation result.
* @return evaluation result.
*/
<T> T accept(CredentialsVisitor<T> visitor);
/**
* Serialize as JSON.
*
* @return JSON representation of this object.
*/
JsonObject toJson();
/**
* Register a deserializer for a subclass. Each subclass should call this method at least once before
* they are considered for deserialization.
* <p>
* Warning: Putting deserializer registration in static blocks of subclasses does not guarantee registration
* because static blocks are executed only if classes are loaded for some other reason.
* </p>
*
* @param type the credential type.
* @param deserializer deserializer of the subclass.
*/
static void registerDeserializer(final String type,
final Function<JsonObject, Credentials> deserializer) {
JsonFields.DESERIALIZER_MAP.put(type, deserializer);
}
/**
* Deserialize credentials from JSON.
*
* @param jsonObject credentials in JSON format.
* @return deserialized credentials.
*/
static Credentials fromJson(final JsonObject jsonObject) {
final String type = jsonObject.getValueOrThrow(JsonFields.TYPE);
final Function<JsonObject, Credentials> deserializer = JsonFields.DESERIALIZER_MAP.get(type);
if (deserializer == null) {
throw JsonParseException.newBuilder()
.message(String.format("Unknown credential type <%s>", type))
.description("Original JSON: " + jsonObject.toString())
.build();
}
return deserializer.apply(jsonObject);
}
/**
* JSON fields common to all subclasses.
*/
abstract class JsonFields {
private static final ConcurrentMap<String, Function<JsonObject, Credentials>> DESERIALIZER_MAP =
new ConcurrentHashMap<>();
/**
* JSON field definition of the credential type identifier. All subclasses should identify themselves by this
* field in their serialization.
*/
public static final JsonFieldDefinition<String> TYPE = JsonFieldDefinition.ofString("type");
static {
// load subclasses in this package.
registerDeserializer(ClientCertificateCredentials.TYPE, ClientCertificateCredentials::fromJson);
registerDeserializer(SshPublicKeyCredentials.TYPE, SshPublicKeyCredentials::fromJson);
registerDeserializer(UserPasswordCredentials.TYPE, UserPasswordCredentials::fromJson);
}
}
}