Coverage Summary for Class: EthSubscribeParamsDeserializer (co.rsk.rpc.modules.eth.subscribe)
Class |
Class, %
|
Method, %
|
Line, %
|
EthSubscribeParamsDeserializer |
0%
(0/1)
|
0%
(0/2)
|
0%
(0/22)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2019 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.rpc.modules.eth.subscribe;
20
21 import com.fasterxml.jackson.core.JsonParser;
22 import com.fasterxml.jackson.core.JsonToken;
23 import com.fasterxml.jackson.databind.DeserializationContext;
24 import com.fasterxml.jackson.databind.JsonDeserializer;
25
26 import java.io.IOException;
27 import java.util.HashMap;
28
29 /**
30 * This class is necessary until https://github.com/FasterXML/jackson-databind/issues/2467 is integrated.
31 * It is expected to be included in Jackson 2.10 version.
32 */
33 public class EthSubscribeParamsDeserializer extends JsonDeserializer {
34
35 private final HashMap<String, Class<? extends EthSubscribeParams>> subscriptionTypes;
36
37 public EthSubscribeParamsDeserializer() {
38 this.subscriptionTypes = new HashMap<>();
39 this.subscriptionTypes.put("newHeads", EthSubscribeNewHeadsParams.class);
40 this.subscriptionTypes.put("logs", EthSubscribeLogsParams.class);
41 }
42
43 @Override
44 public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
45 if (!p.isExpectedStartArrayToken()) {
46 return ctxt.handleUnexpectedToken(
47 EthSubscribeParams.class,
48 p.currentToken(),
49 p,
50 "eth_subscribe parameters are expected to be arrays"
51 );
52 }
53 p.nextToken(); // skip '['
54 String subscriptionType = p.getText();
55 Class<? extends EthSubscribeParams> subscriptionTypeClass = subscriptionTypes.get(subscriptionType);
56 p.nextToken();
57 EthSubscribeParams params;
58 if (p.isExpectedStartObjectToken()) {
59 params = p.readValueAs(subscriptionTypeClass);
60 p.nextToken();
61 } else {
62 try {
63 params = subscriptionTypeClass.newInstance();
64 } catch (InstantiationException | IllegalAccessException e) {
65 return ctxt.handleInstantiationProblem(
66 subscriptionTypeClass,
67 null,
68 e
69 );
70 }
71 }
72 if (p.currentToken() != JsonToken.END_ARRAY) {
73 return ctxt.handleUnexpectedToken(
74 EthSubscribeParams.class,
75 p.currentToken(),
76 p,
77 "eth_subscribe can only have one object to configure subscription"
78 );
79 }
80 return params;
81 }
82 }