|
|
|
@ -14,8 +14,6 @@ |
|
|
|
|
*/ |
|
|
|
|
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; |
|
|
|
|
|
|
|
|
|
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters; |
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
@ -40,13 +38,12 @@ public class JsonRpcParameter { |
|
|
|
|
* @param <T> The type of parameter. |
|
|
|
|
* @return Returns the parameter cast as T if available, otherwise throws exception. |
|
|
|
|
*/ |
|
|
|
|
// TODO: update to throw JsonRpcParameterException as a checked exception, forcing callers to
|
|
|
|
|
// handle it to supply appropriate context
|
|
|
|
|
public <T> T required(final Object[] params, final int index, final Class<T> paramClass) { |
|
|
|
|
public <T> T required(final Object[] params, final int index, final Class<T> paramClass) |
|
|
|
|
throws JsonRpcParameterException { |
|
|
|
|
return optional(params, index, paramClass) |
|
|
|
|
.orElseThrow( |
|
|
|
|
() -> |
|
|
|
|
new InvalidJsonRpcParameters( |
|
|
|
|
new JsonRpcParameterException( |
|
|
|
|
"Missing required json rpc parameter at index " + index)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -60,11 +57,8 @@ public class JsonRpcParameter { |
|
|
|
|
* @param <T> The type of parameter. |
|
|
|
|
* @return Returns the parameter cast as T if available. |
|
|
|
|
*/ |
|
|
|
|
// TODO: update to throw JsonRpcParameterException as a checked exception, forcing callers to
|
|
|
|
|
// handle it to supply appropriate context
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
public <T> Optional<T> optional( |
|
|
|
|
final Object[] params, final int index, final Class<T> paramClass) { |
|
|
|
|
public <T> Optional<T> optional(final Object[] params, final int index, final Class<T> paramClass) |
|
|
|
|
throws JsonRpcParameterException { |
|
|
|
|
if (params == null || params.length <= index || params[index] == null) { |
|
|
|
|
return Optional.empty(); |
|
|
|
|
} |
|
|
|
@ -73,14 +67,14 @@ public class JsonRpcParameter { |
|
|
|
|
final Object rawParam = params[index]; |
|
|
|
|
if (paramClass.isAssignableFrom(rawParam.getClass())) { |
|
|
|
|
// If we're dealing with a simple type, just cast the value
|
|
|
|
|
param = (T) rawParam; |
|
|
|
|
param = paramClass.cast(rawParam); |
|
|
|
|
} else { |
|
|
|
|
// Otherwise, serialize param back to json and then deserialize to the paramClass type
|
|
|
|
|
try { |
|
|
|
|
final String json = mapper.writeValueAsString(rawParam); |
|
|
|
|
param = mapper.readValue(json, paramClass); |
|
|
|
|
} catch (final JsonProcessingException e) { |
|
|
|
|
throw new InvalidJsonRpcParameters( |
|
|
|
|
throw new JsonRpcParameterException( |
|
|
|
|
String.format( |
|
|
|
|
"Invalid json rpc parameter at index %d. Supplied value was: '%s' of type: '%s' - expected type: '%s'", |
|
|
|
|
index, rawParam, rawParam.getClass().getName(), paramClass.getName()), |
|
|
|
@ -91,10 +85,9 @@ public class JsonRpcParameter { |
|
|
|
|
return Optional.of(param); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: update to throw JsonRpcParameterException as a checked exception, forcing callers to
|
|
|
|
|
// handle it to supply appropriate context
|
|
|
|
|
public <T> Optional<List<T>> optionalList( |
|
|
|
|
final Object[] params, final int index, final Class<T> listClass) { |
|
|
|
|
final Object[] params, final int index, final Class<T> listClass) |
|
|
|
|
throws JsonRpcParameterException { |
|
|
|
|
if (params == null || params.length <= index || params[index] == null) { |
|
|
|
|
return Optional.empty(); |
|
|
|
|
} |
|
|
|
@ -105,7 +98,7 @@ public class JsonRpcParameter { |
|
|
|
|
List<T> returnedList = mapper.readValue(listJson, new TypeReference<List<T>>() {}); |
|
|
|
|
return Optional.of(returnedList); |
|
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
|
throw new InvalidJsonRpcParameters( |
|
|
|
|
throw new JsonRpcParameterException( |
|
|
|
|
String.format( |
|
|
|
|
"Invalid json rpc parameter at index %d. Supplied value was: '%s' of type: '%s' - expected type: '%s'", |
|
|
|
|
index, rawParam, rawParam.getClass().getName(), listClass.getName()), |
|
|
|
|