use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.
the class JsonDeserializer method readInt.
@Override
public int readInt(String name) throws RpcException {
JsonNode node;
if (name == null) {
// The object is not under root, but directly in the response
node = root;
name = "root";
} else {
node = root.get(name);
}
if (node == null) {
throw new RpcException(RpcException.Type.MISSING_VALUE, name);
}
if (!node.isInt()) {
if (!node.isTextual()) {
throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int");
} else {
try {
return Integer.parseInt(node.getTextValue());
} catch (NumberFormatException ex) {
throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int", ex);
}
}
}
return node.getIntValue();
}
use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.
the class RpcCallerImpl method processRpcServerException.
protected void processRpcServerException(InputStream errorStream) throws RpcException, InternalErrorException, PerunException {
JsonDeserializer errDes;
try {
errDes = new JsonDeserializer(errorStream);
} catch (IOException e) {
this.processIOException(e);
return;
}
// Error occured, read the Exception if it is in response
String errorId = errDes.readString("errorId");
if (errorId != null) {
String exceptionName = errDes.readString("name");
if (!exceptionName.equals(RpcException.class.getSimpleName())) {
String errorClass = errDes.readString("name");
String errorInfo = errDes.readString("message");
try {
Class<?> exceptionClass = Class.forName("cz.metacentrum.perun.core.api.exceptions." + errorClass);
Class<?>[] constructorParams = new Class[1];
constructorParams[0] = String.class;
Constructor<?> exceptionConstructor = exceptionClass.getConstructor(constructorParams);
Object[] arglist = new Object[1];
arglist[0] = errorInfo;
PerunException exception = (PerunException) exceptionConstructor.newInstance(arglist);
exception.setErrorId(errorId);
throw exception;
} catch (ClassNotFoundException e1) {
throw new InternalErrorException(e1);
} catch (InstantiationException e1) {
throw new InternalErrorException(e1);
} catch (IllegalAccessException e1) {
throw new InternalErrorException(e1);
} catch (IllegalArgumentException e1) {
throw new InternalErrorException(e1);
} catch (InvocationTargetException e1) {
throw new InternalErrorException(e1);
} catch (NoSuchMethodException e1) {
throw new InternalErrorException(e1);
}
} else {
// RPC Exception
String errorClass = errDes.readString("type");
String errorInfo = errDes.readString("errorInfo");
throw new RpcException(errorClass, errorInfo);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.
the class JsonDeserializer method readArrayOfInts.
@Override
public int[] readArrayOfInts(String name) throws RpcException {
JsonNode node;
if (name == null) {
// The object is not under root, but directly in the response
node = root;
} else {
node = root.get(name);
}
if (node.isNull()) {
return null;
}
if (!node.isArray()) {
throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int[] - not an array");
}
int[] array = new int[node.size()];
for (int i = 0; i < node.size(); ++i) {
JsonNode value = node.get(i);
if (!value.isInt()) {
throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int");
}
array[i] = node.get(i).getIntValue();
}
return array;
}
use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.
the class JsonDeserializer method readList.
@Override
public <T> List<T> readList(String name, Class<T> valueType) throws RpcException {
JsonNode node;
if (name == null) {
// The object is not under root, but directly in the response
node = root;
name = "root";
} else {
node = root.get(name);
}
if (node == null) {
throw new RpcException(RpcException.Type.MISSING_VALUE, name);
}
if (node.isNull()) {
return null;
}
if (!node.isArray()) {
throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<" + valueType.getSimpleName() + "> - not an array");
}
try {
List<T> list = new ArrayList<>(node.size());
for (JsonNode e : node) {
list.add(mapper.readValue(e, valueType));
}
return list;
} catch (IOException ex) {
throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<" + valueType.getSimpleName() + ">", ex);
}
}
use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.
the class JsonDeserializer method read.
@Override
public <T> T read(String name, Class<T> valueType) throws RpcException {
JsonNode node;
if (name == null) {
// The object is not under root, but directly in the response
node = root;
name = "root";
} else {
node = root.get(name);
}
if (node == null) {
throw new RpcException(RpcException.Type.MISSING_VALUE, name);
}
if (node.isNull()) {
return null;
}
if (!node.isObject()) {
throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as " + valueType.getSimpleName());
}
try {
return mapper.readValue(node, valueType);
} catch (IOException ex) {
throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as " + valueType.getSimpleName(), ex);
}
}
Aggregations