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